Add support for optional icon to Button
(#3479)
This PR extends `Button` with support for an optional icon to be displayed next to the label. As part of this, the functionality for displaying an icon within a button has been factored out into an internal `ButtonIcon` component. `ButtonIcon` is now used by both `IconButton` and `Button` to encapsulate the concerns of an icon that is rendered within a button. Release Notes: - N/A
This commit is contained in:
parent
4b23c5c658
commit
c3e7732eab
5 changed files with 157 additions and 25 deletions
84
crates/ui2/src/components/button/button_icon.rs
Normal file
84
crates/ui2/src/components/button/button_icon.rs
Normal file
|
@ -0,0 +1,84 @@
|
|||
use crate::{prelude::*, Icon, IconElement, IconSize};
|
||||
|
||||
/// An icon that appears within a button.
|
||||
///
|
||||
/// Can be used as either an icon alongside a label, like in [`Button`](crate::Button),
|
||||
/// or as a standalone icon, like in [`IconButton`](crate::IconButton).
|
||||
#[derive(IntoElement)]
|
||||
pub(super) struct ButtonIcon {
|
||||
icon: Icon,
|
||||
size: IconSize,
|
||||
color: Color,
|
||||
disabled: bool,
|
||||
selected: bool,
|
||||
selected_icon: Option<Icon>,
|
||||
}
|
||||
|
||||
impl ButtonIcon {
|
||||
pub fn new(icon: Icon) -> Self {
|
||||
Self {
|
||||
icon,
|
||||
size: IconSize::default(),
|
||||
color: Color::default(),
|
||||
disabled: false,
|
||||
selected: false,
|
||||
selected_icon: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn size(mut self, size: impl Into<Option<IconSize>>) -> Self {
|
||||
if let Some(size) = size.into() {
|
||||
self.size = size;
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
pub fn color(mut self, color: impl Into<Option<Color>>) -> Self {
|
||||
if let Some(color) = color.into() {
|
||||
self.color = color;
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
pub fn selected_icon(mut self, icon: impl Into<Option<Icon>>) -> Self {
|
||||
self.selected_icon = icon.into();
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Disableable for ButtonIcon {
|
||||
fn disabled(mut self, disabled: bool) -> Self {
|
||||
self.disabled = disabled;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Selectable for ButtonIcon {
|
||||
fn selected(mut self, selected: bool) -> Self {
|
||||
self.selected = selected;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderOnce for ButtonIcon {
|
||||
type Rendered = IconElement;
|
||||
|
||||
fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
|
||||
let icon = self
|
||||
.selected_icon
|
||||
.filter(|_| self.selected)
|
||||
.unwrap_or(self.icon);
|
||||
|
||||
let icon_color = if self.disabled {
|
||||
Color::Disabled
|
||||
} else if self.selected {
|
||||
Color::Selected
|
||||
} else {
|
||||
self.color
|
||||
};
|
||||
|
||||
IconElement::new(icon).size(self.size).color(icon_color)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue