Some more refactoring

This commit is contained in:
Krzosa Karol
2024-08-13 09:55:28 +02:00
parent 7b21cba834
commit 5ce418995f
5 changed files with 33 additions and 32 deletions

View File

@@ -1,14 +1,14 @@
void Command_MoveCursorsByPageSize(Window *window, int direction, bool shift = false) { void Command_MoveCursorsByPageSize(Window *window, int direction, bool shift = false) {
Assert(direction == DIR_UP || direction == DIR_DOWN); Assert(direction == DIR_UP || direction == DIR_DOWN);
View &view = *GetActiveView(window); View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view.active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
Rect2I visible_cells_rect = GetVisibleCells(window); Rect2I visible_cells_rect = GetVisibleCells(window);
Int y = GetSize(visible_cells_rect).y - 2; Int y = GetSize(visible_cells_rect).y - 2;
if (direction == DIR_UP) y = -y; if (direction == DIR_UP) y = -y;
For(view.carets) { For(view->carets) {
XY xy = PosToXY(*buffer, GetFront(it)); XY xy = PosToXY(*buffer, GetFront(it));
if (direction == DIR_DOWN && xy.line == buffer->line_starts.len - 1) { if (direction == DIR_DOWN && xy.line == buffer->line_starts.len - 1) {
Range line_range = GetLineRange(*buffer, xy.line); 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) { void UpdateScroll(Window *window, bool update_caret_scrolling) {
ProfileFunction(); ProfileFunction();
View *view = GetActiveView(window); BSet set = GetBSet(window);
Buffer *buffer = GetBuffer(view->active_buffer);
// Scrolling with caret // Scrolling with caret
if (update_caret_scrolling) { if (update_caret_scrolling) {
Caret c = view->carets[0]; Caret c = set.view->carets[0];
Int front = GetFront(c); Int front = GetFront(c);
XY xy = PosToXY(*buffer, front); XY xy = PosToXY(*set.buffer, front);
Rect2I visible = GetVisibleCells(window); Rect2I visible = GetVisibleCells(window);
Vec2I visible_cells = GetSize(visible); Vec2I visible_cells = GetSize(visible);
@@ -949,32 +948,32 @@ void UpdateScroll(Window *window, bool update_caret_scrolling) {
if (xy.line >= visible.max.y - 2) { if (xy.line >= visible.max.y - 2) {
Int set_view_at_line = xy.line - (visible_cells.y - 1); Int set_view_at_line = xy.line - (visible_cells.y - 1);
Int cut_off_y = Max((Int)0, visible_size.y - rect_size.y); 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) { 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) { if (xy.col >= visible.max.x - 1) {
Int set_view_at_line = xy.col - (visible_cells.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); 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) { if (xy.col <= visible.min.x) {
view->scroll.x = xy.col * FontCharSpacing; set.view->scroll.x = xy.col * FontCharSpacing;
} }
} }
// Clip scroll // Clip scroll
{ {
Int last_line = LastLine(*buffer); Int last_line = LastLine(*set.buffer);
view->scroll.y = Clamp(view->scroll.y, (Int)0, Max((Int)0, (last_line - 1) * FontLineSpacing)); set.view->scroll.y = Clamp(set.view->scroll.y, (Int)0, Max((Int)0, (last_line - 1) * FontLineSpacing));
// @note: // @note:
// GetCharCountOfLongestLine is a bottleneck, there is probably an algorithm for // GetCharCountOfLongestLine is a bottleneck, there is probably an algorithm for
// calculating this value incrementally but do we even need X scrollbar or x clipping? // 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);
} }
} }

View File

@@ -78,7 +78,7 @@ inline View *GetView(ViewID id) {
inline bool IsNull(Buffer *buffer) { return buffer->id.id == NullBufferID.id; } inline bool IsNull(Buffer *buffer) { return buffer->id.id == NullBufferID.id; }
inline Window *GetActiveWindow() { return GetWindow(ActiveWindow); } 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) { void InitBuffer(Allocator allocator, Buffer *buffer, String name, Int size = 4096) {
buffer->id = AllocBufferID(buffer); buffer->id = AllocBufferID(buffer);
@@ -191,6 +191,11 @@ BSet GetMainSet(Window *window) {
return result; return result;
} }
BSet GetActiveSet() {
Window *window = GetWindow(ActiveWindow);
return GetBSet(window);
}
BSet GetActiveMainSet() { BSet GetActiveMainSet() {
Window *window = GetWindow(ActiveWindow); Window *window = GetWindow(ActiveWindow);
if (window->is_title_bar) window = GetWindow(window->title_bar_window); if (window->is_title_bar) window = GetWindow(window->title_bar_window);

View File

@@ -159,10 +159,9 @@ Event TranslateSDLEvent(SDL_Event *input_event) {
void HandleEvent(Event event) { void HandleEvent(Event event) {
bool run_window_command = GlobalCommand(event); bool run_window_command = GlobalCommand(event);
if (run_window_command) { if (run_window_command) {
Window *window = GetActiveWindow(); BSet active = GetActiveSet();
View *view = GetActiveView(window); WindowCommand(event, active.window, active.view);
WindowCommand(event, window, view); MergeCarets(active.view);
MergeCarets(view);
} }
For(Windows) if (it.o->is_title_bar) ReplaceTitleBarData(it.o); For(Windows) if (it.o->is_title_bar) ReplaceTitleBarData(it.o);
} }
@@ -176,7 +175,7 @@ void Update(Event event) {
For(order) { For(order) {
Window *window = Windows[it].o; Window *window = Windows[it].o;
if (!window->visible) continue; if (!window->visible) continue;
View *view = GetActiveView(window); View *view = GetView(window->active_view);
view->main_caret_on_begin_frame = view->carets[0]; view->main_caret_on_begin_frame = view->carets[0];
view->update_scroll = true; view->update_scroll = true;
} }

View File

@@ -2,7 +2,7 @@ void UpdateDebugBuffer() {
Buffer *buffer = GetBuffer(DebugBufferID); Buffer *buffer = GetBuffer(DebugBufferID);
Window *window = GetActiveWindow(); Window *window = GetActiveWindow();
View *view = GetActiveView(window); View *view = GetView(window->active_view);
if (view->active_buffer.id == buffer->id.id) return; if (view->active_buffer.id == buffer->id.id) return;
BSet main = GetActiveMainSet(); BSet main = GetActiveMainSet();
@@ -27,12 +27,10 @@ void ReplaceTitleBarData(Window *window) {
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
view->scroll.y = 0; view->scroll.y = 0;
Window *last_window = GetWindow(window->title_bar_window); BSet main = GetMainSet(window);
View *last_view = GetActiveView(last_window);
Buffer *last_buffer = GetBuffer(last_view->active_buffer);
Scratch scratch; Scratch scratch;
Caret caret = last_view->carets[0]; Caret caret = main.view->carets[0];
XY xy = PosToXY(*last_buffer, GetFront(caret)); XY xy = PosToXY(*main.buffer, GetFront(caret));
Array<Caret> caret_copy = Copy(GetSystemAllocator(), view->carets); Array<Caret> caret_copy = Copy(GetSystemAllocator(), view->carets);
defer { defer {
@@ -48,7 +46,7 @@ void ReplaceTitleBarData(Window *window) {
AdjustCarets(edits, &caret_copy); 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 = ToString16(scratch, s);
String16 string_to_replace = GetString(*buffer, replace_range); String16 string_to_replace = GetString(*buffer, replace_range);
if (string_to_replace != string) { if (string_to_replace != string) {

View File

@@ -5,7 +5,7 @@ Vec2I GetCellSize() {
Rect2I GetVisibleCells(Window *window) { Rect2I GetVisibleCells(Window *window) {
ProfileFunction(); ProfileFunction();
View *view = GetActiveView(window); View *view = GetView(window->active_view);
Vec2I size = GetSize(window->document_rect); Vec2I size = GetSize(window->document_rect);
Int _cx = size.x / FontCharSpacing; Int _cx = size.x / FontCharSpacing;
@@ -21,7 +21,7 @@ Rect2I GetVisibleCells(Window *window) {
} }
Scroller ComputeScrollerRect(Window *window) { Scroller ComputeScrollerRect(Window *window) {
View *view = GetActiveView(window); View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
Vec2I size = GetSize(window->scrollbar_rect); Vec2I size = GetSize(window->scrollbar_rect);
Rect2I vis = GetVisibleCells(window); Rect2I vis = GetVisibleCells(window);
@@ -40,7 +40,7 @@ Scroller ComputeScrollerRect(Window *window) {
void DrawVisibleText(Window *window, Color tint) { void DrawVisibleText(Window *window, Color tint) {
ProfileFunction(); ProfileFunction();
View *view = GetActiveView(window); View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
Rect2I visible = GetVisibleCells(window); Rect2I visible = GetVisibleCells(window);
@@ -81,7 +81,7 @@ Rect2I XYToRect(View *view, XY xy) {
} }
void DrawCaret(Window *window, XY xy, float size, Color color) { 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 = XYToRect(view, xy);
Rect2I rect = CutLeft(&_rect, (Int)(size * (float)FontCharSpacing)); Rect2I rect = CutLeft(&_rect, (Int)(size * (float)FontCharSpacing));
rect -= view->scroll; rect -= view->scroll;
@@ -106,7 +106,7 @@ void DrawUnderline(Window *window, View *view, Buffer *buffer, Range range, Colo
} }
void DrawWindow(Window *window, Event &event) { void DrawWindow(Window *window, Event &event) {
View *view = GetActiveView(window); View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
SetScissor(GetScreenRectF()); SetScissor(GetScreenRectF());