mirror of
https://github.com/RGBCube/serenity
synced 2025-05-28 09:05:09 +00:00
LibGUI: Add Widget::has_focus_within()
This returns true if the widget has focus, or if one of its descendant widgets does. Use this in StackWidget and TabWidget. This also fixes HackStudio crashing on startup in StackWidget, due to running before the window has a focused widget.
This commit is contained in:
parent
dee639f19b
commit
b11b4b29e9
4 changed files with 18 additions and 2 deletions
|
@ -43,7 +43,7 @@ void StackWidget::set_active_widget(Widget* widget)
|
|||
if (widget == m_active_widget)
|
||||
return;
|
||||
|
||||
bool active_widget_had_focus = m_active_widget && window() && (window()->focused_widget() == m_active_widget || m_active_widget->is_ancestor_of(*window()->focused_widget()));
|
||||
bool active_widget_had_focus = m_active_widget && m_active_widget->has_focus_within();
|
||||
|
||||
if (m_active_widget)
|
||||
m_active_widget->set_visible(false);
|
||||
|
|
|
@ -88,12 +88,13 @@ void TabWidget::update_focus_policy()
|
|||
policy = FocusPolicy::NoFocus;
|
||||
set_focus_policy(policy);
|
||||
}
|
||||
|
||||
void TabWidget::set_active_widget(Widget* widget)
|
||||
{
|
||||
if (widget == m_active_widget)
|
||||
return;
|
||||
|
||||
bool active_widget_had_focus = m_active_widget && window() && (window()->focused_widget() == m_active_widget || m_active_widget->is_ancestor_of(*window()->focused_widget()));
|
||||
bool active_widget_had_focus = m_active_widget && m_active_widget->has_focus_within();
|
||||
|
||||
if (m_active_widget)
|
||||
m_active_widget->set_visible(false);
|
||||
|
|
|
@ -989,4 +989,16 @@ Widget* Widget::find_descendant_by_name(const String& name)
|
|||
});
|
||||
return found_widget;
|
||||
}
|
||||
|
||||
bool Widget::has_focus_within() const
|
||||
{
|
||||
auto* window = this->window();
|
||||
if (!window)
|
||||
return false;
|
||||
if (!window->focused_widget())
|
||||
return false;
|
||||
auto& effective_focus_widget = focus_proxy() ? *focus_proxy() : *this;
|
||||
return window->focused_widget() == &effective_focus_widget || is_ancestor_of(*window->focused_widget());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -165,6 +165,9 @@ public:
|
|||
bool is_focused() const;
|
||||
void set_focus(bool, FocusSource = FocusSource::Programmatic);
|
||||
|
||||
// Returns true if this widget or one of its descendants is focused.
|
||||
bool has_focus_within() const;
|
||||
|
||||
Widget* focus_proxy() { return m_focus_proxy; }
|
||||
const Widget* focus_proxy() const { return m_focus_proxy; }
|
||||
void set_focus_proxy(Widget*);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue