Skip to content

Commit

Permalink
Fixed top app bar trailing icons resizing and hover behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
chillibasket committed Oct 24, 2024
1 parent 5ff9d0d commit e73ddda
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 169 deletions.
39 changes: 16 additions & 23 deletions kivymd/uix/appbar/appbar.kv
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<MDTopAppBarLeadingButtonContainer>
size_hint_x: None
width: self.minimum_width
padding: "8dp", 0, "16dp", 0
padding: "8dp", 0, 0, 0


<MDTopAppBarTrailingButtonContainer>
size_hint_x: None
width: self.minimum_width
padding: "16dp", 0, "16dp", 0
padding: 0, 0, "8dp", 0
spacing: "4dp"


Expand All @@ -20,13 +20,10 @@


<MDTopAppBarTitle>
padding:
[
self._appbar._left_padding if self._appbar else 0,
0,
self._appbar._right_padding if self._appbar else 0,
0,
]
text_size: self.size
halign: "left"
valign: "center"
padding: 0, "5dp", 0, 0
font_style:
{ \
"small": "Title", \
Expand All @@ -39,20 +36,8 @@
"medium": "small", \
"large": "medium", \
}[self._appbar.type if self._appbar else "large"]
adaptive_width:
( \
True \
if self._appbar.type == "small" else \
False \
) \
if self._appbar else True
size_hint_x:
( \
None \
if self._appbar.type == "small" else \
1 \
) \
if self._appbar else None
adaptive_width: True
size_hint_x: 1


<MDTopAppBar>
Expand Down Expand Up @@ -82,9 +67,17 @@
BoxLayout:
id: root_box

BoxLayout:
id: text_box
padding: "16dp", 0, "16dp", 0

BoxLayout:
id: title_box
padding: "16dp", 0, "16dp", 0
size_hint:
(0, 0) \
if self.parent.type == "small" else \
(1, 1)


<MDFabBottomAppBarButton>
Expand Down
150 changes: 4 additions & 146 deletions kivymd/uix/appbar/appbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ def add_widget(self, widget, *args, **kwargs):
return super().add_widget(widget)

def _check_icon_color(self, widget):
if widget.theme_icon_color == "Primary" and widget.icon_color == None:
if widget.theme_icon_color == "Primary" and widget.icon_color is None:
widget.theme_icon_color = "Custom"
widget.icon_color = widget.theme_cls.onSurfaceColor

Expand Down Expand Up @@ -625,25 +625,6 @@ class MDTopAppBarTitle(MDLabel):
"""

_appbar = ObjectProperty()
_title_width = NumericProperty(0)

# FIXME: When changing the text, the texture_size property returns an
# incorrect value, which causes the panel title to shift.
def on_text(self, instance, value) -> None:
"""Fired when the :attr:`text` value changes."""

def set_title_width(*args) -> None:
self._title_width = self.texture_size[0]

Clock.schedule_once(set_title_width)

def on_pos_hint(self, instance, value) -> None:
"""Fired when the :attr:`pos_hint` value changes."""

if self._appbar:
Clock.schedule_once(
lambda x: self._appbar._set_padding_title(value)
)


class MDTopAppBarLeadingButtonContainer(BaseTopAppBarButtonContainer):
Expand Down Expand Up @@ -672,11 +653,6 @@ class MDTopAppBarTrailingButtonContainer(BaseTopAppBarButtonContainer):
"""


# FIXME: The on_enter/on_leave event is not triggered for
# MDActionTopAppBarButton buttons in the MDTopAppBarTrailingButtonContainer
# container.
# When the screen size is changed on desktop devices, the position of the
# trailing container is shifted until the screen size change is completed.
class MDTopAppBar(
DeclarativeBehavior,
ThemableBehavior,
Expand Down Expand Up @@ -731,150 +707,32 @@ class MDTopAppBar(
_trailing_button_container = ObjectProperty()
_leading_button_container = ObjectProperty()
_appbar_title = ObjectProperty()
_right_padding = NumericProperty(0)
_left_padding = NumericProperty(0)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Clock.schedule_once(lambda x: self.on_size(self, (0, 0)))

def on_type(self, instance, value) -> None:
def on_type(*args):
if value in ("medium", "large"):
self.ids.root_box.add_widget(Widget(), index=1)

Clock.schedule_once(on_type, 0.5)

def on_size(self, instance, size) -> None:
"""Fired when the application screen size changes."""

if self._appbar_title:
if not self._appbar_title._title_width:
self._appbar_title._title_width = (
self._appbar_title.texture_size[0]
)
self._right_padding = 0
self._left_padding = 0
self._appbar_title.on_pos_hint(
self._appbar_title, self._appbar_title.pos_hint
)

def add_widget(self, widget, *args, **kwargs):
if isinstance(widget, MDTopAppBarTitle):
widget._appbar = self
self._appbar_title = widget
widget.bind(text=lambda *x: self.on_size(*x))
Clock.schedule_once(lambda x: self._add_title(widget))
elif isinstance(widget, MDTopAppBarTrailingButtonContainer):
self._trailing_button_container = widget
widget._appbar = self
Clock.schedule_once(lambda x: self.ids.root_box.add_widget(widget))
elif isinstance(widget, MDTopAppBarLeadingButtonContainer):
widget._appbar = self
self._leading_button_container = widget
Clock.schedule_once(lambda x: self.ids.root_box.add_widget(widget))
widget._appbar = self
Clock.schedule_once(lambda x: self.ids.root_box.add_widget(widget, len(self.ids.root_box.children)))
else:
return super().add_widget(widget)

def _add_title(self, widget):
if self.type == "small":
self.ids.root_box.add_widget(widget)
self.ids.text_box.add_widget(widget)
else:
self.ids.title_box.add_widget(widget)

def _set_padding_title(self, value):
if value.get("center_x", 0) == 0.5 and self.type == "small":
if (
not self._trailing_button_container
and self._leading_button_container
):
self._left_padding = (
(self.width // 2)
- (
self._leading_button_container.width
+ (self._appbar_title._title_width // 2)
)
) - self._left_padding
elif (
self._trailing_button_container
and not self._leading_button_container
):
self._left_padding = (
(self.width // 2) - (self._appbar_title._title_width // 2)
) - self._left_padding
self._right_padding = (
(self.width // 2)
- (
self._trailing_button_container.width
+ (self._appbar_title._title_width // 2)
)
) - self._right_padding
elif (
not self._trailing_button_container
and not self._leading_button_container
):
self._left_padding = (
(self.width // 2) - (self._appbar_title._title_width // 2)
) - self._left_padding
self._right_padding = (
(self.width // 2) - (self._appbar_title._title_width // 2)
) - self._right_padding
elif (
self._trailing_button_container
and self._leading_button_container
):
self._left_padding = (
(self.width // 2)
- (
self._leading_button_container.width
+ (self._appbar_title._title_width // 2)
)
) - self._left_padding
self._right_padding = (
(self.width // 2)
- (
self._trailing_button_container.width
+ (self._appbar_title._title_width // 2)
)
) - self._right_padding
elif (
not value
and self._trailing_button_container
and self._leading_button_container
):
if self.type == "small":

self._right_padding = (
self.width
- (
self._trailing_button_container.width
+ self._leading_button_container.width
+ self._appbar_title._title_width
)
- self._right_padding
)
elif (
not value
and self._trailing_button_container
and not self._leading_button_container
):
if self.type == "small":
self._right_padding = (
self.width
- (
self._trailing_button_container.width
+ self._appbar_title._title_width
)
- self._right_padding
)
self._left_padding = dp(16)
elif (
not value
and not self._trailing_button_container
and not self._leading_button_container
):
self._left_padding = dp(16)


class MDBottomAppBar(
DeclarativeBehavior,
Expand Down

0 comments on commit e73ddda

Please sign in to comment.