diff options
author | Andreas Kling <kling@serenityos.org> | 2020-10-30 16:57:51 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-10-30 17:03:29 +0100 |
commit | ddad7575a9d227b2bd19b6d840f545bfdd2b993d (patch) | |
tree | 7dfda995f33b9f9a32c38bc71690f39167f1fec3 /Libraries/LibGUI/Window.cpp | |
parent | 28df2ede063336332516101f8887905e7c08683f (diff) | |
download | serenity-ddad7575a9d227b2bd19b6d840f545bfdd2b993d.zip |
LibGUI: Improve automatic focus guessing somewhat
When opening a new window, we'll now try to find a suitable widget for
initial focus by picking the first available mouse-focusable one.
Whenever you press the tab key in a window with no focused widget,
we'll attempt to find a keyboard-focusable widget and give it focus.
This should make all applications keyboard-interactive immediately
without having to manually place focus with the mouse.
Diffstat (limited to 'Libraries/LibGUI/Window.cpp')
-rw-r--r-- | Libraries/LibGUI/Window.cpp | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/Libraries/LibGUI/Window.cpp b/Libraries/LibGUI/Window.cpp index 7b8e6cd901..9ca5b352f2 100644 --- a/Libraries/LibGUI/Window.cpp +++ b/Libraries/LibGUI/Window.cpp @@ -356,6 +356,11 @@ void Window::handle_multi_paint_event(MultiPaintEvent& event) void Window::handle_key_event(KeyEvent& event) { + if (!m_focused_widget && event.type() == Event::KeyDown && event.key() == Key_Tab && !event.ctrl() && !event.alt() && !event.logo()) { + focus_a_widget_if_possible(FocusSource::Keyboard); + return; + } + if (m_focused_widget) return m_focused_widget->dispatch_event(event, this); if (m_main_widget) @@ -864,10 +869,10 @@ void Window::set_resize_aspect_ratio(const Optional<Gfx::IntSize>& ratio) WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowResizeAspectRatio>(m_window_id, m_resize_aspect_ratio); } -void Window::did_add_widget(Badge<Widget>, Widget& widget) +void Window::did_add_widget(Badge<Widget>, Widget&) { - if (!m_focused_widget && widget.focus_policy() != FocusPolicy::NoFocus) - set_focused_widget(&widget); + if (!m_focused_widget) + focus_a_widget_if_possible(FocusSource::Mouse); } void Window::did_remove_widget(Badge<Widget>, Widget& widget) @@ -907,4 +912,11 @@ void Window::update_cursor() WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowCursor>(m_window_id, (u32)m_effective_cursor); } +void Window::focus_a_widget_if_possible(FocusSource source) +{ + auto focusable_widgets = this->focusable_widgets(source); + if (!focusable_widgets.is_empty()) + set_focused_widget(focusable_widgets[0], source); +} + } |