-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add support for specifying text alignment on selectables #2347
Conversation
Hello @haldean, Could you clarify what are you using this for? My intuition is we ought to avoid adding too many rarely used parameters to function, and this could be a better fit as a style variable. See |
I am using it in a couple places in my UI; one in which I want the text to be properly centered (for a UI not unlike the one in the demo, I have a grid of selectables I want to turn on and off), and everywhere else I want it to be center-left (I have a bunch of places where I manually set the height to be larger to make it a bigger click target, and I want the text to be vertically centered in the taller box). I'll make the change to be a style variable; using PushStyleVar for my grid UI makes sense, because it's kind of an odd thing anyway. I was just going off of what was said in #1260 on this, but I actually think the style variable makes more sense. |
Thank you, that would be perfect! For the names in the demo, I would suggest using snprintf to generate a string. I am wondering if it would make sense changing the default from (0.0f,0.0f) to (0.0f,0.5f) ? |
I think (0, 0.5) is a nicer default but I also was exercising some caution around changing the current behavior; if you are OK with changing behavior to have a nicer default that sounds good to me! |
I don't know yet to be honest. Left-align is pretty much a required default to keep, and center-left align can look a little odder than embracing upper-left? You can do the PR with (0.0f,0.0f) and it'll be easy to change soon or later if if deem it viable. |
Yeah, I just tested with (0, 0.5) and actually a number of things draw poorly; menus in particular end up looking strange. I think keeping the default at (0, 0) is the way to go for now |
I think the
Passing an different clipping rectangle from the inner rectangle. Otherwise alignment will be incorrect (more noticeable if you increase style.FramePadding). |
1490435
to
d948cb3
Compare
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabRounding) }, // ImGuiStyleVar_GrabRounding | ||
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, TabRounding) }, // ImGuiStyleVar_TabRounding | ||
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ButtonTextAlign) }, // ImGuiStyleVar_ButtonTextAlign | ||
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, SelectableTextAlign) }, // ImGuiStyleVar_SelectableTextAlign |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all of these had to change because SelectableTextAlign was too long for the comment to be aligned properly
d948cb3
to
7fb48aa
Compare
You're right, that fixed it. I still left the default at (0, 0). It's a style variable now, and I updated the demo and added the appropriate sliders to the style editor. Let me know if there's any more changes you would like to see! |
7fb48aa
to
6896c0b
Compare
Adds a style variable to Selectable that allows clients to specify the text alignment within Selectables, adds a section in the demo to demonstrate selectable text alignment, and a pair of sliders in the style editor to change selectable alignment on the fly. In terms of implementation, this one is extremely simple: Selectable was already calling an API that supports text alignment, but had hard-coded it to top-left. This changes that to just pass the style variable straight through to RenderTextClipped. Backwards-compatibility is preserved by defaulting the text_align parameter to (0, 0), i.e., top-left. This also fixes a bug with selectable text rendering that caused right-aligned text in a selectable to be clipped incorrectly, because the wrong clipping rectangle was being used.
Thanks, I'll merge it shortly! |
…2347) Adds a style variable to Selectable that allows clients to specify the text alignment within Selectables, adds a section in the demo to demonstrate selectable text alignment, and a pair of sliders in the style editor to change selectable alignment on the fly. In terms of implementation, this one is extremely simple: Selectable was already calling an API that supports text alignment, but had hard-coded it to top-left. This changes that to just pass the style variable straight through to RenderTextClipped. Backwards-compatibility is preserved by defaulting the text_align parameter to (0, 0), i.e., top-left. This also fixes a bug with selectable text rendering that caused right-aligned text in a selectable to be clipped incorrectly, because the wrong clipping rectangle was being used.
Merged now, thank you Haldean ! |
Adds an optional parameter to
Selectable
that allows clients to specify the text alignment within theSelectable
, and adds a section in the demo to demonstrate text alignment support:In terms of implementation, this is extremely simple:
Selectable
was already calling an API that supports text alignment, but had hard-coded it to top-left. This adds atext_align
parameter on theSelectable
function and just passes that straight through toRenderTextClipped
instead of the hard-coded (0, 0). Backwards-compatibility is preserved by defaulting thetext_align
parameter to (0, 0), i.e., top-left.A video of the demo in action:
Works on #1260