This commit is contained in:
yumoqing 2021-02-28 10:23:27 +08:00
parent 18f3de6032
commit 4a9de787c7
2 changed files with 86 additions and 12 deletions

View File

@ -1,8 +1,10 @@
from kivy.properties import StringProperty from kivy.properties import StringProperty, NumericProperty, ListProperty
from kivy.factory import Factory from kivy.factory import Factory
from kivy.clock import Clock from kivy.clock import Clock
from kivy.logger import Logger
from kivy.uix.image import AsyncImage
from .baseWidget import ScrollWidget, getDataHandler from .baseWidget import ScrollWidget, getDataHandler, CSize
from .utils import CSize from .utils import CSize
import re import re
@ -68,7 +70,7 @@ class MarkDownParser(object):
self.text = f'{VIDEO_LEAD}' self.text = f'{VIDEO_LEAD}'
return return
title = x[0] title = x[0]
y = x[0].split(REFER_END,1) y = x[1].split(REFER_END,1)
if len(y) < 2: if len(y) < 2:
self.text = f'{VIDEO_LEAD}' self.text = f'{VIDEO_LEAD}'
return return
@ -89,7 +91,7 @@ class MarkDownParser(object):
self.text = f'{VIDEO_LEAD}' self.text = f'{VIDEO_LEAD}'
return return
title = x[0] title = x[0]
y = x[0].split(REFER_END,1) y = x[1].split(REFER_END,1)
if len(y) < 2: if len(y) < 2:
self.text = f'{VIDEO_LEAD}' self.text = f'{VIDEO_LEAD}'
return return
@ -101,8 +103,7 @@ class MarkDownParser(object):
} }
} }
self.result.append(d) self.result.append(d)
l = len(title) + 2 + len(url) self.mdtext = y[1]
self.mdtext = self.mdtext[l:]
def check_key(self,t): def check_key(self,t):
for k in self.mdkeys: for k in self.mdkeys:
@ -131,6 +132,36 @@ class MarkDownParser(object):
self.text = '' self.text = ''
return self.result return self.result
class MDImage(AsyncImage):
parent_width=NumericProperty(None)
image_size = ListProperty(None)
def __init__(self, **kw):
super().__init__(**kw)
self.image_size = None
self.parent_width = None
self.bind(texture=self.image_loaded)
def image_loaded(self,o, *args):
self.image_size = self.texture.size
def resize(self, size):
self.parent_width = size[0]
Logger.info('MDImage: resize called, size=%s', size)
def on_image_size(self, *args):
if self.parent_width:
self.set_new_height()
def on_parent_width(self, *args):
if self.image_size:
self.set_new_height()
def set_new_height(self):
self.height = self.parent_width * self.image_size[1] \
/ self.image_size[0]
Logger.info('MDImage: set_new_height called, size=%s', self.size)
class Markdown(ScrollWidget): class Markdown(ScrollWidget):
""" """
# Markdown # Markdown
@ -167,22 +198,34 @@ description file format
self.parse_line(l) self.parse_line(l)
def build_image(self,img_desc): def build_image(self,img_desc):
w = Factory.AsyncImage(soure=img_desc['url'], w = MDImage(source=img_desc['url'],
keep_ratio=True, allow_stretch=True,
size_hint_y=None size_hint_y=None,
height=CSize(10),
keep_ratio=True
) )
w.bind(minimum_height=w.setter('height'))
self.add_widget(w) self.add_widget(w)
w.resize(self.size)
def build_video(self, video_desc): def build_video(self, video_desc):
w = Factory.NewVideo(source=video_desc['url'], w = Factory.NewVideo(source=video_desc['url'],
keep_ratio=True, keep_ratio=True,
play=True, play=False,
allow_stretch = True, allow_stretch = True,
size_hint_y=None size_hint_y=None
) )
w.height=self.width * 10 / 16 w.height=self.width * 10 / 16
w.state = 'pause'
def f1(x):
x.state = 'play'
def f2(x):
x.state = 'stop'
w.bind(on_enter_focus=f1)
w.bind(on_leave_focus=f2)
self.add_widget(w) self.add_widget(w)
w.resize(self.size)
def build_audio(self, audio_desc): def build_audio(self, audio_desc):
w = Factory.APlayer(source=audio_desc.url) w = Factory.APlayer(source=audio_desc.url)

View File

@ -4,6 +4,7 @@ from kivy.logger import Logger
from kivy.core.window import Window from kivy.core.window import Window
from kivy.factory import Factory from kivy.factory import Factory
from kivy.properties import BooleanProperty from kivy.properties import BooleanProperty
from kivy.clock import Clock
from ffpyplayer.tools import set_log_callback from ffpyplayer.tools import set_log_callback
@ -15,6 +16,7 @@ logger_func = {'quiet': Logger.critical, 'panic': Logger.critical,
class NewVideo(BGColorBehavior, Video): class NewVideo(BGColorBehavior, Video):
center_screen = BooleanProperty(False)
def __init__(self,color_level=-1,radius=[],**kw): def __init__(self,color_level=-1,radius=[],**kw):
Video.__init__(self, **kw) Video.__init__(self, **kw)
BGColorBehavior.__init__(self, BGColorBehavior.__init__(self,
@ -23,9 +25,34 @@ class NewVideo(BGColorBehavior, Video):
Window.allow_screensaver = False Window.allow_screensaver = False
set_log_callback(self.ffplayerLog) set_log_callback(self.ffplayerLog)
self.register_event_type('on_open_failed') self.register_event_type('on_open_failed')
self.register_event_type('on_leave_focus')
self.register_event_type('on_enter_focus')
self.register_event_type('on_load_success') self.register_event_type('on_load_success')
self.bind(source=self.record_start_time) self.bind(source=self.record_start_time)
self.load_status = None self.load_status = None
Clock.schedule_interval(self.check_focus,0.2)
def on_enter_focus(self, v=None):
Logger.info('NewView: enter focus')
def on_leave_focus(self, v=None):
Logger.info('NewView: leave focus')
def check_focus(self,*args):
if not self.parent:
return
w = self.parent
pos = w.to_widget(*Window.center)
if self.collide_point(*pos):
self.center_screen = True
else:
self.center_screen = False
def on_center_screen(self,o, v):
if v:
self.dispatch('on_enter_focus')
else:
self.dispatch('on_leave_focus')
def resize(self, size): def resize(self, size):
v_size = self.get_video_size() v_size = self.get_video_size()
@ -67,9 +94,13 @@ class NewVideo(BGColorBehavior, Video):
def get_video_size(self): def get_video_size(self):
if hasattr(self._video,'_ffplayer'): if hasattr(self._video,'_ffplayer'):
return self._video._ffplayer.get_frame()[0][0].get_size() try:
return self._video._ffplayer.get_frame()[0][0].get_size()
except:
return None
else: else:
Logger.error('NewVideo: _video has not _ffplayer, do nothong') Logger.error('NewVideo: _video has not _ffplayer, do nothong')
return None
def audioswitch(self,btn=None): def audioswitch(self,btn=None):
if hasattr(self._video,'_ffplayer'): if hasattr(self._video,'_ffplayer'):