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

@ -2,7 +2,11 @@
// SPDX-License-Identifier: MPL-2.0
use super::{button, Builder, Style};
use crate::{widget::icon::Handle, Element};
use crate::widget::{
icon::{self, Handle},
tooltip,
};
use crate::Element;
use apply::Apply;
use iced_core::{font::Weight, text::LineHeight, widget::Id, Alignment, Length, Padding};
use std::borrow::Cow;
@ -11,14 +15,12 @@ pub type Button<'a, Message> = Builder<'a, Message, Icon>;
pub struct Icon {
handle: Handle,
selected: bool,
vertical: bool,
}
pub fn icon<Message>(handle: impl Into<Handle>) -> Button<'static, Message> {
pub fn icon<'a, Message>(handle: impl Into<Handle>) -> Button<'a, Message> {
Button::new(Icon {
handle: handle.into(),
selected: false,
vertical: false,
})
}
@ -33,12 +35,13 @@ impl<'a, Message> Button<'a, Message> {
Self {
id: Id::unique(),
label: Cow::Borrowed(""),
tooltip: Cow::Borrowed(""),
on_press: None,
width: Length::Shrink,
height: Length::Fixed(46.0),
padding: Padding::from(padding),
spacing: theme.space_xxxs(),
icon_size: 16,
icon_size: if icon.handle.symbolic { 16 } else { 24 },
line_height: 20,
font_size: 14,
font_weight: Weight::Normal,
@ -91,7 +94,7 @@ impl<'a, Message> Button<'a, Message> {
let theme = theme.cosmic();
self.font_size = 28;
self.font_weight = Weight::Light;
self.font_weight = Weight::Normal;
self.icon_size = 40;
self.line_height = 36;
self.height = Length::Fixed(64.0);
@ -121,16 +124,21 @@ impl<'a, Message> Button<'a, Message> {
self
}
pub fn selected(mut self, selected: bool) -> Self {
self.variant.selected = selected;
pub fn vertical(mut self, vertical: bool) -> Self {
self.variant.vertical = vertical;
self.style = Style::IconVertical;
self
}
}
impl<'a, Message: Clone + 'static> From<Button<'a, Message>> for Element<'a, Message> {
fn from(builder: Button<'a, Message>) -> Element<'a, Message> {
fn from(mut builder: Button<'a, Message>) -> Element<'a, Message> {
let mut content = Vec::with_capacity(2);
if let icon::Data::Name(ref mut named) = builder.variant.handle.data {
named.size = Some(builder.icon_size);
}
content.push(
crate::widget::icon(builder.variant.handle.clone())
.size(builder.icon_size)
@ -154,8 +162,8 @@ impl<'a, Message: Clone + 'static> From<Button<'a, Message>> for Element<'a, Mes
let button = if builder.variant.vertical {
crate::widget::column::with_children(content)
.padding(builder.padding)
.width(builder.width)
.height(builder.height)
// .width(builder.width)
// .height(builder.height)
.spacing(builder.spacing)
.align_items(Alignment::Center)
.apply(button)
@ -169,11 +177,23 @@ impl<'a, Message: Clone + 'static> From<Button<'a, Message>> for Element<'a, Mes
.apply(button)
};
button
let button = button
.padding(0)
.id(builder.id)
.on_press_maybe(builder.on_press)
.style(builder.style)
.into()
.style(builder.style);
if builder.tooltip.is_empty() {
button.into()
} else {
tooltip(button, builder.tooltip, tooltip::Position::Top)
.size(builder.font_size)
.font({
let mut font = crate::font::DEFAULT;
font.weight = builder.font_weight;
font
})
.into()
}
}
}