Simplify theming for Button widget
This commit is contained in:
parent
db92e1c942
commit
f4a4845ddb
16 changed files with 306 additions and 412 deletions
|
|
@ -11,6 +11,19 @@ pub enum Background {
|
||||||
// TODO: Add image variant
|
// TODO: Add image variant
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Background {
|
||||||
|
/// Increases the translucency of the [`Background`]
|
||||||
|
/// by the given factor.
|
||||||
|
pub fn transparentize(self, factor: f32) -> Self {
|
||||||
|
match self {
|
||||||
|
Self::Color(color) => Self::Color(color.transparentize(factor)),
|
||||||
|
Self::Gradient(gradient) => {
|
||||||
|
Self::Gradient(gradient.transparentize(factor))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<Color> for Background {
|
impl From<Color> for Background {
|
||||||
fn from(color: Color) -> Self {
|
fn from(color: Color) -> Self {
|
||||||
Background::Color(color)
|
Background::Color(color)
|
||||||
|
|
|
||||||
|
|
@ -151,6 +151,14 @@ impl Color {
|
||||||
pub fn inverse(self) -> Color {
|
pub fn inverse(self) -> Color {
|
||||||
Color::new(1.0f32 - self.r, 1.0f32 - self.g, 1.0f32 - self.b, self.a)
|
Color::new(1.0f32 - self.r, 1.0f32 - self.g, 1.0f32 - self.b, self.a)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Transparentizes the [`Color`] by the given factor.
|
||||||
|
pub fn transparentize(self, factor: f32) -> Color {
|
||||||
|
Self {
|
||||||
|
a: self.a * factor,
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<[f32; 3]> for Color {
|
impl From<[f32; 3]> for Color {
|
||||||
|
|
|
||||||
|
|
@ -13,11 +13,11 @@ pub enum Gradient {
|
||||||
|
|
||||||
impl Gradient {
|
impl Gradient {
|
||||||
/// Adjust the opacity of the gradient by a multiplier applied to each color stop.
|
/// Adjust the opacity of the gradient by a multiplier applied to each color stop.
|
||||||
pub fn mul_alpha(mut self, alpha_multiplier: f32) -> Self {
|
pub fn transparentize(mut self, factor: f32) -> Self {
|
||||||
match &mut self {
|
match &mut self {
|
||||||
Gradient::Linear(linear) => {
|
Gradient::Linear(linear) => {
|
||||||
for stop in linear.stops.iter_mut().flatten() {
|
for stop in linear.stops.iter_mut().flatten() {
|
||||||
stop.color.a *= alpha_multiplier;
|
stop.color.a *= factor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -290,7 +290,7 @@ fn action<'a, Message: Clone + 'a>(
|
||||||
.style(theme::Container::Box)
|
.style(theme::Container::Box)
|
||||||
.into()
|
.into()
|
||||||
} else {
|
} else {
|
||||||
action.style(theme::Button::Secondary).into()
|
action.style(button::secondary).into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ use grid::Grid;
|
||||||
use preset::Preset;
|
use preset::Preset;
|
||||||
|
|
||||||
use iced::executor;
|
use iced::executor;
|
||||||
use iced::theme::{self, Theme};
|
|
||||||
use iced::time;
|
use iced::time;
|
||||||
use iced::widget::{
|
use iced::widget::{
|
||||||
button, checkbox, column, container, pick_list, row, slider, text,
|
button, checkbox, column, container, pick_list, row, slider, text,
|
||||||
|
|
@ -14,6 +13,7 @@ use iced::widget::{
|
||||||
use iced::window;
|
use iced::window;
|
||||||
use iced::{
|
use iced::{
|
||||||
Alignment, Application, Command, Element, Length, Settings, Subscription,
|
Alignment, Application, Command, Element, Length, Settings, Subscription,
|
||||||
|
Theme,
|
||||||
};
|
};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
|
@ -171,7 +171,7 @@ fn view_controls<'a>(
|
||||||
.on_press(Message::TogglePlayback),
|
.on_press(Message::TogglePlayback),
|
||||||
button("Next")
|
button("Next")
|
||||||
.on_press(Message::Next)
|
.on_press(Message::Next)
|
||||||
.style(theme::Button::Secondary),
|
.style(button::secondary),
|
||||||
]
|
]
|
||||||
.spacing(10);
|
.spacing(10);
|
||||||
|
|
||||||
|
|
@ -195,7 +195,7 @@ fn view_controls<'a>(
|
||||||
.text_size(16),
|
.text_size(16),
|
||||||
button("Clear")
|
button("Clear")
|
||||||
.on_press(Message::Clear)
|
.on_press(Message::Clear)
|
||||||
.style(theme::Button::Destructive),
|
.style(button::destructive),
|
||||||
]
|
]
|
||||||
.padding(10)
|
.padding(10)
|
||||||
.spacing(20)
|
.spacing(20)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
use iced::theme;
|
|
||||||
use iced::widget::{
|
use iced::widget::{
|
||||||
button, column, horizontal_space, lazy, pick_list, row, scrollable, text,
|
button, column, horizontal_space, lazy, pick_list, row, scrollable, text,
|
||||||
text_input,
|
text_input,
|
||||||
|
|
@ -181,7 +180,7 @@ impl Sandbox for App {
|
||||||
column(items.into_iter().map(|item| {
|
column(items.into_iter().map(|item| {
|
||||||
let button = button("Delete")
|
let button = button("Delete")
|
||||||
.on_press(Message::DeleteItem(item.clone()))
|
.on_press(Message::DeleteItem(item.clone()))
|
||||||
.style(theme::Button::Destructive);
|
.style(button::destructive);
|
||||||
|
|
||||||
row![
|
row![
|
||||||
text(&item.name).color(item.color),
|
text(&item.name).color(item.color),
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
use iced::alignment::{self, Alignment};
|
use iced::alignment::{self, Alignment};
|
||||||
use iced::executor;
|
use iced::executor;
|
||||||
use iced::keyboard;
|
use iced::keyboard;
|
||||||
use iced::theme::{self, Theme};
|
|
||||||
use iced::widget::pane_grid::{self, PaneGrid};
|
use iced::widget::pane_grid::{self, PaneGrid};
|
||||||
use iced::widget::{
|
use iced::widget::{
|
||||||
button, column, container, responsive, row, scrollable, text,
|
button, column, container, responsive, row, scrollable, text,
|
||||||
};
|
};
|
||||||
use iced::{
|
use iced::{
|
||||||
Application, Color, Command, Element, Length, Settings, Size, Subscription,
|
Application, Color, Command, Element, Length, Settings, Size, Subscription,
|
||||||
|
Theme,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
|
|
@ -287,10 +287,7 @@ fn view_content<'a>(
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
.push_maybe(if total_panes > 1 && !is_pinned {
|
.push_maybe(if total_panes > 1 && !is_pinned {
|
||||||
Some(
|
Some(button("Close", Message::Close(pane)).style(button::destructive))
|
||||||
button("Close", Message::Close(pane))
|
|
||||||
.style(theme::Button::Destructive),
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
})
|
})
|
||||||
|
|
@ -327,7 +324,7 @@ fn view_controls<'a>(
|
||||||
|
|
||||||
Some(
|
Some(
|
||||||
button(text(content).size(14))
|
button(text(content).size(14))
|
||||||
.style(theme::Button::Secondary)
|
.style(button::secondary)
|
||||||
.padding(3)
|
.padding(3)
|
||||||
.on_press(message),
|
.on_press(message),
|
||||||
)
|
)
|
||||||
|
|
@ -336,7 +333,7 @@ fn view_controls<'a>(
|
||||||
});
|
});
|
||||||
|
|
||||||
let close = button(text("Close").size(14))
|
let close = button(text("Close").size(14))
|
||||||
.style(theme::Button::Destructive)
|
.style(button::destructive)
|
||||||
.padding(3)
|
.padding(3)
|
||||||
.on_press_maybe(if total_panes > 1 && !is_pinned {
|
.on_press_maybe(if total_panes > 1 && !is_pinned {
|
||||||
Some(Message::Close(pane))
|
Some(Message::Close(pane))
|
||||||
|
|
|
||||||
|
|
@ -216,9 +216,9 @@ impl Application for Example {
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
button(centered_text("Saving..."))
|
button(centered_text("Saving..."))
|
||||||
.style(theme::Button::Secondary)
|
.style(button::secondary)
|
||||||
}
|
}
|
||||||
.style(theme::Button::Secondary)
|
.style(button::secondary)
|
||||||
.padding([10, 20, 10, 20])
|
.padding([10, 20, 10, 20])
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
]
|
]
|
||||||
|
|
@ -227,7 +227,7 @@ impl Application for Example {
|
||||||
crop_controls,
|
crop_controls,
|
||||||
button(centered_text("Crop"))
|
button(centered_text("Crop"))
|
||||||
.on_press(Message::Crop)
|
.on_press(Message::Crop)
|
||||||
.style(theme::Button::Destructive)
|
.style(button::destructive)
|
||||||
.padding([10, 20, 10, 20])
|
.padding([10, 20, 10, 20])
|
||||||
.width(Length::Fill),
|
.width(Length::Fill),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
use iced::alignment;
|
use iced::alignment;
|
||||||
use iced::executor;
|
use iced::executor;
|
||||||
use iced::keyboard;
|
use iced::keyboard;
|
||||||
use iced::theme::{self, Theme};
|
|
||||||
use iced::time;
|
use iced::time;
|
||||||
use iced::widget::{button, column, container, row, text};
|
use iced::widget::{button, column, container, row, text};
|
||||||
use iced::{
|
use iced::{
|
||||||
Alignment, Application, Command, Element, Length, Settings, Subscription,
|
Alignment, Application, Command, Element, Length, Settings, Subscription,
|
||||||
|
Theme,
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
@ -136,7 +136,7 @@ impl Application for Stopwatch {
|
||||||
};
|
};
|
||||||
|
|
||||||
let reset_button = button("Reset")
|
let reset_button = button("Reset")
|
||||||
.style(theme::Button::Destructive)
|
.style(button::destructive)
|
||||||
.on_press(Message::Reset);
|
.on_press(Message::Reset);
|
||||||
|
|
||||||
let controls = row![toggle_button, reset_button].spacing(20);
|
let controls = row![toggle_button, reset_button].spacing(20);
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,14 @@
|
||||||
use iced::alignment::{self, Alignment};
|
use iced::alignment::{self, Alignment};
|
||||||
use iced::font::{self, Font};
|
use iced::font::{self, Font};
|
||||||
use iced::keyboard;
|
use iced::keyboard;
|
||||||
use iced::theme::{self, Theme};
|
|
||||||
use iced::widget::{
|
use iced::widget::{
|
||||||
self, button, checkbox, column, container, keyed_column, row, scrollable,
|
self, button, checkbox, column, container, keyed_column, row, scrollable,
|
||||||
text, text_input, Text,
|
text, text_input, Text,
|
||||||
};
|
};
|
||||||
use iced::window;
|
use iced::window;
|
||||||
use iced::{Application, Element};
|
use iced::{
|
||||||
use iced::{Command, Length, Settings, Size, Subscription};
|
Application, Command, Element, Length, Settings, Size, Subscription, Theme,
|
||||||
|
};
|
||||||
|
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
@ -362,7 +362,7 @@ impl Task {
|
||||||
button(edit_icon())
|
button(edit_icon())
|
||||||
.on_press(TaskMessage::Edit)
|
.on_press(TaskMessage::Edit)
|
||||||
.padding(10)
|
.padding(10)
|
||||||
.style(theme::Button::Text),
|
.style(button::text),
|
||||||
]
|
]
|
||||||
.spacing(20)
|
.spacing(20)
|
||||||
.align_items(Alignment::Center)
|
.align_items(Alignment::Center)
|
||||||
|
|
@ -385,7 +385,7 @@ impl Task {
|
||||||
)
|
)
|
||||||
.on_press(TaskMessage::Delete)
|
.on_press(TaskMessage::Delete)
|
||||||
.padding(10)
|
.padding(10)
|
||||||
.style(theme::Button::Destructive)
|
.style(button::destructive)
|
||||||
]
|
]
|
||||||
.spacing(20)
|
.spacing(20)
|
||||||
.align_items(Alignment::Center)
|
.align_items(Alignment::Center)
|
||||||
|
|
@ -402,9 +402,9 @@ fn view_controls(tasks: &[Task], current_filter: Filter) -> Element<Message> {
|
||||||
let label = text(label);
|
let label = text(label);
|
||||||
|
|
||||||
let button = button(label).style(if filter == current_filter {
|
let button = button(label).style(if filter == current_filter {
|
||||||
theme::Button::Primary
|
button::primary
|
||||||
} else {
|
} else {
|
||||||
theme::Button::Text
|
button::text
|
||||||
});
|
});
|
||||||
|
|
||||||
button.on_press(Message::FilterChanged(filter)).padding(8)
|
button.on_press(Message::FilterChanged(filter)).padding(8)
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
use iced::alignment::{self, Alignment};
|
use iced::alignment::{self, Alignment};
|
||||||
use iced::theme;
|
|
||||||
use iced::widget::{
|
use iced::widget::{
|
||||||
checkbox, column, container, horizontal_space, image, radio, row,
|
button, checkbox, column, container, horizontal_space, image, radio, row,
|
||||||
scrollable, slider, text, text_input, toggler, vertical_space,
|
scrollable, slider, text, text_input, toggler, vertical_space,
|
||||||
};
|
};
|
||||||
use iced::widget::{Button, Column, Container, Slider};
|
use iced::widget::{Button, Column, Container, Slider};
|
||||||
|
|
@ -56,18 +55,17 @@ impl Sandbox for Tour {
|
||||||
fn view(&self) -> Element<Message> {
|
fn view(&self) -> Element<Message> {
|
||||||
let Tour { steps, .. } = self;
|
let Tour { steps, .. } = self;
|
||||||
|
|
||||||
let controls = row![]
|
let controls =
|
||||||
.push_maybe(steps.has_previous().then(|| {
|
row![]
|
||||||
button("Back")
|
.push_maybe(steps.has_previous().then(|| {
|
||||||
.on_press(Message::BackPressed)
|
padded_button("Back")
|
||||||
.style(theme::Button::Secondary)
|
.on_press(Message::BackPressed)
|
||||||
}))
|
.style(button::secondary)
|
||||||
.push(horizontal_space())
|
}))
|
||||||
.push_maybe(
|
.push(horizontal_space())
|
||||||
steps
|
.push_maybe(steps.can_continue().then(|| {
|
||||||
.can_continue()
|
padded_button("Next").on_press(Message::NextPressed)
|
||||||
.then(|| button("Next").on_press(Message::NextPressed)),
|
}));
|
||||||
);
|
|
||||||
|
|
||||||
let content: Element<_> = column![
|
let content: Element<_> = column![
|
||||||
steps.view(self.debug).map(Message::StepMessage),
|
steps.view(self.debug).map(Message::StepMessage),
|
||||||
|
|
@ -676,8 +674,8 @@ fn ferris<'a>(
|
||||||
.center_x()
|
.center_x()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn button<'a, Message: Clone>(label: &str) -> Button<'a, Message> {
|
fn padded_button<'a, Message: Clone>(label: &str) -> Button<'a, Message> {
|
||||||
iced::widget::button(text(label)).padding([12, 24])
|
button(text(label)).padding([12, 24])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn color_slider<'a>(
|
fn color_slider<'a>(
|
||||||
|
|
|
||||||
|
|
@ -1,79 +1 @@
|
||||||
//! Change the apperance of a button.
|
|
||||||
use iced_core::{Background, Border, Color, Shadow, Vector};
|
|
||||||
|
|
||||||
/// The appearance of a button.
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
pub struct Appearance {
|
|
||||||
/// The amount of offset to apply to the shadow of the button.
|
|
||||||
pub shadow_offset: Vector,
|
|
||||||
/// The [`Background`] of the button.
|
|
||||||
pub background: Option<Background>,
|
|
||||||
/// The text [`Color`] of the button.
|
|
||||||
pub text_color: Color,
|
|
||||||
/// The [`Border`] of the buton.
|
|
||||||
pub border: Border,
|
|
||||||
/// The [`Shadow`] of the butoon.
|
|
||||||
pub shadow: Shadow,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::default::Default for Appearance {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
shadow_offset: Vector::default(),
|
|
||||||
background: None,
|
|
||||||
text_color: Color::BLACK,
|
|
||||||
border: Border::default(),
|
|
||||||
shadow: Shadow::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A set of rules that dictate the style of a button.
|
|
||||||
pub trait StyleSheet {
|
|
||||||
/// The supported style of the [`StyleSheet`].
|
|
||||||
type Style: Default;
|
|
||||||
|
|
||||||
/// Produces the active [`Appearance`] of a button.
|
|
||||||
fn active(&self, style: &Self::Style) -> Appearance;
|
|
||||||
|
|
||||||
/// Produces the hovered [`Appearance`] of a button.
|
|
||||||
fn hovered(&self, style: &Self::Style) -> Appearance {
|
|
||||||
let active = self.active(style);
|
|
||||||
|
|
||||||
Appearance {
|
|
||||||
shadow_offset: active.shadow_offset + Vector::new(0.0, 1.0),
|
|
||||||
..active
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Produces the pressed [`Appearance`] of a button.
|
|
||||||
fn pressed(&self, style: &Self::Style) -> Appearance {
|
|
||||||
Appearance {
|
|
||||||
shadow_offset: Vector::default(),
|
|
||||||
..self.active(style)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Produces the disabled [`Appearance`] of a button.
|
|
||||||
fn disabled(&self, style: &Self::Style) -> Appearance {
|
|
||||||
let active = self.active(style);
|
|
||||||
|
|
||||||
Appearance {
|
|
||||||
shadow_offset: Vector::default(),
|
|
||||||
background: active.background.map(|background| match background {
|
|
||||||
Background::Color(color) => Background::Color(Color {
|
|
||||||
a: color.a * 0.5,
|
|
||||||
..color
|
|
||||||
}),
|
|
||||||
Background::Gradient(gradient) => {
|
|
||||||
Background::Gradient(gradient.mul_alpha(0.5))
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
text_color: Color {
|
|
||||||
a: active.text_color.a * 0.5,
|
|
||||||
..active.text_color
|
|
||||||
},
|
|
||||||
..active
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -30,15 +30,7 @@ pub trait StyleSheet {
|
||||||
let active = self.active(style, is_checked);
|
let active = self.active(style, is_checked);
|
||||||
|
|
||||||
Appearance {
|
Appearance {
|
||||||
background: match active.background {
|
background: active.background.transparentize(0.5),
|
||||||
Background::Color(color) => Background::Color(Color {
|
|
||||||
a: color.a * 0.5,
|
|
||||||
..color
|
|
||||||
}),
|
|
||||||
Background::Gradient(gradient) => {
|
|
||||||
Background::Gradient(gradient.mul_alpha(0.5))
|
|
||||||
}
|
|
||||||
},
|
|
||||||
..active
|
..active
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ pub mod palette;
|
||||||
pub use palette::Palette;
|
pub use palette::Palette;
|
||||||
|
|
||||||
use crate::application;
|
use crate::application;
|
||||||
use crate::button;
|
|
||||||
use crate::checkbox;
|
use crate::checkbox;
|
||||||
use crate::container;
|
use crate::container;
|
||||||
use crate::core::widget::text;
|
use crate::core::widget::text;
|
||||||
|
|
@ -22,7 +21,7 @@ use crate::text_editor;
|
||||||
use crate::text_input;
|
use crate::text_input;
|
||||||
use crate::toggler;
|
use crate::toggler;
|
||||||
|
|
||||||
use crate::core::{Background, Border, Color, Shadow, Vector};
|
use crate::core::{Background, Border, Color, Shadow};
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
@ -285,126 +284,6 @@ impl<T: Fn(&Theme) -> application::Appearance> application::StyleSheet for T {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The style of a button.
|
|
||||||
#[derive(Default)]
|
|
||||||
pub enum Button {
|
|
||||||
/// The primary style.
|
|
||||||
#[default]
|
|
||||||
Primary,
|
|
||||||
/// The secondary style.
|
|
||||||
Secondary,
|
|
||||||
/// The positive style.
|
|
||||||
Positive,
|
|
||||||
/// The destructive style.
|
|
||||||
Destructive,
|
|
||||||
/// The text style.
|
|
||||||
///
|
|
||||||
/// Useful for links!
|
|
||||||
Text,
|
|
||||||
/// A custom style.
|
|
||||||
Custom(Box<dyn button::StyleSheet<Style = Theme>>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Button {
|
|
||||||
/// Creates a custom [`Button`] style variant.
|
|
||||||
pub fn custom(
|
|
||||||
style_sheet: impl button::StyleSheet<Style = Theme> + 'static,
|
|
||||||
) -> Self {
|
|
||||||
Self::Custom(Box::new(style_sheet))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl button::StyleSheet for Theme {
|
|
||||||
type Style = Button;
|
|
||||||
|
|
||||||
fn active(&self, style: &Self::Style) -> button::Appearance {
|
|
||||||
let palette = self.extended_palette();
|
|
||||||
|
|
||||||
let appearance = button::Appearance {
|
|
||||||
border: Border::with_radius(2),
|
|
||||||
..button::Appearance::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let from_pair = |pair: palette::Pair| button::Appearance {
|
|
||||||
background: Some(pair.color.into()),
|
|
||||||
text_color: pair.text,
|
|
||||||
..appearance
|
|
||||||
};
|
|
||||||
|
|
||||||
match style {
|
|
||||||
Button::Primary => from_pair(palette.primary.strong),
|
|
||||||
Button::Secondary => from_pair(palette.secondary.base),
|
|
||||||
Button::Positive => from_pair(palette.success.base),
|
|
||||||
Button::Destructive => from_pair(palette.danger.base),
|
|
||||||
Button::Text => button::Appearance {
|
|
||||||
text_color: palette.background.base.text,
|
|
||||||
..appearance
|
|
||||||
},
|
|
||||||
Button::Custom(custom) => custom.active(self),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn hovered(&self, style: &Self::Style) -> button::Appearance {
|
|
||||||
let palette = self.extended_palette();
|
|
||||||
|
|
||||||
if let Button::Custom(custom) = style {
|
|
||||||
return custom.hovered(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
let active = self.active(style);
|
|
||||||
|
|
||||||
let background = match style {
|
|
||||||
Button::Primary => Some(palette.primary.base.color),
|
|
||||||
Button::Secondary => Some(palette.background.strong.color),
|
|
||||||
Button::Positive => Some(palette.success.strong.color),
|
|
||||||
Button::Destructive => Some(palette.danger.strong.color),
|
|
||||||
Button::Text | Button::Custom(_) => None,
|
|
||||||
};
|
|
||||||
|
|
||||||
button::Appearance {
|
|
||||||
background: background.map(Background::from),
|
|
||||||
..active
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn pressed(&self, style: &Self::Style) -> button::Appearance {
|
|
||||||
if let Button::Custom(custom) = style {
|
|
||||||
return custom.pressed(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
button::Appearance {
|
|
||||||
shadow_offset: Vector::default(),
|
|
||||||
..self.active(style)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn disabled(&self, style: &Self::Style) -> button::Appearance {
|
|
||||||
if let Button::Custom(custom) = style {
|
|
||||||
return custom.disabled(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
let active = self.active(style);
|
|
||||||
|
|
||||||
button::Appearance {
|
|
||||||
shadow_offset: Vector::default(),
|
|
||||||
background: active.background.map(|background| match background {
|
|
||||||
Background::Color(color) => Background::Color(Color {
|
|
||||||
a: color.a * 0.5,
|
|
||||||
..color
|
|
||||||
}),
|
|
||||||
Background::Gradient(gradient) => {
|
|
||||||
Background::Gradient(gradient.mul_alpha(0.5))
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
text_color: Color {
|
|
||||||
a: active.text_color.a * 0.5,
|
|
||||||
..active.text_color
|
|
||||||
},
|
|
||||||
..active
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The style of a checkbox.
|
/// The style of a checkbox.
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub enum Checkbox {
|
pub enum Checkbox {
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,11 @@ use crate::core::touch;
|
||||||
use crate::core::widget::tree::{self, Tree};
|
use crate::core::widget::tree::{self, Tree};
|
||||||
use crate::core::widget::Operation;
|
use crate::core::widget::Operation;
|
||||||
use crate::core::{
|
use crate::core::{
|
||||||
Background, Clipboard, Color, Element, Layout, Length, Padding, Rectangle,
|
Background, Border, Clipboard, Color, Element, Layout, Length, Padding,
|
||||||
Shell, Size, Vector, Widget,
|
Rectangle, Shadow, Shell, Size, Vector, Widget,
|
||||||
};
|
};
|
||||||
|
use crate::style::theme::palette;
|
||||||
pub use crate::style::button::{Appearance, StyleSheet};
|
use crate::style::Theme;
|
||||||
|
|
||||||
/// A generic widget that produces a message when pressed.
|
/// A generic widget that produces a message when pressed.
|
||||||
///
|
///
|
||||||
|
|
@ -53,7 +53,7 @@ pub use crate::style::button::{Appearance, StyleSheet};
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Button<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer>
|
pub struct Button<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer>
|
||||||
where
|
where
|
||||||
Theme: StyleSheet,
|
Theme: Style,
|
||||||
Renderer: crate::core::Renderer,
|
Renderer: crate::core::Renderer,
|
||||||
{
|
{
|
||||||
content: Element<'a, Message, Theme, Renderer>,
|
content: Element<'a, Message, Theme, Renderer>,
|
||||||
|
|
@ -62,12 +62,12 @@ where
|
||||||
height: Length,
|
height: Length,
|
||||||
padding: Padding,
|
padding: Padding,
|
||||||
clip: bool,
|
clip: bool,
|
||||||
style: Theme::Style,
|
style: fn(&Theme, Status) -> Appearance,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Message, Theme, Renderer> Button<'a, Message, Theme, Renderer>
|
impl<'a, Message, Theme, Renderer> Button<'a, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
Theme: StyleSheet,
|
Theme: Style,
|
||||||
Renderer: crate::core::Renderer,
|
Renderer: crate::core::Renderer,
|
||||||
{
|
{
|
||||||
/// Creates a new [`Button`] with the given content.
|
/// Creates a new [`Button`] with the given content.
|
||||||
|
|
@ -84,7 +84,7 @@ where
|
||||||
height: size.height.fluid(),
|
height: size.height.fluid(),
|
||||||
padding: Padding::new(5.0),
|
padding: Padding::new(5.0),
|
||||||
clip: false,
|
clip: false,
|
||||||
style: Theme::Style::default(),
|
style: Theme::DEFAULT,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -124,7 +124,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the style variant of this [`Button`].
|
/// Sets the style variant of this [`Button`].
|
||||||
pub fn style(mut self, style: impl Into<Theme::Style>) -> Self {
|
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self {
|
||||||
self.style = style.into();
|
self.style = style.into();
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
@ -137,11 +137,16 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||||
|
struct State {
|
||||||
|
is_pressed: bool,
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
||||||
for Button<'a, Message, Theme, Renderer>
|
for Button<'a, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
Message: 'a + Clone,
|
Message: 'a + Clone,
|
||||||
Theme: StyleSheet,
|
Theme: Style,
|
||||||
Renderer: 'a + crate::core::Renderer,
|
Renderer: 'a + crate::core::Renderer,
|
||||||
{
|
{
|
||||||
fn tag(&self) -> tree::Tag {
|
fn tag(&self) -> tree::Tag {
|
||||||
|
|
@ -149,7 +154,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn state(&self) -> tree::State {
|
fn state(&self) -> tree::State {
|
||||||
tree::State::new(State::new())
|
tree::State::new(State::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn children(&self) -> Vec<Tree> {
|
fn children(&self) -> Vec<Tree> {
|
||||||
|
|
@ -173,13 +178,19 @@ where
|
||||||
renderer: &Renderer,
|
renderer: &Renderer,
|
||||||
limits: &layout::Limits,
|
limits: &layout::Limits,
|
||||||
) -> layout::Node {
|
) -> layout::Node {
|
||||||
layout(limits, self.width, self.height, self.padding, |limits| {
|
layout::padded(
|
||||||
self.content.as_widget().layout(
|
limits,
|
||||||
&mut tree.children[0],
|
self.width,
|
||||||
renderer,
|
self.height,
|
||||||
limits,
|
self.padding,
|
||||||
)
|
|limits| {
|
||||||
})
|
self.content.as_widget().layout(
|
||||||
|
&mut tree.children[0],
|
||||||
|
renderer,
|
||||||
|
limits,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn operate(
|
fn operate(
|
||||||
|
|
@ -223,9 +234,48 @@ where
|
||||||
return event::Status::Captured;
|
return event::Status::Captured;
|
||||||
}
|
}
|
||||||
|
|
||||||
update(event, layout, cursor, shell, &self.on_press, || {
|
match event {
|
||||||
tree.state.downcast_mut::<State>()
|
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||||
})
|
| Event::Touch(touch::Event::FingerPressed { .. }) => {
|
||||||
|
if self.on_press.is_some() {
|
||||||
|
let bounds = layout.bounds();
|
||||||
|
|
||||||
|
if cursor.is_over(bounds) {
|
||||||
|
let state = tree.state.downcast_mut::<State>();
|
||||||
|
|
||||||
|
state.is_pressed = true;
|
||||||
|
|
||||||
|
return event::Status::Captured;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
|
||||||
|
| Event::Touch(touch::Event::FingerLifted { .. }) => {
|
||||||
|
if let Some(on_press) = self.on_press.clone() {
|
||||||
|
let state = tree.state.downcast_mut::<State>();
|
||||||
|
|
||||||
|
if state.is_pressed {
|
||||||
|
state.is_pressed = false;
|
||||||
|
|
||||||
|
let bounds = layout.bounds();
|
||||||
|
|
||||||
|
if cursor.is_over(bounds) {
|
||||||
|
shell.publish(on_press);
|
||||||
|
}
|
||||||
|
|
||||||
|
return event::Status::Captured;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Event::Touch(touch::Event::FingerLost { .. }) => {
|
||||||
|
let state = tree.state.downcast_mut::<State>();
|
||||||
|
|
||||||
|
state.is_pressed = false;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
event::Status::Ignored
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw(
|
fn draw(
|
||||||
|
|
@ -240,16 +290,39 @@ where
|
||||||
) {
|
) {
|
||||||
let bounds = layout.bounds();
|
let bounds = layout.bounds();
|
||||||
let content_layout = layout.children().next().unwrap();
|
let content_layout = layout.children().next().unwrap();
|
||||||
|
let is_mouse_over = cursor.is_over(bounds);
|
||||||
|
|
||||||
let styling = draw(
|
let status = if self.on_press.is_none() {
|
||||||
renderer,
|
Status::Disabled
|
||||||
bounds,
|
} else if is_mouse_over {
|
||||||
cursor,
|
let state = tree.state.downcast_ref::<State>();
|
||||||
self.on_press.is_some(),
|
|
||||||
theme,
|
if state.is_pressed {
|
||||||
&self.style,
|
Status::Pressed
|
||||||
|| tree.state.downcast_ref::<State>(),
|
} else {
|
||||||
);
|
Status::Hovered
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Status::Active
|
||||||
|
};
|
||||||
|
|
||||||
|
let styling = (self.style)(theme, status);
|
||||||
|
|
||||||
|
if styling.background.is_some()
|
||||||
|
|| styling.border.width > 0.0
|
||||||
|
|| styling.shadow.color.a > 0.0
|
||||||
|
{
|
||||||
|
renderer.fill_quad(
|
||||||
|
renderer::Quad {
|
||||||
|
bounds,
|
||||||
|
border: styling.border,
|
||||||
|
shadow: styling.shadow,
|
||||||
|
},
|
||||||
|
styling
|
||||||
|
.background
|
||||||
|
.unwrap_or(Background::Color(Color::TRANSPARENT)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
let viewport = if self.clip {
|
let viewport = if self.clip {
|
||||||
bounds.intersection(viewport).unwrap_or(*viewport)
|
bounds.intersection(viewport).unwrap_or(*viewport)
|
||||||
|
|
@ -278,7 +351,13 @@ where
|
||||||
_viewport: &Rectangle,
|
_viewport: &Rectangle,
|
||||||
_renderer: &Renderer,
|
_renderer: &Renderer,
|
||||||
) -> mouse::Interaction {
|
) -> mouse::Interaction {
|
||||||
mouse_interaction(layout, cursor, self.on_press.is_some())
|
let is_mouse_over = cursor.is_over(layout.bounds());
|
||||||
|
|
||||||
|
if is_mouse_over && self.on_press.is_some() {
|
||||||
|
mouse::Interaction::Pointer
|
||||||
|
} else {
|
||||||
|
mouse::Interaction::default()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn overlay<'b>(
|
fn overlay<'b>(
|
||||||
|
|
@ -301,7 +380,7 @@ impl<'a, Message, Theme, Renderer> From<Button<'a, Message, Theme, Renderer>>
|
||||||
for Element<'a, Message, Theme, Renderer>
|
for Element<'a, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
Message: Clone + 'a,
|
Message: Clone + 'a,
|
||||||
Theme: StyleSheet + 'a,
|
Theme: Style + 'a,
|
||||||
Renderer: crate::core::Renderer + 'a,
|
Renderer: crate::core::Renderer + 'a,
|
||||||
{
|
{
|
||||||
fn from(button: Button<'a, Message, Theme, Renderer>) -> Self {
|
fn from(button: Button<'a, Message, Theme, Renderer>) -> Self {
|
||||||
|
|
@ -309,143 +388,150 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The local state of a [`Button`].
|
/// The possible status of a [`Button`].
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub struct State {
|
pub enum Status {
|
||||||
is_pressed: bool,
|
/// The [`Button`] can be pressed.
|
||||||
|
Active,
|
||||||
|
/// The [`Button`] can be pressed and it is being hovered.
|
||||||
|
Hovered,
|
||||||
|
/// The [`Button`] is being pressed.
|
||||||
|
Pressed,
|
||||||
|
/// The [`Button`] cannot be pressed.
|
||||||
|
Disabled,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
/// The appearance of a button.
|
||||||
/// Creates a new [`State`].
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub fn new() -> State {
|
pub struct Appearance {
|
||||||
State::default()
|
/// The amount of offset to apply to the shadow of the button.
|
||||||
|
pub shadow_offset: Vector,
|
||||||
|
/// The [`Background`] of the button.
|
||||||
|
pub background: Option<Background>,
|
||||||
|
/// The text [`Color`] of the button.
|
||||||
|
pub text_color: Color,
|
||||||
|
/// The [`Border`] of the buton.
|
||||||
|
pub border: Border,
|
||||||
|
/// The [`Shadow`] of the butoon.
|
||||||
|
pub shadow: Shadow,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::default::Default for Appearance {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
shadow_offset: Vector::default(),
|
||||||
|
background: None,
|
||||||
|
text_color: Color::BLACK,
|
||||||
|
border: Border::default(),
|
||||||
|
shadow: Shadow::default(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Processes the given [`Event`] and updates the [`State`] of a [`Button`]
|
/// The default style of a [`Button`] for a given theme.
|
||||||
/// accordingly.
|
pub trait Style {
|
||||||
pub fn update<'a, Message: Clone>(
|
/// The default style.
|
||||||
event: Event,
|
const DEFAULT: fn(&Self, Status) -> Appearance;
|
||||||
layout: Layout<'_>,
|
|
||||||
cursor: mouse::Cursor,
|
|
||||||
shell: &mut Shell<'_, Message>,
|
|
||||||
on_press: &Option<Message>,
|
|
||||||
state: impl FnOnce() -> &'a mut State,
|
|
||||||
) -> event::Status {
|
|
||||||
match event {
|
|
||||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
|
||||||
| Event::Touch(touch::Event::FingerPressed { .. }) => {
|
|
||||||
if on_press.is_some() {
|
|
||||||
let bounds = layout.bounds();
|
|
||||||
|
|
||||||
if cursor.is_over(bounds) {
|
|
||||||
let state = state();
|
|
||||||
|
|
||||||
state.is_pressed = true;
|
|
||||||
|
|
||||||
return event::Status::Captured;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
|
|
||||||
| Event::Touch(touch::Event::FingerLifted { .. }) => {
|
|
||||||
if let Some(on_press) = on_press.clone() {
|
|
||||||
let state = state();
|
|
||||||
|
|
||||||
if state.is_pressed {
|
|
||||||
state.is_pressed = false;
|
|
||||||
|
|
||||||
let bounds = layout.bounds();
|
|
||||||
|
|
||||||
if cursor.is_over(bounds) {
|
|
||||||
shell.publish(on_press);
|
|
||||||
}
|
|
||||||
|
|
||||||
return event::Status::Captured;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Event::Touch(touch::Event::FingerLost { .. }) => {
|
|
||||||
let state = state();
|
|
||||||
|
|
||||||
state.is_pressed = false;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
event::Status::Ignored
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draws a [`Button`].
|
impl Style for Theme {
|
||||||
pub fn draw<'a, Theme, Renderer: crate::core::Renderer>(
|
const DEFAULT: fn(&Self, Status) -> Appearance = primary;
|
||||||
renderer: &mut Renderer,
|
}
|
||||||
bounds: Rectangle,
|
|
||||||
cursor: mouse::Cursor,
|
|
||||||
is_enabled: bool,
|
|
||||||
theme: &Theme,
|
|
||||||
style: &Theme::Style,
|
|
||||||
state: impl FnOnce() -> &'a State,
|
|
||||||
) -> Appearance
|
|
||||||
where
|
|
||||||
Theme: StyleSheet,
|
|
||||||
{
|
|
||||||
let is_mouse_over = cursor.is_over(bounds);
|
|
||||||
|
|
||||||
let styling = if !is_enabled {
|
/// A primary button; denoting a main action.
|
||||||
theme.disabled(style)
|
pub fn primary(theme: &Theme, status: Status) -> Appearance {
|
||||||
} else if is_mouse_over {
|
let palette = theme.extended_palette();
|
||||||
let state = state();
|
let base = styled(palette.primary.strong);
|
||||||
|
|
||||||
if state.is_pressed {
|
match status {
|
||||||
theme.pressed(style)
|
Status::Active | Status::Pressed => base,
|
||||||
} else {
|
Status::Hovered => Appearance {
|
||||||
theme.hovered(style)
|
background: Some(Background::Color(palette.primary.base.color)),
|
||||||
}
|
..base
|
||||||
} else {
|
},
|
||||||
theme.active(style)
|
Status::Disabled => disabled(base),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A secondary button; denoting a complementary action.
|
||||||
|
pub fn secondary(theme: &Theme, status: Status) -> Appearance {
|
||||||
|
let palette = theme.extended_palette();
|
||||||
|
let base = styled(palette.secondary.base);
|
||||||
|
|
||||||
|
match status {
|
||||||
|
Status::Active | Status::Pressed => base,
|
||||||
|
Status::Hovered => Appearance {
|
||||||
|
background: Some(Background::Color(palette.secondary.strong.color)),
|
||||||
|
..base
|
||||||
|
},
|
||||||
|
Status::Disabled => disabled(base),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A positive button; denoting a good outcome.
|
||||||
|
pub fn positive(theme: &Theme, status: Status) -> Appearance {
|
||||||
|
let palette = theme.extended_palette();
|
||||||
|
let base = styled(palette.success.base);
|
||||||
|
|
||||||
|
match status {
|
||||||
|
Status::Active | Status::Pressed => base,
|
||||||
|
Status::Hovered => Appearance {
|
||||||
|
background: Some(Background::Color(palette.success.strong.color)),
|
||||||
|
..base
|
||||||
|
},
|
||||||
|
Status::Disabled => disabled(base),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A destructive button; denoting a dangerous action.
|
||||||
|
pub fn destructive(theme: &Theme, status: Status) -> Appearance {
|
||||||
|
let palette = theme.extended_palette();
|
||||||
|
let base = styled(palette.danger.base);
|
||||||
|
|
||||||
|
match status {
|
||||||
|
Status::Active | Status::Pressed => base,
|
||||||
|
Status::Hovered => Appearance {
|
||||||
|
background: Some(Background::Color(palette.danger.strong.color)),
|
||||||
|
..base
|
||||||
|
},
|
||||||
|
Status::Disabled => disabled(base),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A text button; useful for links.
|
||||||
|
pub fn text(theme: &Theme, status: Status) -> Appearance {
|
||||||
|
let palette = theme.extended_palette();
|
||||||
|
|
||||||
|
let base = Appearance {
|
||||||
|
text_color: palette.background.base.text,
|
||||||
|
..Appearance::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
if styling.background.is_some()
|
match status {
|
||||||
|| styling.border.width > 0.0
|
Status::Active | Status::Pressed => base,
|
||||||
|| styling.shadow.color.a > 0.0
|
Status::Hovered => Appearance {
|
||||||
{
|
text_color: palette.background.base.text.transparentize(0.8),
|
||||||
renderer.fill_quad(
|
..base
|
||||||
renderer::Quad {
|
},
|
||||||
bounds,
|
Status::Disabled => disabled(base),
|
||||||
border: styling.border,
|
}
|
||||||
shadow: styling.shadow,
|
}
|
||||||
},
|
|
||||||
styling
|
fn styled(pair: palette::Pair) -> Appearance {
|
||||||
.background
|
Appearance {
|
||||||
.unwrap_or(Background::Color(Color::TRANSPARENT)),
|
background: Some(Background::Color(pair.color)),
|
||||||
);
|
text_color: pair.text,
|
||||||
}
|
border: Border::with_radius(2),
|
||||||
|
..Appearance::default()
|
||||||
styling
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Computes the layout of a [`Button`].
|
fn disabled(appearance: Appearance) -> Appearance {
|
||||||
pub fn layout(
|
Appearance {
|
||||||
limits: &layout::Limits,
|
background: appearance
|
||||||
width: Length,
|
.background
|
||||||
height: Length,
|
.map(|background| background.transparentize(0.5)),
|
||||||
padding: Padding,
|
text_color: appearance.text_color.transparentize(0.5),
|
||||||
layout_content: impl FnOnce(&layout::Limits) -> layout::Node,
|
..appearance
|
||||||
) -> layout::Node {
|
|
||||||
layout::padded(limits, width, height, padding, layout_content)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the [`mouse::Interaction`] of a [`Button`].
|
|
||||||
pub fn mouse_interaction(
|
|
||||||
layout: Layout<'_>,
|
|
||||||
cursor: mouse::Cursor,
|
|
||||||
is_enabled: bool,
|
|
||||||
) -> mouse::Interaction {
|
|
||||||
let is_mouse_over = cursor.is_over(layout.bounds());
|
|
||||||
|
|
||||||
if is_mouse_over && is_enabled {
|
|
||||||
mouse::Interaction::Pointer
|
|
||||||
} else {
|
|
||||||
mouse::Interaction::default()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -118,7 +118,7 @@ pub fn button<'a, Message, Theme, Renderer>(
|
||||||
) -> Button<'a, Message, Theme, Renderer>
|
) -> Button<'a, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
Renderer: core::Renderer,
|
Renderer: core::Renderer,
|
||||||
Theme: button::StyleSheet,
|
Theme: button::Style,
|
||||||
{
|
{
|
||||||
Button::new(content)
|
Button::new(content)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue