Fixes and improvements
This commit is contained in:
committed by
Guillaume Meunier
parent
a5b1b6dbeb
commit
12834824ab
@@ -24,6 +24,7 @@
|
||||
#include "constants.h"
|
||||
#include "image_loader.h"
|
||||
#include "openxr/openxr.h"
|
||||
#include "ui_theme.h"
|
||||
#include "utils/mapped_file.h"
|
||||
#include "utils/ranges.h"
|
||||
#include "utils/strings.h"
|
||||
@@ -1280,10 +1281,14 @@ void imgui_context::tooltip(std::string_view text)
|
||||
viewport->Pos = ImVec2(tooltip_layer.vp_origin.x, tooltip_layer.vp_origin.y);
|
||||
viewport->Size = ImVec2(tooltip_layer.vp_size.x, tooltip_layer.vp_size.y);
|
||||
|
||||
// Draw the tooltip in the tooltip layer
|
||||
// Draw the tooltip in the tooltip layer, styled to match the active theme
|
||||
// Clamp position to avoid overflowing on the left or the right
|
||||
const wivrn::ui::theme & t = wivrn::ui::current();
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, constants::style::tooltip_padding);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, constants::style::tooltip_rounding);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, t.card_rounding);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, t.border_size);
|
||||
ImGui::PushStyleColor(ImGuiCol_PopupBg, t.card);
|
||||
ImGui::PushStyleColor(ImGuiCol_Border, t.border);
|
||||
|
||||
auto & style = ImGui::GetStyle();
|
||||
const ImVec2 text_size = ImGui::CalcTextSize(text.data(), text.data() + text.size(), true);
|
||||
@@ -1293,12 +1298,13 @@ void imgui_context::tooltip(std::string_view text)
|
||||
ImGui::SetNextWindowSizeConstraints({0, 0}, viewport->Size);
|
||||
if (ImGui::BeginTooltip())
|
||||
{
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, 0xffffffff);
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, t.col(t.text));
|
||||
ImGui::TextUnformatted(text.data(), text.data() + text.size());
|
||||
ImGui::PopStyleColor();
|
||||
ImGui::EndTooltip();
|
||||
}
|
||||
ImGui::PopStyleVar(2);
|
||||
ImGui::PopStyleColor(2);
|
||||
ImGui::PopStyleVar(3);
|
||||
|
||||
viewport->Pos = pos_backup;
|
||||
viewport->Size = size_backup;
|
||||
|
||||
@@ -56,8 +56,8 @@ static theme dark_default()
|
||||
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
|
||||
t.font_scale = 1.0; // user multiplier; 100% maps to metrics::font_base
|
||||
t.background_alpha = 1.0f; // fully opaque panel by default
|
||||
return t;
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ static theme light()
|
||||
{
|
||||
theme t = dark_default();
|
||||
t.name = "Light";
|
||||
t.background = rgb(236, 238, 242);
|
||||
t.background = rgb(221, 224, 230);
|
||||
t.card = rgb(255, 255, 255);
|
||||
t.card_hovered = rgb(246, 247, 250);
|
||||
t.control = rgb(212, 216, 224);
|
||||
@@ -136,7 +136,7 @@ static theme light()
|
||||
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);
|
||||
t.border = rgb(176, 182, 194);
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
@@ -97,18 +97,20 @@ 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 icon_button_glyph = 0.58; // glyph height as a fraction of the button side
|
||||
constexpr float button_label_glyph = 1.35; // leading icon size in a labeled button, x base text size
|
||||
|
||||
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 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 list_row_box = 52; // leading icon/thumbnail size in a list row
|
||||
constexpr float list_row_pad = 12; // inner padding of a list row, both axes
|
||||
constexpr ImVec2 chip_padding = {12, 6}; // inner padding of a chip/badge
|
||||
constexpr float chip_pill_padding_x = 18; // roomier horizontal padding for height-override pill chips (top bar)
|
||||
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_bottom_pad = 6; // breathing room below the description
|
||||
|
||||
+138
-36
@@ -171,15 +171,25 @@ float setting_label(const std::string & title, const std::string & description,
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {style.ItemSpacing.x, metrics::label_line_gap});
|
||||
ImGui::PushTextWrapPos(start.x + text_w);
|
||||
ImGui::TextUnformatted(title.c_str());
|
||||
ImGui::PopTextWrapPos();
|
||||
if (not description.empty())
|
||||
{
|
||||
ImGui::PushFont(nullptr, style.FontSizeBase * metrics::font_description);
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, t.text_muted);
|
||||
ImGui::TextUnformatted(description.c_str());
|
||||
// single line, capped at half the row width and ellipsized so it never wraps or crowds the control
|
||||
const float desc_w = ImMin(text_w, avail * 0.5f);
|
||||
std::string text = description;
|
||||
if (ImGui::CalcTextSize(text.c_str()).x > desc_w)
|
||||
{
|
||||
const float ell_w = ImGui::CalcTextSize("...").x;
|
||||
while (not text.empty() and ImGui::CalcTextSize(text.c_str()).x + ell_w > desc_w)
|
||||
text.pop_back();
|
||||
text += "...";
|
||||
}
|
||||
ImGui::TextUnformatted(text.c_str());
|
||||
ImGui::PopStyleColor();
|
||||
ImGui::PopFont();
|
||||
}
|
||||
ImGui::PopTextWrapPos();
|
||||
ImGui::PopStyleVar();
|
||||
|
||||
// right-aligned control slot, vertically centered against the label block
|
||||
@@ -190,6 +200,30 @@ float setting_label(const std::string & title, const std::string & description,
|
||||
return start.y + label_h;
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
struct button_colors
|
||||
{
|
||||
ImVec4 bg, bg_hovered, bg_active, text;
|
||||
};
|
||||
|
||||
button_colors colors_for(button_style s, const theme & t)
|
||||
{
|
||||
switch (s)
|
||||
{
|
||||
case button_style::primary:
|
||||
return {t.accent, t.accent_hovered, t.accent_active, t.on_accent};
|
||||
case button_style::secondary:
|
||||
return {t.control, t.control_hovered, t.control_active, t.text};
|
||||
case button_style::danger:
|
||||
return {t.danger, t.danger_hovered, t.danger, t.on_accent};
|
||||
case button_style::ghost:
|
||||
return {{0, 0, 0, 0}, t.control, t.control_active, t.text};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool button(const std::string & label, button_style s, const ImVec2 & size)
|
||||
{
|
||||
const theme & t = current();
|
||||
@@ -197,33 +231,11 @@ bool button(const std::string & label, button_style s, const ImVec2 & size)
|
||||
// control height by default, width 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;
|
||||
}
|
||||
const button_colors c = colors_for(s, t);
|
||||
ImGui::PushStyleColor(ImGuiCol_Button, c.bg);
|
||||
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, c.bg_hovered);
|
||||
ImGui::PushStyleColor(ImGuiCol_ButtonActive, c.bg_active);
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, c.text);
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, t.rounding);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, metrics::button_padding);
|
||||
@@ -234,6 +246,69 @@ bool button(const std::string & label, button_style s, const ImVec2 & size)
|
||||
return pressed;
|
||||
}
|
||||
|
||||
bool button(const char * icon, const std::string & label, button_style s, const ImVec2 & size)
|
||||
{
|
||||
ImGuiWindow * window = ImGui::GetCurrentWindow();
|
||||
if (window->SkipItems)
|
||||
return false;
|
||||
|
||||
ImGuiContext & g = *GImGui;
|
||||
const ImGuiStyle & style = g.Style;
|
||||
const theme & t = current();
|
||||
|
||||
const float h = size.y > 0 ? size.y : ImGui::GetFrameHeight() * metrics::control_height;
|
||||
const float glyph_size = ImGui::GetFontSize() * metrics::button_label_glyph;
|
||||
const float gap = ImGui::GetFontSize() * 0.4f;
|
||||
|
||||
ImGui::PushFont(nullptr, glyph_size);
|
||||
const float icon_w = ImGui::CalcTextSize(icon).x;
|
||||
ImGui::PopFont();
|
||||
const ImVec2 label_size = ImGui::CalcTextSize(label.c_str());
|
||||
const float content_w = icon_w + gap + label_size.x;
|
||||
const float w = size.x > 0 ? size.x : content_w + metrics::button_padding.x * 2;
|
||||
|
||||
const ImVec2 pos = window->DC.CursorPos;
|
||||
const ImRect bb(pos, pos + ImVec2{w, h});
|
||||
ImGui::ItemSize(bb, style.FramePadding.y);
|
||||
const ImGuiID id = window->GetID(label.c_str());
|
||||
if (not ImGui::ItemAdd(bb, id))
|
||||
return false;
|
||||
|
||||
bool hovered, held;
|
||||
const bool pressed = ImGui::ButtonBehavior(bb, id, &hovered, &held);
|
||||
hover_haptic();
|
||||
|
||||
const button_colors c = colors_for(s, t);
|
||||
ImVec4 bg = c.bg;
|
||||
if (held)
|
||||
bg = c.bg_active;
|
||||
else if (hovered)
|
||||
bg = c.bg_hovered;
|
||||
|
||||
ImDrawList * draw = window->DrawList;
|
||||
draw->AddRectFilled(bb.Min, bb.Max, t.col(bg), t.rounding);
|
||||
|
||||
const ImU32 col = t.col(c.text);
|
||||
const ImVec2 center = bb.GetCenter();
|
||||
float x = center.x - content_w * 0.5f;
|
||||
|
||||
// icon centred vertically on its ink box, like icon_button
|
||||
ImGui::PushFont(nullptr, glyph_size);
|
||||
unsigned int codepoint = 0;
|
||||
ImTextCharFromUtf8(&codepoint, icon, nullptr);
|
||||
const ImFontGlyph * glyph = ImGui::GetFontBaked()->FindGlyph((ImWchar)codepoint);
|
||||
if (glyph)
|
||||
draw->AddText({x, center.y - (glyph->Y0 + glyph->Y1) * 0.5f}, col, icon);
|
||||
else
|
||||
draw->AddText({x, center.y - glyph_size * 0.5f}, col, icon);
|
||||
ImGui::PopFont();
|
||||
|
||||
x += icon_w + gap;
|
||||
draw->AddText({x, center.y - label_size.y * 0.5f}, col, label.c_str());
|
||||
|
||||
return pressed;
|
||||
}
|
||||
|
||||
bool icon_button(const char * icon, const ImVec2 & size_arg, bool active, const std::string & tooltip)
|
||||
{
|
||||
ImGuiWindow * window = ImGui::GetCurrentWindow();
|
||||
@@ -404,7 +479,11 @@ bool slider_int(const char * id, int * v, int v_min, int v_max, const char * for
|
||||
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);
|
||||
{
|
||||
// round the right corners too once the fill reaches the box edge, else they stay sharp over the rounded background
|
||||
const ImDrawFlags fill_flags = fill_x >= bb.Max.x - t.rounding ? ImDrawFlags_RoundCornersAll : ImDrawFlags_RoundCornersLeft;
|
||||
draw->AddRectFilled(bb.Min, {fill_x, bb.Max.y}, t.col(t.accent_active), t.rounding, fill_flags);
|
||||
}
|
||||
// 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);
|
||||
@@ -498,6 +577,8 @@ namespace
|
||||
{
|
||||
// Where combo modals open, in imgui display coords. Set each frame by the lobby.
|
||||
ImVec2 popup_center = {0, 0};
|
||||
// Height of the popup layer; caps tall combo lists so they scroll. 0 = unbounded.
|
||||
float popup_available_height = 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.
|
||||
@@ -554,9 +635,10 @@ bool combo_row(const combo_item & item, bool selected)
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void set_popup_center(const ImVec2 & center)
|
||||
void set_popup_center(const ImVec2 & center, float available_height)
|
||||
{
|
||||
popup_center = center;
|
||||
popup_available_height = available_height;
|
||||
}
|
||||
|
||||
bool combo(const char * id, const std::string & title, const std::vector<combo_item> & items, int * selected, float width, const int * default_value)
|
||||
@@ -636,15 +718,33 @@ bool combo(const char * id, const std::string & title, const std::vector<combo_i
|
||||
ImGui::Dummy({0, style.ItemSpacing.y});
|
||||
}
|
||||
|
||||
// cap the list height so a long list scrolls inside the modal rather than
|
||||
// overflowing the popup layer, which would clip the top and bottom rows
|
||||
const float row_h = ImGui::GetFrameHeight() * metrics::combo_row_height;
|
||||
float list_h = int(items.size()) * row_h;
|
||||
if (popup_available_height > 0)
|
||||
{
|
||||
const float title_h = title.empty() ? 0 : ImGui::GetTextLineHeight() + style.ItemSpacing.y;
|
||||
const float max_list = popup_available_height * 0.9f - 2 * metrics::card_padding.y - title_h;
|
||||
list_h = ImMin(list_h, ImMax(max_list, row_h));
|
||||
}
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {0, 0});
|
||||
ImGui::BeginChild("##list", {0, list_h});
|
||||
for (int i = 0; i < int(items.size()); ++i)
|
||||
{
|
||||
if (combo_row(items[i], i == sel))
|
||||
const bool is_sel = i == sel;
|
||||
if (combo_row(items[i], is_sel))
|
||||
{
|
||||
*selected = i;
|
||||
changed = true;
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
if (is_sel and ImGui::IsWindowAppearing())
|
||||
ImGui::SetScrollHereY(0.5f); // reveal the current value when opening
|
||||
}
|
||||
ImGui::EndChild();
|
||||
ImGui::PopStyleVar();
|
||||
|
||||
// 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))
|
||||
@@ -703,9 +803,11 @@ void chip(const std::string & label, chip_style style, bool dot, float height)
|
||||
|
||||
const ImVec2 ts = ImGui::CalcTextSize(label.c_str());
|
||||
const ImVec2 pad = metrics::chip_padding;
|
||||
// tall pill chips (height override, e.g. the top bar) get roomier sides
|
||||
const float pad_x = height > 0 ? metrics::chip_pill_padding_x : pad.x;
|
||||
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, height > 0 ? height : ts.y + pad.y * 2};
|
||||
const float dot_gap = dot ? dot_r * 2 + pad_x * 0.55f : 0;
|
||||
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);
|
||||
@@ -716,11 +818,11 @@ void chip(const std::string & label, chip_style style, bool dot, float height)
|
||||
ImDrawList * draw = window->DrawList;
|
||||
draw->AddRectFilled(bb.Min, bb.Max, t.col(bg), size.y * 0.5f);
|
||||
|
||||
float tx = bb.Min.x + pad.x;
|
||||
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;
|
||||
tx += dot_r * 2 + pad_x * 0.55f;
|
||||
}
|
||||
draw->AddText({tx, bb.Min.y + (size.y - ts.y) * 0.5f}, t.col(fg), label.c_str());
|
||||
}
|
||||
|
||||
@@ -53,6 +53,8 @@ void row_separator();
|
||||
float setting_label(const std::string & title, const std::string & description, float control_width);
|
||||
|
||||
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
|
||||
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 = {});
|
||||
|
||||
@@ -77,8 +79,10 @@ struct combo_item
|
||||
// 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 (display coords) where combo and begin_modal popups open. Set once per frame.
|
||||
void set_popup_center(const ImVec2 & center);
|
||||
// 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.
|
||||
void set_popup_center(const ImVec2 & center, float available_height = 0);
|
||||
|
||||
// Hook fired after an interactive item, for a hover haptic. Install once.
|
||||
void set_hover_haptic(std::function<void()> hook);
|
||||
|
||||
Reference in New Issue
Block a user