Skip to content

Commit

Permalink
Some minor cleanup of UI code
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
guillaumechereau committed Mar 3, 2024
1 parent c4173d9 commit a05eeef
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
22 changes: 18 additions & 4 deletions src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,37 +748,51 @@ 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)
flags |= ImGuiWindowFlags_NoMouseInputs;
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));
}
ImGui::Begin(label, NULL, flags);

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)
Expand Down
7 changes: 6 additions & 1 deletion src/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 1 addition & 2 deletions src/gui/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit a05eeef

Please sign in to comment.