This commit is contained in:
yumoqing 2020-12-25 15:39:28 +08:00
parent 1d4161e516
commit 0e748ceff8
7 changed files with 190 additions and 77 deletions

View File

@ -71,6 +71,7 @@ from .tab import TabsPanel
from .qrdata import QRCodeWidget from .qrdata import QRCodeWidget
from .threadcall import HttpClient from .threadcall import HttpClient
from .i18n import I18n from .i18n import I18n
from .utils import CSize
if platform == 'android': if platform == 'android':
from .widgetExt.phonebutton import PhoneButton from .widgetExt.phonebutton import PhoneButton
@ -82,17 +83,75 @@ class WrapText(Label):
self.bind(width=lambda *x: self.setter('text_size')(self, (self.width, None)), self.bind(width=lambda *x: self.setter('text_size')(self, (self.width, None)),
texture_size=lambda *x: self.setter('height')(self, self.texture_size[1])) texture_size=lambda *x: self.setter('height')(self, self.texture_size[1]))
class Box(BGColorBehavior, BoxLayout):
def __init__(self,color_level=-1,radius=[],**kw):
BoxLayout.__init__(self, **kw)
BGColorBehavior.__init__(self, color_level=color_level,
radius=radius)
def add_widget(self, w, *args, **kw):
super().add_widget(w, *args, **kw)
self.setWidgetTextColor(w)
def setWidgetTextColor(self,w):
if isinstance(w,Box):
return
if isinstance(w,Text):
if not w.bgcolor:
w.color = self.fgcolor
return
for c in w.children:
self.setWidgetTextColor(c)
def selected(self):
super().selected()
if self.fgcolor == self.selected_fgcolor:
return
for c in self.children:
self.setWidgetTextColor(c)
def unselected(self):
super().unselected()
if self.fgcolor == self.normal_fgcolor:
return
for c in self.children:
self.setWidgetTextColor(c)
class HBox(Box):
def __init__(self,**kw):
kw.update({'orientation':'horizontal'})
Box.__init__(self, **kw)
class VBox(Box):
def __init__(self,color_level=-1,radius=[],**kw):
kw.update({'orientation':'vertical'})
Box.__init__(self, **kw)
class Text(Label): class Text(Label):
lang=StringProperty('') lang=StringProperty('')
otext = StringProperty('') otext = StringProperty('')
def __init__(self,i18n=False, textype='text', wrap=False, **kw): def __init__(self,i18n=False, texttype='text', wrap=False,
fgcolor=None, **kw):
fontsize={'font_size':CSize(1)}
offset={
'text':0,
'title1':CSize(0.6),
'title2':CSize(0.5),
'title3':CSize(0.4),
'title4':CSize(0.3),
'title5':CSize(0.2),
'title6':CSize(0.1),
}
fontsize = {'font_size': CSize(1) + offset.get(texttype,0)}
self._i18n = i18n self._i18n = i18n
self.bgcolor = fgcolor
kwargs = kw.copy() kwargs = kw.copy()
config = getConfig() config = getConfig()
self.wrap = wrap self.wrap = wrap
if config.texttypes: kwargs.update(fontsize)
attrs = config.texttypes.get('texttype',{})
kwargs.update(attrs)
super().__init__(**kwargs) super().__init__(**kwargs)
if self._i18n: if self._i18n:
self.i18n = I18n() self.i18n = I18n()
@ -101,6 +160,8 @@ class Text(Label):
if self.wrap: if self.wrap:
font_size = self.font_size font_size = self.font_size
self.text_size = self.width, None self.text_size = self.width, None
if self.bgcolor:
self.color = self.bgcolor
def on_size(self,o,size): def on_size(self,o,size):
# super().on_size(o,size) # super().on_size(o,size)
@ -120,6 +181,62 @@ class Text(Label):
def on_lang(self,o,lang): def on_lang(self,o,lang):
self.text = self.i18n(self.otext) self.text = self.i18n(self.otext)
class Title1(Text):
def __init__(self, **kw):
kw.update({'texttype':'title1','bold':True})
Text.__init__(self, **kw)
class Title2(Text):
def __init__(self, **kw):
kw.update({'texttype':'title2','bold':True})
Text.__init__(self, **kw)
class Title3(Text):
def __init__(self, **kw):
kw.update({'texttype':'title3','bold':True})
Text.__init__(self, **kw)
class Title4(Text):
def __init__(self, **kw):
kw.update({'texttype':'title4','bold':True})
Text.__init__(self, **kw)
class Title5(Text):
def __init__(self, **kw):
kw.update({'texttype':'title5','bold':True})
Text.__init__(self, **kw)
class Title6(Text):
def __init__(self, **kw):
kw.update({'texttype':'title6','bold':True})
Text.__init__(self, **kw)
class Modal(BGColorBehavior, ModalView):
def __init__(self, auto_open=False, color_level=-1, radius=[], **kw):
kw.update({'auto_dismiss':False})
ModalView.__init__(self, **kw)
BGColorBehavior.__init__(self, color_level=color_level,
radius=radius)
self.auto_open = auto_open
if self.auto_open:
self.open()
class TimedModal(Modal):
def __init__(self, show_time=5, **kw):
self.show_time = show_time
self.time_task = None
Modal.__init__(self, **kw)
def open(self, *args, **kw):
self.time_task = Clock.schedule_once(self.dismiss, self.show_time)
super().open(*args, **kw)
def dismiss(self, *args, **kw):
if self.time_task:
self.time_task.cancel()
self.time_task = None
super().dismiss(*args, **kw)
class PressableImage(ButtonBehavior,AsyncImage): class PressableImage(ButtonBehavior,AsyncImage):
def on_press(self): def on_press(self):
pass pass

View File

@ -6,47 +6,24 @@ from .color_definitions import getColors
_logcnt = 0 _logcnt = 0
class BGColorBehavior(object): class BGColorBehavior(object):
def __init__(self, color_level=-1,radius=[],**kwargs): def __init__(self, color_level=-1,radius=[],**kwargs):
if color_level==-1:
color_level = 0
self.color_level = color_level self.color_level = color_level
self.radius = radius self.radius = radius
self.bgcolor = [] self.bgcolor = []
self.fgcolor = [] self.fgcolor = []
self.useOwnColor = False fg,bg= getColors(color_level)
if color_level != -1: self.fgcolor = fg
fg,bg= getColors(color_level) self.bgcolor = bg
self.fgcolor = fg self.normal_bgcolor = bg
self.bgcolor = bg self.normal_fgcolor = fg
self.normal_bgcolor = bg fg,bg= getColors(color_level,selected=True)
self.normal_fgcolor = fg self.selected_bgcolor = bg
fg,bg= getColors(color_level,selected=True) self.selected_fgcolor = fg
self.selected_bgcolor = bg self.on_bgcolor()
self.selected_fgcolor = fg
self.useOwnColor = True
self.on_bgcolor()
self.bind(size=self.onSize_bgcolor_behavior, self.bind(size=self.onSize_bgcolor_behavior,
pos=self.onSize_bgcolor_behavior) pos=self.onSize_bgcolor_behavior)
def useAcestorColor(self,selected=False):
p = self.parent
cnt = 0
while p:
if isinstance(p,BGColorBehavior) and p.useOwnColor:
if selected:
self.bgcolor = p.selected_bgcolor
self.color = self.fgcolor = p.selected_fgcolor
else:
self.bgcolor = p.normal_bgcolor
self.color = self.fgcolor = p.normal_fgcolor
self.on_bgcolor()
return
p = p.parent
cnt += 1
if cnt > 100:
break
fg,bg= getColors(0,selected=selected)
self.bgcolor = bg
self.color = self.fgcolor = fg
self.on_bgcolor()
def onSize_bgcolor_behavior(self,o,v=None): def onSize_bgcolor_behavior(self,o,v=None):
if not hasattr(self,'rect'): if not hasattr(self,'rect'):
@ -72,21 +49,15 @@ class BGColorBehavior(object):
print('on_bgcolor():self.canvas is None') print('on_bgcolor():self.canvas is None')
def selected(self): def selected(self):
if self.useOwnColor: if self.bgcolor == self.selected_bgcolor:
if self.bgcolor == self.selected_bgcolor: return
return self.bgcolor = self.selected_bgcolor
self.bgcolor = self.selected_bgcolor self.color = self.fgcolor = self.selected_fgcolor
self.color = self.fgcolor = self.selected_fgcolor self.on_bgcolor()
self.on_bgcolor()
else:
self.useAcestorColor(selected=True)
def unselected(self): def unselected(self):
if self.useOwnColor: if self.bgcolor == self.normal_bgcolor:
if self.bgcolor == self.normal_bgcolor: return
return self.bgcolor = self.normal_bgcolor
self.bgcolor = self.normal_bgcolor self.color = self.fgcolor = self.normal_fgcolor
self.color = self.fgcolor = self.normal_fgcolor self.on_bgcolor()
self.on_bgcolor()
else:
self.useAcestorColor(selected=False)

View File

@ -361,6 +361,7 @@ class Blocks(EventDispatcher):
w = Blocks.getWidgetById(desc.get('wid','self'),from_widget=widget) w = Blocks.getWidgetById(desc.get('wid','self'),from_widget=widget)
if not w: if not w:
print(desc.get('wid','self'),'not found via Blocks.getWidgetById()') print(desc.get('wid','self'),'not found via Blocks.getWidgetById()')
return
event = desc.get('event') event = desc.get('event')
if event is None: if event is None:
return return
@ -532,22 +533,34 @@ class Blocks(EventDispatcher):
@classmethod @classmethod
def getWidgetById(self,id,from_widget=None): def getWidgetById(self,id,from_widget=None):
def find_widget_by_id(id, from_widget=None):
if id=='self':
return from_widget
if hasattr(from_widget,'widget_id'):
if from_widget.widget_id == id:
return from_widget
if hasattr(from_widget, id):
w = getattr(from_widget,id)
if isinstance(w,Widget):
return w
for c in from_widget.children:
ret = find_widget_by_id(id,from_widget=c)
if ret:
return ret
return None
ids = id.split('.')
app = App.get_running_app() app = App.get_running_app()
if id in ['root','/self']: if id.startswith('/self') or id.startswith('root'):
print('return app.root',app.root) from_widget = app.root
return app.root ids[0] = 'self'
if id=='self':
return from_widget
if from_widget is None: if from_widget is None:
from_widget = app.root from_widget = app.root
if hasattr(from_widget,'widget_id'): for id in ids:
if from_widget.widget_id == id: w = find_widget_by_id(id,from_widget=from_widget)
return from_widget if w is None:
for c in from_widget.children: return None
ret = Blocks.getWidgetById(id,from_widget=c) from_widget = w
if ret: return w
return ret
return None
def on_built(self,v=None): def on_built(self,v=None):
return return

View File

@ -359,8 +359,7 @@ level_bg_colors = [
] ]
text_colors = { text_colors = {
'normal':['eeeeee','111111'], 'normal':['ffff99','8b210c'],
'highlight':['ffffff','000000']
} }
def getConfigStyle(): def getConfigStyle():

View File

@ -5,10 +5,12 @@ from kivy.uix.button import Button
from kivy.uix.label import Label from kivy.uix.label import Label
from kivy.app import App from kivy.app import App
from .utils import CSize from .utils import CSize
from .bgcolorbehavior import BGColorBehavior
class PageContainer(FloatLayout): class PageContainer(BGColorBehavior, FloatLayout):
def __init__(self,**kw): def __init__(self,**kw):
super().__init__(**kw) FloatLayout.__init__(self, **kw)
BGColorBehavior.__init__(self)
self.show_back = True self.show_back = True
self.pageWidgets = [] self.pageWidgets = []
self.backButton = Button(text='<',size_hint=(None,None), self.backButton = Button(text='<',size_hint=(None,None),

View File

@ -54,6 +54,16 @@ r('HostImage',HostImage)
r('APlayer',APlayer) r('APlayer',APlayer)
r('WrapText',WrapText) r('WrapText',WrapText)
r('PressableBox',PressableBox) r('PressableBox',PressableBox)
r('Title1',Title1)
r('Title2',Title2)
r('Title3',Title3)
r('Title4',Title4)
r('Title5',Title5)
r('Title6',Title6)
r('Modal',Modal)
r('TimedModal',TimedModal)
r('HBox',HBox)
r('VBox',VBox)
r('ToggleItems',ToggleItems) r('ToggleItems',ToggleItems)
if platform == 'android': if platform == 'android':
r('PhoneButton',PhoneButton) r('PhoneButton',PhoneButton)

View File

@ -7,21 +7,22 @@ from kivy.factory import Factory
from kivyblocks.ready import WidgetReady from kivyblocks.ready import WidgetReady
from kivyblocks.bgcolorbehavior import BGColorBehavior from kivyblocks.bgcolorbehavior import BGColorBehavior
from kivyblocks.utils import CSize from kivyblocks.utils import CSize
from kivyblocks.baseWidget import Box
class PressableBox(BGColorBehavior, TouchRippleButtonBehavior, BoxLayout): class PressableBox(TouchRippleButtonBehavior, Box):
def __init__(self,border_width=1, def __init__(self,border_width=1,
color_level=-1, color_level=-1,
user_data=None, user_data=None,
radius=[], radius=[],
**kw): **kw):
BoxLayout.__init__(self, padding=[border_width, Box.__init__(self, padding=[border_width,
border_width, border_width,
border_width, border_width,
border_width], border_width],
color_level=color_level,
radius=radius,
**kw) **kw)
TouchRippleButtonBehavior.__init__(self) TouchRippleButtonBehavior.__init__(self)
BGColorBehavior.__init__(self,color_level=color_level,
radius=radius)
self.border_width = border_width self.border_width = border_width
self.user_data = user_data self.user_data = user_data
self.unselected() self.unselected()