summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-05-21 17:26:09 +0200
committerAndreas Kling <kling@serenityos.org>2020-05-21 17:26:09 +0200
commit4806cd122d3cebcd8808b2a7c3f37a83d1e193bf (patch)
treef326057924cb1fd0a1db470f896a75ba2e4c38ed
parent65d94a1e74259a23d189195a9d9d9c17988cf078 (diff)
downloadserenity-4806cd122d3cebcd8808b2a7c3f37a83d1e193bf.zip
LibGUI: Focus the first focusable widget added to a window
It feels really awkward if nothing is focused when opening a window.
-rw-r--r--Libraries/LibGUI/Widget.cpp2
-rw-r--r--Libraries/LibGUI/Window.cpp8
-rw-r--r--Libraries/LibGUI/Window.h3
3 files changed, 11 insertions, 2 deletions
diff --git a/Libraries/LibGUI/Widget.cpp b/Libraries/LibGUI/Widget.cpp
index 06fd723fda..661f77f638 100644
--- a/Libraries/LibGUI/Widget.cpp
+++ b/Libraries/LibGUI/Widget.cpp
@@ -115,6 +115,8 @@ void Widget::child_event(Core::ChildEvent& event)
else
layout()->add_widget(Core::to<Widget>(*event.child()));
}
+ if (window() && event.child() && Core::is<Widget>(*event.child()))
+ window()->did_add_widget({}, Core::to<Widget>(*event.child()));
}
if (event.type() == Event::ChildRemoved) {
if (layout()) {
diff --git a/Libraries/LibGUI/Window.cpp b/Libraries/LibGUI/Window.cpp
index c0607948e4..805c8353f0 100644
--- a/Libraries/LibGUI/Window.cpp
+++ b/Libraries/LibGUI/Window.cpp
@@ -761,7 +761,13 @@ void Window::set_size_increment(const Gfx::Size& size_increment)
WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement>(m_window_id, m_base_size, m_size_increment);
}
-void Window::did_remove_widget(Badge<Widget>, const Widget& widget)
+void Window::did_add_widget(Badge<Widget>, Widget& widget)
+{
+ if (!m_focused_widget && widget.accepts_focus())
+ set_focused_widget(&widget);
+}
+
+void Window::did_remove_widget(Badge<Widget>, Widget& widget)
{
if (m_focused_widget == &widget)
m_focused_widget = nullptr;
diff --git a/Libraries/LibGUI/Window.h b/Libraries/LibGUI/Window.h
index 0f3750253d..c2ca561f60 100644
--- a/Libraries/LibGUI/Window.h
+++ b/Libraries/LibGUI/Window.h
@@ -183,7 +183,8 @@ public:
Action* action_for_key_event(const KeyEvent&);
- void did_remove_widget(Badge<Widget>, const Widget&);
+ void did_add_widget(Badge<Widget>, Widget&);
+ void did_remove_widget(Badge<Widget>, Widget&);
Window* find_parent_window();