Popup fixes and improvements

This commit is contained in:
ahsanswr
2026-07-26 17:24:36 +02:00
committed by Guillaume Meunier
parent e4b4bfa66d
commit 9352db046a
6 changed files with 201 additions and 85 deletions
+17 -17
View File
@@ -32,9 +32,9 @@ 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.accent = rgb(30, 115, 255);
t.accent_hovered = rgb(64, 140, 255);
t.accent_active = rgb(18, 95, 228);
t.on_accent = rgb(255, 255, 255);
t.background = rgb(10, 11, 13);
@@ -128,14 +128,14 @@ static theme light()
{
theme t = dark_default();
t.name = "Light";
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);
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.background = rgb(188, 193, 202);
t.card = rgb(252, 252, 253);
t.card_hovered = rgb(242, 243, 246);
t.control = rgb(206, 210, 219);
t.control_hovered = rgb(193, 198, 208);
t.control_active = rgb(178, 184, 196);
t.text = rgb(14, 16, 20);
t.text_muted = rgb(66, 72, 84);
t.border = rgb(176, 182, 194);
return t;
}
@@ -148,12 +148,12 @@ std::vector<theme> presets()
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)},
{"Blue", rgb(30, 115, 255), rgb(64, 140, 255), rgb(18, 95, 228)},
{"Violet", rgb(132, 72, 255), rgb(152, 98, 255), rgb(112, 52, 232)},
{"Emerald", rgb(0, 200, 132), rgb(28, 214, 150), rgb(0, 172, 112)},
{"Amber", rgb(255, 160, 0), rgb(255, 176, 36), rgb(228, 140, 0)},
{"Rose", rgb(255, 40, 82), rgb(255, 74, 108), rgb(230, 28, 68)},
{"Cyan", rgb(0, 190, 225), rgb(34, 205, 236), rgb(0, 168, 200)},
};
}
+3 -1
View File
@@ -84,7 +84,8 @@ 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};
constexpr ImVec2 card_padding = {28, 22}; // regular cards (settings, etc.)
constexpr ImVec2 list_card_padding = {18, 18}; // cards holding a list of rows (server list)
// Shared height of every full-width control (slider, segmented, combo, input)
// so they line up in a setting row, in multiples of ImGui::GetFrameHeight()
@@ -106,6 +107,7 @@ 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_box_large = 80; // min thumbnail size for full-height list rows
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)
+135 -32
View File
@@ -27,6 +27,8 @@
#include "IconsFontAwesome6.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <unordered_map>
@@ -94,12 +96,12 @@ struct card_frame
float width;
float content_max_x_backup;
float work_max_x_backup;
ImVec2 padding;
};
std::vector<card_frame> card_stack;
std::unordered_map<ImGuiID, float> card_heights;
} // namespace
bool begin_card(const char * id, const ImVec2 &)
bool begin_card_impl(const char * id, ImVec2 padding)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
const theme & t = current();
@@ -109,7 +111,8 @@ bool begin_card(const char * id, const ImVec2 &)
window->DC.CursorPos,
ImGui::GetContentRegionAvail().x,
window->ContentRegionRect.Max.x,
window->WorkRect.Max.x});
window->WorkRect.Max.x,
padding});
const card_frame & cf = card_stack.back();
if (const float h = card_heights[gid]; h > 0)
@@ -121,13 +124,24 @@ bool begin_card(const char * id, const ImVec2 &)
}
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});
window->ContentRegionRect.Max.x -= padding.x;
window->WorkRect.Max.x -= padding.x;
ImGui::Indent(padding.x);
ImGui::SetCursorScreenPos({ImGui::GetCursorScreenPos().x, cf.origin.y + padding.y});
ImGui::BeginGroup();
return true;
}
} // namespace
bool begin_card(const char * id, const ImVec2 &)
{
return begin_card_impl(id, metrics::card_padding);
}
bool begin_list_card(const char * id)
{
return begin_card_impl(id, metrics::list_card_padding);
}
void end_card()
{
@@ -136,12 +150,12 @@ void end_card()
card_stack.pop_back();
ImGui::EndGroup();
ImGui::Unindent(metrics::card_padding.x);
ImGui::Unindent(cf.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;
const float height = ImGui::GetItemRectMax().y + cf.padding.y - cf.origin.y;
card_heights[cf.id] = height;
// reserve the full card extent so layout and scrolling account for it
@@ -152,7 +166,9 @@ void end_card()
void row_separator()
{
const theme & t = current();
ImGui::PushStyleColor(ImGuiCol_Separator, t.border);
ImVec4 edge = t.border;
edge.w *= t.background_alpha; // match the panel/card opacity
ImGui::PushStyleColor(ImGuiCol_Separator, edge);
ImGui::Separator();
ImGui::PopStyleColor();
}
@@ -391,6 +407,70 @@ bool icon_button(const char * icon, const ImVec2 & size_arg, bool active, const
return pressed;
}
bool cancel_progress_button(const char * id, float fraction, const ImVec2 & size_arg, const std::string & tooltip)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
if (window->SkipItems)
return false;
ImGuiContext & g = *GImGui;
const theme & t = current();
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, g.Style.FramePadding.y);
const ImGuiID gid = window->GetID(id);
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);
ImDrawList * draw = window->DrawList;
const ImVec2 center = bb.GetCenter();
const float thickness = ImMax(2.f, size.y * 0.09f);
const float radius = ImMin(size.x, size.y) * 0.5f - thickness;
// faint full ring as the track
draw->PathArcTo(center, radius, 0, 2 * IM_PI, 48);
draw->PathStroke(t.col(t.control), ImDrawFlags_None, thickness);
const ImU32 accent = t.col(t.accent);
if (fraction >= 0)
{
// determinate arc, clockwise from the top
const float a0 = -IM_PI * 0.5f;
draw->PathArcTo(center, radius, a0, a0 + ImClamp(fraction, 0.f, 1.f) * 2 * IM_PI, 48);
draw->PathStroke(accent, ImDrawFlags_None, thickness);
}
else
{
// indeterminate: a rotating quarter arc
const float a0 = (float)g.Time * 3.5f;
draw->PathArcTo(center, radius, a0, a0 + IM_PI * 0.5f, 24);
draw->PathStroke(accent, ImDrawFlags_None, thickness);
}
// stop glyph in the centre, brightening on hover to read as a cancel target
const float glyph_size = size.y * metrics::icon_button_glyph * 0.7f;
ImGui::PushFont(nullptr, glyph_size);
const ImU32 col = t.col(hovered or held ? t.text : t.text_muted);
unsigned int codepoint = 0;
ImTextCharFromUtf8(&codepoint, ICON_FA_STOP, 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_FA_STOP);
ImGui::PopFont();
return pressed;
}
// Horizontal space a trailing reset slot consumes, gap included
float reset_slot_width()
{
@@ -891,21 +971,35 @@ bool nav_item(const char * icon, const std::string & label, bool selected)
hover_haptic();
ImDrawList * draw = window->DrawList;
// Subtle accent tint at a fixed alpha so the highlight reads the same in every
// theme, instead of the per-theme control surface whose contrast varies.
ImVec4 bg = t.accent;
bg.w = selected ? 0.16f : (hovered ? 0.08f : 0);
if (bg.w > 0)
draw->AddRectFilled(bb.Min, bb.Max, t.col(bg), t.rounding);
const float lum = t.background.x * 0.299f + t.background.y * 0.587f + t.background.z * 0.114f;
if (selected)
{
// Pure accent fill for the active tab.
draw->AddRectFilled(bb.Min, bb.Max, t.col(t.accent), t.rounding);
}
else if (hovered)
{
// Subtle lighten on dark themes, darken on light ones. The framebuffer
// blends in linear light, so a fixed alpha would look far stronger on a
// dark surface than a light one. Solve for the alpha that yields a constant
// *perceived* (gamma-space) step instead.
const float step = lum < 0.5f ? 0.10f : 0.06f; // perceptual lightness delta; stronger on dark
auto to_linear = [](float c) { return c <= 0.04045f ? c / 12.92f : std::pow((c + 0.055f) / 1.055f, 2.4f); };
const float overlay = lum < 0.5f ? 1.f : 0.f; // lighten vs darken
const float ys = to_linear(lum);
const float yt = to_linear(std::clamp(lum + (overlay - lum) / std::abs(overlay - lum) * step, 0.f, 1.f));
const float a = (yt - ys) / (overlay - ys);
draw->AddRectFilled(bb.Min, bb.Max, t.col({overlay, overlay, overlay, a}), t.rounding);
}
const float pad = metrics::combo_padding.x;
const float icon_w = ImGui::GetFontSize() * 1.5f; // icon column
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);
draw->AddText({bb.Min.x + pad + (icon_w - its.x) * 0.5f, bb.Min.y + (h - its.y) * 0.5f}, t.col(selected ? t.on_accent : t.text_muted), icon);
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());
draw->AddText({bb.Min.x + pad + icon_w + pad * 0.5f, bb.Min.y + (h - ls.y) * 0.5f}, t.col(selected ? t.on_accent : t.text), label.c_str());
return pressed;
}
@@ -1070,7 +1164,7 @@ 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)
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, bool large_thumb, bool interactive)
{
ImGuiWindow * window = ImGui::GetCurrentWindow();
const theme & t = current();
@@ -1078,7 +1172,7 @@ list_row_result begin_list_row(const char * id, const char * icon, ImTextureID i
// 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 float box = large_thumb ? metrics::list_row_box_large : metrics::list_row_box;
const ImVec2 ts = ImGui::CalcTextSize(title.c_str());
ImVec2 sub_sz{0, 0};
if (not subtitle.empty())
@@ -1092,23 +1186,32 @@ list_row_result begin_list_row(const char * id, const char * icon, ImTextureID i
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();
// non-interactive rows (the body does nothing, only trailing controls act) just reserve
// layout space -- no click area and no hover highlight on the row itself
bool clicked = false;
if (interactive)
{
// the click area excludes the trailing controls so they receive their own clicks
ImGui::SetNextItemAllowOverlap();
clicked = ImGui::InvisibleButton(id, {ImMax(avail - trailing_width, 1.f), row_h});
hover_haptic();
}
else
ImGui::Dummy({avail, row_h});
const bool hovered = interactive and ImGui::IsItemHovered();
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)
// No tinted background for the selected row -- callers indicate the active item with a
// chip instead. Still show the hover highlight.
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);
// leading thumbnail or icon box; large_thumb spans the full row height
const float thumb = large_thumb ? row_h - 2 * pad : box;
const ImVec2 box_min = {p0.x + pad, p0.y + (row_h - thumb) * 0.5f};
const ImVec2 box_max = box_min + ImVec2(thumb, thumb);
if (image)
draw->AddImageRounded(image, box_min, box_max, {0, 0}, {1, 1}, IM_COL32_WHITE, t.rounding);
else
@@ -1117,7 +1220,7 @@ list_row_result begin_list_row(const char * id, const char * icon, ImTextureID i
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);
draw->AddText(box_min + (ImVec2(thumb, thumb) - is) * 0.5f, t.col(selected ? t.on_accent : t.text_muted), icon);
}
}
+12 -1
View File
@@ -42,6 +42,8 @@ void page_header(const std::string & title, const std::string & subtitle = {});
// Rounded panel, pair with end_card
bool begin_card(const char * id, const ImVec2 & size = {0, 0});
// Same, but with tighter padding for cards that hold a list of rows
bool begin_list_card(const char * id);
void end_card();
// Full-width divider between rows of a card
@@ -71,6 +73,11 @@ bool button(const char * icon, const std::string & label, button_style style = b
// tooltip, if not empty, is shown while hovered
bool icon_button(const char * icon, const ImVec2 & size = {0, 0}, bool active = false, const std::string & tooltip = {});
// Circular download-progress indicator with a cancel (stop) glyph in the centre.
// fraction in [0,1] draws a determinate ring; < 0 draws an indeterminate spinner.
// Returns true when clicked (i.e. cancel requested). Sized like icon_button by default.
bool cancel_progress_button(const char * id, float fraction, const ImVec2 & size = {0, 0}, const std::string & tooltip = {});
bool toggle(const char * id, bool * v, const bool * default_value = nullptr);
// Width a trailing reset slot consumes (gap + square), for laying out toggles
@@ -161,7 +168,11 @@ struct list_row_result
// 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);
// large_thumb makes the leading thumbnail/icon box span the full row height (icon boxes
// keep their background at that same size).
// interactive=false reserves layout only -- no row-body click area and no hover highlight,
// for rows whose body does nothing (only trailing controls act).
list_row_result begin_list_row(const char * id, const char * icon, ImTextureID image, const std::string & title, const std::string & subtitle, bool selected = false, float trailing_width = 0, float height = 0, bool large_thumb = false, bool interactive = true);
void end_list_row();
// Confirmation dialog (open with ImGui::OpenPopup(id)). Returns 1 if confirmed, -1 if