LibGUI: Add Widget focus proxies

A Widget can now have a focus proxy widget. Questions about focus are
redirected to the proxy if present. This is useful if a widget could
logically get focus, but wants one of its child widgets to actually
handle it.
This commit is contained in:
Andreas Kling 2020-08-25 11:21:49 +02:00
parent 9ba3862ee9
commit af16552624
Notes: sideshowbarker 2024-07-19 03:09:38 +09:00
2 changed files with 23 additions and 0 deletions

View file

@ -499,8 +499,22 @@ void Widget::set_window(Window* window)
m_window = window;
}
void Widget::set_focus_proxy(Widget* proxy)
{
if (m_focus_proxy == proxy)
return;
if (proxy)
m_focus_proxy = proxy->make_weak_ptr();
else
m_focus_proxy = nullptr;
}
bool Widget::is_focused() const
{
if (m_focus_proxy)
return m_focus_proxy->is_focused();
auto* win = window();
if (!win)
return false;
@ -514,6 +528,9 @@ bool Widget::is_focused() const
void Widget::set_focus(bool focus, FocusSource source)
{
if (m_focus_proxy)
return m_focus_proxy->set_focus(focus, source);
auto* win = window();
if (!win)
return;

View file

@ -150,6 +150,10 @@ public:
bool is_focused() const;
void set_focus(bool, FocusSource = FocusSource::Programmatic);
Widget* focus_proxy() { return m_focus_proxy; }
const Widget* focus_proxy() const { return m_focus_proxy; }
void set_focus_proxy(Widget*);
enum class ShouldRespectGreediness { No = 0,
Yes };
struct HitTestResult {
@ -340,6 +344,8 @@ private:
bool m_accepts_emoji_input { false };
NonnullRefPtr<Gfx::PaletteImpl> m_palette;
WeakPtr<Widget> m_focus_proxy;
};
inline Widget* Widget::parent_widget()