Skip to content

Instantly share code, notes, and snippets.

@gottadiveintopython
Last active May 31, 2020 03:33
Show Gist options
  • Save gottadiveintopython/513629555a6eb83d8fec45a93725b958 to your computer and use it in GitHub Desktop.
Save gottadiveintopython/513629555a6eb83d8fec45a93725b958 to your computer and use it in GitHub Desktop.
manually tests RecycleGridLayout
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.factory import Factory
from kivy.app import runTouchApp
from kivy.uix.recyclegridlayout import RecycleGridLayout
internal_attr_names = (
'_has_hint_bound_x',
'_has_hint_bound_y',
'_cols_min_size_none',
'_rows_min_size_none',
'_cols',
'_cols_sh',
'_cols_sh_min',
'_cols_sh_max',
'_rows',
'_rows_sh',
'_rows_sh_min',
'_rows_sh_max',
'_cols_pos',
'_rows_pos',
'_cols_count',
'_rows_count',
)
def print_internal_attrs(gl):
s = '\n'.join(
f"{name}: {getattr(gl, name)}"
for name in internal_attr_names
if hasattr(gl, name)
)
print(s, '\n')
class DebugRecycleGridLayout(RecycleGridLayout):
pass
def wrap(func):
def wrapper(self, *args, **kwargs):
print("------------------------------------------")
r = func(self, *args, **kwargs)
print(f"after '{func.__name__}()' was called")
print_internal_attrs(self)
return r
return wrapper
for method_name in (
# "_init_rows_cols_sizes",
# "_fill_rows_cols_sizes",
# "_update_minimum_size",
# "_finalize_rows_cols_sizes",
"_update_rows_cols_sizes",
# "compute_visible_views",
):
method = getattr(DebugRecycleGridLayout, method_name)
setattr(DebugRecycleGridLayout, method_name, wrap(method))
KV_CODE = '''
#:import random random
<Separator@Widget>:
size: 1, 1
canvas:
Color:
rgb: 1, 0, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
BoxLayout:
size_hint_x: .2
size_hint_min_x: 300
orientation: 'vertical'
Label:
text: 'orientation'
color: 0, 1, 0, 1
BoxLayout:
CheckBox:
group: 'orientation'
on_active: gl.orientation = 'lr-tb'
Label:
text: 'lr-tb'
BoxLayout:
CheckBox:
group: 'orientation'
on_active: gl.orientation = 'lr-bt'
Label:
text: 'lr-bt'
BoxLayout:
CheckBox:
group: 'orientation'
on_active: gl.orientation = 'rl-tb'
Label:
text: 'rl-tb'
BoxLayout:
CheckBox:
group: 'orientation'
on_active: gl.orientation = 'rl-bt'
Label:
text: 'rl-bt'
BoxLayout:
CheckBox:
group: 'orientation'
on_active: gl.orientation = 'tb-lr'
Label:
text: 'tb-lr'
BoxLayout:
CheckBox:
group: 'orientation'
on_active: gl.orientation = 'tb-rl'
Label:
text: 'tb-rl'
BoxLayout:
CheckBox:
group: 'orientation'
on_active: gl.orientation = 'bt-lr'
Label:
text: 'bt-lr'
BoxLayout:
CheckBox:
group: 'orientation'
on_active: gl.orientation = 'bt-rl'
Label:
text: 'bt-rl'
Separator:
size_hint_y: None
Label:
text: 'number of columns\\n{}'.format(int(n_cols.value) or None)
halign: 'center'
color: 0, 1, 0, 1
Slider:
id: n_cols
min: 0
max: 10
step: 1
value: 10
Separator:
size_hint_y: None
height: 1
Label:
text: 'number of rows\\n{}'.format(int(n_rows.value) or None)
halign: 'center'
color: 0, 1, 0, 1
Slider:
id: n_rows
min: 0
max: 10
step: 1
value: 10
Separator:
size_hint_y: None
height: 1
Separator:
size_hint_y: None
height: 1
Button:
text: "trigger _update_rows_cols_sizes()"
on_press: random.choice(gl.children).size = (50, 50, )
Button:
text: 'random number of children'
on_press:
rv.data = (
{'text': str(i)} for i in range(random.randint(2, 40))
)
Separator:
size_hint_x: None
RecycleView:
id: rv
viewclass: 'Button'
data: ({'text': str(i)} for i in range(20))
DebugRecycleGridLayout:
id: gl
spaing: 5
padding: 5
default_size_hint: None, None
default_size: 100, 100
size_hint: None, None
size: self.minimum_size
cols: int(n_cols.value) or None
rows: int(n_rows.value) or None
'''
root = Builder.load_string(KV_CODE)
runTouchApp(root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment