Code cleanup

This commit is contained in:
ahsanswr
2026-07-26 17:24:36 +02:00
committed by Guillaume Meunier
parent 5e359fe983
commit 80ee73128d
18 changed files with 159 additions and 207 deletions
+43 -57
View File
@@ -24,8 +24,8 @@
#include <string>
#include <vector>
// Themed, reusable UI widgets. Colors and metrics come from wivrn::ui::current().
// Display text is taken by const std::string &; ids and icon glyphs stay const char *.
// Themed, reusable UI widgets, colors and metrics from wivrn::ui::current()
// Display text takes const std::string &, ids and icon glyphs stay const char *
namespace wivrn::ui
{
@@ -42,49 +42,44 @@ void page_header(const std::string & title, const std::string & subtitle = {});
// Rounded panel, pair with end_card
bool begin_card(const char * id, const ImVec2 & size = {0, 0});
// Same, but with tighter padding for cards that hold a list of rows
// Tighter padding, for cards holding a list of rows
bool begin_list_card(const char * id);
void end_card();
// Full-width divider between rows of a card
void row_separator();
// Title and wrapped muted description on the left, control_width reserved on the right.
// Returns the window-local y of the label bottom, so the caller can keep the row tall
// enough for a multi-line label.
// Title and wrapped muted description on the left, control_width reserved on the right
// Returns the window-local y of the label bottom, to keep the row tall enough for a multi-line label
float setting_label(const std::string & title, const std::string & description, float control_width);
// "<icon> <label>" with the standard two-space gap, for chips and text buttons that
// take a single label string with an embedded glyph.
// "<icon> <label>" with the standard two-space gap
std::string icon_label(const char * icon, const std::string & label);
// Auto-fit width of a button, matching what button() lays out when size.x is left at 0.
// Use to reserve/right-align a button before drawing it. The icon overload accounts for
// the leading glyph drawn at metrics::button_label_glyph.
// Auto-fit width of a button, as laid out when size.x is 0
float button_width(const std::string & label);
float button_width(const char * icon, const std::string & label);
// Footprint of a chip() with standard padding (no dot, no height override).
// Footprint of a chip() with standard padding, no dot, no height override
ImVec2 chip_size(const std::string & label);
// Exact layout width of a chip(), accounting for the optional status dot and the roomier
// padding used when height > 0 (the tall pill chips in the top bar).
// Exact layout width of a chip(), accounting for the optional dot and the height override padding
float chip_width(const std::string & label, bool dot = false, float height = 0);
bool button(const std::string & label, button_style style = button_style::primary, const ImVec2 & size = {0, 0});
// Same, with a larger leading icon glyph drawn before the label
// Same, with a larger leading icon glyph
bool button(const char * icon, const std::string & label, button_style style = button_style::primary, const ImVec2 & size = {0, 0});
// tooltip, if not empty, is shown while hovered
bool icon_button(const char * icon, const ImVec2 & size = {0, 0}, bool active = false, const std::string & tooltip = {});
// Circular download-progress indicator with a cancel (stop) glyph in the centre.
// fraction in [0,1] draws a determinate ring; < 0 draws an indeterminate spinner.
// Returns true when clicked (i.e. cancel requested). Sized like icon_button by default.
// Circular download-progress indicator with a centred cancel glyph
// fraction in [0,1] draws a determinate ring, < 0 an indeterminate spinner
// Returns true when cancel is clicked
bool cancel_progress_button(const char * id, float fraction, const ImVec2 & size = {0, 0}, const std::string & tooltip = {});
bool toggle(const char * id, bool * v, const bool * default_value = nullptr);
// Width a trailing reset slot consumes (gap + square), for laying out toggles
// Width a trailing reset slot consumes, gap + square
float reset_slot_width();
// The value controls below draw a reset button when the value differs from default_value
@@ -99,19 +94,17 @@ struct combo_item
const char * description = nullptr;
};
// Combo box whose options open in a modal list on the popup layer. *selected is the
// chosen index, title labels the modal. Returns true when the selection changes.
// Combo box whose options open in a modal list, *selected is the chosen index, title labels the modal
bool combo(const char * id, const std::string & title, const std::vector<combo_item> & items, int * selected, float width = 0, const int * default_value = nullptr);
// Centre (display coords) where combo and begin_modal popups open, plus the height
// available on the popup layer so tall combo lists cap and scroll instead of being
// clipped. available_height of 0 leaves the list unbounded. Set once per frame.
// Centre (display coords) where combo and begin_modal popups open, plus the popup-layer height
// available_height of 0 leaves the list unbounded, set once per frame
void set_popup_center(const ImVec2 & center, float available_height = 0);
// Hook fired after an interactive item, for a hover haptic. Install once.
// hover-haptic hook, fired after an interactive item
void set_hover_haptic(std::function<void()> hook);
// Hook to show a tooltip for the last item. Install once.
// tooltip hook for the last item
void set_tooltip_hook(std::function<void(const char *)> hook);
enum class chip_style
@@ -123,24 +116,24 @@ enum class chip_style
warning,
danger,
};
// Status badge. label may include a leading icon glyph; dot draws a status dot.
// height overrides the pill height (default sizes to the text + padding)
// Status badge, label may include a leading icon glyph, dot draws a status dot
// height overrides the pill height, default sizes to text + padding
void chip(const std::string & label, chip_style style = chip_style::neutral, bool dot = false, float height = 0);
// Muted sidebar group header
void nav_section(const std::string & label);
// Sidebar entry: icon + label, highlighted when selected. Returns true when clicked
// sidebar entry: icon + label, highlighted when selected, returns true when clicked
bool nav_item(const char * icon, const std::string & label, bool selected);
// Single-line text field at control height; hint is optional placeholder text
// Single-line text field at control height, hint is optional placeholder text
bool input_text(const char * id, std::string & text, const char * hint = nullptr, float width = 0);
// Number field with - / + stepper buttons. Returns true when the value changes
// number field with -/+ steppers, returns true on change
bool input_int(const char * id, int * v, int step = 1, int v_min = 0, int v_max = 0, float width = 0);
// Centred modal on the popup layer. Open with ImGui::OpenPopup(id), dismiss with
// ImGui::CloseCurrentPopup(); pair with end_modal only when this returns true.
// Centred modal on the popup layer, open with ImGui::OpenPopup(id), dismiss with ImGui::CloseCurrentPopup()
// Pair with end_modal only when this returns true
bool begin_modal(const char * id, const std::string & title, float width = 0);
void end_modal();
@@ -152,7 +145,7 @@ struct action_item
bool checked = false;
};
// Overflow button: an icon button opening a popup of actions. Returns the picked index, or -1
// overflow icon button opening a popup of actions, returns the picked index or -1
int action_menu(const char * id, const char * icon, const std::vector<action_item> & items);
struct list_row_result
@@ -160,51 +153,44 @@ struct list_row_result
ImVec2 min, max; // row rect (screen) for right-aligning trailing controls
bool clicked; // the row body was clicked
// Screen position for a trailing control of the given size, right-aligned to right_x
// and vertically centred in the row. Feed to ImGui::SetCursorScreenPos.
// Screen position for a size trailing control, right-aligned to right_x and vertically centred
ImVec2 trailing(float right_x, const ImVec2 & size) const
{
return {right_x - size.x, min.y + (max.y - min.y - size.y) * 0.5f};
}
};
// List row: leading thumbnail (image, if non-zero) or icon box, title and muted subtitle.
// trailing_width reserves space on the right for trailing controls and excludes it from
// the row's click area. Place controls right-aligned at .max with SetCursorScreenPos,
// then call end_list_row().
// large_thumb makes the leading thumbnail/icon box span the full row height (icon boxes
// keep their background at that same size).
// interactive=false reserves layout only -- no row-body click area and no hover highlight,
// for rows whose body does nothing (only trailing controls act).
// List row: leading thumbnail (image, if non-zero) or icon box, title and muted subtitle
// trailing_width reserves right-side space for trailing controls, excluded from the click area
// large_thumb spans the leading thumbnail/icon box across the full row height
// interactive=false reserves layout only, no row-body click area or hover highlight
list_row_result begin_list_row(const char * id, const char * icon, ImTextureID image, const std::string & title, const std::string & subtitle, bool selected = false, float trailing_width = 0, float height = 0, bool large_thumb = false, bool interactive = true);
void end_list_row();
// Confirmation dialog (open with ImGui::OpenPopup(id)). Returns 1 if confirmed, -1 if
// cancelled or dismissed, 0 otherwise. danger styles the confirm button as destructive.
// confirmation dialog, open with ImGui::OpenPopup(id), returns 1 confirmed / -1 cancelled / 0 otherwise
// danger styles the confirm button as destructive
int confirm_modal(const char * id, const std::string & title, const std::string & message, const std::string & confirm_label, const std::string & cancel_label, bool danger = false);
// --- Shared window shell: top bar, sidebar and dividers used by the lobby and the
// in-stream GUI so the two screens stay identical. Call inside the main window.
// shared window shell: top bar, sidebar and dividers, called inside the main window
// One slot in the top bar's right-aligned cluster. width is the slot's layout width
// (use chip_width() for chips, the control side for icon buttons); draw() renders it at
// the cursor, vertically centred, at the standard control height.
// One slot in the top bar's right-aligned cluster, width is the slot's layout width
// draw() renders it at the cursor, vertically centred, at the standard control height
struct top_bar_item
{
float width;
std::function<void()> draw;
};
// Top bar: WiVRn logo + wordmark on the left, right_items right-aligned. Manages its own
// child window; height is the bar height in pixels.
// Top bar: WiVRn logo + wordmark on the left, right_items right-aligned, height in pixels
void top_bar(float height, ImTextureID logo, const std::vector<top_bar_item> & right_items);
// Sidebar nav frame at the standard width/offset. Fill with nav_section()/nav_item()
// between the calls; pair begin_sidebar with end_sidebar.
void begin_sidebar(float top_bar_h, float tab_width);
// sidebar nav frame: scrolling top section, footer_items rows pinned at the bottom
// always pair: begin_sidebar -> sidebar_footer -> end_sidebar
void begin_sidebar(float top_bar_h, float tab_width, int footer_items = 0);
void sidebar_footer();
void end_sidebar();
// Hairline dividers separating the top bar, sidebar and content, dimmed with the panel.
// Hairline dividers separating the top bar, sidebar and content, dimmed with the panel
void shell_dividers(float top_bar_h, float tab_width);
} // namespace wivrn::ui