Kaynağa Gözat

LibGUI: Focus the first focusable widget added to a window

It feels really awkward if nothing is focused when opening a window.
Andreas Kling 5 yıl önce
ebeveyn
işleme
4806cd122d

+ 2 - 0
Libraries/LibGUI/Widget.cpp

@@ -115,6 +115,8 @@ void Widget::child_event(Core::ChildEvent& event)
             else
             else
                 layout()->add_widget(Core::to<Widget>(*event.child()));
                 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 (event.type() == Event::ChildRemoved) {
         if (layout()) {
         if (layout()) {

+ 7 - 1
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);
         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)
     if (m_focused_widget == &widget)
         m_focused_widget = nullptr;
         m_focused_widget = nullptr;

+ 2 - 1
Libraries/LibGUI/Window.h

@@ -183,7 +183,8 @@ public:
 
 
     Action* action_for_key_event(const KeyEvent&);
     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();
     Window* find_parent_window();