diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 5b1ab0c..7f343bf 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -1,14 +1,14 @@ void Command_MoveCursorsByPageSize(Window *window, int direction, bool shift = false) { Assert(direction == DIR_UP || direction == DIR_DOWN); - View &view = *GetActiveView(window); - Buffer *buffer = GetBuffer(view.active_buffer); + View *view = GetView(window->active_view); + Buffer *buffer = GetBuffer(view->active_buffer); Rect2I visible_cells_rect = GetVisibleCells(window); Int y = GetSize(visible_cells_rect).y - 2; if (direction == DIR_UP) y = -y; - For(view.carets) { + For(view->carets) { XY xy = PosToXY(*buffer, GetFront(it)); if (direction == DIR_DOWN && xy.line == buffer->line_starts.len - 1) { Range line_range = GetLineRange(*buffer, xy.line); @@ -932,14 +932,13 @@ void WindowCommand(Event event, Window *window, View *view) { void UpdateScroll(Window *window, bool update_caret_scrolling) { ProfileFunction(); - View *view = GetActiveView(window); - Buffer *buffer = GetBuffer(view->active_buffer); + BSet set = GetBSet(window); // Scrolling with caret if (update_caret_scrolling) { - Caret c = view->carets[0]; + Caret c = set.view->carets[0]; Int front = GetFront(c); - XY xy = PosToXY(*buffer, front); + XY xy = PosToXY(*set.buffer, front); Rect2I visible = GetVisibleCells(window); Vec2I visible_cells = GetSize(visible); @@ -949,32 +948,32 @@ void UpdateScroll(Window *window, bool update_caret_scrolling) { 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; + set.view->scroll.y = (set_view_at_line * FontLineSpacing) + cut_off_y; } if (xy.line < visible.min.y + 1) { - view->scroll.y = xy.line * FontLineSpacing; + set.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; + set.view->scroll.x = (set_view_at_line * FontCharSpacing) + cut_off_x; } if (xy.col <= visible.min.x) { - view->scroll.x = xy.col * FontCharSpacing; + set.view->scroll.x = xy.col * FontCharSpacing; } } // 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(*set.buffer); + set.view->scroll.y = Clamp(set.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); + set.view->scroll.x = ClampBottom(set.view->scroll.x, (Int)0); } } \ No newline at end of file diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index 064cdb6..8439bf9 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -78,7 +78,7 @@ inline View *GetView(ViewID id) { inline bool IsNull(Buffer *buffer) { return buffer->id.id == NullBufferID.id; } inline Window *GetActiveWindow() { return GetWindow(ActiveWindow); } -inline View *GetActiveView(Window *window) { return GetView(window->active_view); } +// inline View *GetActiveView(Window *window) { return GetView(window->active_view); } void InitBuffer(Allocator allocator, Buffer *buffer, String name, Int size = 4096) { buffer->id = AllocBufferID(buffer); @@ -191,6 +191,11 @@ BSet GetMainSet(Window *window) { return result; } +BSet GetActiveSet() { + Window *window = GetWindow(ActiveWindow); + return GetBSet(window); +} + BSet GetActiveMainSet() { Window *window = GetWindow(ActiveWindow); if (window->is_title_bar) window = GetWindow(window->title_bar_window); diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 2dbfbef..b0e884c 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -159,10 +159,9 @@ Event TranslateSDLEvent(SDL_Event *input_event) { void HandleEvent(Event event) { bool run_window_command = GlobalCommand(event); if (run_window_command) { - Window *window = GetActiveWindow(); - View *view = GetActiveView(window); - WindowCommand(event, window, view); - MergeCarets(view); + BSet active = GetActiveSet(); + WindowCommand(event, active.window, active.view); + MergeCarets(active.view); } For(Windows) if (it.o->is_title_bar) ReplaceTitleBarData(it.o); } @@ -176,7 +175,7 @@ void Update(Event event) { For(order) { Window *window = Windows[it].o; if (!window->visible) continue; - View *view = GetActiveView(window); + View *view = GetView(window->active_view); view->main_caret_on_begin_frame = view->carets[0]; view->update_scroll = true; } diff --git a/src/text_editor/title_bar.cpp b/src/text_editor/title_bar.cpp index 7185648..3b1e02f 100644 --- a/src/text_editor/title_bar.cpp +++ b/src/text_editor/title_bar.cpp @@ -2,7 +2,7 @@ void UpdateDebugBuffer() { Buffer *buffer = GetBuffer(DebugBufferID); Window *window = GetActiveWindow(); - View *view = GetActiveView(window); + View *view = GetView(window->active_view); if (view->active_buffer.id == buffer->id.id) return; BSet main = GetActiveMainSet(); @@ -27,12 +27,10 @@ void ReplaceTitleBarData(Window *window) { Buffer *buffer = GetBuffer(view->active_buffer); view->scroll.y = 0; - Window *last_window = GetWindow(window->title_bar_window); - View *last_view = GetActiveView(last_window); - Buffer *last_buffer = GetBuffer(last_view->active_buffer); + BSet main = GetMainSet(window); Scratch scratch; - Caret caret = last_view->carets[0]; - XY xy = PosToXY(*last_buffer, GetFront(caret)); + Caret caret = main.view->carets[0]; + XY xy = PosToXY(*main.buffer, GetFront(caret)); Array caret_copy = Copy(GetSystemAllocator(), view->carets); defer { @@ -48,7 +46,7 @@ void ReplaceTitleBarData(Window *window) { AdjustCarets(edits, &caret_copy); } - String s = Format(scratch, "%.*s:%lld:%lld", FmtString(last_buffer->name), (long long)xy.line + 1ll, (long long)xy.col + 1ll); + String s = Format(scratch, "%.*s:%lld:%lld", FmtString(main.buffer->name), (long long)xy.line + 1ll, (long long)xy.col + 1ll); String16 string = ToString16(scratch, s); String16 string_to_replace = GetString(*buffer, replace_range); if (string_to_replace != string) { diff --git a/src/text_editor/window_draw.cpp b/src/text_editor/window_draw.cpp index 1200378..90fe550 100644 --- a/src/text_editor/window_draw.cpp +++ b/src/text_editor/window_draw.cpp @@ -5,7 +5,7 @@ Vec2I GetCellSize() { Rect2I GetVisibleCells(Window *window) { ProfileFunction(); - View *view = GetActiveView(window); + View *view = GetView(window->active_view); Vec2I size = GetSize(window->document_rect); Int _cx = size.x / FontCharSpacing; @@ -21,7 +21,7 @@ Rect2I GetVisibleCells(Window *window) { } Scroller ComputeScrollerRect(Window *window) { - View *view = GetActiveView(window); + View *view = GetView(window->active_view); Buffer *buffer = GetBuffer(view->active_buffer); Vec2I size = GetSize(window->scrollbar_rect); Rect2I vis = GetVisibleCells(window); @@ -40,7 +40,7 @@ Scroller ComputeScrollerRect(Window *window) { void DrawVisibleText(Window *window, Color tint) { ProfileFunction(); - View *view = GetActiveView(window); + View *view = GetView(window->active_view); Buffer *buffer = GetBuffer(view->active_buffer); Rect2I visible = GetVisibleCells(window); @@ -81,7 +81,7 @@ Rect2I XYToRect(View *view, XY xy) { } void DrawCaret(Window *window, XY xy, float size, Color color) { - View *view = GetActiveView(window); + View *view = GetView(window->active_view); Rect2I _rect = XYToRect(view, xy); Rect2I rect = CutLeft(&_rect, (Int)(size * (float)FontCharSpacing)); rect -= view->scroll; @@ -106,7 +106,7 @@ void DrawUnderline(Window *window, View *view, Buffer *buffer, Range range, Colo } void DrawWindow(Window *window, Event &event) { - View *view = GetActiveView(window); + View *view = GetView(window->active_view); Buffer *buffer = GetBuffer(view->active_buffer); SetScissor(GetScreenRectF());