From 48dc808dcb1538475ff28aea8f7fe3c5cdafa1b9 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 2 Aug 2024 12:49:03 +0200 Subject: [PATCH] Improve command window --- build_file.cpp | 2 ++ src/platform/render_opengl.cpp | 20 ++++++++----- src/text_editor/commands.cpp | 2 +- src/text_editor/commands_window.cpp | 25 ++++++++-------- src/text_editor/generated.cpp | 3 ++ src/text_editor/todo.txt | 1 - src/text_editor/window.cpp | 46 ++++++++++++++++++----------- src/text_editor/window_draw.cpp | 21 +++++++++++++ 8 files changed, 80 insertions(+), 40 deletions(-) diff --git a/build_file.cpp b/build_file.cpp index 51d0468..49aa7f4 100644 --- a/build_file.cpp +++ b/build_file.cpp @@ -369,6 +369,8 @@ void GenerateConfig() { colors.add({"Selection" , "GruvboxLight1"}); colors.add({"WhitespaceDuringSelection", "GruvboxLight4"}); + colors.add({"FuzzySearchLineHighlight", "GruvboxLight4"}); + colors.add({"ScrollbarBackground" , "GruvboxLight2"}); colors.add({"ScrollbarScroller" , "GruvboxLight1"}); colors.add({"ScrollbarScrollerSelected", "GruvboxLight0Hard"}); diff --git a/src/platform/render_opengl.cpp b/src/platform/render_opengl.cpp index 22451de..aaabcdf 100644 --- a/src/platform/render_opengl.cpp +++ b/src/platform/render_opengl.cpp @@ -299,15 +299,19 @@ void DrawRect(Rect2I rect, Color color) { PushQuad2D(RenderArena, &Vertices, ToRect2(rect), MainFont.white_texture_bounding_box, color); } +void DrawRectOutline(Rect2 rect, Color color, float thickness = 2) { + Rect2 a = CutLeft(&rect, thickness); + Rect2 b = CutRight(&rect, thickness); + Rect2 c = CutTop(&rect, thickness); + Rect2 d = CutBottom(&rect, thickness); + PushQuad2D(RenderArena, &Vertices, a, MainFont.white_texture_bounding_box, color); + PushQuad2D(RenderArena, &Vertices, b, MainFont.white_texture_bounding_box, color); + PushQuad2D(RenderArena, &Vertices, c, MainFont.white_texture_bounding_box, color); + PushQuad2D(RenderArena, &Vertices, d, MainFont.white_texture_bounding_box, color); +} + void DrawRectOutline(Rect2I rect, Color color, Int thickness = 2) { - Rect2I a = CutLeft(&rect, thickness); - Rect2I b = CutRight(&rect, thickness); - Rect2I c = CutTop(&rect, thickness); - Rect2I d = CutBottom(&rect, thickness); - PushQuad2D(RenderArena, &Vertices, ToRect2(a), MainFont.white_texture_bounding_box, color); - PushQuad2D(RenderArena, &Vertices, ToRect2(b), MainFont.white_texture_bounding_box, color); - PushQuad2D(RenderArena, &Vertices, ToRect2(c), MainFont.white_texture_bounding_box, color); - PushQuad2D(RenderArena, &Vertices, ToRect2(d), MainFont.white_texture_bounding_box, color); + DrawRectOutline(ToRect2(rect), color, (float)thickness); } Int GetCharSpacing(Font *font, int codepoint = '_') { diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index a2123aa..8fb01cf 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -276,7 +276,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 (window->mouse_selecting || mouse_in_total) { SDL_MouseCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_TEXT); SDL_SetCursor(SDL_MouseCursor); } else if (mouse_in_scrollbar || window->mouse_selecting_scrollbar) { diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 695b15a..6ff7693 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -583,6 +583,7 @@ void WindowCommand(Event event, Window *window, View *view) { } } + // @todo: consider making this view command if (window->fuzzy_search && search) { Scratch scratch; String16 first_line_string = GetLineStringWithoutNL(*buffer, 0); @@ -604,19 +605,19 @@ void WindowCommand(Event event, Window *window, View *view) { view->carets[0] = caret; } - // if (Ctrl(SDLK_Q) || Mouse(MIDDLE)) { - // // @todo: Consider applying this to all cursors - // if (GetSize(view->carets[0].range) == 0) { - // Command_EvalLuaLine(view); - // } else { - // String16 string = GetString(*buffer, view->carets[0].range); - // Command_EvalLua(view, string); - // } - // } - - if (window->execute_line) { + if (window->fuzzy_search) { if (Press(SDLK_RETURN)) { - Command_EvalLuaLine(view); + Scratch scratch; + Buffer *buffer = GetBuffer(view->active_buffer); + Int line = PosToLine(*buffer, GetFront(view->carets[0])); + if (line == 0) line = 1; + + String16 string = GetLineStringWithoutNL(*buffer, line); + EvalString(scratch, string); + + // Clear text + Command_SelectRangeOneCursor(view, GetLineRangeWithoutNL(*buffer, 0)); + Command_Replace(view, {}); } } else if (window->id.id == SearchWindowID.id) { } else { diff --git a/src/text_editor/generated.cpp b/src/text_editor/generated.cpp index a2c4906..f3ed04a 100644 --- a/src/text_editor/generated.cpp +++ b/src/text_editor/generated.cpp @@ -44,6 +44,7 @@ Color ColorMainCaret = GruvboxDark0Hard; Color ColorSubCaret = GruvboxGray245; Color ColorSelection = GruvboxLight1; Color ColorWhitespaceDuringSelection = GruvboxLight4; +Color ColorFuzzySearchLineHighlight = GruvboxLight4; Color ColorScrollbarBackground = GruvboxLight2; Color ColorScrollbarScroller = GruvboxLight1; Color ColorScrollbarScrollerSelected = GruvboxLight0Hard; @@ -98,6 +99,7 @@ Color.MainCaret = GruvboxDark0Hard Color.SubCaret = GruvboxGray245 Color.Selection = GruvboxLight1 Color.WhitespaceDuringSelection = GruvboxLight4 +Color.FuzzySearchLineHighlight = GruvboxLight4 Color.ScrollbarBackground = GruvboxLight2 Color.ScrollbarScroller = GruvboxLight1 Color.ScrollbarScrollerSelected = GruvboxLight0Hard @@ -237,6 +239,7 @@ void ReloadColors() { ColorSubCaret = GetColor("SubCaret", ColorSubCaret); ColorSelection = GetColor("Selection", ColorSelection); ColorWhitespaceDuringSelection = GetColor("WhitespaceDuringSelection", ColorWhitespaceDuringSelection); + ColorFuzzySearchLineHighlight = GetColor("FuzzySearchLineHighlight", ColorFuzzySearchLineHighlight); ColorScrollbarBackground = GetColor("ScrollbarBackground", ColorScrollbarBackground); ColorScrollbarScroller = GetColor("ScrollbarScroller", ColorScrollbarScroller); ColorScrollbarScrollerSelected = GetColor("ScrollbarScrollerSelected", ColorScrollbarScrollerSelected); diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index b78d42a..a11c5bb 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -33,7 +33,6 @@ - open selected string or auto enclosed word when right clicking or middle (this needs to be also handled on keyboard level) - - page up and down should also scroll and leave you in exactly same scroll - word completion diff --git a/src/text_editor/window.cpp b/src/text_editor/window.cpp index 2beac70..38fb343 100644 --- a/src/text_editor/window.cpp +++ b/src/text_editor/window.cpp @@ -123,11 +123,10 @@ void InitWindows(View *null_view) { Window *window = CreateWindow(); window->absolute_position = true; window->dont_save_in_active_window_history = true; - window->deactivate_on_escape = true; - Buffer *buffer = CreateBuffer(sys_allocator, "*console*"); - buffer->no_history = true; - View *view = CreateView(buffer->id); + Buffer *buffer = CreateBuffer(sys_allocator, "*console*"); + // buffer->no_history = true; + View *view = CreateView(buffer->id); SetActiveView(window, view->id); CreateInfobar(window); SetVisibility(window, false); @@ -147,6 +146,7 @@ void InitWindows(View *null_view) { buffer->no_history = true; View *view = CreateView(buffer->id); window->active_view = view->id; + window->z = 2; SetActiveView(window, view->id); DebugWindowID = window->id; } @@ -157,7 +157,6 @@ void InitWindows(View *null_view) { w->draw_line_numbers = false; w->visible = false; w->fuzzy_search = true; - w->execute_line = true; w->invisible_when_inactive = true; w->absolute_position = true; w->dont_save_in_active_window_history = true; @@ -165,6 +164,11 @@ void InitWindows(View *null_view) { Buffer *b = CreateBuffer(sys_allocator, "*commands*"); View *v = CreateView(b->id); SetActiveView(w, v->id); + w->z = 1; + + Window *titlebar = CreateInfobar(w); + titlebar->z = 1; + SetVisibility(w, false); CommandWindowID = w->id; Command_EvalLua(v, L"list_buffers()"); @@ -195,6 +199,7 @@ void InitWindows(View *null_view) { w->invisible_when_inactive = true; w->absolute_position = true; w->deactivate_on_escape = true; + w->z = 2; Buffer *b = CreateBuffer(sys_allocator, "*popup*"); b->no_history = true; View *v = CreateView(b->id); @@ -203,7 +208,7 @@ void InitWindows(View *null_view) { PopupWindowID = w->id; } - SetActiveWindow({1}); + SetActiveWindow({0}); } void LayoutWindows() { @@ -273,23 +278,29 @@ void LayoutWindows() { Rect2 b = CutBottom(&a, 0.4f * size.y); Rect2 c = Shrink(b, 20); - window->z = 2; window->total_rect = ToRect2I(c); window->document_rect = window->total_rect; } { - Window *window = GetWindow(CommandWindowID); - Rect2 screen_rect = GetScreenRectF(); - Vec2 size = GetSize(screen_rect); - CutTop(&screen_rect, size.y * 0.05f); - CutLeft(&screen_rect, size.x * 0.2f); - CutRight(&screen_rect, size.x * 0.2f); - Rect2 r = CutTop(&screen_rect, FontLineSpacing * 30.f); + Window *window = GetWindow(CommandWindowID); + if (window->visible) { + Rect2 screen_rect = GetScreenRectF(); + Vec2 size = GetSize(screen_rect); + CutTop(&screen_rect, size.y * 0.05f); + CutLeft(&screen_rect, size.x * 0.2f); + CutRight(&screen_rect, size.x * 0.2f); + Rect2 r = CutTop(&screen_rect, FontLineSpacing * 30.f); - window->z = 1; - window->total_rect = ToRect2I(r); - window->document_rect = window->total_rect; + window->total_rect = ToRect2I(r); + window->document_rect = window->total_rect; + + Window *title_bar_window = GetWindow(window->title_bar_window); + if (title_bar_window) { + title_bar_window->total_rect = CutBottom(&window->total_rect, GetTitleBarSize(title_bar_window)); + title_bar_window->document_rect = title_bar_window->total_rect; + } + } } { @@ -301,7 +312,6 @@ void LayoutWindows() { Rect2 b = CutBottom(&a, 0.15f * size.y); Rect2 c = Shrink(b, 20); - window->z = 2; window->total_rect = ToRect2I(c); window->document_rect = window->total_rect; } diff --git a/src/text_editor/window_draw.cpp b/src/text_editor/window_draw.cpp index 51dc6d9..dc73c02 100644 --- a/src/text_editor/window_draw.cpp +++ b/src/text_editor/window_draw.cpp @@ -166,6 +166,22 @@ void DrawWindow(Window *window) { DrawRect(rect, color_line_highlight); } } + + if (window->fuzzy_search) { + Caret it = view->carets[0]; + Int front = GetFront(it); + XY fxy = PosToXY(*buffer, front); + if (fxy.line == 0) fxy.line += 1; + Vec2I w = XYToWorldPos(view, XYLine(fxy.line)); + w -= view->scroll; + w += window->document_rect.min; + Rect2 rect = { + {(float)window->total_rect.min.x, (float)w.y}, + {(float)window->total_rect.max.x, (float)w.y + (float)FontLineSpacing} + }; + DrawRectOutline(rect, ColorFuzzySearchLineHighlight); + } + EndProfileScope(); DrawVisibleText(window, color_text); @@ -216,6 +232,11 @@ void DrawWindow(Window *window) { DrawRect(rect, color); } + if (window->z == 1) { + SetScissor(window->total_rect); + DrawRect(window->total_rect, {255, 255, 255, 25}); + } + if (!is_active) { SetScissor(GetScreenRectF()); DrawRect(window->total_rect, ColorInactiveWindow);