kivyblocks/kivyblocks/responsivelayout.py

56 lines
1.5 KiB
Python
Raw Normal View History

2021-08-15 16:57:20 +08:00
2022-01-27 11:07:46 +08:00
from math import floor
from kivy.properties import NumericProperty
2021-08-15 16:57:20 +08:00
from kivy.utils import platform
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.graphics import Color, Ellipse,Rectangle
from kivy.clock import Clock
2022-01-27 11:07:46 +08:00
from .utils import CSize, isHandHold, show_widget_info
2021-08-15 16:57:20 +08:00
class VResponsiveLayout(ScrollView):
2022-01-27 11:07:46 +08:00
box_width = NumericProperty(1)
def __init__(self, **kw):
self._inner = None
2021-08-15 16:57:20 +08:00
super(VResponsiveLayout, self).__init__(**kw)
self.options = kw
2022-01-27 11:07:46 +08:00
self._inner = GridLayout(cols=1, padding=2,
2021-08-15 16:57:20 +08:00
spacing=2,size_hint=(1,None))
2022-01-27 11:07:46 +08:00
self._inner.col_force_default = True
self._inner.col_default_width = self.box_width
2021-08-15 16:57:20 +08:00
super(VResponsiveLayout,self).add_widget(self._inner)
self._inner.bind(
minimum_height=self._inner.setter('height'))
2022-01-27 11:07:46 +08:00
self.setCols()
2021-08-15 16:57:20 +08:00
self.bind(pos=self.setCols,size=self.setCols)
2022-01-27 11:07:46 +08:00
def on_box_width(self, *args):
if not self._inner:
return
self._inner.col_default_width = self.box_width
for w in self._inner.children:
w.size_hint_x = None
w.width = self.box_width
self.setCols()
2021-08-15 16:57:20 +08:00
def on_orientation(self,o):
self.setCols()
def add_widget(self,widget,**kw):
a = self._inner.add_widget(widget,**kw)
return a
def clear_widgets(self,**kw):
a = self._inner.clear_widgets(**kw)
def remove_widget(self,widget,**kw):
a = self._inner.remove_widget(widget,**kw)
return a
def setCols(self,*args):
2022-01-27 11:07:46 +08:00
cols = floor(self.width / self.box_width)
2021-08-15 16:57:20 +08:00
if cols < 1:
cols = 1
self._inner.cols = cols