1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0

use super::{Builder, Style};
use crate::{
    widget::{self, image::Handle},
    Element,
};
use iced_core::{font::Weight, widget::Id, Length, Padding};
use std::borrow::Cow;

pub type Button<'a, Message> = Builder<'a, Message, Image<'a, Handle, Message>>;

/// A button constructed from an image handle, using image button styling.
pub fn image<'a, Message>(handle: impl Into<Handle> + 'a) -> Button<'a, Message> {
    Button::new(Image {
        image: widget::image(handle).border_radius([9.0; 4]),
        selected: false,
        on_remove: None,
    })
}

/// The image variant of a button.
pub struct Image<'a, Handle, Message> {
    image: widget::Image<'a, Handle>,
    selected: bool,
    on_remove: Option<Message>,
}

impl<'a, Message> Button<'a, Message> {
    pub fn new(variant: Image<'a, Handle, Message>) -> Self {
        Self {
            id: Id::unique(),
            label: Cow::Borrowed(""),
            tooltip: Cow::Borrowed(""),
            on_press: None,
            width: Length::Shrink,
            height: Length::Shrink,
            padding: Padding::from(0),
            spacing: 0,
            icon_size: 16,
            line_height: 20,
            font_size: 14,
            font_weight: Weight::Normal,
            class: crate::theme::style::Button::Image,
            variant,
        }
    }

    pub fn on_remove(mut self, message: Message) -> Self {
        self.variant.on_remove = Some(message);
        self
    }

    pub fn on_remove_maybe(mut self, message: Option<Message>) -> Self {
        self.variant.on_remove = message;
        self
    }

    pub fn selected(mut self, selected: bool) -> Self {
        self.variant.selected = selected;
        self
    }
}

impl<'a, Message> From<Button<'a, Message>> for Element<'a, Message>
where
    Handle: Clone + std::hash::Hash,
    Message: Clone + 'static,
{
    fn from(builder: Button<'a, Message>) -> Element<'a, Message> {
        let content = builder
            .variant
            .image
            .width(builder.width)
            .height(builder.height);

        super::custom_image_button(content, builder.variant.on_remove)
            .padding(0)
            .selected(builder.variant.selected)
            .id(builder.id)
            .on_press_maybe(builder.on_press)
            .class(builder.class)
            .into()
    }
}