refactor(widget): improvements to button and icon widgets

This commit is contained in:
Michael Aaron Murphy 2023-09-13 15:47:32 +02:00 committed by Michael Murphy
parent 7f0943924a
commit 9dbc1be269
20 changed files with 399 additions and 558 deletions

View file

@ -1,8 +1,11 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
pub mod hyperlink;
pub use hyperlink::Button as LinkButton;
pub use crate::theme::Button as Style;
pub mod link;
pub use link::link;
pub use link::Button as LinkButton;
mod icon;
pub use icon::icon;
@ -18,7 +21,6 @@ pub use text::{destructive, standard, suggested, text};
mod widget;
pub use widget::{draw, focus, layout, mouse_interaction, Button};
pub use crate::theme::Button as Style;
use crate::Element;
use iced_core::font::Weight;
use iced_core::widget::Id;
@ -35,7 +37,7 @@ pub fn button<'a, Message>(
pub struct Builder<'a, Message, Variant> {
id: Id,
label: Cow<'a, str>,
// tooltip: Cow<'a, str>,
tooltip: Cow<'a, str>,
on_press: Option<Message>,
width: Length,
height: Length,
@ -49,13 +51,8 @@ pub struct Builder<'a, Message, Variant> {
variant: Variant,
}
// /// A [`Button`] with an icon, which may be used in place of text.
// pub const fn icon<'a>(selected: bool) -> Button<'a> {
// Builder::new(Standard::Icon { selected })
// }
impl<'a, Message, Variant> Builder<'a, Message, Variant> {
/// Sets the [`Id`] of the [`Button`].
/// Sets the [`Id`] of the button.
pub fn id(mut self, id: Id) -> Self {
self.id = id;
self
@ -66,48 +63,50 @@ impl<'a, Message, Variant> Builder<'a, Message, Variant> {
self
}
/// Sets the width of the [`Button`].
/// Sets the width of the button.
pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width.into();
self
}
/// Sets the height of the [`Button`].
/// Sets the height of the button.
pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height.into();
self
}
/// Sets the [`Padding`] of the [`Button`].
/// Sets the [`Padding`] of the button.
pub fn padding<P: Into<Padding>>(mut self, padding: P) -> Self {
self.padding = padding.into();
self
}
/// Sets the message that will be produced when the [`Button`] is pressed.
/// Sets the message that will be produced when the button is pressed.
///
/// Unless `on_press` is called, the [`Button`] will be disabled.
/// Unless `on_press` is called, the button will be disabled.
pub fn on_press(mut self, on_press: Message) -> Self {
self.on_press = Some(on_press);
self
}
/// Sets the message that will be produced when the [`Button`] is pressed,
/// Sets the message that will be produced when the button is pressed,
/// if `Some`.
///
/// If `None`, the [`Button`] will be disabled.
/// If `None`, the button will be disabled.
pub fn on_press_maybe(mut self, on_press: Option<Message>) -> Self {
self.on_press = on_press;
self
}
/// Overrides the preferred style of the button.
pub fn style(mut self, style: Style) -> Self {
self.style = style;
self
}
pub fn tooltip(mut self, label: impl Into<Cow<'a, str>>) -> Self {
self.label = label.into();
/// Adds a tooltip to the button.
pub fn tooltip(mut self, tooltip: impl Into<Cow<'a, str>>) -> Self {
self.tooltip = tooltip.into();
self
}
}