Skip to content

Commit

Permalink
Convenience const fn for Margin, Rounding and Shadow (#4080)
Browse files Browse the repository at this point in the history
I often write constants at the top of my widget files, as a "config". I
kept writing stuff like that :
```rust
const DEFAULT_INNER_MARGIN: Margin = Margin { left: 17., right: 17., top: 7., bottom: 7. };
```
So I prefixed constructors for `Margin`, `Rounding` and `Shadow` const.
No code was changed.

I also added a `Shadow::new()` for similar reasons.
  • Loading branch information
0Qwel authored Feb 21, 2024
1 parent cdb7d15 commit 23e8312
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
8 changes: 4 additions & 4 deletions crates/egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ impl Margin {
};

#[inline]
pub fn same(margin: f32) -> Self {
pub const fn same(margin: f32) -> Self {
Self {
left: margin,
right: margin,
Expand All @@ -633,7 +633,7 @@ impl Margin {

/// Margins with the same size on opposing sides
#[inline]
pub fn symmetric(x: f32, y: f32) -> Self {
pub const fn symmetric(x: f32, y: f32) -> Self {
Self {
left: x,
right: x,
Expand All @@ -649,12 +649,12 @@ impl Margin {
}

#[inline]
pub fn left_top(&self) -> Vec2 {
pub const fn left_top(&self) -> Vec2 {
vec2(self.left, self.top)
}

#[inline]
pub fn right_bottom(&self) -> Vec2 {
pub const fn right_bottom(&self) -> Vec2 {
vec2(self.right, self.bottom)
}

Expand Down
12 changes: 8 additions & 4 deletions crates/epaint/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,36 @@ impl Shadow {
color: Color32::TRANSPARENT,
};

pub const fn new(extrusion: f32, color: Color32) -> Self {
Self { extrusion, color }
}

/// Tooltips, menus, …, for dark mode.
pub fn small_dark() -> Self {
pub const fn small_dark() -> Self {
Self {
extrusion: 16.0,
color: Color32::from_black_alpha(96),
}
}

/// Tooltips, menus, …, for light mode.
pub fn small_light() -> Self {
pub const fn small_light() -> Self {
Self {
extrusion: 16.0,
color: Color32::from_black_alpha(20),
}
}

/// Used for egui windows in dark mode.
pub fn big_dark() -> Self {
pub const fn big_dark() -> Self {
Self {
extrusion: 32.0,
color: Color32::from_black_alpha(96),
}
}

/// Used for egui windows in light mode.
pub fn big_light() -> Self {
pub const fn big_light() -> Self {
Self {
extrusion: 32.0,
color: Color32::from_black_alpha(16),
Expand Down
2 changes: 1 addition & 1 deletion crates/epaint/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ impl Rounding {
};

#[inline]
pub fn same(radius: f32) -> Self {
pub const fn same(radius: f32) -> Self {
Self {
nw: radius,
ne: radius,
Expand Down

0 comments on commit 23e8312

Please sign in to comment.