Adding slider thickness (rebased 2026/10/12) (3599)

This commit is contained in:
ocornut 2026-10-12 19:36:20 +02:00
parent 5639db380c
commit 93d05ee4f9
4 changed files with 28 additions and 5 deletions

View File

@ -1581,6 +1581,7 @@ ImGuiStyle::ImGuiStyle()
ScrollbarPadding = 2.0f; // Padding of scrollbar grab within its frame (same for both axes)
GrabMinSize = 12.0f; // Minimum width/height of a grab box for slider/scrollbar
GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.
SliderThickness = 1.0f; // Thickness of sliders. Can be set between 0.0f (rectangle width is null) and 1.0f (full rectangle is drawn).
SliderContrast = 0.5f; // Contrast between the left and right sides of the slider track.
LogSliderDeadzone = 4.0f; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero.
ImageRounding = 0.0f; // Rounding of Image() calls.
@ -3792,6 +3793,7 @@ static const ImGuiStyleVarInfo GStyleVarsInfo[] =
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ScrollbarPadding) }, // ImGuiStyleVar_ScrollbarPadding
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, GrabMinSize) }, // ImGuiStyleVar_GrabMinSize
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, GrabRounding) }, // ImGuiStyleVar_GrabRounding
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, SliderThickness) }, // ImGuiStyleVar_SliderThickness
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, SliderContrast) }, // ImGuiStyleVar_SliderContrast
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ImageRounding) }, // ImGuiStyleVar_ImageRounding
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ImageBorderSize) }, // ImGuiStyleVar_ImageBorderSize

View File

@ -1878,6 +1878,7 @@ enum ImGuiStyleVar_
ImGuiStyleVar_ScrollbarSize, // float ScrollbarSize
ImGuiStyleVar_ScrollbarRounding, // float ScrollbarRounding
ImGuiStyleVar_ScrollbarPadding, // float ScrollbarPadding
ImGuiStyleVar_SliderThickness, // float SliderThickness
ImGuiStyleVar_SliderContrast, // float SliderContrast
ImGuiStyleVar_GrabMinSize, // float GrabMinSize
ImGuiStyleVar_GrabRounding, // float GrabRounding
@ -2361,6 +2362,7 @@ struct ImGuiStyle
float ScrollbarPadding; // Padding of scrollbar grab within its frame (same for both axes).
float GrabMinSize; // Minimum width/height of a grab box for slider/scrollbar.
float GrabRounding; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.
float SliderThickness; // Thickness of sliders. Can be set between 0.0f (rectangle width is null) and 1.0f (full rectangle is drawn),
float SliderContrast; // Contrast between the left and right sides of the slider track.
float LogSliderDeadzone; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero.
float ImageRounding; // Rounding of Image() calls.

View File

@ -4495,6 +4495,10 @@ static void DemoWindowWidgetsVerticalSliders()
const float spacing = 4;
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(spacing, spacing));
static float sliderThickness = 0.25f;
ImGui::SliderFloat("Slider Thickness", &sliderThickness, 0.0f, 1.0f, "%.2f");
ImGui::PushStyleVar(ImGuiStyleVar_SliderThickness, sliderThickness);
static int int_value = 0;
ImGui::VSliderInt("##int", ImVec2(18, 160), &int_value, 0, 5);
ImGui::SameLine();
@ -4551,7 +4555,7 @@ static void DemoWindowWidgetsVerticalSliders()
ImGui::PopID();
}
ImGui::PopID();
ImGui::PopStyleVar();
ImGui::PopStyleVar(2);
ImGui::TreePop();
}
}
@ -8786,6 +8790,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref)
SliderFloat("SeparatorTextBorderSize", &style.SeparatorTextBorderSize, 0.0f, 10.0f, "%.0f");
SliderFloat2("SeparatorTextAlign", (float*)&style.SeparatorTextAlign, 0.0f, 1.0f, "%.2f");
SliderFloat2("SeparatorTextPadding", (float*)&style.SeparatorTextPadding, 0.0f, 40.0f, "%.0f");
SliderFloat("SliderThickness", &style.SliderThickness, 0.0f, 1.0f, "%.2f");
SliderFloat("SliderContrast", &style.SliderContrast, -1.0f, 1.0f, "%.2f");
SliderFloat("LogSliderDeadzone", &style.LogSliderDeadzone, 0.0f, 12.0f, "%.0f");
SliderFloat("ImageRounding", &style.ImageRounding, 0.0f, 12.0f, "%.0f");

View File

@ -3432,10 +3432,17 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat
MarkItemEdited(id);
// Render track
ImRect draw_bb = frame_bb;
if (g.Style.SliderThickness != 1.0f)
{
float shrink_amount = IM_ROUND((frame_bb.Max.y - frame_bb.Min.y) * 0.5f * (1.0f - g.Style.SliderThickness));
draw_bb.Min.y += shrink_amount;
draw_bb.Max.y -= shrink_amount;
}
//RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, false, style.FrameRounding);
float mid_x = IM_ROUND(grab_bb.Min.x + (grab_bb.Max.x - grab_bb.Min.x) * 0.50f);
window->DrawList->AddRectFilled(frame_bb.Min, ImVec2(mid_x, frame_bb.Max.y), frame_col, style.FrameRounding, ImDrawFlags_RoundLeft);
window->DrawList->AddRectFilled(ImVec2(mid_x, frame_bb.Min.y), frame_bb.Max, frame_col_after, style.FrameRounding, ImDrawFlags_RoundRight);
window->DrawList->AddRectFilled(draw_bb.Min, ImVec2(mid_x, draw_bb.Max.y), frame_col, style.FrameRounding, ImDrawFlags_RoundLeft);
window->DrawList->AddRectFilled(ImVec2(mid_x, draw_bb.Min.y), draw_bb.Max, frame_col_after, style.FrameRounding, ImDrawFlags_RoundRight);
if (color_marker != 0 && style.ColorMarkerSize > 0.0f)
RenderColorComponentMarker(frame_bb, GetColorU32(color_marker), style.FrameRounding);
RenderFrameBorder(frame_bb.Min, frame_bb.Max, g.Style.FrameRounding);
@ -3599,10 +3606,17 @@ bool ImGui::VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType d
MarkItemEdited(id);
// Render track
ImRect draw_bb = frame_bb;
if (g.Style.SliderThickness != 1.0f)
{
float shrink_amount = (float)(int)((frame_bb.Max.x - frame_bb.Min.x) * 0.5f * (1.0f - g.Style.SliderThickness));
draw_bb.Min.x += shrink_amount;
draw_bb.Max.x -= shrink_amount;
}
//RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, g.Style.FrameRounding);
float mid_y = IM_ROUND(grab_bb.Max.y - (grab_bb.Max.y - grab_bb.Min.y) * 0.50f);
window->DrawList->AddRectFilled(ImVec2(frame_bb.Min.x, mid_y), frame_bb.Max, frame_col, style.FrameRounding, ImDrawFlags_RoundBottom);
window->DrawList->AddRectFilled(frame_bb.Min, ImVec2(frame_bb.Max.x, mid_y), frame_col_after, style.FrameRounding, ImDrawFlags_RoundTop);
window->DrawList->AddRectFilled(ImVec2(draw_bb.Min.x, mid_y), draw_bb.Max, frame_col, style.FrameRounding, ImDrawFlags_RoundBottom);
window->DrawList->AddRectFilled(draw_bb.Min, ImVec2(draw_bb.Max.x, mid_y), frame_col_after, style.FrameRounding, ImDrawFlags_RoundTop);
// Render grab
if (grab_bb.Max.y > grab_bb.Min.y)