Skip to content

Commit

Permalink
DragScalarN, SliderScalarN, InputScalarN, PushMultiItemsWidths: fixed…
Browse files Browse the repository at this point in the history
… multi-components width in tight space (#7120, #7121)

+ extra tweak color ColorEdit4() label. Amend 86512ea, 3464662
  • Loading branch information
ocornut committed Dec 19, 2023
1 parent 0000739 commit 0bd6489
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9872,10 +9872,10 @@ void ImGui::PushMultiItemsWidths(int components, float w_full)
for (int i = components - 1; i > 0; i--)
{
float next_split = IM_TRUNC(w_items * i / components);
window->DC.ItemWidthStack.push_back(prev_split - next_split);
window->DC.ItemWidthStack.push_back(ImMax(prev_split - next_split, 1.0f));

This comment has been minimized.

Copy link
@Nahor

Nahor Dec 19, 2023

Contributor

Alternatively, you could have w_items = ImMax(components, w_full - style.ItemInnerSpacing.x * (components - 1));

Pros:

  • One call to ImMax per widget instead of one per component (not that this would have any measurable impact)
  • w_items set to a reasonable value (always >0, instead of possibly negative), which may help with future-proofing (e.g. if w_items is re-used for other things later)

Cons:

  • component width computation is less future-proof
prev_split = next_split;
}
window->DC.ItemWidth = prev_split;
window->DC.ItemWidth = ImMax(prev_split, 1.0f);
g.NextItemData.Flags &= ~ImGuiNextItemDataFlags_HasWidth;
}

Expand Down
6 changes: 3 additions & 3 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5128,8 +5128,6 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
const ImGuiStyle& style = g.Style;
const float square_sz = GetFrameHeight();
const float w_full = CalcItemWidth();
const float w_button = (flags & ImGuiColorEditFlags_NoSmallPreview) ? 0.0f : (square_sz + style.ItemInnerSpacing.x);
const float w_inputs = w_full - w_button;
const char* label_display_end = FindRenderedTextEnd(label);
g.NextItemData.ClearFlags();

Expand Down Expand Up @@ -5164,6 +5162,8 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
const bool alpha = (flags & ImGuiColorEditFlags_NoAlpha) == 0;
const bool hdr = (flags & ImGuiColorEditFlags_HDR) != 0;
const int components = alpha ? 4 : 3;
const float w_button = (flags & ImGuiColorEditFlags_NoSmallPreview) ? 0.0f : (square_sz + style.ItemInnerSpacing.x);
const float w_inputs = ImMax(w_full - w_button, 1.0f * components);

This comment has been minimized.

Copy link
@Nahor

Nahor Dec 19, 2023

Contributor

Is 1.0f * components really OK? It's used in the ImGuiColorEditFlags_DisplayHex mode to set the width of a single InputText, so that input minimum width with be 3 or 4 instead of a more typical 1

This comment has been minimized.

Copy link
@ocornut

ocornut Dec 19, 2023

Author Owner

That’s to keep it coherent when changing display mode, and it’s just simpler.
ColorEdit4 never displayed perfectly at very small sizes so i am not too worried about details, maybe wanted to get the biggest issue/regression clearer.

This comment has been minimized.

Copy link
@Nahor

Nahor Dec 19, 2023

Contributor

That’s to keep it coherent when changing display mode, and it’s just simpler.

But then the alignment is off with other widgets (color3 vs color4 in particular), plus the button will be clipped away more quickly than it needs.

But I guess it's a "rob Peter to pay Paul" scenario, and either way something will break. Moreover the UI is probably useless at that point anyway so visuals are not likely to be the main issue the user will be focused on 😛.

This comment has been minimized.

Copy link
@ocornut

ocornut Dec 20, 2023

Author Owner

I pushed further minor tweaks now for ColorEdit3/4 (036a6c8) but it's a bit of never-ending thing tbh so I'd prefer to not overly focus on it. Widgets like InputFloat with the +/- buttons always offset the label slightly so I guess this is acceptable.


// Convert to the formats we need
float f[4] = { col[0], col[1], col[2], alpha ? col[3] : 1.0f };
Expand Down Expand Up @@ -5211,7 +5211,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
if (n > 0)
SameLine(0, style.ItemInnerSpacing.x);
float next_split = IM_TRUNC(w_items * (n + 1) / components);
SetNextItemWidth(next_split - prev_split);
SetNextItemWidth(ImMax(next_split - prev_split, 1.0f));
prev_split = next_split;

// FIXME: When ImGuiColorEditFlags_HDR flag is passed HS values snap in weird ways when SV values go below 0.
Expand Down

0 comments on commit 0bd6489

Please sign in to comment.