Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove code duplication in Button #93389

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 24 additions & 50 deletions scene/gui/button.cpp
Original file line number Diff line number Diff line change
@@ -53,73 +53,39 @@ void Button::_queue_update_size_cache() {
void Button::_update_theme_item_cache() {
Control::_update_theme_item_cache();

theme_cache.max_style_size = Vector2();
theme_cache.style_margin_left = 0;
theme_cache.style_margin_right = 0;
theme_cache.style_margin_top = 0;
theme_cache.style_margin_bottom = 0;

const bool rtl = is_layout_rtl();
if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have theme cache here I wonder if we can replace this check with a check against:

Suggested change
if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) {
if (rtl && theme_cache.normal_mirrored.is_valid()) {

I'm not extremely well versed with the theme system but looking at the code it seems it should be equivalent

Copy link
Member Author

@KoBeWi KoBeWi Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about that, see #84944 (comment)

Copy link
Member

@AThousandShips AThousandShips Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Though I realized (hence unresolving) that if they aren't equivalent this code will segfault, so even if it's not a 100% drop in it does handle potential crashes, hasn't been a problem in the past though it seems

theme_cache.max_style_size = theme_cache.normal_mirrored->get_minimum_size();
theme_cache.style_margin_left = theme_cache.normal_mirrored->get_margin(SIDE_LEFT);
theme_cache.style_margin_right = theme_cache.normal_mirrored->get_margin(SIDE_RIGHT);
theme_cache.style_margin_top = theme_cache.normal_mirrored->get_margin(SIDE_TOP);
theme_cache.style_margin_bottom = theme_cache.normal_mirrored->get_margin(SIDE_BOTTOM);
_update_style_margins(theme_cache.normal_mirrored);
} else {
theme_cache.max_style_size = theme_cache.normal->get_minimum_size();
theme_cache.style_margin_left = theme_cache.normal->get_margin(SIDE_LEFT);
theme_cache.style_margin_right = theme_cache.normal->get_margin(SIDE_RIGHT);
theme_cache.style_margin_top = theme_cache.normal->get_margin(SIDE_TOP);
theme_cache.style_margin_bottom = theme_cache.normal->get_margin(SIDE_BOTTOM);
_update_style_margins(theme_cache.normal);
}
if (has_theme_stylebox("hover_pressed")) {
if (rtl && has_theme_stylebox(SNAME("hover_pressed_mirrored"))) {
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover_pressed_mirrored->get_minimum_size());
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover_pressed_mirrored->get_margin(SIDE_LEFT));
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover_pressed_mirrored->get_margin(SIDE_RIGHT));
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover_pressed_mirrored->get_margin(SIDE_TOP));
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover_pressed_mirrored->get_margin(SIDE_BOTTOM));
_update_style_margins(theme_cache.hover_pressed_mirrored);
} else {
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover_pressed->get_minimum_size());
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover_pressed->get_margin(SIDE_LEFT));
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover_pressed->get_margin(SIDE_RIGHT));
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover_pressed->get_margin(SIDE_TOP));
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover_pressed->get_margin(SIDE_BOTTOM));
_update_style_margins(theme_cache.hover_pressed);
}
}
if (rtl && has_theme_stylebox(SNAME("pressed_mirrored"))) {
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.pressed_mirrored->get_minimum_size());
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.pressed_mirrored->get_margin(SIDE_LEFT));
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.pressed_mirrored->get_margin(SIDE_RIGHT));
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.pressed_mirrored->get_margin(SIDE_TOP));
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.pressed_mirrored->get_margin(SIDE_BOTTOM));
_update_style_margins(theme_cache.pressed_mirrored);
} else {
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.pressed->get_minimum_size());
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.pressed->get_margin(SIDE_LEFT));
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.pressed->get_margin(SIDE_RIGHT));
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.pressed->get_margin(SIDE_TOP));
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.pressed->get_margin(SIDE_BOTTOM));
_update_style_margins(theme_cache.pressed);
}
if (rtl && has_theme_stylebox(SNAME("hover_mirrored"))) {
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover_mirrored->get_minimum_size());
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover_mirrored->get_margin(SIDE_LEFT));
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover_mirrored->get_margin(SIDE_RIGHT));
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover_mirrored->get_margin(SIDE_TOP));
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover_mirrored->get_margin(SIDE_BOTTOM));
_update_style_margins(theme_cache.hover_mirrored);
} else {
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover->get_minimum_size());
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover->get_margin(SIDE_LEFT));
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover->get_margin(SIDE_RIGHT));
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover->get_margin(SIDE_TOP));
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover->get_margin(SIDE_BOTTOM));
_update_style_margins(theme_cache.hover);
}
if (rtl && has_theme_stylebox(SNAME("disabled_mirrored"))) {
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.disabled_mirrored->get_minimum_size());
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.disabled_mirrored->get_margin(SIDE_LEFT));
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.disabled_mirrored->get_margin(SIDE_RIGHT));
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.disabled_mirrored->get_margin(SIDE_TOP));
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.disabled_mirrored->get_margin(SIDE_BOTTOM));
_update_style_margins(theme_cache.disabled_mirrored);
} else {
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.disabled->get_minimum_size());
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.disabled->get_margin(SIDE_LEFT));
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.disabled->get_margin(SIDE_RIGHT));
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.disabled->get_margin(SIDE_TOP));
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.disabled->get_margin(SIDE_BOTTOM));
_update_style_margins(theme_cache.disabled);
}
theme_cache.max_style_size = theme_cache.max_style_size.max(Vector2(theme_cache.style_margin_left + theme_cache.style_margin_right, theme_cache.style_margin_top + theme_cache.style_margin_bottom));
}
@@ -672,6 +638,14 @@ void Button::_texture_changed() {
update_minimum_size();
}

void Button::_update_style_margins(const Ref<StyleBox> &p_stylebox) {
theme_cache.max_style_size = theme_cache.max_style_size.max(p_stylebox->get_minimum_size());
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, p_stylebox->get_margin(SIDE_LEFT));
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, p_stylebox->get_margin(SIDE_RIGHT));
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, p_stylebox->get_margin(SIDE_TOP));
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, p_stylebox->get_margin(SIDE_BOTTOM));
}

Ref<Texture2D> Button::get_icon() const {
return icon;
}
1 change: 1 addition & 0 deletions scene/gui/button.h
Original file line number Diff line number Diff line change
@@ -104,6 +104,7 @@ class Button : public BaseButton {

void _shape(Ref<TextParagraph> p_paragraph = Ref<TextParagraph>(), String p_text = "");
void _texture_changed();
void _update_style_margins(const Ref<StyleBox> &p_stylebox);

protected:
virtual void _update_theme_item_cache() override;