From 487c8f1dda9d572731b2b656de9fa277e90b6ea7 Mon Sep 17 00:00:00 2001 From: Lucas Meurer Date: Tue, 25 Aug 2026 09:27:05 +0200 Subject: [PATCH] Let the theme set a button's atom gap `Button` hard-coded `spacing.icon_spacing` as the gap between its icon and its text. A theme that gives a button its own metrics wants to set that gap too, so it becomes a `ButtonStyle` field, overridden by `Button::gap`. Co-Authored-By: Claude Opus 5 (1M context) --- crates/egui/src/atomics/atom_layout.rs | 7 +++++++ crates/egui/src/theme/default_style.rs | 1 + crates/egui/src/widget_style/mod.rs | 5 +++++ crates/egui/src/widgets/button.rs | 3 +++ examples/styling_engine/src/main.rs | 1 + 5 files changed, 17 insertions(+) diff --git a/crates/egui/src/atomics/atom_layout.rs b/crates/egui/src/atomics/atom_layout.rs index 5c7da5cc7..3bc5211b1 100644 --- a/crates/egui/src/atomics/atom_layout.rs +++ b/crates/egui/src/atomics/atom_layout.rs @@ -107,6 +107,13 @@ impl<'a> AtomLayout<'a> { self } + /// Set the gap between atoms, unless [`Self::gap`] already set one. + #[inline] + pub fn fallback_gap(mut self, gap: f32) -> Self { + self.gap.get_or_insert(gap); + self + } + /// Set the [`Frame`]. #[inline] pub fn frame(mut self, frame: Frame) -> Self { diff --git a/crates/egui/src/theme/default_style.rs b/crates/egui/src/theme/default_style.rs index d27ba0a66..f68ae63eb 100644 --- a/crates/egui/src/theme/default_style.rs +++ b/crates/egui/src/theme/default_style.rs @@ -91,6 +91,7 @@ impl StyleProvider for DefaultStyle { // Historically only the height was floored, so that a button is at least as tall as // any other interactive widget on the same row. min_size: Vec2::new(0.0, spacing.interact_size.y), + gap: spacing.icon_spacing, } } } diff --git a/crates/egui/src/widget_style/mod.rs b/crates/egui/src/widget_style/mod.rs index ff699ec35..ea2e234ab 100644 --- a/crates/egui/src/widget_style/mod.rs +++ b/crates/egui/src/widget_style/mod.rs @@ -58,6 +58,11 @@ pub struct ButtonStyle { /// Ignored by a [`crate::Button::small`] button, which sizes itself purely from its contents /// and its own [`crate::Button::min_size`]. pub min_size: Vec2, + + /// The gap between the button's atoms, e.g. between its icon and its text. + /// + /// Overridden by [`crate::Button::gap`]. + pub gap: f32, } impl WidgetStyle for ButtonStyle {} diff --git a/crates/egui/src/widgets/button.rs b/crates/egui/src/widgets/button.rs index 88195518d..29cf68eed 100644 --- a/crates/egui/src/widgets/button.rs +++ b/crates/egui/src/widgets/button.rs @@ -327,8 +327,11 @@ impl<'a> Button<'a> { frame, text_style, min_size: style_min_size, + gap, } = ui.widget_style(id, &classes); + layout = layout.fallback_gap(gap); + // The theme decides how small a button may get — unless it is a `small` one, which sizes // itself purely from its contents. if !small { diff --git a/examples/styling_engine/src/main.rs b/examples/styling_engine/src/main.rs index 677fc3dd9..275cc59fc 100644 --- a/examples/styling_engine/src/main.rs +++ b/examples/styling_engine/src/main.rs @@ -69,6 +69,7 @@ impl StyleProvider for MyTheme { .inner_margin(8), text_style: base.text, min_size: args.style.spacing.interact_size, + gap: args.style.spacing.icon_spacing, } } }