Skip to content
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

inconsistent sizing between Button and ImageButton #6901

Closed
pozemka opened this issue Oct 5, 2023 · 3 comments
Closed

inconsistent sizing between Button and ImageButton #6901

pozemka opened this issue Oct 5, 2023 · 3 comments
Labels

Comments

@pozemka
Copy link

pozemka commented Oct 5, 2023

Version/Branch of Dear ImGui:

Version: v1.89.9
Branch: https://github.com/thedmd/imgui/tree/layouts

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_opengl3.h, imgui_impl_glfw.h
Compiler: msvc
Operating System: windows 10

My Issue/Question:
Hello. When displaying buttons with this code:

ImGuiIO& io = ImGui::GetIO();
ImTextureID my_tex_id = io.Fonts->TexID;

ImGui::Button("Hi1", { 32,32 });
ImGui::ImageButton("Hi2", my_tex_id, { 32, 32 });

their sizes are different:

Screenshots/Video
image

Standalone, minimal, complete and verifiable example:

// Here's some code anyone can copy and paste to reproduce your issue
ImGuiIO& io = ImGui::GetIO();
ImTextureID my_tex_id = io.Fonts->TexID;

ImGui::Button("Hi1", { 32,32 });
ImGui::ImageButton("Hi2", my_tex_id, { 32, 32 });

Is this expected behavior? If yes how should I correct size of the ImageButton to match size of regular Button?

@pozemka pozemka changed the title Inconsistant sizing between Button and ImageButton inconsistent sizing between Button and ImageButton Oct 5, 2023
@ocornut
Copy link
Owner

ocornut commented Oct 5, 2023

Is this expected behavior? If yes how should I correct size of the ImageButton to match size of regular Butto

Yes.
When passing a size to most widgets it is a total size and include the FramePadding.

ImageButton() adds FramePadding over the passed size. It makes sense but I do agree it should be clarified in the documentation.

The difference is (style.FramePadding * 2.0f)

So those will be equivalent:

ImGui::Button("Hi1", { 32,32 } + ImGui::GetStyle().FramePadding * 2.0f );
ImGui::ImageButton("Hi2", my_tex_id, { 32, 32 });

Those will also be equivalent:

ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, { 0, 0 });
ImGui::Button("Hi1", { 32,32 } );
ImGui::ImageButton("Hi2", my_tex_id, { 32, 32 });
ImGui::PopStyleVar();

@pozemka
Copy link
Author

pozemka commented Oct 5, 2023

Thank you. That was unexpected for me.

If this is intended despite not very obvious (at least for me) behavior I wouldn't suggest to change calculations since many could rely on this sizing. Hope adding information about sizing to documentation would help future users.

Feel free to close this ticket.

@ocornut
Copy link
Owner

ocornut commented Oct 5, 2023

What happened is the following:

  • prior to August 2022 we had a different API to ImageButton() which was less consistent with others API has it had both size and padding parameter, and it possible to set or override padding via a parameter.
  • we switched it to a new logic and kept old redirecting one (see 4a2ae06) but as a side-effect of being more consistent it got a little more error-prone/misleading on this specific topic. I admit I didn't consider this aspect back then.

Unlike e.g. Button() where automatic size may be referred from the text contents we cannot do the same with ImageButton().

I have now renamed the argument to image_size and added various comments to clarify this. See commit 0312a29

@ocornut ocornut closed this as completed Oct 6, 2023
@ocornut ocornut added the doc label Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants