Skip to content

Commit 4ad9114

Browse files
committed
Merge pull request #111386 from YeldhamDev/scrcon_focus_fix
Fix incorrect margins in `ScrollContainer` with focus border enabled
2 parents 1cf0bc2 + e9a5208 commit 4ad9114

File tree

2 files changed

+46
-64
lines changed

2 files changed

+46
-64
lines changed

scene/gui/scroll_container.cpp

Lines changed: 44 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,8 @@ Size2 ScrollContainer::get_minimum_size() const {
7373
}
7474
}
7575

76-
Size2 panel_size = theme_cache.panel_style->get_minimum_size();
77-
min_size += panel_size;
78-
if (draw_focus_border) {
79-
Size2 focus_size = theme_cache.focus_style->get_minimum_size();
80-
// Only update the minimum size if the focus style's minimum size doesn't fit into the panel style's minimum size.
81-
if (focus_size > panel_size) {
82-
min_size += focus_size - panel_size;
83-
}
84-
}
76+
Rect2 margins = _get_margins();
77+
min_size += margins.position + margins.size;
8578

8679
return min_size;
8780
}
@@ -111,6 +104,34 @@ bool ScrollContainer::_is_v_scroll_visible() const {
111104
return v_scroll->is_visible() && v_scroll->get_parent() == this;
112105
}
113106

107+
Rect2 ScrollContainer::_get_margins() const {
108+
float right_margin = theme_cache.panel_style->get_margin(SIDE_RIGHT);
109+
float left_margin = theme_cache.panel_style->get_margin(SIDE_LEFT);
110+
float top_margin = theme_cache.panel_style->get_margin(SIDE_TOP);
111+
float bottom_margin = theme_cache.panel_style->get_margin(SIDE_BOTTOM);
112+
if (draw_focus_border) {
113+
// Only update margins if the focus style's margins don't fit into the panel style's margins.
114+
float focus_margin = theme_cache.focus_style->get_margin(SIDE_RIGHT);
115+
if (focus_margin > right_margin) {
116+
right_margin = focus_margin;
117+
}
118+
focus_margin = theme_cache.focus_style->get_margin(SIDE_LEFT);
119+
if (focus_margin > left_margin) {
120+
left_margin = focus_margin;
121+
}
122+
focus_margin = theme_cache.focus_style->get_margin(SIDE_TOP);
123+
if (focus_margin > top_margin) {
124+
top_margin = focus_margin;
125+
}
126+
focus_margin = theme_cache.focus_style->get_margin(SIDE_BOTTOM);
127+
if (focus_margin > bottom_margin) {
128+
bottom_margin = focus_margin;
129+
}
130+
}
131+
132+
return Rect2(left_margin, top_margin, right_margin, bottom_margin);
133+
}
134+
114135
void ScrollContainer::gui_input(const Ref<InputEvent> &p_gui_input) {
115136
ERR_FAIL_COND(p_gui_input.is_null());
116137

@@ -268,45 +289,23 @@ void ScrollContainer::_update_scrollbar_position() {
268289
return;
269290
}
270291

271-
float right_margin = theme_cache.panel_style->get_margin(SIDE_RIGHT);
272-
float left_margin = theme_cache.panel_style->get_margin(SIDE_LEFT);
273-
float top_margin = theme_cache.panel_style->get_margin(SIDE_TOP);
274-
float bottom_margin = theme_cache.panel_style->get_margin(SIDE_BOTTOM);
275-
if (draw_focus_border) {
276-
// Only update margins if the focus style's margins don't fit into the panel style's margins.
277-
float focus_margin = theme_cache.focus_style->get_margin(SIDE_RIGHT);
278-
if (focus_margin > right_margin) {
279-
right_margin += focus_margin;
280-
}
281-
focus_margin = theme_cache.focus_style->get_margin(SIDE_LEFT);
282-
if (focus_margin > left_margin) {
283-
left_margin += focus_margin;
284-
}
285-
focus_margin = theme_cache.focus_style->get_margin(SIDE_TOP);
286-
if (focus_margin > top_margin) {
287-
top_margin += focus_margin;
288-
}
289-
focus_margin = theme_cache.focus_style->get_margin(SIDE_BOTTOM);
290-
if (focus_margin > bottom_margin) {
291-
bottom_margin += focus_margin;
292-
}
293-
}
292+
Rect2 margins = _get_margins();
294293

295294
Size2 hmin = h_scroll->is_visible() ? h_scroll->get_combined_minimum_size() : Size2();
296295
Size2 vmin = v_scroll->is_visible() ? v_scroll->get_combined_minimum_size() : Size2();
297296

298-
int lmar = is_layout_rtl() ? right_margin : left_margin;
299-
int rmar = is_layout_rtl() ? left_margin : right_margin;
297+
int lmar = is_layout_rtl() ? margins.size.x : margins.position.x;
298+
int rmar = is_layout_rtl() ? margins.position.x : margins.size.x;
300299

301300
h_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, lmar);
302301
h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -rmar - vmin.width);
303-
h_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height - bottom_margin);
304-
h_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -bottom_margin);
302+
h_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height - margins.size.y);
303+
h_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -margins.size.y);
305304

306305
v_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width - rmar);
307306
v_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -rmar);
308-
v_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, top_margin);
309-
v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -hmin.height - bottom_margin);
307+
v_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, margins.position.y);
308+
v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -hmin.height - margins.size.y);
310309

311310
_updating_scrollbars = false;
312311
}
@@ -344,24 +343,11 @@ void ScrollContainer::ensure_control_visible(Control *p_control) {
344343

345344
void ScrollContainer::_reposition_children() {
346345
update_scrollbars();
347-
Size2 size = get_size();
348-
Point2 ofs;
349346

350-
Size2 panel_size = theme_cache.panel_style->get_minimum_size();
351-
Point2 panel_offset = theme_cache.panel_style->get_offset();
352-
size -= panel_size;
353-
ofs += panel_offset;
354-
if (draw_focus_border) {
355-
// Only update the size and offset if focus style's doesn't fit into the panel style's.
356-
Size2 focus_size = theme_cache.focus_style->get_minimum_size();
357-
if (focus_size > panel_size) {
358-
size -= focus_size - panel_size;
359-
}
360-
Point2 focus_offset = theme_cache.focus_style->get_offset();
361-
if (focus_offset > panel_offset) {
362-
ofs += focus_offset - panel_offset;
363-
}
364-
}
347+
Rect2 margins = _get_margins();
348+
Size2 size = get_size();
349+
size -= margins.position + margins.size;
350+
Point2 ofs = margins.position;
365351

366352
bool rtl = is_layout_rtl();
367353
bool reserve_vscroll = _is_v_scroll_visible() || vertical_scroll_mode == SCROLL_MODE_RESERVE;
@@ -594,16 +580,10 @@ void ScrollContainer::_notification(int p_what) {
594580
}
595581

596582
void ScrollContainer::update_scrollbars() {
583+
Rect2 margins = _get_margins();
584+
597585
Size2 size = get_size();
598-
Size2 panel_size = theme_cache.panel_style->get_minimum_size();
599-
size -= panel_size;
600-
if (draw_focus_border) {
601-
Size2 focus_size = theme_cache.focus_style->get_minimum_size();
602-
// Only update the size if the focus style's minimum size doesn't fit into the panel style's minimum size.
603-
if (focus_size > panel_size) {
604-
size -= focus_size - panel_size;
605-
}
606-
}
586+
size -= margins.position + margins.size;
607587

608588
Size2 hmin = h_scroll->get_combined_minimum_size();
609589
Size2 vmin = v_scroll->get_combined_minimum_size();

scene/gui/scroll_container.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class ScrollContainer : public Container {
8585
bool _is_h_scroll_visible() const;
8686
bool _is_v_scroll_visible() const;
8787

88+
Rect2 _get_margins() const;
89+
8890
bool draw_focus_border = false;
8991
bool focus_border_is_drawn = false;
9092
bool child_has_focus();

0 commit comments

Comments
 (0)