2023-01-03 19:35:34 +01:00
|
|
|
// Copyright 2023 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
|
|
use crate::theme::Theme;
|
|
|
|
|
use crate::widget::segmented_button;
|
2023-01-04 05:37:20 +01:00
|
|
|
use iced_core::{Background, BorderRadius};
|
2023-01-09 16:18:02 +01:00
|
|
|
use palette::{rgb::Rgb, Alpha};
|
2023-01-03 19:35:34 +01:00
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Default)]
|
|
|
|
|
pub enum SegmentedButton {
|
|
|
|
|
/// A tabbed widget for switching between views in an interface.
|
|
|
|
|
#[default]
|
|
|
|
|
ViewSwitcher,
|
|
|
|
|
/// A widget for multiple choice selection.
|
|
|
|
|
Selection,
|
|
|
|
|
/// Or implement any custom theme of your liking.
|
|
|
|
|
Custom(fn(&Theme) -> segmented_button::Appearance),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl segmented_button::StyleSheet for Theme {
|
|
|
|
|
type Style = SegmentedButton;
|
|
|
|
|
|
2023-01-06 01:40:21 +01:00
|
|
|
#[allow(clippy::too_many_lines)]
|
2023-01-03 19:35:34 +01:00
|
|
|
fn horizontal(&self, style: &Self::Style) -> segmented_button::Appearance {
|
|
|
|
|
match style {
|
|
|
|
|
SegmentedButton::ViewSwitcher => {
|
|
|
|
|
let cosmic = self.cosmic();
|
2023-01-09 16:18:02 +01:00
|
|
|
let active = horizontal::view_switcher_active(cosmic);
|
2023-01-03 19:35:34 +01:00
|
|
|
segmented_button::Appearance {
|
|
|
|
|
border_radius: BorderRadius::from(0.0),
|
2023-01-04 05:37:20 +01:00
|
|
|
inactive: segmented_button::ButtonStatusAppearance {
|
2023-01-03 19:35:34 +01:00
|
|
|
background: None,
|
2023-01-04 05:37:20 +01:00
|
|
|
first: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from(0.0),
|
|
|
|
|
border_bottom: Some((1.0, cosmic.accent.base.into())),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
middle: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from(0.0),
|
|
|
|
|
border_bottom: Some((1.0, cosmic.accent.base.into())),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
last: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from(0.0),
|
|
|
|
|
border_bottom: Some((1.0, cosmic.accent.base.into())),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
2023-01-03 19:35:34 +01:00
|
|
|
text_color: cosmic.primary.on.into(),
|
|
|
|
|
},
|
2023-01-09 16:18:02 +01:00
|
|
|
hover: hover(cosmic, &active),
|
|
|
|
|
focus: focus(cosmic, &active),
|
|
|
|
|
active,
|
2023-01-04 05:37:20 +01:00
|
|
|
..Default::default()
|
2023-01-03 19:35:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SegmentedButton::Selection => {
|
|
|
|
|
let cosmic = self.cosmic();
|
2023-01-09 16:18:02 +01:00
|
|
|
let active = horizontal::selection_active(cosmic);
|
2023-01-03 19:35:34 +01:00
|
|
|
segmented_button::Appearance {
|
|
|
|
|
border_radius: BorderRadius::from(0.0),
|
2023-01-04 05:37:20 +01:00
|
|
|
inactive: segmented_button::ButtonStatusAppearance {
|
2023-01-03 19:35:34 +01:00
|
|
|
background: Some(Background::Color(cosmic.secondary.component.base.into())),
|
2023-01-04 05:37:20 +01:00
|
|
|
first: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from([24.0, 0.0, 0.0, 24.0]),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
middle: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from(0.0),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
last: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from([0.0, 24.0, 24.0, 0.0]),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
2023-01-03 19:35:34 +01:00
|
|
|
text_color: cosmic.primary.on.into(),
|
|
|
|
|
},
|
2023-01-09 16:18:02 +01:00
|
|
|
hover: hover(cosmic, &active),
|
|
|
|
|
focus: focus(cosmic, &active),
|
|
|
|
|
active,
|
2023-01-04 05:37:20 +01:00
|
|
|
..Default::default()
|
2023-01-03 19:35:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SegmentedButton::Custom(func) => func(self),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 01:40:21 +01:00
|
|
|
#[allow(clippy::too_many_lines)]
|
2023-01-03 19:35:34 +01:00
|
|
|
fn vertical(&self, style: &Self::Style) -> segmented_button::Appearance {
|
|
|
|
|
match style {
|
|
|
|
|
SegmentedButton::ViewSwitcher => {
|
|
|
|
|
let cosmic = self.cosmic();
|
2023-01-09 16:18:02 +01:00
|
|
|
let active = vertical::view_switcher_active(cosmic);
|
2023-01-03 19:35:34 +01:00
|
|
|
segmented_button::Appearance {
|
|
|
|
|
border_radius: BorderRadius::from(0.0),
|
2023-01-04 05:37:20 +01:00
|
|
|
inactive: segmented_button::ButtonStatusAppearance {
|
2023-01-03 19:35:34 +01:00
|
|
|
background: None,
|
|
|
|
|
text_color: cosmic.primary.on.into(),
|
2023-01-09 16:18:02 +01:00
|
|
|
..active
|
2023-01-03 19:35:34 +01:00
|
|
|
},
|
2023-01-09 16:18:02 +01:00
|
|
|
hover: hover(cosmic, &active),
|
|
|
|
|
focus: focus(cosmic, &active),
|
|
|
|
|
active,
|
2023-01-04 05:37:20 +01:00
|
|
|
..Default::default()
|
2023-01-03 19:35:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SegmentedButton::Selection => {
|
|
|
|
|
let cosmic = self.cosmic();
|
2023-01-09 16:18:02 +01:00
|
|
|
let active = vertical::selection_active(cosmic);
|
2023-01-03 19:35:34 +01:00
|
|
|
segmented_button::Appearance {
|
|
|
|
|
border_radius: BorderRadius::from(0.0),
|
2023-01-04 05:37:20 +01:00
|
|
|
inactive: segmented_button::ButtonStatusAppearance {
|
2023-01-03 19:35:34 +01:00
|
|
|
background: Some(Background::Color(cosmic.secondary.component.base.into())),
|
2023-01-04 05:37:20 +01:00
|
|
|
first: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from([24.0, 24.0, 0.0, 0.0]),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
middle: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from(0.0),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
last: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from([0.0, 0.0, 24.0, 24.0]),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
2023-01-03 19:35:34 +01:00
|
|
|
text_color: cosmic.primary.on.into(),
|
|
|
|
|
},
|
2023-01-09 16:18:02 +01:00
|
|
|
hover: hover(cosmic, &active),
|
|
|
|
|
focus: focus(cosmic, &active),
|
|
|
|
|
active,
|
2023-01-04 05:37:20 +01:00
|
|
|
..Default::default()
|
2023-01-03 19:35:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SegmentedButton::Custom(func) => func(self),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-09 16:18:02 +01:00
|
|
|
|
|
|
|
|
mod horizontal {
|
|
|
|
|
use crate::widget::segmented_button;
|
|
|
|
|
use iced_core::{Background, BorderRadius};
|
|
|
|
|
use palette::{rgb::Rgb, Alpha};
|
|
|
|
|
|
|
|
|
|
pub fn selection_active(
|
|
|
|
|
cosmic: &cosmic_theme::Theme<Alpha<Rgb, f32>>,
|
|
|
|
|
) -> segmented_button::ButtonStatusAppearance {
|
|
|
|
|
segmented_button::ButtonStatusAppearance {
|
|
|
|
|
background: Some(Background::Color(cosmic.secondary.component.divider.into())),
|
|
|
|
|
first: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from([24.0, 0.0, 0.0, 24.0]),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
middle: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from(0.0),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
last: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from([0.0, 24.0, 24.0, 0.0]),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
text_color: cosmic.accent.base.into(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn view_switcher_active(
|
|
|
|
|
cosmic: &cosmic_theme::Theme<Alpha<Rgb, f32>>,
|
|
|
|
|
) -> segmented_button::ButtonStatusAppearance {
|
|
|
|
|
segmented_button::ButtonStatusAppearance {
|
|
|
|
|
background: Some(Background::Color(cosmic.primary.component.base.into())),
|
|
|
|
|
first: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from([8.0, 8.0, 0.0, 0.0]),
|
|
|
|
|
border_bottom: Some((4.0, cosmic.accent.base.into())),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
middle: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from([8.0, 8.0, 0.0, 0.0]),
|
|
|
|
|
border_bottom: Some((4.0, cosmic.accent.base.into())),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
last: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from([8.0, 8.0, 0.0, 0.0]),
|
|
|
|
|
border_bottom: Some((4.0, cosmic.accent.base.into())),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
text_color: cosmic.accent.base.into(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn focus(
|
|
|
|
|
cosmic: &cosmic_theme::Theme<Alpha<Rgb, f32>>,
|
|
|
|
|
default: &segmented_button::ButtonStatusAppearance,
|
|
|
|
|
) -> segmented_button::ButtonStatusAppearance {
|
|
|
|
|
segmented_button::ButtonStatusAppearance {
|
|
|
|
|
background: Some(Background::Color(cosmic.primary.component.focus.into())),
|
|
|
|
|
text_color: cosmic.primary.base.into(),
|
|
|
|
|
..*default
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn hover(
|
|
|
|
|
cosmic: &cosmic_theme::Theme<Alpha<Rgb, f32>>,
|
|
|
|
|
default: &segmented_button::ButtonStatusAppearance,
|
|
|
|
|
) -> segmented_button::ButtonStatusAppearance {
|
|
|
|
|
segmented_button::ButtonStatusAppearance {
|
|
|
|
|
background: Some(Background::Color(cosmic.primary.component.hover.into())),
|
|
|
|
|
text_color: cosmic.accent.base.into(),
|
|
|
|
|
..*default
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mod vertical {
|
|
|
|
|
use crate::widget::segmented_button;
|
|
|
|
|
use iced_core::{Background, BorderRadius};
|
|
|
|
|
use palette::{rgb::Rgb, Alpha};
|
|
|
|
|
|
|
|
|
|
pub fn selection_active(
|
|
|
|
|
cosmic: &cosmic_theme::Theme<Alpha<Rgb, f32>>,
|
|
|
|
|
) -> segmented_button::ButtonStatusAppearance {
|
|
|
|
|
segmented_button::ButtonStatusAppearance {
|
|
|
|
|
background: Some(Background::Color(cosmic.secondary.component.divider.into())),
|
|
|
|
|
first: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from([24.0, 24.0, 0.0, 0.0]),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
middle: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from(0.0),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
last: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from([0.0, 0.0, 24.0, 24.0]),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
text_color: cosmic.accent.base.into(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn view_switcher_active(
|
|
|
|
|
cosmic: &cosmic_theme::Theme<Alpha<Rgb, f32>>,
|
|
|
|
|
) -> segmented_button::ButtonStatusAppearance {
|
|
|
|
|
segmented_button::ButtonStatusAppearance {
|
|
|
|
|
background: Some(Background::Color(cosmic.secondary.component.divider.into())),
|
|
|
|
|
first: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from(24.0),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
middle: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from(24.0),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
last: segmented_button::ButtonAppearance {
|
|
|
|
|
border_radius: BorderRadius::from(24.0),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
text_color: cosmic.accent.base.into(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|