Skip to content

Commit

Permalink
fix: Bookmark comment shortcuts (issue #2055)
Browse files Browse the repository at this point in the history
Added the registrations for the editor shortcuts and reorganized the registration calls into events and handlers.

Needed a way to select which of the possibly multiple editors sees the effects of the keyboard so a pointer to the current text editor in use was added to the view bookmarks class. The pointer may be empty so the shortcuts need to check it before using it.

To detect which of the editors needs to see the effects of the shortcuts a function ws added to text editor that tells if window has focus.

To avoid errors when editors become read only, asserts were exchanged with do-nothing early returns on the text editor.

To avoid using the default language coloring (HLSL), the colorize enabling variable was set to false.

A suggestion by cppcheck simplified the logic of a conditional on view bookmarks.

On view pattern editor some icons were added to console menu entries.
  • Loading branch information
paxcut committed Jan 6, 2025
1 parent d8c3d67 commit 8359339
Show file tree
Hide file tree
Showing 5 changed files with 381 additions and 198 deletions.
2 changes: 2 additions & 0 deletions lib/third_party/imgui/ColorTextEditor/include/TextEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ class TextEditor
mFocusAtCoords = coords;
mUpdateFocus = true;
}
bool GetWindowFocused() { return mWindowFocused; }
void SetOverwrite(bool aValue) { mOverwrite = aValue; }

std::string ReplaceStrings(std::string string, const std::string &search, const std::string &replace);
Expand Down Expand Up @@ -641,6 +642,7 @@ class TextEditor
bool mRaiseContextMenu = false;
Coordinates mFocusAtCoords = {};
bool mUpdateFocus = false;
bool mWindowFocused = false;

std::vector<std::string> mClickableText;

Expand Down
40 changes: 18 additions & 22 deletions lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,11 @@ void TextEditor::Advance(Coordinates &aCoordinates) const {
}

void TextEditor::DeleteRange(const Coordinates &aStart, const Coordinates &aEnd) {
IM_ASSERT(aEnd >= aStart);
IM_ASSERT(!mReadOnly);
if (mReadOnly || aEnd <= aStart)
return;

// printf("D(%d.%d)-(%d.%d)\n", aStart.mLine, aStart.mColumn, aEnd.mLine, aEnd.mColumn);

if (aEnd == aStart)
return;

auto start = GetCharacterIndex(aStart);
auto end = GetCharacterIndex(aEnd);
if (start == -1 || end == -1)
Expand Down Expand Up @@ -293,7 +290,8 @@ int TextEditor::InsertTextAt(Coordinates & /* inout */ aWhere, const char *aValu
}

void TextEditor::AddUndo(UndoRecord &aValue) {
IM_ASSERT(!mReadOnly);
if (mReadOnly)
return;
// printf("AddUndo: (@%d.%d) +\'%s' [%d.%d .. %d.%d], -\'%s', [%d.%d .. %d.%d] (@%d.%d)\n",
// aValue.mBefore.mCursorPosition.mLine, aValue.mBefore.mCursorPosition.mColumn,
// aValue.mAdded.c_str(), aValue.mAddedStart.mLine, aValue.mAddedStart.mColumn, aValue.mAddedEnd.mLine, aValue.mAddedEnd.mColumn,
Expand Down Expand Up @@ -587,8 +585,8 @@ bool TextEditor::IsOnWordBoundary(const Coordinates &aAt) const {
}

void TextEditor::RemoveLine(int aStart, int aEnd) {
IM_ASSERT(!mReadOnly);
IM_ASSERT(aEnd >= aStart);
if (mReadOnly || aEnd < aStart)
return;

ErrorMarkers etmp;
for (auto &i : mErrorMarkers) {
Expand Down Expand Up @@ -620,8 +618,8 @@ void TextEditor::RemoveLine(int aStart, int aEnd) {
}

void TextEditor::RemoveLine(int aIndex) {
IM_ASSERT(!mReadOnly);
IM_ASSERT(mLines.size() > 1);
if (mReadOnly || mLines.size() <= 1)
return;

ErrorMarkers etmp;
for (auto &i : mErrorMarkers) {
Expand Down Expand Up @@ -948,7 +946,7 @@ void TextEditor::RenderText(const char *aTitle, const ImVec2 &lineNumbersStartPo
}
ImVec2 lineNoStartScreenPos = ImVec2(position.x, mTopMargin + cursorScreenPos.y + std::floor(lineNo) * mCharAdvance.y);
auto start = ImVec2(lineNoStartScreenPos.x + mLineNumberFieldWidth, lineStartScreenPos.y);
bool focused = ImGui::IsWindowFocused();
mWindowFocused = ImGui::IsWindowFocused();
if (!mIgnoreImGuiChild)
ImGui::EndChild();
// Draw line number (right aligned)
Expand Down Expand Up @@ -980,7 +978,7 @@ void TextEditor::RenderText(const char *aTitle, const ImVec2 &lineNumbersStartPo
// Highlight the current line (where the cursor is)
if (!HasSelection()) {
auto end = ImVec2(lineNoStartScreenPos.x + contentSize.x + mLineNumberFieldWidth, lineStartScreenPos.y + mCharAdvance.y);
drawList->AddRectFilled(ImVec2(lineNumbersStartPos.x, lineStartScreenPos.y), end, mPalette[(int)(focused ? PaletteIndex::CurrentLineFill : PaletteIndex::CurrentLineFillInactive)]);
drawList->AddRectFilled(ImVec2(lineNumbersStartPos.x, lineStartScreenPos.y), end, mPalette[(int)(mWindowFocused ? PaletteIndex::CurrentLineFill : PaletteIndex::CurrentLineFillInactive)]);
drawList->AddRect(ImVec2(lineNumbersStartPos.x, lineStartScreenPos.y), end, mPalette[(int)PaletteIndex::CurrentLineEdge], 1.0f);
}
}
Expand All @@ -991,7 +989,7 @@ void TextEditor::RenderText(const char *aTitle, const ImVec2 &lineNumbersStartPo
ImGui::BeginChild(aTitle);
if (mState.mCursorPosition.mLine == lineNo && mShowCursor) {
// Render the cursor
if (focused) {
if (mWindowFocused) {
auto timeEnd = ImGui::GetTime() * 1000;
auto elapsed = timeEnd - mStartTime;
if (elapsed > sCursorBlinkOnTime) {
Expand Down Expand Up @@ -1330,7 +1328,8 @@ void TextEditor::SetTextLines(const std::vector<std::string> &aLines) {
}

void TextEditor::EnterCharacter(ImWchar aChar, bool aShift) {
IM_ASSERT(!mReadOnly);
if (mReadOnly)
return;

UndoRecord u;

Expand Down Expand Up @@ -1900,12 +1899,10 @@ void TextEditor::MoveEnd(bool aSelect) {
}

void TextEditor::Delete() {
ResetCursorBlinkTime();
IM_ASSERT(!mReadOnly);

if (isEmpty())
if (mReadOnly || isEmpty())
return;

ResetCursorBlinkTime();
UndoRecord u;
u.mBefore = mState;

Expand Down Expand Up @@ -1957,12 +1954,11 @@ void TextEditor::Delete() {
}

void TextEditor::Backspace() {
ResetCursorBlinkTime();
IM_ASSERT(!mReadOnly);

if (isEmpty())
if (mReadOnly || isEmpty())
return;

ResetCursorBlinkTime();

UndoRecord u;
u.mBefore = mState;

Expand Down
3 changes: 3 additions & 0 deletions plugins/builtin/include/content/views/view_bookmarks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ namespace hex::plugin::builtin {
bool importBookmarks(hex::prv::Provider *provider, const nlohmann::json &json);
bool exportBookmarks(hex::prv::Provider *provider, nlohmann::json &json);

void registerEvents();
void registerMenuItems();
void registerHandlers();

private:
std::string m_currFilter;

PerProvider<std::list<Bookmark>> m_bookmarks;
PerProvider<u64> m_currBookmarkId;
TextEditor *m_currTextEditor;
};

}
Loading

0 comments on commit 8359339

Please sign in to comment.