Skip to content

Commit

Permalink
Add rounding fn to Button, to enable rounded buttons (emilk#2616)
Browse files Browse the repository at this point in the history
  • Loading branch information
Weasy666 authored and lictex committed Jan 23, 2023
1 parent cea4f8c commit a474a7d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG
* Add `ScrollArea::drag_to_scroll` if you want to turn off that feature.
* Add `Response::on_hover_and_drag_cursor`.
* Add `Window::default_open` ([#2539](https://github.com/emilk/egui/pull/2539))
* Add `Button::rounding` to enable round buttons ([#2539](https://github.com/emilk/egui/pull/2539))

### Changed 🔧
* `Frame::paint` now takes an extra `&Context` argument ([#2564](https://github.com/emilk/egui/pull/2564)).
Expand Down
18 changes: 12 additions & 6 deletions crates/egui/src/widgets/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct Button {
small: bool,
frame: Option<bool>,
min_size: Vec2,
rounding: Option<Rounding>,
image: Option<widgets::Image>,
}

Expand All @@ -45,6 +46,7 @@ impl Button {
small: false,
frame: None,
min_size: Vec2::ZERO,
rounding: None,
image: None,
}
}
Expand Down Expand Up @@ -117,6 +119,12 @@ impl Button {
self
}

/// Set the rounding of the button.
pub fn rounding(mut self, rounding: impl Into<Rounding>) -> Self {
self.rounding = Some(rounding.into());
self
}

/// Show some text on the right side of the button, in weak color.
///
/// Designed for menu buttons, for setting a keyboard shortcut text (e.g. `Ctrl+S`).
Expand All @@ -140,6 +148,7 @@ impl Widget for Button {
small,
frame,
min_size,
rounding,
image,
} = self;

Expand Down Expand Up @@ -186,12 +195,9 @@ impl Widget for Button {
if frame {
let fill = fill.unwrap_or(visuals.bg_fill);
let stroke = stroke.unwrap_or(visuals.bg_stroke);
ui.painter().rect(
rect.expand(visuals.expansion),
visuals.rounding,
fill,
stroke,
);
let rounding = rounding.unwrap_or(visuals.rounding);
ui.painter()
.rect(rect.expand(visuals.expansion), rounding, fill, stroke);
}

let text_pos = if let Some(image) = image {
Expand Down

0 comments on commit a474a7d

Please sign in to comment.