From fae379b1f081064b890147232c80550515e08678 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 22 Aug 2019 11:09:25 +0200 Subject: [PATCH] TextEditor: Search box should find on return, close itself on escape This patch adds basic keyboard access to the search box. We also yield focus back gracefully to the text document when the search box is no longer wanted. Focus should probably move automatically when an ancestor of the currently focused widget if made invisible.. --- Applications/TextEditor/TextEditorWidget.cpp | 31 +++++++++++++------- Applications/TextEditor/TextEditorWidget.h | 1 + 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Applications/TextEditor/TextEditorWidget.cpp b/Applications/TextEditor/TextEditorWidget.cpp index 8e80520895..c92106f080 100644 --- a/Applications/TextEditor/TextEditorWidget.cpp +++ b/Applications/TextEditor/TextEditorWidget.cpp @@ -24,17 +24,17 @@ TextEditorWidget::TextEditorWidget() m_editor->set_ruler_visible(true); m_editor->set_automatic_indentation_enabled(true); - auto* find_widget = new GWidget(this); - find_widget->set_fill_with_background_color(true); - find_widget->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); - find_widget->set_preferred_size(0, 22); - find_widget->set_layout(make(Orientation::Horizontal)); - find_widget->layout()->set_margins({ 2, 2, 2, 2 }); - find_widget->set_visible(false); + m_find_widget = new GWidget(this); + m_find_widget->set_fill_with_background_color(true); + m_find_widget->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); + m_find_widget->set_preferred_size(0, 22); + m_find_widget->set_layout(make(Orientation::Horizontal)); + m_find_widget->layout()->set_margins({ 2, 2, 2, 2 }); + m_find_widget->set_visible(false); - m_find_textbox = new GTextBox(find_widget); + m_find_textbox = new GTextBox(m_find_widget); - m_find_button = new GButton("Find", find_widget); + m_find_button = new GButton("Find", m_find_widget); m_find_button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); m_find_button->set_preferred_size(100, 0); @@ -53,8 +53,17 @@ TextEditorWidget::TextEditorWidget() } }; - m_find_action = GAction::create("Find...", { Mod_Ctrl, Key_F }, [this, find_widget](auto&) { - find_widget->set_visible(true); + m_find_textbox->on_return_pressed = [this] { + m_find_button->click(); + }; + + m_find_textbox->on_escape_pressed = [this] { + m_find_widget->set_visible(false); + m_editor->set_focus(true); + }; + + m_find_action = GAction::create("Find...", { Mod_Ctrl, Key_F }, [this](auto&) { + m_find_widget->set_visible(true); m_find_textbox->set_focus(true); }); diff --git a/Applications/TextEditor/TextEditorWidget.h b/Applications/TextEditor/TextEditorWidget.h index d8dab46777..cf7d8820de 100644 --- a/Applications/TextEditor/TextEditorWidget.h +++ b/Applications/TextEditor/TextEditorWidget.h @@ -32,4 +32,5 @@ private: GTextBox* m_find_textbox { nullptr }; GButton* m_find_button { nullptr }; + GWidget* m_find_widget { nullptr }; };