-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtab.py
1626 lines (1301 loc) · 51.4 KB
/
tab.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Components/Tabs
===============
.. seealso::
`Material Design spec, Tabs <https://material.io/components/tabs>`_
.. rubric:: Tabs organize content across different screens, data sets,
and other interactions.
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tabs.png
:align: center
.. Note:: Module provides tabs in the form of icons or text.
Usage
-----
To create a tab, you must create a new class that inherits from the
:class:`~MDTabsBase` class and the `Kivy` container, in which you will create
content for the tab.
.. code-block:: python
class Tab(MDFloatLayout, MDTabsBase):
'''Class implementing content for a tab.'''
content_text = StringProperty("")
.. code-block:: kv
<Tab>
content_text
MDLabel:
text: root.content_text
pos_hint: {"center_x": .5, "center_y": .5}
All tabs must be contained inside a :class:`~MDTabs` widget:
.. code-block:: kv
Root:
MDTabs:
Tab:
title: "Tab 1"
content_text: f"This is an example text for {self.title}"
Tab:
title: "Tab 2"
content_text: f"This is an example text for {self.title}"
...
Example with tab icon
---------------------
.. code-block:: python
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.tab import MDTabsBase
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.icon_definitions import md_icons
KV = '''
MDBoxLayout:
orientation: "vertical"
MDToolbar:
title: "Example Tabs"
MDTabs:
id: tabs
on_tab_switch: app.on_tab_switch(*args)
<Tab>
MDIconButton:
id: icon
icon: root.icon
user_font_size: "48sp"
pos_hint: {"center_x": .5, "center_y": .5}
'''
class Tab(MDFloatLayout, MDTabsBase):
'''Class implementing content for a tab.'''
class Example(MDApp):
icons = list(md_icons.keys())[15:30]
def build(self):
return Builder.load_string(KV)
def on_start(self):
for tab_name in self.icons:
self.root.ids.tabs.add_widget(Tab(icon=tab_name))
def on_tab_switch(
self, instance_tabs, instance_tab, instance_tab_label, tab_text
):
'''
Called when switching tabs.
:type instance_tabs: <kivymd.uix.tab.MDTabs object>;
:param instance_tab: <__main__.Tab object>;
:param instance_tab_label: <kivymd.uix.tab.MDTabsLabel object>;
:param tab_text: text or name icon of tab;
'''
# get the tab icon.
count_icon = instance_tab.icon
# print it on shell/bash.
print(f"Welcome to {count_icon}' tab'")
Example().run()
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tabs-simple-example.gif
:align: center
Example with tab text
---------------------
.. Note:: The :class:`~MDTabsBase` class has an icon parameter and, by default,
tries to find the name of the icon in the file
``kivymd/icon_definitions.py``.
If the name of the icon is not found, the class will send a message
stating that the icon could not be found.
if the tab has no icon, title or tab_label_text, the class will raise a
ValueError.
.. code-block:: python
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.tab import MDTabsBase
KV = '''
MDBoxLayout:
orientation: "vertical"
MDToolbar:
title: "Example Tabs"
MDTabs:
id: tabs
on_tab_switch: app.on_tab_switch(*args)
<Tab>
MDLabel:
id: label
text: "Tab 0"
halign: "center"
'''
class Tab(MDFloatLayout, MDTabsBase):
'''Class implementing content for a tab.'''
class Example(MDApp):
def build(self):
return Builder.load_string(KV)
def on_start(self):
for i in range(20):
self.root.ids.tabs.add_widget(Tab(title=f"Tab {i}"))
def on_tab_switch(
self, instance_tabs, instance_tab, instance_tab_label, tab_text
):
'''Called when switching tabs.
:type instance_tabs: <kivymd.uix.tab.MDTabs object>;
:param instance_tab: <__main__.Tab object>;
:param instance_tab_label: <kivymd.uix.tab.MDTabsLabel object>;
:param tab_text: text or name icon of tab;
'''
instance_tab.ids.label.text = tab_text
Example().run()
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tabs-simple-example-text.gif
:align: center
Example with tab icon and text
------------------------------
.. code-block:: python
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.tab import MDTabsBase
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.icon_definitions import md_icons
KV = '''
MDBoxLayout:
orientation: "vertical"
MDToolbar:
title: "Example Tabs"
MDTabs:
id: tabs
'''
class Tab(MDFloatLayout, MDTabsBase):
pass
class Example(MDApp):
def build(self):
return Builder.load_string(KV)
def on_start(self):
for name_tab in list(md_icons.keys())[15:30]:
self.root.ids.tabs.add_widget(Tab(icon=name_tab, title=name_tab))
Example().run()
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tabs-simple-example-icon-text.png
:align: center
Dynamic tab management
----------------------
.. code-block:: python
from kivy.lang import Builder
from kivy.uix.scrollview import ScrollView
from kivymd.app import MDApp
from kivymd.uix.tab import MDTabsBase
KV = '''
MDBoxLayout:
orientation: "vertical"
MDToolbar:
title: "Example Tabs"
MDTabs:
id: tabs
<Tab>
MDList:
MDBoxLayout:
adaptive_height: True
MDFlatButton:
text: "ADD TAB"
on_release: app.add_tab()
MDFlatButton:
text: "REMOVE LAST TAB"
on_release: app.remove_tab()
MDFlatButton:
text: "GET TAB LIST"
on_release: app.get_tab_list()
'''
class Tab(ScrollView, MDTabsBase):
'''Class implementing content for a tab.'''
class Example(MDApp):
index = 0
def build(self):
return Builder.load_string(KV)
def on_start(self):
self.add_tab()
def get_tab_list(self):
'''Prints a list of tab objects.'''
print(self.root.ids.tabs.get_tab_list())
def add_tab(self):
self.index += 1
self.root.ids.tabs.add_widget(Tab(text=f"{self.index} tab"))
def remove_tab(self):
if self.index > 1:
self.index -= 1
self.root.ids.tabs.remove_widget(
self.root.ids.tabs.get_tab_list()[-1]
)
Example().run()
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tabs-dynamic-managmant.gif
:align: center
Use on_ref_press method
-----------------------
You can use markup for the text of the tabs and use the ``on_ref_press``
method accordingly:
.. code-block:: python
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.font_definitions import fonts
from kivymd.uix.tab import MDTabsBase
from kivymd.icon_definitions import md_icons
KV = '''
MDBoxLayout:
orientation: "vertical"
MDToolbar:
title: "Example Tabs"
MDTabs:
id: tabs
on_ref_press: app.on_ref_press(*args)
<Tab>
MDIconButton:
id: icon
icon: app.icons[0]
user_font_size: "48sp"
pos_hint: {"center_x": .5, "center_y": .5}
'''
class Tab(MDFloatLayout, MDTabsBase):
'''Class implementing content for a tab.'''
class Example(MDApp):
icons = list(md_icons.keys())[15:30]
def build(self):
return Builder.load_string(KV)
def on_start(self):
for name_tab in self.icons:
self.root.ids.tabs.add_widget(
Tab(
text=f"[ref={name_tab}][font={fonts[-1]['fn_regular']}]{md_icons['close']}[/font][/ref] {name_tab}"
)
)
def on_ref_press(
self,
instance_tabs,
instance_tab_label,
instance_tab,
instance_tab_bar,
instance_carousel,
):
'''
The method will be called when the ``on_ref_press`` event
occurs when you, for example, use markup text for tabs.
:param instance_tabs: <kivymd.uix.tab.MDTabs object>
:param instance_tab_label: <kivymd.uix.tab.MDTabsLabel object>
:param instance_tab: <__main__.Tab object>
:param instance_tab_bar: <kivymd.uix.tab.MDTabsBar object>
:param instance_carousel: <kivymd.uix.tab.MDTabsCarousel object>
'''
# Removes a tab by clicking on the close icon on the left.
for instance_tab in instance_carousel.slides:
if instance_tab.text == instance_tab_label.text:
instance_tabs.remove_widget(instance_tab_label)
break
Example().run()
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tabs-on-ref-press.gif
:align: center
Switching the tab by name
-------------------------
.. code-block:: python
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.icon_definitions import md_icons
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.tab import MDTabsBase
KV = '''
MDBoxLayout:
orientation: "vertical"
MDToolbar:
title: "Example Tabs"
MDTabs:
id: tabs
<Tab>
MDBoxLayout:
orientation: "vertical"
pos_hint: {"center_x": .5, "center_y": .5}
size_hint: None, None
spacing: dp(48)
MDIconButton:
id: icon
icon: "arrow-right"
user_font_size: "48sp"
on_release:
app.switch_tab_by_name()
MDIconButton:
id: icon2
icon: "page-next"
user_font_size: "48sp"
on_release:
app.switch_tab_by_object()
'''
class Tab(MDFloatLayout, MDTabsBase):
'''Class implementing content for a tab.'''
class Example(MDApp):
icons = list(md_icons.keys())[15:30]
def build(self):
self.iter_list_names = iter(list(self.icons))
root = Builder.load_string(KV)
return root
def on_start(self):
for name_tab in list(self.icons):
self.root.ids.tabs.add_widget(Tab(tab_label_text=name_tab))
self.iter_list_objects = iter(list(self.root.ids.tabs.get_tab_list()))
def switch_tab_by_object(self):
try:
x = next(self.iter_list_objects)
print(f"Switch slide by object, \n\tnex element to show: [{x}]")
self.root.ids.tabs.switch_tab(x)
except StopIteration:
# reset the iterator an begin again.
self.iter_list_objects = iter(list(self.root.ids.tabs.get_tab_list()))
self.switch_tab_by_object()
pass
def switch_tab_by_name(self):
'''Switching the tab by name.'''
try:
x = next(self.iter_list_names)
print(f"Switch slide by name, \n\tnex element to show: [{x}]")
self.root.ids.tabs.switch_tab(x)
except StopIteration:
# reset the iterator an begin again.
self.iter_list_names = iter(list(self.icons))
self.switch_tab_by_name()
pass
Example().run()
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/switching-tab-by-name.gif
:align: center
"""
__all__ = ("MDTabs", "MDTabsBase")
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.logger import Logger
from kivy.metrics import dp
from kivy.properties import (
AliasProperty,
BooleanProperty,
BoundedNumericProperty,
ColorProperty,
ListProperty,
NumericProperty,
ObjectProperty,
OptionProperty,
StringProperty,
)
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.behaviors import ToggleButtonBehavior
from kivy.uix.scrollview import ScrollView
from kivy.uix.widget import Widget
from kivy.utils import boundary
from kivymd.font_definitions import fonts, theme_font_styles
from kivymd.icon_definitions import md_icons
from kivymd.theming import ThemableBehavior
from kivymd.uix.behaviors import (
FakeRectangularElevationBehavior,
RectangularRippleBehavior,
SpecificBackgroundColorBehavior,
)
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.carousel import MDCarousel
from kivymd.uix.label import MDLabel
Builder.load_string(
"""
#:import DampedScrollEffect kivy.effects.dampedscroll.DampedScrollEffect
<MDTabsLabel>
_set_start_tab: False
size_hint: None, 1
halign: "center"
valign: "center"
group: "tabs"
font: root.font_name
allow_no_selection: False
markup: True
on_width:
if not self._set_start_tab: \
self.tab_bar.parent._update_indicator( \
self.tab_bar.parent.carousel.current_slide.tab_label); \
self._set_start_tab = True
on_tab_bar:
self.text_size = (None, None) \
if self.tab_bar.parent.allow_stretch else (self.width, None)
on_ref_press:
self.tab_bar.parent.dispatch( \
"on_ref_press",
self, \
self.tab, \
self.tab_bar, \
self.tab_bar.parent.carousel)
color:
( \
self.text_color_active \
if self.text_color_active else self.specific_secondary_text_color \
) \
if self.state == "down" else \
( \
self.text_color_normal \
if self.text_color_normal else self.theme_cls.text_color \
)
<MDTabsScrollView>
size_hint: 1, 1
do_scroll_y: False
bar_color: 0, 0, 0, 0
bar_inactive_color: 0, 0, 0, 0
bar_width: 0
effect_cls: DampedScrollEffect
<MDTabs>
carousel: carousel
tab_bar: tab_bar
anchor_y: "top"
background_palette: "Primary"
_line_x: 0
_line_width: 0
_line_height: 0
_line_radius: 0
on_size:
root._update_padding(layout)
MDTabsMain:
padding: 0, tab_bar.height, 0, 0
MDTabsCarousel:
id: carousel
lock_swiping: root.lock_swiping
ignore_perpendicular_swipes: True
anim_move_duration: root.anim_duration
on_index: root.on_carousel_index(*args)
on__offset: tab_bar.android_animation(*args)
MDTabsBar:
id: tab_bar
padding: root.tab_padding
carousel: carousel
scrollview: scrollview
layout: layout
size_hint: 1, None
elevation: root.elevation
height: root.tab_bar_height
md_bg_color:
self.theme_cls.primary_color \
if not root.background_color else \
root.background_color
MDTabsScrollView:
id: scrollview
do_scroll_x: False if layout.width <= self.width else True
MDGridLayout:
id: layout
rows: 1
size_hint_y: 1
adaptive_width: True
on_size: root._update_padding(layout)
canvas.before:
Color:
rgba: root.underline_color
Line:
width: dp(2)
rectangle: [0, 0, layout.width, dp(2)]
Color:
rgba:
root.theme_cls.accent_color \
if not root.indicator_color else \
root.indicator_color
RoundedRectangle:
group: "Indicator_line"
pos: self.pos
size: 0, root.tab_indicator_height
radius: [0,]
Line:
width: dp(2)
rounded_rectangle:
[ \
root._line_x, \
self.pos[1], \
root._line_width, \
root._line_height, \
root._line_radius \
]
"""
)
class MDTabsException(Exception):
pass
class MDTabsLabel(ToggleButtonBehavior, RectangularRippleBehavior, MDLabel):
"""This class it represent the label of each tab."""
text_color_normal = ColorProperty(None)
text_color_active = ColorProperty(None)
tab = ObjectProperty()
tab_bar = ObjectProperty()
font_name = StringProperty("Roboto")
def __init__(self, **kwargs):
self.split_str = " ,-"
super().__init__(**kwargs)
self.max_lines = 2
self.size_hint_x = None
self.size_hint_min_x = dp(90)
self.min_space = dp(98)
self.bind(
text=self._update_text_size,
)
def on_release(self):
self.tab_bar.parent.dispatch("on_tab_switch", self.tab, self, self.text)
# If the label is selected load the relative tab from carousel.
if self.state == "down":
self.tab_bar.parent.carousel.load_slide(self.tab)
def on_texture(self, widget, texture):
# Just save the minimum width of the label based of the content.
if texture:
max_width = dp(360)
min_width = dp(90)
if texture.width > max_width:
self.width = max_width
self.text_size = (max_width, None)
elif texture.width < min_width:
self.width = min_width
else:
self.width = texture.width
def _update_text_size(self, *args):
if not self.tab_bar:
return
if self.tab_bar.parent.allow_stretch is True:
self.text_size = (None, None)
else:
self.width = self.tab_bar.parent.fixed_tab_label_width
self.text_size = (self.width, None)
Clock.schedule_once(self.tab_bar._label_request_indicator_update, 0)
class MDTabsBase(Widget):
"""
This class allow you to create a tab.
You must create a new class that inherits from MDTabsBase.
In this way you have total control over the views of your tabbed panel.
"""
icon = StringProperty()
"""
This property will set the Tab's Label Icon.
:attr:`icon` is an :class:`~kivy.properties.StringProperty`
and defaults to `''`.
"""
title_icon_mode = OptionProperty("Lead", options=["Lead", "Top"])
"""
This property sets the mode in wich the tab's title and icon are shown.
:attr:`title_icon_mode` is an :class:`~kivy.properties.OptionProperty`
and defaults to `'Lead'`.
"""
title = StringProperty()
"""
This property will set the Name of the tab.
.. note::
As a side note.
All tabs have set `markup = True`.
Thanks to this, you can use the kivy markup language to set a colorful
and fully customizable tabs titles.
.. warning::
The material design requires that every title label is written in
capital letters, because of this, the `string.upper()` will be applied
to it's contents.
:attr:`title` is an :class:`~kivy.properties.StringProperty`
and defaults to `''`.
"""
title_is_capital = BooleanProperty(False)
"""
This value controls wether if the title property should be converted to
capital letters.
:attr:`title_is_capital` is an :class:`~kivy.properties.BooleanProperty`
and defaults to `True`.
"""
text = StringProperty(deprecated=True)
"""
This property is the actual title of the tab.
use the property :attr:`icon` and :attr:`title` to set this property
correctly.
This property is kept public for specific and backward compatibility
purposes.
:attr:`text` is an :class:`~kivy.properties.StringProperty`
and defaults to `''`.
.. warning::
This property is deprecated, use :attr:`tab_label_text` instead.
"""
tab_label_text = StringProperty()
"""
This property is the actual title's Label of the tab.
use the property :attr:`icon` and :attr:`title` to set this property
correctly.
This property is kept public for specific and backward compatibility
purposes.
:attr:`tab_label_text` is an :class:`~kivy.properties.StringProperty`
and defaults to `''`.
"""
tab_label = ObjectProperty()
"""
It is the label object reference of the tab.
:attr:`tab_label` is an :class:`~kivy.properties.ObjectProperty`
and defaults to `None`.
"""
def _get_label_font_style(self):
if self.tab_label:
return self.tab_label.font_style
def _set_label_font_style(self, value):
if self.tab_label:
if value in theme_font_styles:
self.tab_label.font_style = value
else:
raise ValueError(
"tab_label_font_style:\n\t"
"font_style not found in theme_font_styles\n\t"
f"font_style = {value}"
)
else:
Clock.schedule_once(lambda x: self._set_label_font_style(value))
return True
tab_label_font_style = AliasProperty(
_get_label_font_style,
_set_label_font_style,
cache=True,
)
"""
:attr:`tab_label_font_style` is an :class:`~kivy.properties.AliasProperty`
that behavies similar to an :class:`~kivy.properties.OptionProperty`.
This property's behavior allows the developer to use any new label style
registered to the app.
This property will affect the Tab's Title Label widget.
"""
def __init__(self, **kwargs):
self.tab_label = MDTabsLabel(tab=self)
super().__init__(**kwargs)
self.bind(
icon=self._update_text,
title=self._update_text,
title_icon_mode=self._update_text,
text=self.update_label_text,
tab_label_text=self.update_label_text,
title_is_capital=self.update_label_text,
)
Clock.schedule_once(
self._update_text
) # this will ensure the text is correct
def _update_text(self, *args):
# Ensures that the title is in capital letters.
if self.title and self.title_is_capital is True:
if self.title != self.title.upper():
self.title = self.title.upper()
# Avoids event recursion.
return
# Add the icon.
if self.icon and self.icon in md_icons:
self.tab_label_text = f"[size=24sp][font={fonts[-1]['fn_regular']}]{md_icons[self.icon]}[/size][/font]"
if self.title:
self.tab_label_text = (
self.text
+ (" " if self.title_icon_mode == "Lead" else "\n")
+ self.title
)
# Add the title.
else:
if self.icon:
Logger.error(
f"{self}: [UID] = [{self.uid}]:\n\t"
f"Icon '{self.icon}' not found in md_icons"
)
if self.title:
self.tab_label_text = self.title
else:
if not self.tab_label_text:
raise ValueError(
f"{self}: [UID] = [{self.uid}]:\n\t"
"No valid Icon was found.\n\t"
"No valid Title was found.\n\t"
f"Icon\t= '{self.icon}'\n\t"
f"Title\t= '{self.title}'\n\t"
)
self.tab_label.padding = dp(16), 0
self.update_label_text(None, self.tab_label_text)
def update_label_text(self, widget, value):
self.tab_label.text = self.text = self.tab_label_text
def on_text(self, widget, text):
self.tab_label_text = self.text
class MDTabsMain(MDBoxLayout):
"""
This class is just a boxlayout that contain the carousel.
It allows you to have control over the carousel.
"""
class MDTabsCarousel(MDCarousel):
lock_swiping = BooleanProperty(False)
"""
If True - disable switching tabs by swipe.
:attr:`lock_swiping` is an :class:`~kivy.properties.BooleanProperty`
and defaults to `False`.
"""
def on_touch_move(self, touch):
if self.lock_swiping: # lock a swiping
return
if not self.touch_mode_change:
if self.ignore_perpendicular_swipes and self.direction in (
"top",
"bottom",
):
if abs(touch.oy - touch.y) < self.scroll_distance:
if abs(touch.ox - touch.x) > self.scroll_distance:
self._change_touch_mode()
self.touch_mode_change = True
elif self.ignore_perpendicular_swipes and self.direction in (
"right",
"left",
):
if abs(touch.ox - touch.x) < self.scroll_distance:
if abs(touch.oy - touch.y) > self.scroll_distance:
self._change_touch_mode()
self.touch_mode_change = True
if self._get_uid("cavoid") in touch.ud:
return
if self._touch is not touch:
super().on_touch_move(touch)
return self._get_uid() in touch.ud
if touch.grab_current is not self:
return True
ud = touch.ud[self._get_uid()]
direction = self.direction[0]
if ud["mode"] == "unknown":
if direction in "rl":
distance = abs(touch.ox - touch.x)
else:
distance = abs(touch.oy - touch.y)
if distance > self.scroll_distance:
ev = self._change_touch_mode_ev
if ev is not None:
ev.cancel()
ud["mode"] = "scroll"
else:
if direction in "rl":
self._offset += touch.dx
if direction in "tb":
self._offset += touch.dy
return True
class MDTabsScrollView(ScrollView):
"""This class hacked version to fix scroll_x manual setting."""
def goto(self, scroll_x, scroll_y):
"""Update event value along with scroll_*."""
def _update(e, x):
if e:
e.value = (e.max + e.min) * x
if not (scroll_x is None):
self.scroll_x = scroll_x
_update(self.effect_x, scroll_x)
if not (scroll_y is None):
self.scroll_y = scroll_y
_update(self.effect_y, scroll_y)
class MDTabsBar(
ThemableBehavior, FakeRectangularElevationBehavior, MDBoxLayout
):
"""
This class is just a boxlayout that contains the scroll view for tabs.
It is also responsible for resizing the tab shortcut when necessary.
"""
target = ObjectProperty(None, allownone=True)
"""
It is the carousel reference of the next tab / slide.
When you go from `'Tab A'` to `'Tab B'`, `'Tab B'` will be the
target tab / slide of the carousel.