From 0921054b6e3b70fbc2de477f9bedf46f4c51593c Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Wed, 31 Dec 2025 10:34:11 +0100 Subject: [PATCH] Fix view centering --- src/backup/todo.txt | 4 ---- src/text_editor/commands.cpp | 16 +++++++++++++++- src/text_editor/text_editor.cpp | 1 + src/text_editor/window_command.cpp | 1 - 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/backup/todo.txt b/src/backup/todo.txt index 1b3a1cb..c145d26 100644 --- a/src/backup/todo.txt +++ b/src/backup/todo.txt @@ -4,8 +4,6 @@ Use session 3: - Maybe status view, commit changes (like to buffer name or line) on enter? -- Upload sublime settings to git and also install script / update config script - How to go about search/replace, opening code and other considerations - We can use sed + find to search and replace, the automatic reopen should do the job @@ -17,7 +15,6 @@ How to go about search/replace, opening code and other considerations Use session 2 - Need configs I can't change browser or vcvarsall currently, maybe syntax like :Set InternetBrowser "firefox" - Tutorial -- When jumping should center the view!!! Debug session: - Report errorf - use coroutine dialogs @@ -49,7 +46,6 @@ Commands TODO: - Special: non editable, hotkeys don't work etc. backlog -FEATURE commands for scrolling: center, cursor_at_bottom_of_screen, cursor_at_top FEATURE Some decl/function indexing in fuzzy format ISSUE? Fix jump scroll, the query ends up the last line on screen, kind of wacky FEATURE dump text editor state to file, restore state diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index b231640..ebd1af0 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -204,6 +204,19 @@ void ReportDebugf(const char *fmt, ...) { RawAppendf(TraceBuffer, "%S\n", string); } +void Command_CenterView() { + BSet set = GetBSet(LastActiveLayoutWindowID); + Caret c = set.view->carets[0]; + Int front = GetFront(c); + XY xy = PosToXY(set.buffer, front); + Vec2I size = GetSize(set.window->document_rect); + Int y = size.y / 2 / set.window->font->line_spacing; + set.view->scroll.x = 0; + if (xy.line > y) { + set.view->scroll.y = (xy.line) * set.window->font->line_spacing - (size.y / 2); + } +} RegisterCommand(Command_CenterView, ""); + void MoveCursorByPageSize(Window *window, int direction, bool shift = false) { Assert(direction == DIR_UP || direction == DIR_DOWN); BSet set = GetBSet(window); @@ -589,6 +602,7 @@ BSet Open(Window *window, String path, String meta, bool set_active = true) { for (FileIter it = IterateFiles(scratch, o.path); IsValid(it); Advance(&it)) { RawAppendf(buffer, "%S\n", it.filename); } + SelectRange(view, GetBufferBeginAsRange(buffer)); } else { View *view = WindowOpenBufferView(set.window, o.path); Buffer *buffer = GetBuffer(view->active_buffer); @@ -598,7 +612,7 @@ BSet Open(Window *window, String path, String meta, bool set_active = true) { view->carets[0] = MakeCaret(pos); } } - UpdateScroll(set.window, true); + Command_CenterView(); } else if (o.kind == OpenKind_Exec) { if (set_active) { NextActiveWindowID = set.window->id; diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index c71e7d5..36bfd74 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -119,6 +119,7 @@ void SetMouseCursor(Event event) { } SetMouseCursor(SDL_SYSTEM_CURSOR_DEFAULT); } + void UpdateScroll(Window *window, bool update_caret_scrolling) { ProfileFunction(); BSet set = GetBSet(window); diff --git a/src/text_editor/window_command.cpp b/src/text_editor/window_command.cpp index 1cf9a0c..52c7ab5 100644 --- a/src/text_editor/window_command.cpp +++ b/src/text_editor/window_command.cpp @@ -149,7 +149,6 @@ void Command_CommandWindowOpen() { BSet main = GetBSet(LastActiveLayoutWindowID); NextActiveWindowID = main.window->id; OpenCommand(active); - SelectRange(main.view, Range{}); } void CommandWindowLayout(Rect2I *rect, Int wx, Int wy) {