From 22dc4be1007d6a725ecfa2e66e7e8e0b699b03c9 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 4 Aug 2024 10:07:36 +0200 Subject: [PATCH] Go to last bread crumb but mouse is bugging --- src/text_editor/commands.cpp | 3 ++- src/text_editor/commands_window.cpp | 37 +++++++++++++++++++++++++---- src/text_editor/lua_api.cpp | 6 +++-- src/text_editor/text_editor.cpp | 35 +++++++++++++++++---------- src/text_editor/text_editor.h | 3 ++- src/text_editor/todo.txt | 8 ++++++- src/text_editor/window_draw.cpp | 2 +- 7 files changed, 70 insertions(+), 24 deletions(-) diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index aeb16d7..bd6661e 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -84,6 +84,7 @@ bool GlobalCommand(Event event) { { Vec2I mouse = MouseVec2I(); Window *window = GetActiveWindow(); + View *view = GetView(window->active_view); bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect); bool mouse_in_total = CheckCollisionPointRec(mouse, window->total_rect); bool mouse_in_line_numbers = CheckCollisionPointRec(mouse, window->line_numbers_rect); @@ -93,7 +94,7 @@ bool GlobalCommand(Event event) { static SDL_Cursor *SDL_MouseCursor; if (SDL_MouseCursor) SDL_DestroyCursor(SDL_MouseCursor); - if (window->mouse_selecting || mouse_in_document) { + if (view->mouse_selecting || mouse_in_document) { SDL_MouseCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_TEXT); SDL_SetCursor(SDL_MouseCursor); } else if (mouse_in_scrollbar || window->mouse_selecting_scrollbar || mouse_in_line_numbers) { diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 11323c0..fcc88b1 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -503,6 +503,29 @@ Array FindAllInBuffer(Allocator allocator, Buffer *buffer, String16 needl return result; } +struct GotoCrumb { + BufferID buffer_id; + Caret caret; +}; +Array GotoCrumbs; + +void CheckpointBeforeGoto() { + Window *window = GetWindow(GetLastActiveWindow()); + View *view = GetView(window->active_view); + Buffer *buffer = GetBuffer(view->active_buffer); + Add(&GotoCrumbs, {buffer->id, view->carets[0]}); +} + +void GoBackToLastCrumb() { + Window *window = GetWindow(ActiveWindow); + if (GotoCrumbs.len <= 0) return; + GotoCrumb c = Pop(&GotoCrumbs); + Buffer *buffer = GetBuffer(c.buffer_id); + View *view = WindowOpenBufferView(window, buffer->name); + view->carets[0] = c.caret; + UpdateScroll(window, true); +} + void Command_IdentedNewLine(View *view) { Buffer *buffer = GetBuffer(view->active_buffer); Scratch scratch; @@ -823,6 +846,10 @@ void WindowCommand(Event event, Window *window, View *view) { } } + if (event.ctrl && Mouse(RIGHT)) { + GoBackToLastCrumb(); + } + { Vec2I mouse = MouseVec2I(); @@ -845,7 +872,7 @@ void WindowCommand(Event event, Window *window, View *view) { bool document_action = false; { bool a = mouse_in_document && IsMouseEvent(event.kind); - bool b = window->mouse_selecting && !mouse_in_document; + bool b = view->mouse_selecting && !mouse_in_document; document_action = a || b; } @@ -876,12 +903,12 @@ void WindowCommand(Event event, Window *window, View *view) { } if (mouse_in_document && Mouse(LEFT)) { - window->mouse_selecting = true; + view->mouse_selecting = true; } - if (window->mouse_selecting) { + if (view->mouse_selecting) { if (Mouse(LEFT_UP)) { - window->mouse_selecting = false; + view->mouse_selecting = false; } MergeCarets(view, &view->selection_anchor); @@ -895,7 +922,7 @@ void WindowCommand(Event event, Window *window, View *view) { } MergeCarets(view, &view->selection_anchor); } - } else if (!(mouse_in_document || window->mouse_selecting) && mouse_in_scrollbar || window->mouse_selecting_scrollbar) { + } else if (!(mouse_in_document || view->mouse_selecting) && mouse_in_scrollbar || window->mouse_selecting_scrollbar) { Vec2 mouse_vec2 = MouseVec2(); Scroller s = ComputeScrollerRect(window); double size_y = (double)GetSize(window->scrollbar_rect).y; diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index f01658e..73bc067 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -47,16 +47,18 @@ void Open(String path) { String col_string = FieldString(LuaState, "col"); Int col = strtoll(col_string.data, NULL, 10); + CheckpointBeforeGoto(); Window *window = GetWindow(GetLastActiveWindow()); View *view = WindowOpenBufferView(window, file_path); Buffer *buffer = GetBuffer(view->active_buffer); if (line != -1 && col != -1) { - view->carets[0] = MakeCaret(XYToPos(*buffer, {col - 1, line - 1})); + Int pos = XYToPos(*buffer, {col - 1, line - 1}); + view->carets[0] = MakeCaret(pos); } UpdateScroll(window, true); SetActiveWindow(window->id); } else { - ReportWarningf("Failed to match any of ApplyRules results!"); + ReportWarningf("Failed to match any of ApplyRules results!"); } } diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index a009ff8..004a402 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -140,9 +140,10 @@ void HandleEvent(Event event) { WindowCommand(event, window, view); MergeCarets(view); } + For(Windows) if (it.is_title_bar) ReplaceTitleBarData(&it); } -void Update(const Event &event) { +void Update(Event event) { WindowSize = {(float)event.xwindow, (float)event.ywindow}; LayoutWindows(); @@ -160,26 +161,33 @@ void Update(const Event &event) { HandleEvent(event); - ReloadLuaConfig(); - For(Windows) if (it.is_title_bar) ReplaceTitleBarData(&it); - ReplaceDebugData(); - // Switch active window { - if (ActiveWindow.id != NextActiveWindow.id) { + bool active_window_changed = ActiveWindow.id != NextActiveWindow.id; + if (active_window_changed) { Window *window = GetWindow(ActiveWindow); - window->mouse_selecting = false; window->mouse_selecting_scrollbar = false; View *view = GetView(window->active_view); view->underline_count = 0; - } - ActiveWindow = NextActiveWindow; - Window *w = GetWindow(ActiveWindow); - if (!w->dont_save_in_active_window_history) { - Add(&WindowSwitchHistory, ActiveWindow); + view->mouse_selecting = false; + ActiveWindow = NextActiveWindow; + { + Window *w = GetWindow(ActiveWindow); + if (!w->dont_save_in_active_window_history) { + Add(&WindowSwitchHistory, ActiveWindow); + } + } + + // @todo: maybe move some of the mouse events to global comamnds? + // the problem here is that we don't want to click twice etc when + // window is inactive, we kind of also don't want to rerun events twice + // because it seems to be buggy and problematic } } + ReloadLuaConfig(); + ReplaceDebugData(); + For(IterateInReverse(&order)) { Window *window = &Windows[it]; { @@ -318,7 +326,8 @@ int main() WaitForEvents = true; Window *window = GetActiveWindow(); - if (window->mouse_selecting || window->mouse_selecting_scrollbar) { + View *view = GetView(window->active_view); + if (view->mouse_selecting || window->mouse_selecting_scrollbar) { WaitForEvents = false; } } diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index 40a95ab..923ce96 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -56,6 +56,7 @@ struct View { struct { bool fuzzy_search : 1; + bool mouse_selecting : 1; }; }; @@ -77,7 +78,6 @@ struct Window { struct { bool mouse_selecting_scrollbar : 1; - bool mouse_selecting : 1; bool mouse_in_scrollbar : 1; bool draw_scrollbar : 1; @@ -124,6 +124,7 @@ void Command_SelectEntireBuffer(View *view); void Command_Replace(View *view, String16 string); void Open(String path); void Open(String16 path); +void UpdateScroll(Window *window, bool update_caret_scrolling); void ReportErrorf(const char *fmt, ...); void ReportWarningf(const char *fmt, ...); \ No newline at end of file diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 382b791..ad9b7d2 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -2,8 +2,14 @@ - page up and down should also scroll and leave you in exactly same scroll - I think the way sublime text and we display line highlights is confusing with multiple cursors (line highlight can be confused with selection) +- mouse needs a rewrite + - you should be able to scroll another window without focusing it + - for now get rid of anchor maybe and do the - word select, line select + - make clicks global so that they work without needing another click to activate + - fix the bug where when we create a new view the anchor value on new view is compromised because and incongruent with position so we select from start of buffer + - hand cursor on hover over underlined word + - ctrl + q to open, alt + q to go back -- when switching to inactive window pass it the mouse click - delete multiple spaces (based on indent size) on delete instead one by one - switch to previous view (ctrl + tab) - save location on open and allow for revert (buffer id? or buffer name? - buffer name can change, buffer id is harder to serialize, I guess if internal only then buffer id) diff --git a/src/text_editor/window_draw.cpp b/src/text_editor/window_draw.cpp index 9e21463..51c2a6c 100644 --- a/src/text_editor/window_draw.cpp +++ b/src/text_editor/window_draw.cpp @@ -240,7 +240,7 @@ void DrawWindow(Window *window) { Scroller scroller = ComputeScrollerRect(window); Rect2 rect = Shrink(scroller.rect, 2); Color color = ColorScrollbarScroller; - if (!window->mouse_selecting && (window->mouse_selecting_scrollbar || window->mouse_in_scrollbar)) { + if (!view->mouse_selecting && (window->mouse_selecting_scrollbar || window->mouse_in_scrollbar)) { if (is_active) color = ColorScrollbarScrollerSelected; } DrawRect(rect, color);