This commit is contained in:
yumoqing 2021-06-29 16:03:15 +08:00
parent 745bdbf0ae
commit 0f924c47c6

View File

@ -2,16 +2,30 @@ from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout from kivy.uix.boxlayout import BoxLayout
from kivy.factory import Factory from kivy.factory import Factory
from kivy.core.window import Window from kivy.core.window import Window
from kivy.properties import StringProperty from kivy.properties import StringProperty, BooleanProperty
from kivy.uix.image import AsyncImage
class TwoSides(BoxLayout): from kivyblocks.utils import CSize
from kivyblocks.ready import WidgetReady
class TwoSides(WidgetReady, BoxLayout):
panel_shape = StringProperty('landscape') panel_shape = StringProperty('landscape')
def __init__(self,landscape={},portrait={},**kw): cannt_rotation = BooleanProperty(True)
def __init__(self,switch_image=None,
landscape={},
portrait={},
**kw):
BoxLayout.__init__(self,**kw) BoxLayout.__init__(self,**kw)
WidgetReady.__init__(self)
blocks = Factory.Blocks() blocks = Factory.Blocks()
self.switch_image = switch_image
self.portrait_modal = None
self.landscape_widget = blocks.widgetBuild(landscape) self.landscape_widget = blocks.widgetBuild(landscape)
blocks = Factory.Blocks() blocks = Factory.Blocks()
self.portrait_widget = blocks.widgetBuild(portrait) self.portrait_widget = blocks.widgetBuild(portrait)
if self.switch_image:
self.build_switch_image()
self.on_size_task = None self.on_size_task = None
self.ready_task = None self.ready_task = None
self.register_event_type('on_interactive') self.register_event_type('on_interactive')
@ -19,6 +33,60 @@ class TwoSides(BoxLayout):
self.register_event_type('on_afterswitch_landscape') self.register_event_type('on_afterswitch_landscape')
self.register_event_type('on_beforeswitch_portrait') self.register_event_type('on_beforeswitch_portrait')
self.register_event_type('on_afterswitch_portrait') self.register_event_type('on_afterswitch_portrait')
self.bind(fullscreen=self.show_switch_image)
def show_switch_image(self, o, v=None):
def show(*args):
if self.switch_image and self.width > self.height:
Window.add_widget(self.switch_button)
print('show switch_buuton ....')
Clock.schedule_once(show, 2)
def build_switch_image(self):
button = Factory.Blocks().widgetBuild({
"widgettype":"PressableBox",
"options":{
"size_hint":(None, None),
"size":CSize(4,4),
"pos":(0, Window.height - CSize(4))
},
"subwidgets":[
{
"widgettype":"AsyncImage",
"options":{
"source":self.switch_image
}
}
]
})
button.bind(on_press=self.switch_portrait_widget)
self.switch_button = button
def switch_portrait_widget(self, *args):
def clear_modal(o, *args):
o.clear_widgets()
if not self.portrait_modal:
y = self.height - CSize(4)
x = self.height * y / self.width
w = Factory.Blocks().widgetBuild({
"widgettype":"Modal",
"options":{
"auto_dismiss":True,
"size_hint":[None,None],
"size":(x,y),
"pos":((self.width - x) / 2, 0)
}
})
w.bind(on_dismiss=clear_modal)
self.portrait_modal = w
if self.portrait_widget.parent:
self.portrait_widget.parent.remove_widget(self.portrait_widget)
self.portrait_modal.clear_widgets()
self.portrait_modal.add_widget(self.portrait_widget)
self.portrait_modal.open()
def on_size(self,*args): def on_size(self,*args):
if self.width >= self.height: if self.width >= self.height:
@ -35,6 +103,7 @@ class TwoSides(BoxLayout):
self.dispatch('on_beforeswitch_landscape') self.dispatch('on_beforeswitch_landscape')
self.clear_widgets() self.clear_widgets()
self.add_widget(self.portrait_widget) self.add_widget(self.portrait_widget)
self.cannt_rotation = False
self.panel_shape = 'portrait' self.panel_shape = 'portrait'
self.dispatch('on_afterswitch_landscape') self.dispatch('on_afterswitch_landscape')
self.dispatch('on_interactive') self.dispatch('on_interactive')