1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 22:30:03 -04:00

Button resize fix + Margin i8 -> i16

This commit is contained in:
Adrien Zianne
2025-10-21 11:06:39 +02:00
committed by adrien
parent 509d17d539
commit 2df6154d9d
3 changed files with 69 additions and 32 deletions

View File

@@ -2600,8 +2600,8 @@ impl Widget for &mut Margin {
} else { } else {
// Make sure it is not same: // Make sure it is not same:
if self.is_same() { if self.is_same() {
if self.right == i8::MAX { if self.right == i16::MAX {
self.right = i8::MAX - 1; self.right = i16::MAX - 1;
} else { } else {
self.right += 1; self.right += 1;
} }

View File

@@ -1,5 +1,7 @@
use std::sync::Arc; use std::sync::Arc;
use epaint::Margin;
use crate::{ use crate::{
Atom, AtomExt as _, AtomKind, AtomLayout, AtomLayoutResponse, Color32, CornerRadius, Image, Atom, AtomExt as _, AtomKind, AtomLayout, AtomLayoutResponse, Color32, CornerRadius, Image,
IntoAtoms, NumExt as _, Response, RichText, Sense, Stroke, TextStyle, TextWrapMode, Ui, Vec2, IntoAtoms, NumExt as _, Response, RichText, Sense, Stroke, TextStyle, TextWrapMode, Ui, Vec2,
@@ -255,14 +257,14 @@ impl<'a> Button<'a> {
pub fn atom_ui(self, ui: &mut Ui) -> AtomLayoutResponse { pub fn atom_ui(self, ui: &mut Ui) -> AtomLayoutResponse {
let Button { let Button {
mut layout, mut layout,
fill: _, fill,
stroke: _, stroke,
small, small,
frame, frame,
frame_when_inactive, frame_when_inactive,
mut min_size, mut min_size,
corner_radius: _, corner_radius,
selected: _, selected,
image_tint_follows_text_color, image_tint_follows_text_color,
limit_image_size, limit_image_size,
} = self; } = self;
@@ -284,14 +286,49 @@ impl<'a> Button<'a> {
let text = layout.text().map(String::from); let text = layout.text().map(String::from);
// Get the widget style by reading the response from the previous pass let state = if selected {
let id = ui.next_auto_id(); // If selected is true then the state is active
let response: Option<Response> = ui.ctx().read_response(id); WidgetState::Active
let state = response.map(|r| r.widget_state()).unwrap_or_default(); } else {
// Get the widget state by reading the response from the previous pass
let id = ui.next_auto_id();
let response: Option<Response> = ui.ctx().read_response(id);
response.map(|r| r.widget_state()).unwrap_or_default()
};
let style = ui.style().button_style(state); let style = ui.style().button_style(state);
let has_frame_margin = frame.unwrap_or_else(|| ui.visuals().button_frame); let has_frame_margin = frame.unwrap_or_else(|| ui.visuals().button_frame);
let mut button_padding = if has_frame_margin {
style.frame.inner_margin
} else {
Margin::ZERO
};
if small {
button_padding.bottom = 0;
button_padding.top = 0;
}
// Override global style by local style
let mut frame = style.frame;
if let Some(fill) = fill {
frame = frame.fill(fill);
}
if let Some(corner_radius) = corner_radius {
frame = frame.corner_radius(corner_radius);
}
if let Some(stroke) = stroke {
frame = frame.stroke(stroke);
}
frame = frame.inner_margin(Margin {
left: button_padding.left - frame.stroke.width as i16,
top: button_padding.top - frame.stroke.width as i16,
right: button_padding.right - frame.stroke.width as i16,
bottom: button_padding.bottom - frame.stroke.width as i16,
});
// Apply the correct font and color if Text // Apply the correct font and color if Text
// We assume that the other WidgetText have already a Fontid and color // We assume that the other WidgetText have already a Fontid and color
layout.map_texts(|t| match t { layout.map_texts(|t| match t {
@@ -307,7 +344,7 @@ impl<'a> Button<'a> {
// Retrocompatibility with button settings // Retrocompatibility with button settings
let mut prepared = let mut prepared =
if has_frame_margin && (state != WidgetState::Inactive || frame_when_inactive) { if has_frame_margin && (state != WidgetState::Inactive || frame_when_inactive) {
layout.frame(style.frame).min_size(min_size).allocate(ui) layout.frame(frame).min_size(min_size).allocate(ui)
} else { } else {
layout.min_size(min_size).allocate(ui) layout.min_size(min_size).allocate(ui)
}; };

View File

@@ -8,15 +8,15 @@ use emath::{Rect, Vec2, vec2};
/// Negative margins are possible, but may produce weird behavior. /// Negative margins are possible, but may produce weird behavior.
/// Use with care. /// Use with care.
/// ///
/// All values are stored as [`i8`] to keep the size of [`Margin`] small. /// All values are stored as [`i16`] to keep the size of [`Margin`] small.
/// If you want floats, use [`crate::MarginF32`] instead. /// If you want floats, use [`crate::MarginF32`] instead.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Margin { pub struct Margin {
pub left: i8, pub left: i16,
pub right: i8, pub right: i16,
pub top: i8, pub top: i16,
pub bottom: i8, pub bottom: i16,
} }
impl Margin { impl Margin {
@@ -30,7 +30,7 @@ impl Margin {
/// The same margin on every side. /// The same margin on every side.
#[doc(alias = "symmetric")] #[doc(alias = "symmetric")]
#[inline] #[inline]
pub const fn same(margin: i8) -> Self { pub const fn same(margin: i16) -> Self {
Self { Self {
left: margin, left: margin,
right: margin, right: margin,
@@ -41,7 +41,7 @@ impl Margin {
/// Margins with the same size on opposing sides /// Margins with the same size on opposing sides
#[inline] #[inline]
pub const fn symmetric(x: i8, y: i8) -> Self { pub const fn symmetric(x: i16, y: i16) -> Self {
Self { Self {
left: x, left: x,
right: x, right: x,
@@ -98,9 +98,9 @@ impl Margin {
} }
} }
impl From<i8> for Margin { impl From<i16> for Margin {
#[inline] #[inline]
fn from(v: i8) -> Self { fn from(v: i16) -> Self {
Self::same(v) Self::same(v)
} }
} }
@@ -134,12 +134,12 @@ impl std::ops::Add for Margin {
} }
} }
/// `Margin + i8` /// `Margin + i16`
impl std::ops::Add<i8> for Margin { impl std::ops::Add<i16> for Margin {
type Output = Self; type Output = Self;
#[inline] #[inline]
fn add(self, v: i8) -> Self { fn add(self, v: i16) -> Self {
Self { Self {
left: self.left.saturating_add(v), left: self.left.saturating_add(v),
right: self.right.saturating_add(v), right: self.right.saturating_add(v),
@@ -149,10 +149,10 @@ impl std::ops::Add<i8> for Margin {
} }
} }
/// `Margin += i8` /// `Margin += i16`
impl std::ops::AddAssign<i8> for Margin { impl std::ops::AddAssign<i16> for Margin {
#[inline] #[inline]
fn add_assign(&mut self, v: i8) { fn add_assign(&mut self, v: i16) {
*self = *self + v; *self = *self + v;
} }
} }
@@ -214,12 +214,12 @@ impl std::ops::Sub for Margin {
} }
} }
/// `Margin - i8` /// `Margin - i16`
impl std::ops::Sub<i8> for Margin { impl std::ops::Sub<i16> for Margin {
type Output = Self; type Output = Self;
#[inline] #[inline]
fn sub(self, v: i8) -> Self { fn sub(self, v: i16) -> Self {
Self { Self {
left: self.left.saturating_sub(v), left: self.left.saturating_sub(v),
right: self.right.saturating_sub(v), right: self.right.saturating_sub(v),
@@ -229,10 +229,10 @@ impl std::ops::Sub<i8> for Margin {
} }
} }
/// `Margin -= i8` /// `Margin -= i16`
impl std::ops::SubAssign<i8> for Margin { impl std::ops::SubAssign<i16> for Margin {
#[inline] #[inline]
fn sub_assign(&mut self, v: i8) { fn sub_assign(&mut self, v: i16) {
*self = *self - v; *self = *self - v;
} }
} }