Browse Source

Search entry histories are now updated correctly

merge-requests/413/merge
eidheim 2 years ago
parent
commit
043a105dc3
  1. 16
      src/entrybox.cpp
  2. 3
      src/entrybox.hpp
  3. 24
      src/window.cpp

16
src/entrybox.cpp

@ -9,12 +9,8 @@ EntryBox::Entry::Entry(const std::string &content, std::function<void(const std:
selected_history = -1; selected_history = -1;
signal_activate().connect([this]() { signal_activate().connect([this]() {
if(this->on_activate) { if(this->on_activate) {
auto &history = EntryBox::entry_histories[get_placeholder_text()]; update_history();
auto text = get_text(); this->on_activate(get_text());
if(!text.empty() && (history.empty() || *history.begin() != text))
history.emplace(history.begin(), text);
selected_history = -1;
this->on_activate(text);
} }
}); });
signal_changed().connect([this] { signal_changed().connect([this] {
@ -57,6 +53,14 @@ EntryBox::Entry::Entry(const std::string &content, std::function<void(const std:
}); });
} }
void EntryBox::Entry::update_history() {
auto &history = EntryBox::entry_histories[get_placeholder_text()];
auto text = get_text();
if(!text.empty() && (history.empty() || *history.begin() != text))
history.emplace(history.begin(), text);
selected_history = -1;
}
EntryBox::Button::Button(const std::string &label, std::function<void()> on_activate_) : Gtk::Button(label), on_activate(std::move(on_activate_)) { EntryBox::Button::Button(const std::string &label, std::function<void()> on_activate_) : Gtk::Button(label), on_activate(std::move(on_activate_)) {
set_focus_on_click(false); set_focus_on_click(false);
signal_clicked().connect([this]() { signal_clicked().connect([this]() {

3
src/entrybox.hpp

@ -13,6 +13,9 @@ public:
Entry(const std::string &content = "", std::function<void(const std::string &content)> on_activate_ = nullptr); Entry(const std::string &content = "", std::function<void(const std::string &content)> on_activate_ = nullptr);
std::function<void(const std::string &content)> on_activate; std::function<void(const std::string &content)> on_activate;
/// Only the default activation on an entry will update its history.
void update_history();
private: private:
long selected_history; long selected_history;
std::string last_content; std::string last_content;

24
src/window.cpp

@ -2102,8 +2102,9 @@ void Window::search_and_replace_entry() {
}); });
auto search_entry_it = EntryBox::get().entries.begin(); auto search_entry_it = EntryBox::get().entries.begin();
search_entry_it->set_placeholder_text("Find"); search_entry_it->set_placeholder_text("Find");
search_entry_it->signal_key_press_event().connect([this](GdkEventKey *event) { search_entry_it->signal_key_press_event().connect([this, search_entry_it](GdkEventKey *event) {
if((event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter) && (event->state & GDK_SHIFT_MASK) > 0) { if((event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter) && (event->state & GDK_SHIFT_MASK) > 0) {
search_entry_it->update_history();
if(focused_view) if(focused_view)
focused_view->search_backward(); focused_view->search_backward();
} }
@ -2115,15 +2116,18 @@ void Window::search_and_replace_entry() {
focused_view->search_highlight(search_entry_it->get_text(), case_sensitive_search, regex_search); focused_view->search_highlight(search_entry_it->get_text(), case_sensitive_search, regex_search);
}); });
EntryBox::get().entries.emplace_back(last_replace, [this](const std::string &content) { EntryBox::get().entries.emplace_back(last_replace, [this, search_entry_it](const std::string &content) {
search_entry_it->update_history();
if(focused_view) if(focused_view)
focused_view->replace_forward(content); focused_view->replace_forward(content);
}); });
auto replace_entry_it = EntryBox::get().entries.begin(); auto replace_entry_it = EntryBox::get().entries.begin();
replace_entry_it++; replace_entry_it++;
replace_entry_it->set_placeholder_text("Replace"); replace_entry_it->set_placeholder_text("Replace");
replace_entry_it->signal_key_press_event().connect([this, replace_entry_it](GdkEventKey *event) { replace_entry_it->signal_key_press_event().connect([this, search_entry_it, replace_entry_it](GdkEventKey *event) {
if((event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter) && (event->state & GDK_SHIFT_MASK) > 0) { if((event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter) && (event->state & GDK_SHIFT_MASK) > 0) {
search_entry_it->update_history();
replace_entry_it->update_history();
if(focused_view) if(focused_view)
focused_view->replace_backward(replace_entry_it->get_text()); focused_view->replace_backward(replace_entry_it->get_text());
} }
@ -2133,22 +2137,28 @@ void Window::search_and_replace_entry() {
last_replace = replace_entry_it->get_text(); last_replace = replace_entry_it->get_text();
}); });
EntryBox::get().buttons.emplace_back("", [this]() { EntryBox::get().buttons.emplace_back("", [this, search_entry_it]() {
search_entry_it->update_history();
if(focused_view) if(focused_view)
focused_view->search_backward(); focused_view->search_backward();
}); });
EntryBox::get().buttons.back().set_tooltip_text("Find Previous\n\nShortcut: Shift+Enter in the Find entry field"); EntryBox::get().buttons.back().set_tooltip_text("Find Previous\n\nShortcut: Shift+Enter in the Find entry field");
EntryBox::get().buttons.emplace_back("", [this, replace_entry_it]() { EntryBox::get().buttons.emplace_back("", [this, search_entry_it, replace_entry_it]() {
search_entry_it->update_history();
replace_entry_it->update_history();
if(focused_view) if(focused_view)
focused_view->replace_forward(replace_entry_it->get_text()); focused_view->replace_forward(replace_entry_it->get_text());
}); });
EntryBox::get().buttons.back().set_tooltip_text("Replace Next\n\nShortcut: Enter in the Replace entry field"); EntryBox::get().buttons.back().set_tooltip_text("Replace Next\n\nShortcut: Enter in the Replace entry field");
EntryBox::get().buttons.emplace_back("", [this]() { EntryBox::get().buttons.emplace_back("", [this, search_entry_it]() {
search_entry_it->update_history();
if(focused_view) if(focused_view)
focused_view->search_forward(); focused_view->search_forward();
}); });
EntryBox::get().buttons.back().set_tooltip_text("Find Next\n\nShortcut: Enter in the Find entry field"); EntryBox::get().buttons.back().set_tooltip_text("Find Next\n\nShortcut: Enter in the Find entry field");
EntryBox::get().buttons.emplace_back("Replace All", [this, replace_entry_it]() { EntryBox::get().buttons.emplace_back("Replace All", [this, search_entry_it, replace_entry_it]() {
search_entry_it->update_history();
replace_entry_it->update_history();
if(focused_view) if(focused_view)
focused_view->replace_all(replace_entry_it->get_text()); focused_view->replace_all(replace_entry_it->get_text());
}); });

Loading…
Cancel
Save