emilk / emilk/egui

ImageButton does not use y value of style.spacing.button_padding

Open
#3,774 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
30.6k
Forks
2.1k
Avg merge
1d 9h
Merged PRs (30d)
72

Description

**Describe the bug**
ImageButton only uses the x value of `ui.style().spacing.button_padding` when drawn. In practice this means that ImageButtons cannot have independent padding on X / Y, and the Y value doesn't impact the layout of the frame.

[Link to code: egui/src/widgets/button.rs](https://github.com/emilk/egui/blob/9faf4b44ff12af00c26763667fdf41bf64a7c060/crates/egui/src/widgets/button.rs#L627C24-L627C25)
```rust
// egui/src/widgets/button.rs

impl<'a> Widget for ImageButton<'a> {
fn ui(self, ui: &mut Ui) -> Response {
let padding = if self.frame {
// so we can see that it is a button:
Vec2::splat(ui.spacing().button_padding.x) // <--- this line
} else {
Vec2::ZERO
};
...
```

**To Reproduce**
Steps to reproduce the behavior:
1. Change the `ui.style_mut().spacing.button_padding` to a Vec2 with different X & Y values.
2. Create an ImageButton.
3. Your ImageButton's padding will only use the value in X.

**Expected behavior**
ImageButton padding's Y value can be set independently of its X value.

**Screenshots**
![image](https://github.com/emilk/egui/assets/1495255/b6a65eae-55bf-4369-aa89-b4990c9304b9)
A = `ui.style_mut().spacing.button_padding = egui::Vec2::new(2.0, 2.0);`
B = `ui.style_mut().spacing.button_padding = egui::Vec2::new(8.0, 2.0);`
C = `ui.style_mut().spacing.button_padding = egui::Vec2::new(2.0, 8.0);`
D = `ui.style_mut().spacing.button_padding = egui::Vec2::new(2.0, 2.0);`

**Desktop (please complete the following information):**
- OS: Windows 10
- Platform: Bevy, using bevy_egui 0.23.0/0.24.0. Also tested on pure egui off master as of 18 Jan 2024

**Additional context**
The fix seems as simple as changing this line:
```rust
Vec2::splat(ui.spacing().button_padding.x)
```
to
```rust
ui.spacing().button_padding
```
But I don't know if there was a specific reason it uses splat now. [Looks like this change was made a few years ago.](https://github.com/emilk/egui/commit/a5a5d6176d6210eed19e9d6e5966d4c04749d9f6)

Contributor guide

Open the contributing guide

Research direction

Start in crates/egui/src/widgets/button.rs at the ImageButton implementation and inspect how button_padding is used for layout. Reproduce the issue with distinct X and Y spacing values, then verify that the ImageButton responds independently to both axes.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
frontend
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.