summaryrefslogtreecommitdiff
path: root/schist_desktop_gui/src/gui/components/button.rs
blob: c531e32aee8717e6c0378f5433d8609ab3073046 (plain)
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
use iced::{Element, Theme, border::Radius, widget::Button};

use crate::style::*;

pub fn button<'a, Content, Message>(
    content: &'a Content,
    message: Message,
    focus: bool,
) -> Button<'a, Message>
where
    Element<'a, Message>: From<&'a Content>,
{
    iced::widget::button(content)
        .on_press(message)
        .style(move |theme, status| match (status, focus) {
            (iced::widget::button::Status::Hovered, _) => hovered_button_style(theme),
            (iced::widget::button::Status::Pressed, _) | (_, true) => focused_button_style(theme),
            (iced::widget::button::Status::Active, false) => base_button_style(theme),
            (iced::widget::button::Status::Disabled, false) => todo!(),
        })
        .width(iced::Length::Fill)
}

fn hovered_button_style(theme: &Theme) -> iced::widget::button::Style {
    iced::widget::button::Style {
        background: Some(iced::Background::Color(
            theme.extended_palette().background.weak.color,
        )),
        text_color: theme.extended_palette().primary.base.text,
        ..base_button_style(theme)
    }
}

fn focused_button_style(theme: &Theme) -> iced::widget::button::Style {
    iced::widget::button::Style {
        background: Some(iced::Background::Color(
            theme.extended_palette().primary.base.text,
        )),
        text_color: theme.extended_palette().primary.base.color,
        ..base_button_style(theme)
    }
}

fn base_button_style(theme: &Theme) -> iced::widget::button::Style {
    iced::widget::button::Style {
        background: Some(iced::Background::Color(
            theme.extended_palette().primary.base.color,
        )),
        border: iced::Border {
            color: theme.extended_palette().primary.base.text,
            radius: Radius::new(u32::from(BORDER_RADIUS)),
            width: f32::from(BORDER_WIDTH),
        },
        text_color: theme.extended_palette().primary.base.text,
        ..Default::default()
    }
}

pub fn panel_button<'a, Content: Into<Element<'a, Message>>, Message>(
    content: Content,
    on_press: Message,
) -> Button<'a, Message> {
    iced::widget::button(content)
        .on_press(on_press)
        .style(|theme: &iced::Theme, _status| iced::widget::button::Style {
            background: None,
            border: iced::Border {
                color: theme.extended_palette().primary.base.text,
                radius: iced::border::Radius::new(0),
                width: f32::from(BORDER_WIDTH_THICK),
            },
            ..Default::default()
        })
        .width(iced::Length::Fill)
        .padding(iced::Padding::new(SPACING_MD.into()))
}