Add new UI framework

This commit is contained in:
ahsanswr authored and Guillaume Meunier committed 2026-07-26 17:24:36 +02:00
1 parent d747f4dd04
commit 44879e45ab
8 files changed
+1697 -2

No files matched your search

+16 -1
View File
@@ -1329,9 +1329,22 @@ 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.
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);
if (active_id == 0 and ImGui::ItemHoverable(window->Rect(), 0, g.CurrentItemFlags) and ImGui::IsMouseClicked(mouse_button, ImGuiInputFlags_None, /*id*/ ImGuiKeyOwner_Any))
// 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.
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))
{
active_id = id;
@@ -1357,6 +1370,8 @@ void ScrollWhenDragging()
g.HoveredIdAllowOverlap = HoveredIdAllowOverlap_backup;
g.ActiveIdAllowOverlap = ActiveIdAllowOverlap_backup;
popup_was_open = popup_open;
}
void CenterTextH(const std::string & text)
+219
View File
@@ -0,0 +1,219 @@
/*
* WiVRn VR streaming
* Copyright (C) 2026 Guillaume Meunier <[email protected]>
* Copyright (C) 2026 Patrick Nicolas <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ui_theme.h"
namespace wivrn::ui
{
static ImVec4 rgb(int r, int g, int b, float a = 1)
{
return {r / 255.f, g / 255.f, b / 255.f, a};
}
static theme dark_default()
{
theme t;
t.name = "Dark";
t.accent = rgb(59, 130, 246);
t.accent_hovered = rgb(82, 146, 247);
t.accent_active = rgb(43, 110, 222);
t.on_accent = rgb(255, 255, 255);
t.background = rgb(10, 11, 13);
t.card = rgb(22, 24, 29);
t.card_hovered = rgb(28, 30, 36);
t.control = rgb(35, 38, 46);
t.control_hovered = rgb(44, 48, 57);
t.control_active = rgb(52, 57, 68);
t.text = rgb(244, 245, 248);
t.text_muted = rgb(150, 156, 167);
t.border = rgb(40, 43, 51);
t.danger = rgb(239, 68, 68);
t.danger_hovered = rgb(248, 96, 96);
t.success = rgb(52, 199, 123);
t.warning = rgb(249, 115, 6);
t.rounding = 8;
t.card_rounding = 14;
t.border_size = 1;
t.font_scale = 1.0; // user multiplier; 100% maps to metrics::font_base
t.background_alpha = 0.88f; // ~ the previous hardcoded 224/255
return t;
}
static theme midnight()
{
theme t = dark_default();
t.name = "Midnight";
t.background = rgb(6, 8, 16);
t.card = rgb(16, 20, 34);
t.card_hovered = rgb(22, 27, 44);
t.control = rgb(28, 34, 54);
t.control_hovered = rgb(36, 43, 66);
t.control_active = rgb(44, 52, 78);
t.border = rgb(30, 37, 58);
return t;
}
static theme slate_light()
{
theme t = dark_default();
t.name = "Slate";
t.background = rgb(28, 30, 36);
t.card = rgb(38, 41, 49);
t.card_hovered = rgb(46, 50, 60);
t.control = rgb(54, 59, 70);
t.control_hovered = rgb(64, 70, 83);
t.control_active = rgb(74, 81, 96);
t.text = rgb(236, 238, 242);
t.text_muted = rgb(168, 174, 186);
t.border = rgb(58, 63, 75);
return t;
}
// True black, ideal for OLED panels
static theme oled()
{
theme t = dark_default();
t.name = "OLED";
t.background = rgb(0, 0, 0);
t.card = rgb(6, 6, 8);
t.card_hovered = rgb(11, 12, 14);
t.control = rgb(22, 24, 28);
t.control_hovered = rgb(30, 32, 38);
t.control_active = rgb(40, 43, 50);
t.border = rgb(26, 28, 33);
return t;
}
// Neutral dark gray
static theme graphite()
{
theme t = dark_default();
t.name = "Graphite";
t.background = rgb(20, 21, 23);
t.card = rgb(31, 33, 36);
t.card_hovered = rgb(39, 41, 45);
t.control = rgb(48, 51, 56);
t.control_hovered = rgb(58, 62, 68);
t.control_active = rgb(68, 72, 80);
t.text = rgb(238, 240, 243);
t.text_muted = rgb(160, 164, 172);
t.border = rgb(50, 53, 59);
return t;
}
// Light surfaces, dark text
static theme light()
{
theme t = dark_default();
t.name = "Light";
t.background = rgb(236, 238, 242);
t.card = rgb(255, 255, 255);
t.card_hovered = rgb(246, 247, 250);
t.control = rgb(212, 216, 224);
t.control_hovered = rgb(199, 204, 213);
t.control_active = rgb(184, 190, 201);
t.text = rgb(22, 24, 30);
t.text_muted = rgb(108, 114, 126);
t.border = rgb(205, 209, 217);
return t;
}
std::vector<theme> presets()
{
return {oled(), dark_default(), midnight(), graphite(), slate_light(), light()};
}
std::vector<accent_swatch> accent_swatches()
{
return {
{"Blue", rgb(59, 130, 246), rgb(82, 146, 247), rgb(43, 110, 222)},
{"Violet", rgb(139, 92, 246), rgb(157, 116, 247), rgb(120, 72, 226)},
{"Emerald", rgb(16, 185, 129), rgb(34, 200, 145), rgb(8, 160, 110)},
{"Amber", rgb(245, 158, 11), rgb(248, 173, 40), rgb(220, 138, 6)},
{"Rose", rgb(244, 63, 94), rgb(247, 90, 117), rgb(222, 48, 78)},
{"Cyan", rgb(6, 182, 212), rgb(34, 197, 224), rgb(6, 158, 184)},
};
}
void set_accent(const accent_swatch & swatch)
{
theme & t = current();
t.accent = swatch.base;
t.accent_hovered = swatch.hovered;
t.accent_active = swatch.active;
}
theme & current()
{
static theme t = dark_default();
return t;
}
void set_theme(const theme & t)
{
current() = t;
}
void theme::apply() const
{
ImGuiStyle & style = ImGui::GetStyle();
style.FontScaleMain = font_scale;
style.WindowRounding = card_rounding;
style.ChildRounding = card_rounding;
style.PopupRounding = card_rounding;
style.FrameRounding = rounding;
style.GrabRounding = rounding;
style.TabRounding = rounding;
style.FrameBorderSize = border_size;
style.WindowBorderSize = border_size;
ImVec4 * c = style.Colors;
c[ImGuiCol_Text] = text;
c[ImGuiCol_TextDisabled] = text_muted;
c[ImGuiCol_WindowBg] = background;
c[ImGuiCol_ChildBg] = card;
c[ImGuiCol_PopupBg] = card;
c[ImGuiCol_Border] = border;
c[ImGuiCol_FrameBg] = control;
c[ImGuiCol_FrameBgHovered] = control_hovered;
c[ImGuiCol_FrameBgActive] = control_active;
c[ImGuiCol_Button] = control;
c[ImGuiCol_ButtonHovered] = control_hovered;
c[ImGuiCol_ButtonActive] = accent;
c[ImGuiCol_Header] = control_hovered;
c[ImGuiCol_HeaderHovered] = control_hovered;
c[ImGuiCol_HeaderActive] = accent;
c[ImGuiCol_SliderGrab] = accent;
c[ImGuiCol_SliderGrabActive] = accent_active;
c[ImGuiCol_CheckMark] = accent;
c[ImGuiCol_ScrollbarBg] = {0, 0, 0, 0};
c[ImGuiCol_ScrollbarGrab] = control;
c[ImGuiCol_ScrollbarGrabHovered] = control_hovered;
c[ImGuiCol_ScrollbarGrabActive] = control_active;
c[ImGuiCol_Separator] = border;
}
} // namespace wivrn::ui
+126
View File
@@ -0,0 +1,126 @@
/*
* WiVRn VR streaming
* Copyright (C) 2026 Guillaume Meunier <[email protected]>
* Copyright (C) 2026 Patrick Nicolas <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <imgui.h>
#include <string>
#include <vector>
namespace wivrn::ui
{
struct theme
{
std::string name;
// Accent
ImVec4 accent;
ImVec4 accent_hovered;
ImVec4 accent_active;
ImVec4 on_accent; // text/icon on an accent fill
// Surfaces, back to front
ImVec4 background;
ImVec4 card;
ImVec4 card_hovered;
ImVec4 control; // slider rail, secondary button, input
ImVec4 control_hovered;
ImVec4 control_active;
// Content
ImVec4 text;
ImVec4 text_muted;
ImVec4 border;
// Semantic
ImVec4 danger;
ImVec4 danger_hovered;
ImVec4 success;
ImVec4 warning;
// Shape, in pixels
float rounding; // buttons, inputs, sliders
float card_rounding; // cards
float border_size;
float font_scale; // global text size multiplier
float background_alpha; // opacity of the main panel background, 0..1
ImU32 col(const ImVec4 & c) const
{
return ImGui::ColorConvertFloat4ToU32(c);
}
// Push colors and metrics into the global ImGui style
void apply() const;
};
// Fixed design tokens shared by the widgets
namespace metrics
{
constexpr float font_base = 0.765; // design default text scale, i.e. what the user-facing 100% maps to
constexpr float font_title = 1.7; // page header, x base font size
constexpr float font_description = 0.82; // muted secondary text, x base font size
constexpr ImVec2 button_padding = {26, 14};
constexpr ImVec2 card_padding = {28, 22};
// Shared height of every full-width control (slider, segmented, combo, input)
// so they line up in a setting row, in multiples of ImGui::GetFrameHeight()
constexpr float control_height = 1.65;
constexpr float toggle_aspect = 1.9; // track width / height
constexpr float toggle_knob_inset = 3; // gap between track edge and knob
constexpr float slider_grab_width = 14;
constexpr float segmented_inset = 3; // gap around the active segment
constexpr float icon_button_glyph = 0.5; // glyph height as a fraction of the button side
constexpr float combo_row_height = 2.2; // x frame height, big touch rows in the modal
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 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
} // namespace metrics
// Theme in effect, call current().apply() after mutating
theme & current();
void set_theme(const theme & t);
std::vector<theme> presets();
struct accent_swatch
{
const char * name;
ImVec4 base;
ImVec4 hovered;
ImVec4 active;
};
std::vector<accent_swatch> accent_swatches();
void set_accent(const accent_swatch & swatch);
} // namespace wivrn::ui
+863
View File
@@ -0,0 +1,863 @@
/*
* WiVRn VR streaming
* Copyright (C) 2026 Guillaume Meunier <[email protected]>
* Copyright (C) 2026 Patrick Nicolas <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define IMGUI_DEFINE_MATH_OPERATORS
#include "ui_widgets.h"
#include "imgui.h"
#include "imgui_internal.h"
#include "ui_theme.h"
#include "IconsFontAwesome6.h"
#include <cstdio>
#include <functional>
#include <unordered_map>
#include <vector>
namespace wivrn::ui
{
namespace
{
std::function<void()> hover_haptic_hook;
std::function<void(const char *)> tooltip_hook;
} // namespace
void set_hover_haptic(std::function<void()> hook)
{
hover_haptic_hook = std::move(hook);
}
void set_tooltip_hook(std::function<void(const char *)> hook)
{
tooltip_hook = std::move(hook);
}
// Fire the hover haptic for the last submitted item, if a hook is installed.
// The hook itself checks whether that item is actually hovered.
static void hover_haptic()
{
if (hover_haptic_hook)
hover_haptic_hook();
}
// Show a tooltip for the last submitted item; caller ensures it is hovered.
static void show_tooltip(const char * text)
{
if (text and text[0] and tooltip_hook)
tooltip_hook(text);
}
void page_header(const char * title, const char * subtitle)
{
const theme & t = current();
ImGui::PushFont(nullptr, ImGui::GetStyle().FontSizeBase * metrics::font_title);
ImGui::TextUnformatted(title);
ImGui::PopFont();
if (subtitle and subtitle[0])
{
ImGui::PushStyleColor(ImGuiCol_Text, t.text_muted);
ImGui::TextUnformatted(subtitle);
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.
namespace
{
struct card_frame
{
ImGuiID id;
ImVec2 origin;
float width;
float content_max_x_backup;
float work_max_x_backup;
};
std::vector<card_frame> card_stack;
std::unordered_map<ImGuiID, float> card_heights;
} // namespace
bool begin_card(const char * id, const ImVec2 &)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
const theme & t = current();
const ImGuiID gid = window->GetID(id);
card_stack.push_back({gid,
window->DC.CursorPos,
ImGui::GetContentRegionAvail().x,
window->ContentRegionRect.Max.x,
window->WorkRect.Max.x});
const card_frame & cf = card_stack.back();
if (const float h = card_heights[gid]; h > 0)
{
const ImVec2 bb_max = {cf.origin.x + cf.width, cf.origin.y + h};
window->DrawList->AddRectFilled(cf.origin, bb_max, t.col(t.card), t.card_rounding);
if (t.border_size > 0)
window->DrawList->AddRect(cf.origin, bb_max, t.col(t.border), t.card_rounding, 0, t.border_size);
}
ImGui::PushID(id);
window->ContentRegionRect.Max.x -= metrics::card_padding.x;
window->WorkRect.Max.x -= metrics::card_padding.x;
ImGui::Indent(metrics::card_padding.x);
ImGui::SetCursorScreenPos({ImGui::GetCursorScreenPos().x, cf.origin.y + metrics::card_padding.y});
ImGui::BeginGroup();
return true;
}
void end_card()
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
const card_frame cf = card_stack.back();
card_stack.pop_back();
ImGui::EndGroup();
ImGui::Unindent(metrics::card_padding.x);
window->ContentRegionRect.Max.x = cf.content_max_x_backup;
window->WorkRect.Max.x = cf.work_max_x_backup;
ImGui::PopID();
const float height = ImGui::GetItemRectMax().y + metrics::card_padding.y - cf.origin.y;
card_heights[cf.id] = height;
// reserve the full card extent so layout and scrolling account for it
ImGui::SetCursorScreenPos(cf.origin);
ImGui::Dummy({cf.width, height});
}
void row_separator()
{
const theme & t = current();
ImGui::PushStyleColor(ImGuiCol_Separator, t.border);
ImGui::Separator();
ImGui::PopStyleColor();
}
void setting_label(const char * title, const char * description, float control_width)
{
const theme & t = current();
const ImGuiStyle & style = ImGui::GetStyle();
const ImVec2 start = ImGui::GetCursorPos();
const float avail = ImGui::GetContentRegionAvail().x;
float text_w = avail - control_width - style.ItemSpacing.x;
if (text_w < 1)
text_w = avail;
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::PushFont(nullptr, ImGui::GetStyle().FontSizeBase * metrics::font_description);
ImGui::PushStyleColor(ImGuiCol_Text, t.text_muted);
ImGui::TextUnformatted(description);
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.
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});
}
bool button(const char * label, button_style s, const ImVec2 & size)
{
const theme & t = current();
// default to the shared control height; width still auto-fits the label
const float h = size.y > 0 ? size.y : ImGui::GetFrameHeight() * metrics::control_height;
switch (s)
{
case button_style::primary:
ImGui::PushStyleColor(ImGuiCol_Button, t.accent);
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, t.accent_hovered);
ImGui::PushStyleColor(ImGuiCol_ButtonActive, t.accent_active);
ImGui::PushStyleColor(ImGuiCol_Text, t.on_accent);
break;
case button_style::secondary:
ImGui::PushStyleColor(ImGuiCol_Button, t.control);
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, t.control_hovered);
ImGui::PushStyleColor(ImGuiCol_ButtonActive, t.control_active);
ImGui::PushStyleColor(ImGuiCol_Text, t.text);
break;
case button_style::danger:
ImGui::PushStyleColor(ImGuiCol_Button, t.danger);
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, t.danger_hovered);
ImGui::PushStyleColor(ImGuiCol_ButtonActive, t.danger);
ImGui::PushStyleColor(ImGuiCol_Text, t.on_accent);
break;
case button_style::ghost:
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{0, 0, 0, 0});
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, t.control);
ImGui::PushStyleColor(ImGuiCol_ButtonActive, t.control_active);
ImGui::PushStyleColor(ImGuiCol_Text, t.text);
break;
}
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, t.rounding);
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, metrics::button_padding);
const bool pressed = ImGui::Button(label, {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)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
if (window->SkipItems)
return false;
ImGuiContext & g = *GImGui;
const ImGuiStyle & style = g.Style;
const theme & t = current();
// square touch target, same height as the sliders and other controls
const float side = ImGui::GetFrameHeight() * metrics::control_height;
const ImVec2 size = size_arg.x > 0 ? size_arg : ImVec2{side, side};
const ImVec2 pos = window->DC.CursorPos;
const ImRect bb(pos, pos + size);
ImGui::ItemSize(bb, style.FramePadding.y);
const ImGuiID gid = window->GetID(icon);
if (not ImGui::ItemAdd(bb, gid))
return false;
bool hovered, held;
const bool pressed = ImGui::ButtonBehavior(bb, gid, &hovered, &held);
hover_haptic();
if (hovered)
show_tooltip(tooltip);
ImVec4 bg = active ? t.accent : t.control;
if (held)
bg = active ? t.accent_active : t.control_active;
else if (hovered)
bg = active ? t.accent_hovered : t.control_hovered;
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.
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);
const ImVec2 center = bb.GetCenter();
unsigned int codepoint = 0;
ImTextCharFromUtf8(&codepoint, icon, nullptr);
const ImFontGlyph * glyph = ImGui::GetFontBaked()->FindGlyph((ImWchar)codepoint);
if (glyph)
draw->AddText({center.x - (glyph->X0 + glyph->X1) * 0.5f, center.y - (glyph->Y0 + glyph->Y1) * 0.5f}, col, icon);
else
{
const ImVec2 ts = ImGui::CalcTextSize(icon);
draw->AddText({center.x - ts.x * 0.5f, center.y - ts.y * 0.5f}, col, icon);
}
ImGui::PopFont();
return pressed;
}
// Horizontal space a trailing reset slot consumes, gap included
static 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.
static bool reset_slot(const char * id, bool show)
{
const float side = ImGui::GetFrameHeight() * metrics::control_height;
ImGui::SameLine(0, ImGui::GetStyle().ItemSpacing.x);
if (not show)
{
ImGui::Dummy({side, side});
return false;
}
ImGui::PushID(id);
const bool clicked = icon_button(ICON_FA_ARROW_ROTATE_LEFT, {side, side}, false, "Reset to default");
ImGui::PopID();
return clicked;
}
bool toggle(const char * id, bool * v, const bool * default_value)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
if (window->SkipItems)
return false;
const theme & t = current();
const ImGuiStyle & style = ImGui::GetStyle();
// match the height of the other controls (sliders, combo, ...)
const float height = ImGui::GetFrameHeight() * metrics::control_height;
const float width = height * metrics::toggle_aspect;
const float radius = height * 0.5f;
const ImVec2 pos = window->DC.CursorPos;
const ImRect bb(pos, pos + ImVec2(width, height));
ImGui::ItemSize(bb, style.FramePadding.y);
const ImGuiID gid = window->GetID(id);
if (not ImGui::ItemAdd(bb, gid))
return false;
bool hovered, held;
bool changed = ImGui::ButtonBehavior(bb, gid, &hovered, &held);
hover_haptic();
if (changed)
*v = not *v;
ImVec4 track = *v ? t.accent : t.control;
if (hovered)
track = *v ? t.accent_hovered : t.control_hovered;
ImDrawList * draw = window->DrawList;
draw->AddRectFilled(bb.Min, bb.Max, t.col(track), radius);
const float cx = *v ? (bb.Max.x - radius) : (bb.Min.x + radius);
draw->AddCircleFilled({cx, bb.Min.y + radius}, radius - metrics::toggle_knob_inset, t.col(t.on_accent));
if (default_value and reset_slot(id, *v != *default_value))
{
*v = *default_value;
changed = true;
}
return changed;
}
bool slider_int(const char * id, int * v, int v_min, int v_max, const char * format, const ImVec2 & size_arg, const int * default_value)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
if (window->SkipItems)
return false;
ImGuiContext & g = *GImGui;
const ImGuiStyle & style = g.Style;
const theme & t = current();
const ImGuiID gid = window->GetID(id);
ImVec2 size = ImGui::CalcItemSize(size_arg, ImGui::CalcItemWidth(), ImGui::GetFrameHeight() * metrics::control_height);
if (default_value)
size.x -= reset_slot_width();
const ImVec2 pos = window->DC.CursorPos;
const ImRect bb(pos, pos + size);
ImGui::ItemSize(bb, style.FramePadding.y);
if (not ImGui::ItemAdd(bb, gid, &bb))
return false;
// activation, mirrors ImGui::SliderScalar
const bool hovered = ImGui::ItemHoverable(bb, gid, g.LastItemData.ItemFlags);
hover_haptic();
const bool clicked = hovered and ImGui::IsMouseClicked(ImGuiMouseButton_Left, ImGuiInputFlags_None, gid);
if (clicked or g.NavActivateId == gid)
{
if (clicked)
ImGui::SetKeyOwner(ImGuiKey_MouseLeft, gid);
ImGui::SetActiveID(gid, window);
ImGui::SetFocusID(gid, window);
ImGui::FocusWindow(window);
g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right);
}
ImRect grab;
const bool changed = ImGui::SliderBehavior(bb, gid, ImGuiDataType_S32, v, &v_min, &v_max, format, ImGuiSliderFlags_None, &grab);
if (changed)
ImGui::MarkItemEdited(gid);
const float fraction = v_max > v_min ? float(*v - v_min) / float(v_max - v_min) : 0;
const float fill_x = bb.Min.x + (bb.Max.x - bb.Min.x) * fraction;
ImDrawList * draw = window->DrawList;
draw->AddRectFilled(bb.Min, bb.Max, t.col(t.control), t.rounding);
if (fraction > 0)
draw->AddRectFilled(bb.Min, {fill_x, bb.Max.y}, t.col(t.accent_active), t.rounding, ImDrawFlags_RoundCornersLeft);
// bright grab at the fill edge
const float grab_w = metrics::slider_grab_width;
const float gx = ImClamp(fill_x, bb.Min.x + grab_w * 0.5f, bb.Max.x - grab_w * 0.5f);
draw->AddRectFilled({gx - grab_w * 0.5f, bb.Min.y}, {gx + grab_w * 0.5f, bb.Max.y}, t.col(t.accent), t.rounding);
char buf[64];
std::snprintf(buf, sizeof(buf), format, *v);
const ImVec2 ts = ImGui::CalcTextSize(buf);
draw->AddText({bb.Min.x + (size.x - ts.x) * 0.5f, bb.Min.y + (size.y - ts.y) * 0.5f}, t.col(t.text), buf);
bool reset = false;
if (default_value and reset_slot(id, *v != *default_value))
{
*v = *default_value;
reset = true;
}
return changed or reset;
}
bool segmented(const char * id, const std::vector<std::string> & options, int * selected, const ImVec2 & size_arg, const int * default_value)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
if (window->SkipItems or options.empty())
return false;
ImGuiContext & g = *GImGui;
const ImGuiStyle & style = g.Style;
const theme & t = current();
ImVec2 size = ImGui::CalcItemSize(size_arg, ImGui::CalcItemWidth(), ImGui::GetFrameHeight() * metrics::control_height);
if (default_value)
size.x -= reset_slot_width();
const ImVec2 pos = window->DC.CursorPos;
const ImRect bb(pos, pos + size);
ImGui::ItemSize(bb, style.FramePadding.y);
const ImGuiID gid = window->GetID(id);
if (not ImGui::ItemAdd(bb, gid))
return false;
const bool hovered = ImGui::ItemHoverable(bb, gid, g.LastItemData.ItemFlags);
hover_haptic();
// claim the active id on press so a drag here scrolls nothing (see ScrollWhenDragging)
if (hovered and ImGui::IsMouseClicked(ImGuiMouseButton_Left))
{
ImGui::SetActiveID(gid, window);
ImGui::FocusWindow(window);
}
if (g.ActiveId == gid and not g.IO.MouseDown[ImGuiMouseButton_Left])
ImGui::ClearActiveID();
ImDrawList * draw = window->DrawList;
draw->AddRectFilled(bb.Min, bb.Max, t.col(t.control), t.rounding);
const int n = int(options.size());
const float seg_w = size.x / n;
const float pad = metrics::segmented_inset;
bool changed = false;
for (int i = 0; i < n; ++i)
{
const ImRect seg({bb.Min.x + seg_w * i, bb.Min.y}, {bb.Min.x + seg_w * (i + 1), bb.Max.y});
const bool seg_hovered = hovered and seg.Contains(g.IO.MousePos);
if (seg_hovered and ImGui::IsMouseClicked(ImGuiMouseButton_Left))
{
*selected = i;
changed = true;
}
const bool active = *selected == i;
if (active)
draw->AddRectFilled(seg.Min + ImVec2(pad, pad), seg.Max - ImVec2(pad, pad), t.col(t.accent), t.rounding);
else if (seg_hovered)
draw->AddRectFilled(seg.Min + ImVec2(pad, pad), seg.Max - ImVec2(pad, pad), t.col(t.control_hovered), t.rounding);
const char * label = options[i].c_str();
const ImVec2 ts = ImGui::CalcTextSize(label);
draw->AddText({seg.Min.x + (seg_w - ts.x) * 0.5f, seg.Min.y + (size.y - ts.y) * 0.5f}, t.col(active ? t.on_accent : t.text), label);
}
if (default_value and reset_slot(id, *selected != *default_value))
{
*selected = *default_value;
changed = true;
}
return changed;
}
namespace
{
// Where combo modals open, in imgui display coords. Set each frame by the lobby.
ImVec2 popup_center = {0, 0};
// One selectable option inside the combo modal. A tall, full-width touch target
// with the name (accent when selected) and an optional muted description.
bool combo_row(const combo_item & item, bool selected)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
const theme & t = current();
const float row_h = ImGui::GetFrameHeight() * metrics::combo_row_height;
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(item.name);
if (not ImGui::ItemAdd(bb, gid))
return false;
bool hovered, held;
const bool pressed = ImGui::ButtonBehavior(bb, gid, &hovered, &held);
hover_haptic();
ImVec4 bg = selected ? t.control_active : t.card;
if (held)
bg = t.control_active;
else if (hovered)
bg = t.control_hovered;
ImDrawList * draw = window->DrawList;
draw->AddRectFilled(bb.Min, bb.Max, t.col(bg), t.rounding);
if (selected)
draw->AddRectFilled(bb.Min, {bb.Min.x + metrics::combo_padding.x * 0.25f, bb.Max.y}, t.col(t.accent), t.rounding, ImDrawFlags_RoundCornersLeft);
const float tx = bb.Min.x + metrics::combo_padding.x;
const ImU32 name_col = t.col(selected ? t.accent : t.text);
const ImVec2 nts = ImGui::CalcTextSize(item.name);
if (item.description and item.description[0])
{
const float desc_size = ImGui::GetStyle().FontSizeBase * metrics::font_description;
ImGui::PushFont(nullptr, desc_size);
const ImVec2 dts = ImGui::CalcTextSize(item.description);
const float total = nts.y + metrics::label_line_gap + dts.y;
const float y = bb.Min.y + (row_h - total) * 0.5f;
draw->AddText({tx, y + nts.y + metrics::label_line_gap}, t.col(t.text_muted), item.description);
ImGui::PopFont();
draw->AddText({tx, y}, name_col, item.name);
}
else
{
draw->AddText({tx, bb.Min.y + (row_h - nts.y) * 0.5f}, name_col, item.name);
}
return pressed;
}
} // namespace
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)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
if (window->SkipItems or items.empty())
return false;
const ImGuiStyle & style = ImGui::GetStyle();
const theme & t = current();
const ImGuiID gid = window->GetID(id);
// closed box, same height as the slider so setting rows line up
ImVec2 size = ImGui::CalcItemSize({width, 0}, ImGui::CalcItemWidth(), ImGui::GetFrameHeight() * metrics::control_height);
if (default_value)
size.x -= reset_slot_width();
const ImVec2 pos = window->DC.CursorPos;
const ImRect bb(pos, pos + size);
ImGui::ItemSize(bb, style.FramePadding.y);
if (not ImGui::ItemAdd(bb, gid))
return false;
bool hovered, held;
const bool clicked = ImGui::ButtonBehavior(bb, gid, &hovered, &held);
hover_haptic();
ImVec4 box = t.control;
if (held)
box = t.control_active;
else if (hovered)
box = t.control_hovered;
ImDrawList * draw = window->DrawList;
draw->AddRectFilled(bb.Min, bb.Max, t.col(box), t.rounding);
if (t.border_size > 0)
draw->AddRect(bb.Min, bb.Max, t.col(t.border), t.rounding, 0, t.border_size);
const int sel = ImClamp(*selected, 0, int(items.size()) - 1);
// preview, left aligned and clipped to the box
const char * preview = items[sel].name;
const ImVec2 ts = ImGui::CalcTextSize(preview);
const float pad_x = metrics::combo_padding.x;
draw->PushClipRect(bb.Min, bb.Max, true);
draw->AddText({bb.Min.x + pad_x, bb.Min.y + (size.y - ts.y) * 0.5f}, t.col(t.text), preview);
draw->PopClipRect();
// chevron on the right
const float cx = bb.Max.x - pad_x - metrics::combo_chevron * 0.5f;
const float cy = bb.Min.y + size.y * 0.5f;
const float r = metrics::combo_chevron * 0.5f;
draw->AddTriangleFilled({cx - r, cy - r * 0.4f}, {cx + r, cy - r * 0.4f}, {cx, cy + r * 0.6f}, t.col(t.text_muted));
char popup_id[64];
std::snprintf(popup_id, sizeof(popup_id), "##combo_%08X", gid);
if (clicked)
ImGui::OpenPopup(popup_id);
ImGui::SetNextWindowPos(popup_center, ImGuiCond_Always, {0.5f, 0.5f});
ImGui::SetNextWindowSize({ImMax(size.x, 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);
bool changed = false;
if (ImGui::BeginPopupModal(popup_id, nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize))
{
if (title and title[0])
{
ImGui::PushStyleColor(ImGuiCol_Text, t.text_muted);
ImGui::TextUnformatted(title);
ImGui::PopStyleColor();
ImGui::Dummy({0, style.ItemSpacing.y});
}
for (int i = 0; i < int(items.size()); ++i)
{
if (combo_row(items[i], i == sel))
{
*selected = i;
changed = true;
ImGui::CloseCurrentPopup();
}
}
// 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.
if (ImGui::IsMouseClicked(ImGuiMouseButton_Left) and not ImGui::IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem))
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
}
ImGui::PopStyleVar(3);
ImGui::PopStyleColor(3);
if (default_value and reset_slot(id, *selected != *default_value))
{
*selected = *default_value;
changed = true;
}
return changed;
}
void chip(const char * label, chip_style style, bool dot)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
if (window->SkipItems)
return;
const theme & t = current();
// foreground (text/dot/icon) and the pill fill
ImVec4 fg = t.text;
ImVec4 bg = t.control;
switch (style)
{
case chip_style::neutral:
break;
case chip_style::muted:
fg = t.text_muted;
break;
case chip_style::accent:
fg = t.accent;
bg = {t.accent.x, t.accent.y, t.accent.z, 0.16f};
break;
case chip_style::success:
fg = t.success;
bg = {t.success.x, t.success.y, t.success.z, 0.16f};
break;
case chip_style::warning:
fg = t.warning;
bg = {t.warning.x, t.warning.y, t.warning.z, 0.16f};
break;
case chip_style::danger:
fg = t.danger;
bg = {t.danger.x, t.danger.y, t.danger.z, 0.16f};
break;
}
const ImVec2 ts = ImGui::CalcTextSize(label);
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 pos = window->DC.CursorPos;
const ImRect bb(pos, pos + size);
ImGui::ItemSize(bb);
if (not ImGui::ItemAdd(bb, 0))
return;
ImDrawList * draw = window->DrawList;
draw->AddRectFilled(bb.Min, bb.Max, t.col(bg), size.y * 0.5f);
float tx = bb.Min.x + pad.x;
if (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);
}
static int input_text_resize(ImGuiInputTextCallbackData * data)
{
if (data->EventFlag == ImGuiInputTextFlags_CallbackResize)
{
std::string & s = *static_cast<std::string *>(data->UserData);
s.resize(data->BufTextLen);
data->Buf = s.data();
}
return 0;
}
// Common frame styling for the input widgets, control height. Returns the count of
// style colors / vars pushed so the caller can pop them.
static void push_input_style(float height)
{
const theme & t = current();
ImGui::PushStyleColor(ImGuiCol_FrameBg, t.control);
ImGui::PushStyleColor(ImGuiCol_FrameBgHovered, t.control_hovered);
ImGui::PushStyleColor(ImGuiCol_FrameBgActive, t.control_active);
ImGui::PushStyleColor(ImGuiCol_Text, t.text);
ImGui::PushStyleColor(ImGuiCol_TextDisabled, t.text_muted);
ImGui::PushStyleColor(ImGuiCol_Border, t.border);
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, t.rounding);
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, t.border_size);
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, {metrics::input_padding_x, (height - ImGui::GetFontSize()) * 0.5f});
}
static void pop_input_style()
{
ImGui::PopStyleVar(3);
ImGui::PopStyleColor(6);
}
bool input_text(const char * id, std::string & text, const char * hint, float width)
{
push_input_style(ImGui::GetFrameHeight() * metrics::control_height);
const bool changed = ImGui::InputTextEx(id, hint, text.data(), int(text.size()) + 1, {width, 0}, ImGuiInputTextFlags_CallbackResize, input_text_resize, &text);
pop_input_style();
hover_haptic();
return changed;
}
bool input_int(const char * id, int * v, int step, int v_min, int v_max, float width)
{
const ImGuiStyle & style = ImGui::GetStyle();
const float side = ImGui::GetFrameHeight() * metrics::control_height;
const float gap = style.ItemSpacing.x;
const float total = width > 0 ? width : ImGui::CalcItemWidth();
const float field_w = total - 2 * (side + gap);
ImGui::PushID(id);
push_input_style(side);
ImGui::SetNextItemWidth(field_w);
bool changed = ImGui::InputInt("##field", v, 0, 0, ImGuiInputTextFlags_CharsDecimal); // step 0 hides native buttons
pop_input_style();
hover_haptic();
ImGui::SameLine(0, gap);
if (icon_button(ICON_FA_MINUS, {side, side}))
{
*v -= step;
changed = true;
}
ImGui::SameLine(0, gap);
if (icon_button(ICON_FA_PLUS, {side, side}))
{
*v += step;
changed = true;
}
if (v_min != v_max)
*v = ImClamp(*v, v_min, v_max);
ImGui::PopID();
return changed;
}
bool begin_modal(const char * id, const char * title, float width)
{
const theme & t = current();
const ImGuiStyle & style = ImGui::GetStyle();
ImGui::SetNextWindowPos(popup_center, ImGuiCond_Always, {0.5f, 0.5f});
if (width > 0)
ImGui::SetNextWindowSize({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);
ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
if (width <= 0)
flags |= ImGuiWindowFlags_AlwaysAutoResize;
const bool open = ImGui::BeginPopupModal(id, nullptr, flags);
if (open)
{
if (title and title[0])
{
ImGui::PushFont(nullptr, style.FontSizeBase * metrics::font_modal_title);
ImGui::TextUnformatted(title);
ImGui::PopFont();
ImGui::Dummy({0, style.ItemSpacing.y});
}
}
else
{
ImGui::PopStyleVar(3);
ImGui::PopStyleColor(3);
}
return open;
}
void end_modal()
{
ImGui::EndPopup();
ImGui::PopStyleVar(3);
ImGui::PopStyleColor(3);
}
} // namespace wivrn::ui
+121
View File
@@ -0,0 +1,121 @@
/*
* WiVRn VR streaming
* Copyright (C) 2026 Guillaume Meunier <[email protected]>
* Copyright (C) 2026 Patrick Nicolas <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <functional>
#include <imgui.h>
#include <string>
#include <vector>
// Themed, reusable UI widgets matching the WiVRn dashboard design.
// All widgets read colors and metrics from wivrn::ui::current().
namespace wivrn::ui
{
enum class button_style
{
primary, // accent fill
secondary, // control fill
danger, // destructive
ghost, // transparent until hovered
};
// Page title with an optional muted subtitle
void page_header(const char * title, const char * subtitle = nullptr);
// Rounded panel, pair with end_card. Returns false when fully clipped
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);
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 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.
// Filled bar slider with the value centered on the rail
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);
// Centre (in imgui display coords) where combo modals open. Set once per frame
// from the lobby's popup layer before any combo() is drawn.
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.
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.
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
muted, // control fill, muted text
accent,
success,
warning,
danger,
};
void chip(const char * label, chip_style style = chip_style::neutral, bool dot = false);
// Themed single-line text field, control height. hint is optional placeholder text.
// Returns true while the text is being edited.
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.
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);
void end_modal();
} // namespace wivrn::ui
+2
View File
@@ -121,6 +121,7 @@ class lobby : public scene_impl<lobby>
settings,
post_processing,
customize,
components,
#if WIVRN_CLIENT_DEBUG_MENU
debug,
#endif
@@ -227,6 +228,7 @@ class lobby : public scene_impl<lobby>
void gui_settings();
void gui_post_processing();
void gui_customize(XrTime predicted_display_time);
void gui_components();
void gui_debug_node_hierarchy(entt::entity root = entt::null);
void gui_debug();
void gui_about();
+26 -1
View File
@@ -28,6 +28,8 @@
#include "gui_common.h"
#include "imgui.h"
#include "lobby.h"
#include "render/ui_theme.h"
#include "render/ui_widgets.h"
#include "scenes/stream.h" // IWYU pragma: keep
#include "utils/async.h"
#include "utils/i18n.h"
@@ -1749,6 +1751,20 @@ std::vector<std::pair<int, XrCompositionLayerQuad>> scenes::lobby::draw_gui(XrTi
update_file_picker();
ImGuiStyle & style = ImGui::GetStyle();
style.FontScaleMain = wivrn::ui::current().font_scale * wivrn::ui::metrics::font_base;
// base text colour follows the theme so plain ImGui text is legible on light themes
style.Colors[ImGuiCol_Text] = wivrn::ui::current().text;
style.Colors[ImGuiCol_TextDisabled] = wivrn::ui::current().text_muted;
// combo modals open centred on the popup layer
const auto & ui_popup_layer = imgui_ctx->layers()[1];
const glm::vec2 ui_popup_center = ui_popup_layer.vp_origin + ui_popup_layer.vp_size / 2;
wivrn::ui::set_popup_center({ui_popup_center.x, ui_popup_center.y});
// let the themed widgets fire the hover haptic and show tooltips
wivrn::ui::set_hover_haptic([this] { imgui_ctx->vibrate_on_hover(); });
wivrn::ui::set_tooltip_hook([this](const char * text) { imgui_ctx->tooltip(text); });
const float TabWidth = 300;
@@ -1759,7 +1775,9 @@ std::vector<std::pair<int, XrCompositionLayerQuad>> scenes::lobby::draw_gui(XrTi
}
ImGui::PushStyleVar(ImGuiStyleVar_ScrollbarSize, 30);
ImGui::PushStyleColor(ImGuiCol_WindowBg, IM_COL32(8, 8, 8, 224));
// window background follows the theme, with a user-controlled opacity
const wivrn::ui::theme & th = wivrn::ui::current();
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4{th.background.x, th.background.y, th.background.z, th.background_alpha});
ImGui::SetNextWindowPos(imgui_ctx->layers()[0].vp_center(), ImGuiCond_Always, {0.5, 0.5});
@@ -1823,6 +1841,10 @@ std::vector<std::pair<int, XrCompositionLayerQuad>> scenes::lobby::draw_gui(XrTi
gui_customize(predicted_display_time);
break;
case tab::components:
gui_components();
break;
#if WIVRN_CLIENT_DEBUG_MENU
case tab::debug:
gui_debug();
@@ -1872,6 +1894,9 @@ std::vector<std::pair<int, XrCompositionLayerQuad>> scenes::lobby::draw_gui(XrTi
RadioButtonWithoutCheckBox(ICON_FA_PANORAMA " " + _("Customize"), current_tab, tab::customize, {TabWidth, 0});
imgui_ctx->vibrate_on_hover();
RadioButtonWithoutCheckBox(ICON_FA_PALETTE " Components", current_tab, tab::components, {TabWidth, 0});
imgui_ctx->vibrate_on_hover();
#if WIVRN_CLIENT_DEBUG_MENU
RadioButtonWithoutCheckBox(ICON_FA_BUG_SLASH " " + _("Debug"), current_tab, tab::debug, {TabWidth, 0});
imgui_ctx->vibrate_on_hover();
+324
View File
@@ -0,0 +1,324 @@
/*
* WiVRn VR streaming
* Copyright (C) 2026 Guillaume Meunier <[email protected]>
* Copyright (C) 2026 Patrick Nicolas <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define IMGUI_DEFINE_MATH_OPERATORS
#include "lobby.h"
#include "render/ui_theme.h"
#include "render/ui_widgets.h"
#include "imgui.h"
#include "IconsFontAwesome6.h"
namespace ui = wivrn::ui;
// Card title shown above the rows
static void card_title(const char * title)
{
ImGui::PushStyleColor(ImGuiCol_Text, ui::current().text_muted);
ImGui::TextUnformatted(title);
ImGui::PopStyleColor();
ImGui::Dummy({0, 4});
}
void scenes::lobby::gui_components()
{
ui::theme & theme = ui::current();
ui::page_header("Components", "Live preview of the WiVRn UI kit");
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {12, 10});
const float control_w = 480;
// Theme
ui::begin_card("##theme");
{
card_title("THEME");
// Accent
ImGui::TextUnformatted("Accent color");
ImGui::Dummy({0, 2});
for (const auto & swatch: ui::accent_swatches())
{
const bool selected = theme.accent.x == swatch.base.x and theme.accent.y == swatch.base.y and theme.accent.z == swatch.base.z;
const auto flags = ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoBorder | (selected ? 0 : ImGuiColorEditFlags_NoDragDrop);
if (ImGui::ColorButton(swatch.name, swatch.base, flags, {ImGui::GetFrameHeight() * 1.4f, ImGui::GetFrameHeight() * 1.4f}))
ui::set_accent(swatch);
ImGui::SameLine();
}
ImGui::NewLine();
ui::row_separator();
// Preset
static int preset = 1; // dark_default
const auto preset_list = ui::presets();
std::vector<ui::combo_item> preset_items;
for (const auto & p: preset_list)
preset_items.push_back({p.name.c_str()});
static const int preset_default = 1;
ui::setting_label("Preset", "Surface and background palette", control_w);
if (ui::combo("##preset", "Theme preset", preset_items, &preset, control_w, &preset_default))
{
const ImVec4 keep_accent = theme.accent;
const ImVec4 keep_hover = theme.accent_hovered;
const ImVec4 keep_active = theme.accent_active;
ui::set_theme(preset_list[preset]);
theme.accent = keep_accent;
theme.accent_hovered = keep_hover;
theme.accent_active = keep_active;
}
ui::row_separator();
// Rounding
static const int rounding_default = 8;
int rounding = int(theme.rounding);
ui::setting_label("Rounding", "Corner radius of controls", control_w);
if (ui::slider_int("##rounding", &rounding, 0, 20, "%d px", {control_w, 0}, &rounding_default))
theme.rounding = float(rounding);
ui::row_separator();
static const int card_rounding_default = 14;
int card_rounding = int(theme.card_rounding);
ui::setting_label("Card rounding", "Corner radius of panels", control_w);
if (ui::slider_int("##card_rounding", &card_rounding, 0, 28, "%d px", {control_w, 0}, &card_rounding_default))
theme.card_rounding = float(card_rounding);
ui::row_separator();
// Text size, applied globally via style.FontScaleMain next frame. 100% is the
// design default; the value is a user multiplier on metrics::font_base.
static const int font_scale_default = 100;
int font_scale = int(theme.font_scale * 100);
ui::setting_label("Text size", "Global font scale", control_w);
if (ui::slider_int("##font_scale", &font_scale, 60, 140, "%d%%", {control_w, 0}, &font_scale_default))
theme.font_scale = float(font_scale) / 100.f;
ui::row_separator();
// Panel transparency
static const int opacity_default = 88;
int opacity = int(theme.background_alpha * 100);
ui::setting_label("Panel opacity", "Opacity of the main panel background", control_w);
if (ui::slider_int("##opacity", &opacity, 20, 100, "%d%%", {control_w, 0}, &opacity_default))
theme.background_alpha = float(opacity) / 100.f;
ui::end_card();
}
// Buttons
ui::begin_card("##buttons");
{
card_title("BUTTONS");
ui::button("Primary", ui::button_style::primary);
ImGui::SameLine();
ui::button("Secondary", ui::button_style::secondary);
ImGui::SameLine();
ui::button(ICON_FA_POWER_OFF " Disconnect", ui::button_style::danger);
ImGui::SameLine();
ui::button("Ghost", ui::button_style::ghost);
ui::row_separator();
card_title("ICON BUTTONS");
static bool mic = true;
if (ui::icon_button(ICON_FA_MICROPHONE, {}, mic, mic ? "Mute microphone" : "Unmute microphone"))
mic = not mic;
ImGui::SameLine();
ui::icon_button(ICON_FA_EYE_SLASH, {}, false, "Toggle passthrough");
ImGui::SameLine();
ui::icon_button(ICON_FA_HAND, {}, false, "Hand tracking");
ImGui::SameLine();
ui::icon_button(ICON_FA_ARROW_ROTATE_LEFT, {}, false, "Recenter");
ui::end_card();
}
// Toggles
ui::begin_card("##toggles");
{
card_title("TOGGLES");
// room for the toggle plus a trailing reset slot
const float toggle_w = ImGui::GetFrameHeight() * ui::metrics::control_height * ui::metrics::toggle_aspect;
const float toggle_row_w = toggle_w + ImGui::GetFrameHeight() * ui::metrics::control_height + ImGui::GetStyle().ItemSpacing.x;
static const bool spacewarp_default = false;
static bool spacewarp = true;
ui::setting_label("Application SpaceWarp", "Synthesises in-between frames so motion stays smooth", toggle_row_w);
ui::toggle("##spacewarp", &spacewarp, &spacewarp_default);
ui::row_separator();
static const bool high_power_default = true;
static bool high_power = true;
ui::setting_label("High power mode", "Higher sustained clocks for steadier frames", toggle_row_w);
ui::toggle("##high_power", &high_power, &high_power_default);
ui::end_card();
}
// Sliders
ui::begin_card("##sliders");
{
card_title("SLIDERS");
static const int resolution_default = 100;
static int resolution = 145;
ui::setting_label("Render resolution", "Pixels rendered per eye", control_w);
ui::slider_int("##resolution", &resolution, 50, 350, "%d%%", {control_w, 0}, &resolution_default);
ui::row_separator();
static const int bitrate_default = 100;
static int bitrate = 165;
ui::setting_label("Bitrate", "Video data rate", control_w);
ui::slider_int("##bitrate", &bitrate, 10, 500, "%d Mbit/s", {control_w, 0}, &bitrate_default);
ui::end_card();
}
// Segmented control
ui::begin_card("##segmented");
{
card_title("SEGMENTED CONTROL");
static const int refresh_default = 0;
static int refresh = 2;
static const std::vector<std::string> rates = {"Auto", "72", "80", "90", "120"};
ui::setting_label("Refresh rate", "Frames per second shown in the headset", control_w);
ui::segmented("##refresh", rates, &refresh, {control_w, 0}, &refresh_default);
ui::end_card();
}
// Combo
ui::begin_card("##combo");
{
card_title("COMBO");
static const std::vector<ui::combo_item> codecs = {
{"Automatic", "Picks the best codec your PC supports"},
{"H.264", "Lowest latency, widest support"},
{"HEVC (H.265)", "Better quality per bit"},
{"AV1", "Best efficiency, newest GPUs only"},
};
static const int codec_default = 0;
static int selected = 2;
ui::setting_label("Video codec", "How video is compressed and sent to the headset", control_w);
ui::combo("##codec", "Video codec", codecs, &selected, control_w, &codec_default);
ui::end_card();
}
// Chips
ui::begin_card("##chips");
{
card_title("CHIPS");
ui::chip(ICON_FA_BOLT " Auto", ui::chip_style::accent);
ImGui::SameLine();
ui::chip(ICON_FA_BATTERY_HALF " 38%", ui::chip_style::success);
ImGui::SameLine();
ui::chip("Connected", ui::chip_style::success, true);
ImGui::SameLine();
ui::chip("Not connected", ui::chip_style::muted, true);
ImGui::SameLine();
ui::chip("Unavailable", ui::chip_style::muted);
ImGui::SameLine();
ui::chip("Beta", ui::chip_style::warning);
ui::end_card();
}
// Text & number inputs
ui::begin_card("##inputs");
{
card_title("INPUTS");
static std::string name, address;
static int port = 9757;
ui::setting_label("Name", "Friendly label for the server", control_w);
ui::input_text("##name", name, "My gaming PC", control_w);
ui::row_separator();
ui::setting_label("Address", "Hostname or IP of the PC", control_w);
ui::input_text("##address", address, "192.168.1.10 or host.local", control_w);
ui::row_separator();
ui::setting_label("Port", "TCP/UDP port the server listens on", control_w);
ui::input_int("##port", &port, 1, 1, 65535, control_w);
ui::end_card();
}
// Dialog
ui::begin_card("##dialog");
{
card_title("DIALOG");
if (ui::button("Add server"))
ImGui::OpenPopup("##add_server_demo");
static std::string d_name, d_address;
static int d_port = 9757;
if (ui::begin_modal("##add_server_demo", "Add server", 620))
{
const float w = ImGui::GetContentRegionAvail().x;
ImGui::TextUnformatted("Name");
ui::input_text("##d_name", d_name, "My gaming PC", w);
ImGui::Dummy({0, 4});
ImGui::TextUnformatted("Address");
ui::input_text("##d_address", d_address, "192.168.1.10 or host.local", w);
ImGui::Dummy({0, 4});
ImGui::TextUnformatted("Port");
ui::input_int("##d_port", &d_port, 1, 1, 65535, w);
ImGui::Dummy({0, 12});
// right-aligned footer
const float gap = ImGui::GetStyle().ItemSpacing.x;
const float cancel_w = ImGui::CalcTextSize("Cancel").x + ui::metrics::button_padding.x * 2;
const float save_w = ImGui::CalcTextSize("Save").x + ui::metrics::button_padding.x * 2;
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x - cancel_w - save_w - gap);
if (ui::button("Cancel", ui::button_style::secondary))
ImGui::CloseCurrentPopup();
ImGui::SameLine();
if (ui::button("Save", ui::button_style::primary))
ImGui::CloseCurrentPopup();
ui::end_modal();
}
ui::end_card();
}
ImGui::PopStyleVar(); // ImGuiStyleVar_ItemSpacing
}