mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:38:10 +00:00
LibGUI: Add up & down arrow hooks and input history to TextBox
This patch adds the ability to enable "input history" on a textbox, allowing to navigate between the history with the arrow keys. Also removes a custom TextBox subclass from HackStudio that added the exact same hooks, and moves it to use the now standard ones.
This commit is contained in:
parent
893a9ff5b0
commit
139dbfd5b5
4 changed files with 74 additions and 28 deletions
|
@ -73,28 +73,6 @@ private:
|
||||||
Vector<String> m_suggestions;
|
Vector<String> m_suggestions;
|
||||||
};
|
};
|
||||||
|
|
||||||
class LocatorTextBox final : public GUI::TextBox {
|
|
||||||
C_OBJECT(LocatorTextBox)
|
|
||||||
public:
|
|
||||||
virtual ~LocatorTextBox() override {}
|
|
||||||
|
|
||||||
Function<void()> on_up;
|
|
||||||
Function<void()> on_down;
|
|
||||||
|
|
||||||
virtual void keydown_event(GUI::KeyEvent& event) override
|
|
||||||
{
|
|
||||||
if (event.key() == Key_Up)
|
|
||||||
on_up();
|
|
||||||
else if (event.key() == Key_Down)
|
|
||||||
on_down();
|
|
||||||
|
|
||||||
GUI::TextBox::keydown_event(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
LocatorTextBox() {}
|
|
||||||
};
|
|
||||||
|
|
||||||
Locator::Locator()
|
Locator::Locator()
|
||||||
{
|
{
|
||||||
if (!s_cplusplus_icon) {
|
if (!s_cplusplus_icon) {
|
||||||
|
@ -106,14 +84,14 @@ Locator::Locator()
|
||||||
set_layout<GUI::VerticalBoxLayout>();
|
set_layout<GUI::VerticalBoxLayout>();
|
||||||
set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
||||||
set_preferred_size(0, 20);
|
set_preferred_size(0, 20);
|
||||||
m_textbox = add<LocatorTextBox>();
|
m_textbox = add<GUI::TextBox>();
|
||||||
m_textbox->on_change = [this] {
|
m_textbox->on_change = [this] {
|
||||||
update_suggestions();
|
update_suggestions();
|
||||||
};
|
};
|
||||||
m_textbox->on_escape_pressed = [this] {
|
m_textbox->on_escape_pressed = [this] {
|
||||||
m_popup_window->hide();
|
m_popup_window->hide();
|
||||||
};
|
};
|
||||||
m_textbox->on_up = [this] {
|
m_textbox->on_up_pressed = [this] {
|
||||||
GUI::ModelIndex new_index = m_suggestion_view->selection().first();
|
GUI::ModelIndex new_index = m_suggestion_view->selection().first();
|
||||||
if (new_index.is_valid())
|
if (new_index.is_valid())
|
||||||
new_index = m_suggestion_view->model()->index(new_index.row() - 1);
|
new_index = m_suggestion_view->model()->index(new_index.row() - 1);
|
||||||
|
@ -125,7 +103,7 @@ Locator::Locator()
|
||||||
m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
|
m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
m_textbox->on_down = [this] {
|
m_textbox->on_down_pressed = [this] {
|
||||||
GUI::ModelIndex new_index = m_suggestion_view->selection().first();
|
GUI::ModelIndex new_index = m_suggestion_view->selection().first();
|
||||||
if (new_index.is_valid())
|
if (new_index.is_valid())
|
||||||
new_index = m_suggestion_view->model()->index(new_index.row() + 1);
|
new_index = m_suggestion_view->model()->index(new_index.row() + 1);
|
||||||
|
|
|
@ -28,8 +28,6 @@
|
||||||
|
|
||||||
#include <LibGUI/Widget.h>
|
#include <LibGUI/Widget.h>
|
||||||
|
|
||||||
class LocatorTextBox;
|
|
||||||
|
|
||||||
class Locator final : public GUI::Widget {
|
class Locator final : public GUI::Widget {
|
||||||
C_OBJECT(Locator)
|
C_OBJECT(Locator)
|
||||||
public:
|
public:
|
||||||
|
@ -44,7 +42,7 @@ private:
|
||||||
|
|
||||||
Locator();
|
Locator();
|
||||||
|
|
||||||
RefPtr<LocatorTextBox> m_textbox;
|
RefPtr<GUI::TextBox> m_textbox;
|
||||||
RefPtr<GUI::Window> m_popup_window;
|
RefPtr<GUI::Window> m_popup_window;
|
||||||
RefPtr<GUI::TableView> m_suggestion_view;
|
RefPtr<GUI::TableView> m_suggestion_view;
|
||||||
};
|
};
|
||||||
|
|
|
@ -37,4 +37,55 @@ TextBox::~TextBox()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TextBox::keydown_event(GUI::KeyEvent& event)
|
||||||
|
{
|
||||||
|
TextEditor::keydown_event(event);
|
||||||
|
|
||||||
|
if (event.key() == Key_Up) {
|
||||||
|
if (on_up_pressed)
|
||||||
|
on_up_pressed();
|
||||||
|
|
||||||
|
if (has_no_history() || !can_go_backwards_in_history())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (m_history_index >= static_cast<int>(m_history.size()))
|
||||||
|
m_saved_input = text();
|
||||||
|
|
||||||
|
m_history_index--;
|
||||||
|
set_text(m_history[m_history_index]);
|
||||||
|
} else if (event.key() == Key_Down) {
|
||||||
|
if (on_down_pressed)
|
||||||
|
on_down_pressed();
|
||||||
|
|
||||||
|
if (has_no_history())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (can_go_forwards_in_history()) {
|
||||||
|
m_history_index++;
|
||||||
|
set_text(m_history[m_history_index]);
|
||||||
|
} else if (m_history_index < static_cast<int>(m_history.size())) {
|
||||||
|
m_history_index++;
|
||||||
|
set_text(m_saved_input);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextBox::add_current_text_to_history()
|
||||||
|
{
|
||||||
|
if (!m_history_enabled)
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto input = text();
|
||||||
|
if (m_history.is_empty() || m_history.last() != input)
|
||||||
|
add_input_to_history(input);
|
||||||
|
m_history_index = static_cast<int>(m_history.size());
|
||||||
|
m_saved_input = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextBox::add_input_to_history(String input)
|
||||||
|
{
|
||||||
|
m_history.append(move(input));
|
||||||
|
m_history_index++;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,25 @@ class TextBox : public TextEditor {
|
||||||
public:
|
public:
|
||||||
TextBox();
|
TextBox();
|
||||||
virtual ~TextBox() override;
|
virtual ~TextBox() override;
|
||||||
|
|
||||||
|
Function<void()> on_up_pressed;
|
||||||
|
Function<void()> on_down_pressed;
|
||||||
|
|
||||||
|
void set_history_enabled(bool enabled) { m_history_enabled = enabled; }
|
||||||
|
void add_current_text_to_history();
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual void keydown_event(GUI::KeyEvent&) override;
|
||||||
|
|
||||||
|
bool has_no_history() const { return !m_history_enabled || m_history.is_empty(); }
|
||||||
|
bool can_go_backwards_in_history() const { return m_history_index > 0; }
|
||||||
|
bool can_go_forwards_in_history() const { return m_history_index < static_cast<int>(m_history.size()) - 1; }
|
||||||
|
void add_input_to_history(String);
|
||||||
|
|
||||||
|
bool m_history_enabled { false };
|
||||||
|
Vector<String> m_history;
|
||||||
|
int m_history_index { -1 };
|
||||||
|
String m_saved_input;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue