diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 9441e2c..e7785b6 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -162,10 +162,9 @@ void Command_EvalLua(View *view) { Command_EvalLua(view, string); } -void HandleActiveWindowBindings(Window *window) { - View &view = *GetActiveView(window); - Buffer *buffer = GetBuffer(view.buffer_id); - Caret main_caret_on_begin_frame = view.carets[0]; +void HandleActiveWindowBindings(Window *window, bool *update_scroll) { + View &view = *GetActiveView(window); + Buffer *buffer = GetBuffer(view.buffer_id); if (CtrlPress(KEY_F2)) { LoadBigLine(buffer); } else if (Press(KEY_F2)) { @@ -294,10 +293,9 @@ void HandleActiveWindowBindings(Window *window) { Command_Replace(&view, L""); } - bool dont_update_scroll = false; if (CtrlPress(KEY_A)) { Command_SelectEntireBuffer(&view); - dont_update_scroll = true; + *update_scroll = false; } if (ShiftPress(KEY_PAGE_UP)) { @@ -509,10 +507,21 @@ void HandleActiveWindowBindings(Window *window) { } } } +} + +void HandleWindowBindings(Window *window) { + ProfileFunction(); + View *view = GetActiveView(window); + Buffer *buffer = GetBuffer(view->buffer_id); + bool update_scroll = true; + + if (IsActive(window)) { + HandleActiveWindowBindings(window, &update_scroll); + } // Scrolling with caret - if (!AreEqual(main_caret_on_begin_frame, view.carets[0]) && !dont_update_scroll) { - Caret c = view.carets[0]; + if (!AreEqual(view->main_caret_on_begin_frame, view->carets[0]) && update_scroll) { + Caret c = view->carets[0]; Int front = GetFront(c); XY xy = PosToXY(*buffer, front); @@ -524,43 +533,34 @@ void HandleActiveWindowBindings(Window *window) { if (xy.line >= visible.max.y - 2) { Int set_view_at_line = xy.line - (visible_cells.y - 1); Int cut_off_y = Max((Int)0, visible_size.y - rect_size.y); - view.scroll.y = (set_view_at_line * FontLineSpacing) + cut_off_y; + view->scroll.y = (set_view_at_line * FontLineSpacing) + cut_off_y; } if (xy.line < visible.min.y + 1) { - view.scroll.y = xy.line * FontLineSpacing; + view->scroll.y = xy.line * FontLineSpacing; } if (xy.col >= visible.max.x - 1) { Int set_view_at_line = xy.col - (visible_cells.x - 1); Int cut_off_x = Max((Int)0, visible_size.x - rect_size.x); - view.scroll.x = (set_view_at_line * FontCharSpacing) + cut_off_x; + view->scroll.x = (set_view_at_line * FontCharSpacing) + cut_off_x; } if (xy.col <= visible.min.x) { - view.scroll.x = xy.col * FontCharSpacing; + view->scroll.x = xy.col * FontCharSpacing; } } -} - -void HandleWindowBindings(Window *window) { - ProfileFunction(); - View &view = *GetActiveView(window); - Buffer *buffer = GetBuffer(view.buffer_id); - bool is_active = IsActive(window); - - if (is_active) HandleActiveWindowBindings(window); // Clip scroll { ProfileScope(clip_scroll); - Int last_line = LastLine(*buffer); - view.scroll.y = Clamp(view.scroll.y, (Int)0, Max((Int)0, (last_line - 1) * FontLineSpacing)); + Int last_line = LastLine(*buffer); + view->scroll.y = Clamp(view->scroll.y, (Int)0, Max((Int)0, (last_line - 1) * FontLineSpacing)); // @note: // GetCharCountOfLongestLine is a bottleneck, there is probably an algorithm for // calculating this value incrementally but do we even need X scrollbar or x clipping? - view.scroll.x = ClampBottom(view.scroll.x, (Int)0); + view->scroll.x = ClampBottom(view->scroll.x, (Int)0); } } @@ -574,9 +574,11 @@ void ChangeActiveWindowAndScroll(Array &order) { } if (!window->visible) continue; - View *view = GetActiveView(window); - Vec2 mouse = GetMousePosition(); - bool mouse_in_window = CheckCollisionPointRec(mouse, ToRectangle(window->total_rect)); + View *view = GetActiveView(window); + view->main_caret_on_begin_frame = view->carets[0]; + + Vec2 mouse = GetMousePosition(); + bool mouse_in_window = CheckCollisionPointRec(mouse, ToRectangle(window->total_rect)); if (mouse_in_window) { if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { SetActiveWindow(window->id); @@ -600,27 +602,21 @@ void ReplaceInfobarData() { View *view = GetView(window->active_view); Buffer *buffer = GetBuffer(view->buffer_id); - XY xy = {}; - String name = {}; - { - Window *window = GetWindow(GetLastActiveWindow()); - View *view = GetActiveView(window); - Buffer *buffer = GetBuffer(view->buffer_id); - Scratch scratch; - Caret caret = view->carets[0]; - xy = PosToXY(*buffer, GetFront(caret)); - name = buffer->name; - } + Window *last_window = GetWindow(GetLastActiveWindow()); + View *last_view = GetActiveView(last_window); + Buffer *last_buffer = GetBuffer(last_view->buffer_id); + Scratch scratch; + Caret caret = last_view->carets[0]; + XY xy = PosToXY(*last_buffer, GetFront(caret)); + String name = last_buffer->name; - Scratch scratch; String16 buffer_string = GetString(*buffer); - - Range replace_range = {0, buffer->len}; + Range replace_range = {0, buffer->len}; if (!Seek(buffer_string, L"|", &replace_range.max)) { ReplaceText(buffer, GetEndAsRange(*buffer), L"|"); } - String s = Format(scratch, "line: %5lld col: %5lld name: %.*s", (long long)xy.line + 1ll, (long long)xy.col + 1ll, FmtString(name)); + String s = Format(scratch, "line: %5lld col: %5lld name: %.*s wid: %d vid: %d bid: %d", (long long)xy.line + 1ll, (long long)xy.col + 1ll, FmtString(name), (int)last_window->id.id, (int)last_view->id.id, (int)last_buffer->id.id); String16 string = ToString16(scratch, s); ReplaceText(buffer, replace_range, string); Command_SelectRangeOneCursor(view, {}); diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index c084dcf..1e7335e 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -43,7 +43,10 @@ struct View { BufferID buffer_id; Vec2I scroll; Array carets; - Range selection_anchor; + + // window | view + Range selection_anchor; + Caret main_caret_on_begin_frame; }; struct Window {