MDTopAppBar - Trailing Items positioning and hover behaviour #1746
+22
−171
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of the problem
All of these problems only occur when using the "small" type of the TopAppBar:
The hover behaviour of the trailing container items does not work.
Is there a way to get hover behavior on Top App Bar's action items? #1550
Incorrect positioning of the trailing container items, especially while resizing the window.
Layout of MDTopAppBar does not take effect (MDTopAppBarTrailingButtonContainer should locate to the far right but not work) #1708
Positioning of MDTopAppBar elements #2 #1700
MDFileManager cancel button goes past the right border of window #1678
Positioning of MDTopAppBar elements #1660
The alignment of the TopAppBar title cannot be changed from "left" to "center" or "right",
Positioning of MDTopAppBar elements #2 #1700
Here is a video of the first two issues, using the "examples\appbar.py" test program:
https://github.com/user-attachments/assets/1a8da5d3-7e5f-4097-9dc2-4198f86b397e
Describe the cause of the issue
1. Hover Behaviour
The hover behaviour of the trailing container is being blocked by the
title_box
widget, which in "small" mode is positioned in front of the right-half of the app bar. The hover detection sees that the items are being covered, so it does not activate. Here is an image of thetitle_box
widget highlighted using the Kivy Inspector tool:2. Incorrect Positioning, 3. Title Alignment
The positioning of the trailing items container is controlled by changing the padding of the title text. For example, if the window size in increased, the right padding of the title would also need to be increased. While this implementation might be able to work, it is prone to errors, does not allow the title alignment to be changed, and does not work if there is no title. For example, currently while resizing the window, the padding is not being updated correctly and is set to 0, meaning the trailing items move to the left:
Reproducing the problem
This code is taken from "examples\appbar.py", modified to show the "small", "medium" and "large" Top App Bar types:
Description of Changes
1. Hover Behaviour
The solution is to reduce the
size_hint
of thetitle_box
container to 0 when in "small" mode as it is not being used. Thetitle_box
widget is only used in "medium" and "large" modes:2. Incorrect Positioning
My solution is to remove this "padding" implementation entirely, and instead add an extra
text_box
box layout widget to theroot_box
container. The TopAppBarTitle gets inserted into this new widget, while the leading and trailing item containers are always inserted to the left and right of this widget, respectively. Thetext_box
uses the default Kivy Widget behaviour to fill the window, meaning the Trailing items container is always automatically pushed to the right of the screen; all without any additional Python code! This implementation works even if any of the TopAppBar "leading", "trailing" and "title" elements are missing. Here is the newtext_box
widget highlighted using Kivy Inspector:3. Title Alignment
With the new
text_box
widget, the title area now fills the full width between the leading and trailing containers. This means the "halign" property of MDTopAppBarTitle can be used to change the position of the text, as is standard expected behaviour for Kivy label alignment. Here is an image of the three title alignments:Example code:
Code for testing new changes
Same code as above in the "Reproducing the problem" section.