From a05eeef029b6cf314227dc107242a1dcd0007fcc Mon Sep 17 00:00:00 2001 From: Guillaume Chereau Date: Sun, 3 Mar 2024 16:45:51 +0800 Subject: [PATCH] Some minor cleanup of UI code - Make gui_window_end returns the size of the window, so that we can use that for manual layout. - Clamp the size of the windows to the screen size, so that we can scroll them. --- src/gui.cpp | 22 ++++++++++++++++++---- src/gui.h | 7 ++++++- src/gui/app.c | 3 +-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/gui.cpp b/src/gui.cpp index 4292ac67d..2a36dbc47 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -748,7 +748,11 @@ void gui_window_begin(const char *label, float x, float y, float w, float h, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoDecoration; float max_h; + ImGuiStorage *storage = ImGui::GetStateStorage(); + ImGuiID key; + float *last_y; + ImGui::PushID(label); if (!gui->can_move_window) flags |= ImGuiWindowFlags_NoMove; if (gui->scrolling) @@ -756,8 +760,12 @@ void gui_window_begin(const char *label, float x, float y, float w, float h, ImGui::SetNextWindowPos(ImVec2(x, y), moved == NULL ? ImGuiCond_Always : ImGuiCond_Appearing); ImGui::SetNextWindowSize(ImVec2(w, h)); + + key = ImGui::GetID("lasty"); + last_y = storage->GetFloatRef(key, y); + if (h == 0) { - max_h = ImGui::GetMainViewport()->Size.y - y; + max_h = ImGui::GetMainViewport()->Size.y - *last_y; ImGui::SetNextWindowSizeConstraints( ImVec2(0, 0), ImVec2(FLT_MAX, max_h)); } @@ -765,20 +773,26 @@ void gui_window_begin(const char *label, float x, float y, float w, float h, if (moved != NULL) { *moved = ImGui::GetWindowPos() != ImVec2(x, y); + *last_y = ImGui::GetWindowPos().y; } ImGui::BeginGroup(); } -void gui_window_end(void) +gui_window_ret_t gui_window_end(void) { - + gui_window_ret_t ret = {}; ImGui::EndGroup(); - if (!GUI_HAS_SCROLLBARS) { + if (!GUI_HAS_SCROLLBARS && !gui->can_move_window) { if (gui_pan_scroll_behavior()) gui->scrolling |= 1; } + ret.h = ImGui::GetWindowHeight(); + ret.w = ImGui::GetWindowWidth(); ImGui::End(); + ImGui::PopID(); + + return ret; } bool gui_input_int(const char *label, int *v, int minv, int maxv) diff --git a/src/gui.h b/src/gui.h index fe8a55e0f..3fccf389f 100644 --- a/src/gui.h +++ b/src/gui.h @@ -35,10 +35,15 @@ # define GUI_PANEL_WIDTH_LARGE 400 #endif +typedef struct { + float h; + float w; +} gui_window_ret_t; + void gui_window_begin(const char *label, float x, float y, float w, float h, bool *moved); -void gui_window_end(void); +gui_window_ret_t gui_window_end(void); bool gui_want_capture_mouse(void); diff --git a/src/gui/app.c b/src/gui/app.c index d18814e56..089f54bed 100644 --- a/src/gui/app.c +++ b/src/gui/app.c @@ -138,9 +138,8 @@ void gui_app(void) gui_window_begin("Top Bar", x, y, 0, 0, NULL); gui_top_bar(); - gui_window_end(); + y += gui_window_end().h; - y += ICON_HEIGHT + 28; gui_window_begin("Left Bar", x, y, 0, 0, NULL); render_left_panel(); gui_window_end();