Skip to content

Commit

Permalink
Find next shortcut doesn't work while Find/replace dialog is focused
Browse files Browse the repository at this point in the history
    fixes: #3566

Signed-off-by: Eran Ifrah <[email protected]>
  • Loading branch information
eranif committed Jan 8, 2025
1 parent b84111d commit 2273511
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
47 changes: 42 additions & 5 deletions LiteEditor/quickfindbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
//////////////////////////////////////////////////////////////////////////////
#include "quickfindbar.h"

#include "Keyboard/clKeyboardManager.h"
#include "bitmap_loader.h"
#include "bookmark_manager.h"
#include "clSystemSettings.h"
Expand Down Expand Up @@ -185,6 +186,7 @@ QuickFindBar::QuickFindBar(wxWindow* parent, wxWindowID id)

wxTheApp->Bind(wxEVT_MENU, &QuickFindBar::OnFindNextCaret, this, XRCID("find_next_at_caret"));
wxTheApp->Bind(wxEVT_MENU, &QuickFindBar::OnFindPreviousCaret, this, XRCID("find_previous_at_caret"));
wxTheApp->Bind(wxEVT_MENU, &QuickFindBar::OnFindNext, this, XRCID("find_next"));

EventNotifier::Get()->Bind(wxEVT_FINDBAR_RELEASE_EDITOR, &QuickFindBar::OnReleaseEditor, this);
Connect(QUICKFIND_COMMAND_EVENT, wxCommandEventHandler(QuickFindBar::OnQuickFindCommandEvent), NULL, this);
Expand Down Expand Up @@ -240,6 +242,7 @@ QuickFindBar::~QuickFindBar()

wxTheApp->Unbind(wxEVT_MENU, &QuickFindBar::OnFindNextCaret, this, XRCID("find_next_at_caret"));
wxTheApp->Unbind(wxEVT_MENU, &QuickFindBar::OnFindPreviousCaret, this, XRCID("find_previous_at_caret"));
wxTheApp->Unbind(wxEVT_MENU, &QuickFindBar::OnFindNext, this, XRCID("id_find"));
EventNotifier::Get()->Unbind(wxEVT_FINDBAR_RELEASE_EDITOR, &QuickFindBar::OnReleaseEditor, this);
EventNotifier::Get()->Unbind(wxEVT_STC_GOT_FOCUS, &QuickFindBar::OnFocusGained, this);
EventNotifier::Get()->Unbind(wxEVT_STC_LOST_FOCUS, &QuickFindBar::OnFocusLost, this);
Expand Down Expand Up @@ -277,6 +280,10 @@ void QuickFindBar::OnText(wxCommandEvent& e)

void QuickFindBar::OnKeyDown(wxKeyEvent& e)
{
if (HandleKeyboardShortcuts(e)) {
return;
}

switch (e.GetKeyCode()) {
case WXK_DOWN: {
// DoArrowDown(m_searchHistory, m_textCtrlFind);
Expand All @@ -300,6 +307,10 @@ void QuickFindBar::OnKeyDown(wxKeyEvent& e)
}
void QuickFindBar::OnReplaceKeyDown(wxKeyEvent& e)
{
if (HandleKeyboardShortcuts(e)) {
return;
}

switch (e.GetKeyCode()) {
case WXK_DOWN: {
// DoArrowDown(m_replaceHistory, m_textCtrlReplace);
Expand Down Expand Up @@ -429,12 +440,8 @@ bool QuickFindBar::Show(const wxString& findWhat, bool showReplace)
return DoShow(true, findWhat, showReplace);
}

bool QuickFindBar::DoShow([[maybe_unused]] bool s, const wxString& findWhat, bool showReplace)
bool QuickFindBar::DoShow(bool s, const wxString& findWhat, bool showReplace)
{
#ifdef __WXMSW__
wxWindowUpdateLocker locker(this);
#endif

int dummy = wxNOT_FOUND;
if (!clConfig::Get().Read("FindBar/Height", dummy)) {
// first time, place it at the top
Expand Down Expand Up @@ -523,6 +530,13 @@ void QuickFindBar::OnFindNextCaret(wxCommandEvent& e)
DoFind(FIND_DEFAULT | FIND_GOTOLINE);
}

void QuickFindBar::OnFindNext(wxCommandEvent& e)
{
clSYSTEM() << "Find next" << endl;
CHECK_FOCUS_WIN(e);
DoFindWithWrap(FIND_DEFAULT | FIND_GOTOLINE);
}

void QuickFindBar::OnFindPreviousCaret(wxCommandEvent& e)
{
CHECK_FOCUS_WIN(e);
Expand Down Expand Up @@ -973,6 +987,9 @@ void QuickFindBar::DoArrowUp(clTerminalHistory& history, wxTextCtrl* ctrl)

void QuickFindBar::OnButtonKeyDown(wxKeyEvent& event)
{
if (HandleKeyboardShortcuts(event)) {
return;
}
switch (event.GetKeyCode()) {
case WXK_ESCAPE: {
wxCommandEvent dummy;
Expand Down Expand Up @@ -1109,3 +1126,23 @@ void QuickFindBar::OnFocusLost(clCommandEvent& e)
void QuickFindBar::OnTimer(wxTimerEvent& event) { event.Skip(); }

void QuickFindBar::OnReplaceTextUI(wxUpdateUIEvent& event) { event.Enable(true); }

bool QuickFindBar::HandleKeyboardShortcuts(wxKeyEvent& event)
{
auto accl_find = clKeyboardManager::Get()->GetShortcutForCommand("find_next");
auto accl_find_prev = clKeyboardManager::Get()->GetShortcutForCommand("find_previous");

auto find_next = accl_find.ToAccelerator("Find Next");
auto find_prev = accl_find_prev.ToAccelerator("Find Previous");

// Note that we compare here: GetFlags() (wxACCL_*) against GetModifiers() (wxMOD_*)
// the primary modifiers: Ctrl, Shift, Alt are the same so we don't care...
if (find_next->GetKeyCode() == event.GetKeyCode() && find_next->GetFlags() == event.GetModifiers()) {
DoFindWithWrap(FIND_DEFAULT | FIND_GOTOLINE);
return true;
} else if (find_prev->GetKeyCode() == event.GetKeyCode() && find_prev->GetFlags() == event.GetModifiers()) {
DoFindWithWrap(FIND_PREV | FIND_GOTOLINE);
return true;
}
return false;
}
3 changes: 2 additions & 1 deletion LiteEditor/quickfindbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,15 @@ class QuickFindBar : public clFindReplaceDialogBase
void OnPaint(wxPaintEvent& e);
void OnFindNextCaret(wxCommandEvent& e);
void OnFindPreviousCaret(wxCommandEvent& e);
void OnFindNext(wxCommandEvent& e);

void OnFocusGained(clCommandEvent& e);
void OnFocusLost(clCommandEvent& e);
void OnTimer(wxTimerEvent& event);

protected:
bool DoShow(bool s, const wxString& findWhat, bool showReplace = false);
wxStyledTextCtrl* DoCheckPlugins();
bool HandleKeyboardShortcuts(wxKeyEvent& event);

public:
QuickFindBar(wxWindow* parent, wxWindowID id = wxID_ANY);
Expand Down
28 changes: 26 additions & 2 deletions Plugin/Keyboard/clKeyboardManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ bool clKeyboardManager::Exists(const clKeyboardShortcut& accel) const
return false;
}

void clKeyboardManager::AddAccelerator(const wxString& resourceID, const wxString& parentMenu, const wxString& action,
void clKeyboardManager::AddAccelerator(const wxString& resourceID,
const wxString& parentMenu,
const wxString& action,
const clKeyboardShortcut& accel)
{
wxASSERT_MSG(m_defaultAccelTable.count(resourceID) == 0, "An accelerator with this resourceID already exists");
Expand Down Expand Up @@ -384,7 +386,10 @@ clKeyboardShortcut::Vec_t clKeyboardManager::GetAllUnassignedKeyboardShortcuts()

// Remove all duplicate entries
clKeyboardShortcut::Vec_t allUnassigned;
std::set_difference(m_allShortcuts.begin(), m_allShortcuts.end(), usedShortcuts.begin(), usedShortcuts.end(),
std::set_difference(m_allShortcuts.begin(),
m_allShortcuts.end(),
usedShortcuts.begin(),
usedShortcuts.end(),
std::back_inserter(allUnassigned));
return allUnassigned;
}
Expand Down Expand Up @@ -532,3 +537,22 @@ wxString clKeyboardShortcut::to_string(bool for_ui) const

wxString clKeyboardShortcut::ToString() const { return to_string(false); }
wxString clKeyboardShortcut::DisplayString() const { return to_string(true); }

clKeyboardShortcut clKeyboardManager::GetShortcutForCommand(const wxString& xrcid_string) const
{
if (m_accelTable.count(xrcid_string)) {
return m_accelTable.find(xrcid_string)->second.accel;
}
if (m_defaultAccelTable.count(xrcid_string)) {
return m_defaultAccelTable.find(xrcid_string)->second.accel;
}
return {};
}

std::shared_ptr<wxAcceleratorEntry> clKeyboardShortcut::ToAccelerator(const wxString& label) const
{
wxString dummyText;
dummyText << label << "\t" << ToString();
wxAcceleratorEntry* entry = wxAcceleratorEntry::Create(dummyText);
return std::shared_ptr<wxAcceleratorEntry>(entry);
}
12 changes: 11 additions & 1 deletion Plugin/Keyboard/clKeyboardManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ class WXDLLIMPEXP_SDK clKeyboardShortcut
/// Similar to `ToString` but convert `rawctrl` & `ctrl` into `Ctrl` & `Cmd` accordingly
wxString DisplayString() const;

/// Convert this into wxAcceleratorEntry
std::shared_ptr<wxAcceleratorEntry> ToAccelerator(const wxString& label) const;

using Vec_t = std::vector<clKeyboardShortcut>;
using Set_t = std::set<clKeyboardShortcut>;
};
Expand Down Expand Up @@ -220,7 +223,9 @@ class WXDLLIMPEXP_SDK clKeyboardManager : public wxEvtHandler
* @brief add keyboard shortcut by specifying the action ID + the shortcut combination
* For example: AddAccelerator("wxID_COPY", _("Edit"), _("Copy the current selection"), "Ctrl-Shift-C");
*/
void AddAccelerator(const wxString& resourceID, const wxString& parentMenu, const wxString& action,
void AddAccelerator(const wxString& resourceID,
const wxString& parentMenu,
const wxString& action,
const clKeyboardShortcut& accel = {});

/**
Expand Down Expand Up @@ -252,6 +257,11 @@ class WXDLLIMPEXP_SDK clKeyboardManager : public wxEvtHandler
* @brief restore keyboard shortcuts to defaults
*/
void RestoreDefaults();

/**
* @brief return keyboard shortcut for given XRCID
*/
clKeyboardShortcut GetShortcutForCommand(const wxString& xrcid_string) const;
};

#endif // KEYBOARDMANAGER_H

0 comments on commit 2273511

Please sign in to comment.