Overhaul settings UI

This commit is contained in:
ahsanswr
2026-07-26 17:24:36 +02:00
committed by Guillaume Meunier
parent 45710151aa
commit a5b1b6dbeb
12 changed files with 1548 additions and 1480 deletions
+4 -9
View File
@@ -1329,20 +1329,15 @@ void ScrollWhenDragging()
static int active_id;
static ImVec2 cumulated_delta;
// Suppress drag-scroll while a popup is open and for the frame one closes: in VR
// the ray snaps from the popup layer back to this window when the popup dismisses,
// which can synthesise a spurious click (see the focused-controller handling) that
// would otherwise start a scroll right after picking a combo value.
// Don't start a drag while a popup is open or just closed: in VR the ray snaps back
// and can synthesise a click that would scroll right after picking a combo value.
static bool popup_was_open;
const bool popup_open = ImGui::IsPopupOpen(nullptr, ImGuiPopupFlags_AnyPopupId | ImGuiPopupFlags_AnyPopupLevel);
bool HoveredIdAllowOverlap_backup = std::exchange(g.HoveredIdAllowOverlap, true);
bool ActiveIdAllowOverlap_backup = std::exchange(g.ActiveIdAllowOverlap, true);
// Begin a drag-scroll only when the press lands on this window or one of its
// child panels and no widget grabbed it. IsWindowHovered(ChildWindows) lets the
// drag work over card child windows (ItemHoverable would fail there), and the
// g.ActiveId guard keeps manipulating a control (slider, toggle, combo...) from
// scrolling the page. Controls must claim the active id on press for this to hold.
// Start only over this window or a child panel with no active widget. ChildWindows
// lets it work over cards; the ActiveId guard keeps controls from scrolling the page.
const bool over = ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows);
if (active_id == 0 and g.ActiveId == 0 and not popup_open and not popup_was_open and over and ImGui::IsMouseClicked(mouse_button, ImGuiInputFlags_None, /*id*/ ImGuiKeyOwner_Any))
{
+10 -2
View File
@@ -63,9 +63,13 @@ struct theme
float font_scale; // global text size multiplier
float background_alpha; // opacity of the main panel background, 0..1
// Convert to packed color, applying the current global alpha so widgets drawn
// through the draw list dim with ImGui::BeginDisabled() (and any pushed alpha).
ImU32 col(const ImVec4 & c) const
{
return ImGui::ColorConvertFloat4ToU32(c);
ImVec4 v = c;
v.w *= ImGui::GetStyle().Alpha;
return ImGui::ColorConvertFloat4ToU32(v);
}
// Push colors and metrics into the global ImGui style
@@ -100,11 +104,15 @@ constexpr float combo_chevron = 12; // chevron size in the closed box
constexpr float combo_modal_min_width = 460; // popup never narrower than this
constexpr ImVec2 combo_padding = {16, 0}; // horizontal inner padding of box and rows
constexpr float list_row_box = 52; // leading icon/thumbnail size in a list row
constexpr float list_row_pad = 16; // inner padding of a list row, both axes
constexpr ImVec2 chip_padding = {12, 6}; // inner padding of a chip/badge
constexpr float input_padding_x = 14; // horizontal text padding inside inputs
constexpr float font_modal_title = 1.2; // modal heading, x base font size
constexpr float label_line_gap = 5; // title to description
constexpr float label_line_gap = 5; // title to description
constexpr float label_bottom_pad = 6; // breathing room below the description
constexpr float nav_section_gap = 18; // space above a sidebar section header
} // namespace metrics
// Theme in effect, call current().apply() after mutating
+235 -55
View File
@@ -59,36 +59,32 @@ static void hover_haptic()
hover_haptic_hook();
}
// Show a tooltip for the last submitted item; caller ensures it is hovered.
static void show_tooltip(const char * text)
// Show a tooltip for the last item; caller ensures it is hovered
static void show_tooltip(const std::string & text)
{
if (text and text[0] and tooltip_hook)
tooltip_hook(text);
if (not text.empty() and tooltip_hook)
tooltip_hook(text.c_str());
}
void page_header(const char * title, const char * subtitle)
void page_header(const std::string & title, const std::string & subtitle)
{
const theme & t = current();
ImGui::PushFont(nullptr, ImGui::GetStyle().FontSizeBase * metrics::font_title);
ImGui::TextUnformatted(title);
ImGui::TextUnformatted(title.c_str());
ImGui::PopFont();
if (subtitle and subtitle[0])
if (not subtitle.empty())
{
ImGui::PushStyleColor(ImGuiCol_Text, t.text_muted);
ImGui::TextUnformatted(subtitle);
ImGui::TextUnformatted(subtitle.c_str());
ImGui::PopStyleColor();
}
ImGui::Dummy({0, ImGui::GetStyle().ItemSpacing.y});
}
// A card is purely a visual rounded panel, NOT a child window: its content is
// laid out directly in the parent window so the page stays a single scroll
// surface (drag-to-scroll and clicks behave uniformly everywhere). The panel is
// drawn behind the upcoming content using the height measured last frame, then
// the full extent is reserved so layout and scrolling account for it.
// Always pair begin_card with end_card.
// A card is a visual rounded panel, not a child window, so the page stays one scroll
// surface. Drawn behind the content using last frame's height; pair with end_card.
namespace
{
struct card_frame
@@ -161,7 +157,7 @@ void row_separator()
ImGui::PopStyleColor();
}
void setting_label(const char * title, const char * description, float control_width)
float setting_label(const std::string & title, const std::string & description, float control_width)
{
const theme & t = current();
const ImGuiStyle & style = ImGui::GetStyle();
@@ -174,31 +170,31 @@ void setting_label(const char * title, const char * description, float control_w
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {style.ItemSpacing.x, metrics::label_line_gap});
ImGui::PushTextWrapPos(start.x + text_w);
ImGui::TextUnformatted(title);
if (description and description[0])
ImGui::TextUnformatted(title.c_str());
if (not description.empty())
{
ImGui::PushFont(nullptr, ImGui::GetStyle().FontSizeBase * metrics::font_description);
ImGui::PushFont(nullptr, style.FontSizeBase * metrics::font_description);
ImGui::PushStyleColor(ImGuiCol_Text, t.text_muted);
ImGui::TextUnformatted(description);
ImGui::TextUnformatted(description.c_str());
ImGui::PopStyleColor();
ImGui::PopFont();
}
ImGui::PopTextWrapPos();
ImGui::PopStyleVar();
// right-aligned control slot, vertically centered against the label block.
// Controls are control_height x frame height, so centre against that.
// right-aligned control slot, vertically centered against the label block
const float label_h = ImGui::GetCursorPos().y - start.y;
const float ctrl_h = ImGui::GetFrameHeight() * metrics::control_height;
const float ctrl_y = start.y + ImMax(0.f, (label_h - ctrl_h) * 0.5f);
ImGui::SetCursorPos({start.x + avail - control_width, ctrl_y});
return start.y + label_h;
}
bool button(const char * label, button_style s, const ImVec2 & size)
bool button(const std::string & label, button_style s, const ImVec2 & size)
{
const theme & t = current();
// default to the shared control height; width still auto-fits the label
// control height by default, width auto-fits the label
const float h = size.y > 0 ? size.y : ImGui::GetFrameHeight() * metrics::control_height;
switch (s)
@@ -231,14 +227,14 @@ bool button(const char * label, button_style s, const ImVec2 & size)
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, t.rounding);
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, metrics::button_padding);
const bool pressed = ImGui::Button(label, {size.x, h});
const bool pressed = ImGui::Button(label.c_str(), {size.x, h});
ImGui::PopStyleVar(2);
ImGui::PopStyleColor(4);
hover_haptic();
return pressed;
}
bool icon_button(const char * icon, const ImVec2 & size_arg, bool active, const char * tooltip)
bool icon_button(const char * icon, const ImVec2 & size_arg, bool active, const std::string & tooltip)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
if (window->SkipItems)
@@ -274,9 +270,7 @@ bool icon_button(const char * icon, const ImVec2 & size_arg, bool active, const
ImDrawList * draw = window->DrawList;
draw->AddRectFilled(bb.Min, bb.Max, t.col(bg), t.rounding);
// glyph drawn manually so it scales with the button. Centre on the glyph's real
// ink box (X0..X1, Y0..Y1) rather than CalcTextSize, which returns the advance
// width and line height and leaves the glyph up-and-left of true centre.
// centre the glyph on its ink box, not CalcTextSize (which includes bearing/leading)
const float glyph_size = size.y * metrics::icon_button_glyph;
ImGui::PushFont(nullptr, glyph_size);
const ImU32 col = t.col(active ? t.on_accent : t.text);
@@ -298,15 +292,13 @@ bool icon_button(const char * icon, const ImVec2 & size_arg, bool active, const
}
// Horizontal space a trailing reset slot consumes, gap included
static float reset_slot_width()
float reset_slot_width()
{
return ImGui::GetFrameHeight() * metrics::control_height + ImGui::GetStyle().ItemSpacing.x;
}
// Trailing reset affordance placed on the same row as a value control, right after
// it (the control should have shrunk its width by reset_slot_width() to make room).
// Draws a reset icon button when show is true, or an empty slot when false so the
// right edge stays put. Returns true when the user taps reset.
// Trailing reset button on a value control's row; empty slot when show is false so the
// right edge stays put. Returns true when tapped.
static bool reset_slot(const char * id, bool show)
{
const float side = ImGui::GetFrameHeight() * metrics::control_height;
@@ -567,7 +559,7 @@ void set_popup_center(const ImVec2 & center)
popup_center = center;
}
bool combo(const char * id, const char * title, const std::vector<combo_item> & items, int * selected, float width, const int * default_value)
bool combo(const char * id, const std::string & title, const std::vector<combo_item> & items, int * selected, float width, const int * default_value)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
if (window->SkipItems or items.empty())
@@ -636,10 +628,10 @@ bool combo(const char * id, const char * title, const std::vector<combo_item> &
bool changed = false;
if (ImGui::BeginPopupModal(popup_id, nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize))
{
if (title and title[0])
if (not title.empty())
{
ImGui::PushStyleColor(ImGuiCol_Text, t.text_muted);
ImGui::TextUnformatted(title);
ImGui::TextUnformatted(title.c_str());
ImGui::PopStyleColor();
ImGui::Dummy({0, style.ItemSpacing.y});
}
@@ -654,10 +646,7 @@ bool combo(const char * id, const char * title, const std::vector<combo_item> &
}
}
// tapping outside the list dismisses without changing the selection.
// AllowWhenBlockedByActiveItem is required: pressing a row sets it as the active
// item, which would otherwise make IsWindowHovered return false on the press
// frame and close the modal before the row's release registers the selection.
// click outside dismisses; AllowWhenBlockedByActiveItem so pressing a row still counts as inside
if (ImGui::IsMouseClicked(ImGuiMouseButton_Left) and not ImGui::IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem))
ImGui::CloseCurrentPopup();
@@ -676,7 +665,7 @@ bool combo(const char * id, const char * title, const std::vector<combo_item> &
return changed;
}
void chip(const char * label, chip_style style, bool dot)
void chip(const std::string & label, chip_style style, bool dot, float height)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
if (window->SkipItems)
@@ -712,11 +701,11 @@ void chip(const char * label, chip_style style, bool dot)
break;
}
const ImVec2 ts = ImGui::CalcTextSize(label);
const ImVec2 ts = ImGui::CalcTextSize(label.c_str());
const ImVec2 pad = metrics::chip_padding;
const float dot_r = ts.y * 0.26f;
const float dot_gap = dot ? dot_r * 2 + pad.x * 0.55f : 0;
const ImVec2 size = {ts.x + pad.x * 2 + dot_gap, ts.y + pad.y * 2};
const ImVec2 size = {ts.x + pad.x * 2 + dot_gap, height > 0 ? height : ts.y + pad.y * 2};
const ImVec2 pos = window->DC.CursorPos;
const ImRect bb(pos, pos + size);
@@ -733,24 +722,26 @@ void chip(const char * label, chip_style style, bool dot)
draw->AddCircleFilled({tx + dot_r, bb.Min.y + size.y * 0.5f}, dot_r, t.col(fg));
tx += dot_r * 2 + pad.x * 0.55f;
}
draw->AddText({tx, bb.Min.y + pad.y}, t.col(fg), label);
draw->AddText({tx, bb.Min.y + (size.y - ts.y) * 0.5f}, t.col(fg), label.c_str());
}
void nav_section(const char * label)
void nav_section(const std::string & label)
{
const theme & t = current();
const ImGuiStyle & style = ImGui::GetStyle();
ImGui::Dummy({0, style.ItemSpacing.y * 0.5f});
ImGui::Dummy({0, metrics::nav_section_gap}); // gap above the section
ImGui::Indent(metrics::combo_padding.x); // align with the nav_item icons
ImGui::PushFont(nullptr, style.FontSizeBase * metrics::font_description);
ImGui::PushStyleColor(ImGuiCol_Text, t.text_muted);
ImGui::TextUnformatted(label);
ImGui::TextUnformatted(label.c_str());
ImGui::PopStyleColor();
ImGui::PopFont();
ImGui::Dummy({0, 2});
ImGui::Unindent(metrics::combo_padding.x);
ImGui::Dummy({0, 4});
}
bool nav_item(const char * icon, const char * label, bool selected)
bool nav_item(const char * icon, const std::string & label, bool selected)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
if (window->SkipItems)
@@ -764,7 +755,7 @@ bool nav_item(const char * icon, const char * label, bool selected)
const ImVec2 pos = window->DC.CursorPos;
const ImRect bb(pos, pos + ImVec2(w, h));
ImGui::ItemSize(bb, style.FramePadding.y);
const ImGuiID gid = window->GetID(label);
const ImGuiID gid = window->GetID(label.c_str());
if (not ImGui::ItemAdd(bb, gid))
return false;
@@ -783,8 +774,8 @@ bool nav_item(const char * icon, const char * label, bool selected)
const ImVec2 its = ImGui::CalcTextSize(icon);
draw->AddText({bb.Min.x + pad + (icon_w - its.x) * 0.5f, bb.Min.y + (h - its.y) * 0.5f}, t.col(selected ? t.accent : t.text_muted), icon);
const ImVec2 ls = ImGui::CalcTextSize(label);
draw->AddText({bb.Min.x + pad + icon_w + pad * 0.5f, bb.Min.y + (h - ls.y) * 0.5f}, t.col(t.text), label);
const ImVec2 ls = ImGui::CalcTextSize(label.c_str());
draw->AddText({bb.Min.x + pad + icon_w + pad * 0.5f, bb.Min.y + (h - ls.y) * 0.5f}, t.col(t.text), label.c_str());
return pressed;
}
@@ -867,7 +858,7 @@ bool input_int(const char * id, int * v, int step, int v_min, int v_max, float w
return changed;
}
bool begin_modal(const char * id, const char * title, float width)
bool begin_modal(const char * id, const std::string & title, float width)
{
const theme & t = current();
const ImGuiStyle & style = ImGui::GetStyle();
@@ -890,10 +881,10 @@ bool begin_modal(const char * id, const char * title, float width)
const bool open = ImGui::BeginPopupModal(id, nullptr, flags);
if (open)
{
if (title and title[0])
if (not title.empty())
{
ImGui::PushFont(nullptr, style.FontSizeBase * metrics::font_modal_title);
ImGui::TextUnformatted(title);
ImGui::TextUnformatted(title.c_str());
ImGui::PopFont();
ImGui::Dummy({0, style.ItemSpacing.y});
}
@@ -913,4 +904,193 @@ void end_modal()
ImGui::PopStyleColor(3);
}
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)
{
int result = 0;
if (begin_modal(id, title, 520))
{
const theme & t = current();
ImGui::PushStyleColor(ImGuiCol_Text, t.text_muted);
ImGui::TextWrapped("%s", message.c_str());
ImGui::PopStyleColor();
ImGui::Dummy({0, 12});
const float gap = ImGui::GetStyle().ItemSpacing.x;
const float cancel_w = ImGui::CalcTextSize(cancel_label.c_str()).x + metrics::button_padding.x * 2;
const float confirm_w = ImGui::CalcTextSize(confirm_label.c_str()).x + metrics::button_padding.x * 2;
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x - cancel_w - confirm_w - gap);
if (button(cancel_label, button_style::secondary, {cancel_w, 0}))
{
result = -1;
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (button(confirm_label, danger ? button_style::danger : button_style::primary, {confirm_w, 0}))
{
result = 1;
ImGui::CloseCurrentPopup();
}
end_modal();
}
return result;
}
namespace
{
std::vector<ImVec2> list_row_after; // cursor to restore at end_list_row
}
list_row_result begin_list_row(const char * id, const char * icon, ImTextureID image, const std::string & title, const std::string & subtitle, bool selected, float trailing_width, float height)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
const theme & t = current();
const ImGuiStyle & style = ImGui::GetStyle();
// measure title + (possibly multi-line) subtitle so the row can auto-size
const float pad = metrics::list_row_pad;
const float box = metrics::list_row_box;
const ImVec2 ts = ImGui::CalcTextSize(title.c_str());
ImVec2 sub_sz{0, 0};
if (not subtitle.empty())
{
ImGui::PushFont(nullptr, style.FontSizeBase * metrics::font_description);
sub_sz = ImGui::CalcTextSize(subtitle.c_str());
ImGui::PopFont();
}
const float text_h = ts.y + (subtitle.empty() ? 0 : metrics::label_line_gap + sub_sz.y);
const float row_h = height > 0 ? height : ImMax(box, text_h) + 2 * pad;
const ImVec2 p0 = window->DC.CursorPos;
const float avail = ImGui::GetContentRegionAvail().x;
// the click area excludes the trailing controls so they receive their own clicks
ImGui::SetNextItemAllowOverlap();
const bool clicked = ImGui::InvisibleButton(id, {ImMax(avail - trailing_width, 1.f), row_h});
const bool hovered = ImGui::IsItemHovered();
hover_haptic();
list_row_after.push_back(window->DC.CursorPos);
ImDrawList * draw = window->DrawList;
const ImRect bb(p0, p0 + ImVec2(avail, row_h));
if (selected)
draw->AddRectFilled(bb.Min, bb.Max, t.col({t.accent.x, t.accent.y, t.accent.z, 0.16f}), t.card_rounding);
else if (hovered)
draw->AddRectFilled(bb.Min, bb.Max, t.col(t.control), t.card_rounding);
// leading thumbnail or icon box
const ImVec2 box_min = {p0.x + pad, p0.y + (row_h - box) * 0.5f};
const ImVec2 box_max = box_min + ImVec2(box, box);
if (image)
draw->AddImageRounded(image, box_min, box_max, {0, 0}, {1, 1}, IM_COL32_WHITE, t.rounding);
else
{
draw->AddRectFilled(box_min, box_max, t.col(selected ? t.accent : t.control), t.rounding);
if (icon and icon[0])
{
const ImVec2 is = ImGui::CalcTextSize(icon);
draw->AddText(box_min + (ImVec2(box, box) - is) * 0.5f, t.col(selected ? t.on_accent : t.text_muted), icon);
}
}
// title + subtitle
const float tx = box_max.x + pad;
const float total = subtitle.empty() ? ts.y : ts.y + metrics::label_line_gap + sub_sz.y;
const float ty = p0.y + (row_h - total) * 0.5f;
draw->AddText({tx, ty}, t.col(t.text), title.c_str());
if (not subtitle.empty())
{
ImGui::PushFont(nullptr, style.FontSizeBase * metrics::font_description);
draw->AddText({tx, ty + ts.y + metrics::label_line_gap}, t.col(t.text_muted), subtitle.c_str());
ImGui::PopFont();
}
// trailing controls right-align against the inner padding
return {bb.Min, {bb.Max.x - pad, bb.Max.y}, clicked};
}
void end_list_row()
{
ImGui::GetCurrentWindow()->DC.CursorPos = list_row_after.back();
list_row_after.pop_back();
}
int action_menu(const char * id, const char * icon, const std::vector<action_item> & items)
{
const theme & t = current();
if (icon_button(icon, {}, false))
ImGui::OpenPopup(id);
int chosen = -1;
const float pad = metrics::combo_padding.x;
const float icon_w = ImGui::GetFontSize() * 1.5f;
float content_w = 0;
for (const auto & it: items)
content_w = ImMax(content_w, icon_w + pad + ImGui::CalcTextSize(it.label).x + pad * 2 + ImGui::GetFontSize());
// centred modal on the popup layer, like combo()
ImGui::SetNextWindowPos(popup_center, ImGuiCond_Always, {0.5f, 0.5f});
ImGui::SetNextWindowSize({ImMax(content_w, metrics::combo_modal_min_width), 0});
ImGui::PushStyleColor(ImGuiCol_PopupBg, t.card);
ImGui::PushStyleColor(ImGuiCol_Border, t.border);
ImGui::PushStyleColor(ImGuiCol_Text, t.text);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, t.card_rounding);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, metrics::card_padding);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, t.border_size > 0 ? t.border_size : 1);
if (ImGui::BeginPopupModal(id, nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize))
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
const float row_h = ImGui::GetFrameHeight() * metrics::control_height;
for (int i = 0; i < int(items.size()); ++i)
{
const action_item & it = items[i];
ImGui::PushID(i);
const float row_w = ImGui::GetContentRegionAvail().x;
const ImVec2 pos = window->DC.CursorPos;
const ImRect bb(pos, pos + ImVec2(row_w, row_h));
ImGui::ItemSize(bb, 0);
const ImGuiID gid = window->GetID("##row");
if (ImGui::ItemAdd(bb, gid))
{
bool hovered, held;
if (ImGui::ButtonBehavior(bb, gid, &hovered, &held))
{
chosen = i;
ImGui::CloseCurrentPopup();
}
hover_haptic();
ImDrawList * draw = window->DrawList;
if (hovered)
draw->AddRectFilled(bb.Min, bb.Max, t.col(t.control_hovered), t.rounding);
const ImU32 fg = t.col(it.danger ? t.danger : t.text);
const ImVec2 its = ImGui::CalcTextSize(it.icon);
draw->AddText({bb.Min.x + pad + (icon_w - its.x) * 0.5f, bb.Min.y + (row_h - its.y) * 0.5f}, fg, it.icon);
const ImVec2 ls = ImGui::CalcTextSize(it.label);
draw->AddText({bb.Min.x + pad + icon_w, bb.Min.y + (row_h - ls.y) * 0.5f}, fg, it.label);
if (it.checked)
{
const ImVec2 cs = ImGui::CalcTextSize(ICON_FA_CHECK);
draw->AddText({bb.Max.x - pad - cs.x, bb.Min.y + (row_h - cs.y) * 0.5f}, t.col(t.accent), ICON_FA_CHECK);
}
}
ImGui::PopID();
}
// click outside dismisses; AllowWhenBlockedByActiveItem so pressing a row still counts as inside
if (ImGui::IsMouseClicked(ImGuiMouseButton_Left) and not ImGui::IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem))
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
}
ImGui::PopStyleVar(3);
ImGui::PopStyleColor(3);
return chosen;
}
} // namespace wivrn::ui
+60 -41
View File
@@ -24,8 +24,8 @@
#include <string>
#include <vector>
// Themed, reusable UI widgets matching the WiVRn dashboard design.
// All widgets read colors and metrics from wivrn::ui::current().
// 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 *.
namespace wivrn::ui
{
@@ -38,62 +38,54 @@ enum class button_style
};
// Page title with an optional muted subtitle
void page_header(const char * title, const char * subtitle = nullptr);
void page_header(const std::string & title, const std::string & subtitle = {});
// Rounded panel, pair with end_card. Returns false when fully clipped
// Rounded panel, pair with end_card
bool begin_card(const char * id, const ImVec2 & size = {0, 0});
void end_card();
// Full-width divider between rows of a card
void row_separator();
// Bold title + wrapped muted description in a left column,
// leaving control_width on the right for a right-aligned control
void setting_label(const char * title, const char * description, float control_width);
// 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.
float setting_label(const std::string & title, const std::string & description, float control_width);
bool button(const char * label, button_style style = button_style::primary, const ImVec2 & size = {0, 0});
// tooltip, if given, is shown while the button is hovered
bool icon_button(const char * icon, const ImVec2 & size = {0, 0}, bool active = false, const char * tooltip = nullptr);
bool button(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 = {});
bool toggle(const char * id, bool * v, const bool * default_value = nullptr);
// Value controls below take an optional default_value. When given and the current
// value differs from it, a reset icon button is drawn in a trailing slot; tapping it
// restores the default. When the value matches (or no default is given) the slot is
// left empty so the control's right edge stays put.
// Width a trailing reset slot consumes (gap + square), for laying out toggles
float reset_slot_width();
// Filled bar slider with the value centered on the rail
// The value controls below draw a reset button when the value differs from default_value
bool slider_int(const char * id, int * v, int v_min, int v_max, const char * format, const ImVec2 & size = {0, 0}, const int * default_value = nullptr);
// Pill group, *selected is the index of the active option
bool segmented(const char * id, const std::vector<std::string> & options, int * selected, const ImVec2 & size = {0, 0}, const int * default_value = nullptr);
// One option of a combo: a label and an optional muted description
struct combo_item
{
const char * name;
const char * description = nullptr;
};
// Combo box whose options open in a modal list centred on the popup layer,
// instead of an inline dropdown. *selected is the chosen index, title labels
// the modal. Returns true when the selection changes.
bool combo(const char * id, const char * title, const std::vector<combo_item> & items, int * selected, float width = 0, const int * default_value = 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.
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 (in imgui display coords) where combo modals open. Set once per frame
// from the lobby's popup layer before any combo() is drawn.
// Centre (display coords) where combo and begin_modal popups open. Set once per frame.
void set_popup_center(const ImVec2 & center);
// Hook the widgets call after submitting an interactive item so it can fire a hover
// haptic (e.g. imgui_context::vibrate_on_hover). Install once from the lobby.
// Hook fired after an interactive item, for a hover haptic. Install once.
void set_hover_haptic(std::function<void()> hook);
// Hook a widget calls to show a tooltip for the last submitted item (e.g.
// imgui_context::tooltip). Only called while the item is hovered. Install once.
// Hook to show a tooltip for the last item. Install once.
void set_tooltip_hook(std::function<void(const char *)> hook);
// Small status badge. The label may include a leading icon glyph; pass dot=true for
// a leading status dot in the style colour. Non-interactive.
enum class chip_style
{
neutral, // control fill, normal text
@@ -103,26 +95,53 @@ enum class chip_style
warning,
danger,
};
void chip(const char * label, chip_style style = chip_style::neutral, bool dot = false);
// 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)
void chip(const std::string & label, chip_style style = chip_style::neutral, bool dot = false, float height = 0);
// Muted sidebar group header (e.g. "SETTINGS")
void nav_section(const char * label);
// Muted sidebar group header
void nav_section(const std::string & label);
// Sidebar entry: icon + label across the full width, highlighted when selected.
// Returns true when clicked.
bool nav_item(const char * icon, const char * label, bool selected);
// Sidebar entry: icon + label, highlighted when selected. Returns true when clicked
bool nav_item(const char * icon, const std::string & label, bool selected);
// Themed single-line text field, control height. hint is optional placeholder text.
// Returns true while the text is being edited.
// 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 - / + stepper buttons. Returns true when the value changes
bool input_int(const char * id, int * v, int step = 1, int v_min = 0, int v_max = 0, float width = 0);
// Centered modal dialog on the popup layer, themed like a card with a title. Open it
// with ImGui::OpenPopup(id); call ImGui::CloseCurrentPopup() to dismiss. Pair with
// end_modal only when this returns true, exactly like ImGui::BeginPopupModal.
bool begin_modal(const char * id, const char * title, 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.
bool begin_modal(const char * id, const std::string & title, float width = 0);
void end_modal();
struct action_item
{
const char * icon;
const char * label;
bool danger = false;
bool checked = false;
};
// Overflow button: an 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
{
ImVec2 min, max; // row rect (screen) for right-aligning trailing controls
bool clicked; // the row body was clicked
};
// 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().
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);
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.
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);
} // namespace wivrn::ui