Draft first-class Theme support

RFC: https://github.com/iced-rs/rfcs/pull/6
This commit is contained in:
Héctor Ramón Jiménez 2022-05-14 01:47:55 +02:00
parent 5de337f214
commit 664251f3f5
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
113 changed files with 767 additions and 878 deletions

View file

@ -47,12 +47,13 @@ impl Sandbox for Component {
}
mod numeric_input {
use iced::pure::{button, row, text, text_input};
use iced_lazy::pure::{self, Component};
use iced_native::alignment::{self, Alignment};
use iced_native::text;
use iced_native::widget;
use iced_native::Length;
use iced_pure::Element;
use iced_pure::{button, row, text, text_input};
pub struct NumericInput<Message> {
value: Option<u32>,
@ -88,6 +89,9 @@ mod numeric_input {
impl<Message, Renderer> Component<Message, Renderer> for NumericInput<Message>
where
Renderer: text::Renderer + 'static,
Renderer::Theme: widget::button::StyleSheet,
<Renderer::Theme as widget::button::StyleSheet>::Variant:
Default + Copy,
{
type State = ();
type Event = Event;
@ -158,6 +162,9 @@ mod numeric_input {
where
Message: 'a,
Renderer: 'static + text::Renderer,
Renderer::Theme: widget::button::StyleSheet,
<Renderer::Theme as widget::button::StyleSheet>::Variant:
Default + Copy,
{
fn from(numeric_input: NumericInput<Message>) -> Self {
pure::component(numeric_input)

View file

@ -9,6 +9,7 @@ use iced::pure::{
button, checkbox, column, container, pick_list, row, slider, text,
};
use iced::pure::{Application, Element};
use iced::theme::{self, Theme};
use iced::time;
use iced::window;
use iced::{Alignment, Color, Command, Length, Settings, Subscription};
@ -52,6 +53,7 @@ enum Message {
impl Application for GameOfLife {
type Message = Message;
type Theme = Theme;
type Executor = executor::Default;
type Flags = ();
@ -168,10 +170,13 @@ fn view_controls<'a>(
.spacing(10)
.push(
button(if is_playing { "Pause" } else { "Play" })
.on_press(Message::TogglePlayback)
.style(style::Button),
.on_press(Message::TogglePlayback),
)
.push(button("Next").on_press(Message::Next).style(style::Button));
.push(
button("Next")
.on_press(Message::Next)
.style(theme::Button::Secondary),
);
let speed_controls = row()
.width(Length::Fill)
@ -201,7 +206,11 @@ fn view_controls<'a>(
.text_size(16)
.style(style::PickList),
)
.push(button("Clear").on_press(Message::Clear).style(style::Clear))
.push(
button("Clear")
.on_press(Message::Clear)
.style(theme::Button::Destructive),
)
.into()
}

View file

@ -1,4 +1,4 @@
use iced::{button, container, pick_list, slider, Background, Color};
use iced::{container, pick_list, slider, Color};
const ACTIVE: Color = Color::from_rgb(
0x72 as f32 / 255.0,
@ -6,12 +6,6 @@ const ACTIVE: Color = Color::from_rgb(
0xDA as f32 / 255.0,
);
const DESTRUCTIVE: Color = Color::from_rgb(
0xC0 as f32 / 255.0,
0x47 as f32 / 255.0,
0x47 as f32 / 255.0,
);
const HOVERED: Color = Color::from_rgb(
0x67 as f32 / 255.0,
0x7B as f32 / 255.0,
@ -35,67 +29,6 @@ impl container::StyleSheet for Container {
}
}
pub struct Button;
impl button::StyleSheet for Button {
fn active(&self) -> button::Style {
button::Style {
background: Some(Background::Color(ACTIVE)),
border_radius: 3.0,
text_color: Color::WHITE,
..button::Style::default()
}
}
fn hovered(&self) -> button::Style {
button::Style {
background: Some(Background::Color(HOVERED)),
text_color: Color::WHITE,
..self.active()
}
}
fn pressed(&self) -> button::Style {
button::Style {
border_width: 1.0,
border_color: Color::WHITE,
..self.hovered()
}
}
}
pub struct Clear;
impl button::StyleSheet for Clear {
fn active(&self) -> button::Style {
button::Style {
background: Some(Background::Color(DESTRUCTIVE)),
border_radius: 3.0,
text_color: Color::WHITE,
..button::Style::default()
}
}
fn hovered(&self) -> button::Style {
button::Style {
background: Some(Background::Color(Color {
a: 0.5,
..DESTRUCTIVE
})),
text_color: Color::WHITE,
..self.active()
}
}
fn pressed(&self) -> button::Style {
button::Style {
border_width: 1.0,
border_color: Color::WHITE,
..self.hovered()
}
}
}
pub struct Slider;
impl slider::StyleSheet for Slider {

View file

@ -4,6 +4,7 @@ use iced::keyboard;
use iced::pure::widget::pane_grid::{self, PaneGrid};
use iced::pure::{button, column, container, row, scrollable, text};
use iced::pure::{Application, Element};
use iced::theme::{self, Theme};
use iced::{Color, Command, Length, Settings, Size, Subscription};
use iced_lazy::pure::responsive;
use iced_native::{event, subscription, Event};
@ -33,6 +34,7 @@ enum Message {
impl Application for Example {
type Message = Message;
type Theme = Theme;
type Executor = executor::Default;
type Flags = ();
@ -161,7 +163,6 @@ impl Application for Example {
text(if pane.is_pinned { "Unpin" } else { "Pin" }).size(14),
)
.on_press(Message::TogglePin(id))
.style(style::Button::Pin)
.padding(3);
let title = row()
@ -259,7 +260,7 @@ fn view_content<'a>(
is_pinned: bool,
size: Size,
) -> Element<'a, Message> {
let button = |label, message, style| {
let button = |label, message| {
button(
text(label)
.width(Length::Fill)
@ -269,7 +270,6 @@ fn view_content<'a>(
.width(Length::Fill)
.padding(8)
.on_press(message)
.style(style)
};
let mut controls = column()
@ -278,20 +278,17 @@ fn view_content<'a>(
.push(button(
"Split horizontally",
Message::Split(pane_grid::Axis::Horizontal, pane),
style::Button::Primary,
))
.push(button(
"Split vertically",
Message::Split(pane_grid::Axis::Vertical, pane),
style::Button::Primary,
));
if total_panes > 1 && !is_pinned {
controls = controls.push(button(
"Close",
Message::Close(pane),
style::Button::Destructive,
));
controls = controls.push(
button("Close", Message::Close(pane))
.style(theme::Button::Destructive),
);
}
let content = column()
@ -315,7 +312,7 @@ fn view_controls<'a>(
is_pinned: bool,
) -> Element<'a, Message> {
let mut button = button(text("Close").size(14))
.style(style::Button::Control)
.style(theme::Button::Destructive)
.padding(3);
if total_panes > 1 && !is_pinned {
@ -326,8 +323,7 @@ fn view_controls<'a>(
}
mod style {
use crate::PANE_ID_COLOR_FOCUSED;
use iced::{button, container, Background, Color, Vector};
use iced::{container, Background, Color};
const SURFACE: Color = Color::from_rgb(
0xF2 as f32 / 255.0,
@ -335,18 +331,6 @@ mod style {
0xF5 as f32 / 255.0,
);
const ACTIVE: Color = Color::from_rgb(
0x72 as f32 / 255.0,
0x89 as f32 / 255.0,
0xDA as f32 / 255.0,
);
const HOVERED: Color = Color::from_rgb(
0x67 as f32 / 255.0,
0x7B as f32 / 255.0,
0xC4 as f32 / 255.0,
);
pub enum TitleBar {
Active,
Focused,
@ -386,51 +370,4 @@ mod style {
}
}
}
pub enum Button {
Primary,
Destructive,
Control,
Pin,
}
impl button::StyleSheet for Button {
fn active(&self) -> button::Style {
let (background, text_color) = match self {
Button::Primary => (Some(ACTIVE), Color::WHITE),
Button::Destructive => {
(None, Color::from_rgb8(0xFF, 0x47, 0x47))
}
Button::Control => (Some(PANE_ID_COLOR_FOCUSED), Color::WHITE),
Button::Pin => (Some(ACTIVE), Color::WHITE),
};
button::Style {
text_color,
background: background.map(Background::Color),
border_radius: 5.0,
shadow_offset: Vector::new(0.0, 0.0),
..button::Style::default()
}
}
fn hovered(&self) -> button::Style {
let active = self.active();
let background = match self {
Button::Primary => Some(HOVERED),
Button::Destructive => Some(Color {
a: 0.2,
..active.text_color
}),
Button::Control => Some(PANE_ID_COLOR_FOCUSED),
Button::Pin => Some(HOVERED),
};
button::Style {
background: background.map(Background::Color),
..active
}
}
}
}

View file

@ -4,6 +4,7 @@ use iced::pure::{
button, checkbox, column, container, row, scrollable, text, text_input,
Application, Element,
};
use iced::theme::{self, Theme};
use iced::window;
use iced::{Command, Font, Length, Settings};
use serde::{Deserialize, Serialize};
@ -44,8 +45,9 @@ enum Message {
}
impl Application for Todos {
type Executor = iced::executor::Default;
type Message = Message;
type Theme = Theme;
type Executor = iced::executor::Default;
type Flags = ();
fn new(_flags: ()) -> (Todos, Command<Message>) {
@ -287,7 +289,7 @@ impl Task {
button(edit_icon())
.on_press(TaskMessage::Edit)
.padding(10)
.style(style::Button::Icon),
.style(theme::Button::Text),
)
.into()
}
@ -313,7 +315,7 @@ impl Task {
)
.on_press(TaskMessage::Delete)
.padding(10)
.style(style::Button::Destructive),
.style(theme::Button::Destructive),
)
.into()
}
@ -328,9 +330,9 @@ fn view_controls(tasks: &[Task], current_filter: Filter) -> Element<Message> {
let label = text(label).size(16);
let button = button(label).style(if filter == current_filter {
style::Button::FilterSelected
theme::Button::Primary
} else {
style::Button::FilterActive
theme::Button::Text
});
button.on_press(Message::FilterChanged(filter)).padding(8)
@ -552,57 +554,3 @@ impl SavedState {
Ok(())
}
}
mod style {
use iced::{button, Background, Color, Vector};
pub enum Button {
FilterActive,
FilterSelected,
Icon,
Destructive,
}
impl button::StyleSheet for Button {
fn active(&self) -> button::Style {
match self {
Button::FilterActive => button::Style::default(),
Button::FilterSelected => button::Style {
background: Some(Background::Color(Color::from_rgb(
0.2, 0.2, 0.7,
))),
border_radius: 10.0,
text_color: Color::WHITE,
..button::Style::default()
},
Button::Icon => button::Style {
text_color: Color::from_rgb(0.5, 0.5, 0.5),
..button::Style::default()
},
Button::Destructive => button::Style {
background: Some(Background::Color(Color::from_rgb(
0.8, 0.2, 0.2,
))),
border_radius: 5.0,
text_color: Color::WHITE,
shadow_offset: Vector::new(1.0, 1.0),
..button::Style::default()
},
}
}
fn hovered(&self) -> button::Style {
let active = self.active();
button::Style {
text_color: match self {
Button::Icon => Color::from_rgb(0.2, 0.2, 0.7),
Button::FilterActive => Color::from_rgb(0.2, 0.2, 0.7),
_ => active.text_color,
},
shadow_offset: active.shadow_offset + Vector::new(0.0, 1.0),
..active
}
}
}
}

View file

@ -5,6 +5,7 @@ use iced::pure::{
scrollable, slider, text, text_input, toggler, vertical_space,
};
use iced::pure::{Element, Sandbox};
use iced::theme;
use iced::{Color, Length, Settings};
pub fn main() -> iced::Result {
@ -55,7 +56,7 @@ impl Sandbox for Tour {
controls = controls.push(
button("Back")
.on_press(Message::BackPressed)
.style(style::Button::Secondary),
.style(theme::Button::Secondary),
);
}
@ -65,7 +66,7 @@ impl Sandbox for Tour {
controls = controls.push(
button("Next")
.on_press(Message::NextPressed)
.style(style::Button::Primary),
.style(theme::Button::Primary),
);
}
@ -669,35 +670,3 @@ pub enum Layout {
Row,
Column,
}
mod style {
use iced::{button, Background, Color, Vector};
pub enum Button {
Primary,
Secondary,
}
impl button::StyleSheet for Button {
fn active(&self) -> button::Style {
button::Style {
background: Some(Background::Color(match self {
Button::Primary => Color::from_rgb(0.11, 0.42, 0.87),
Button::Secondary => Color::from_rgb(0.5, 0.5, 0.5),
})),
border_radius: 12.0,
shadow_offset: Vector::new(1.0, 1.0),
text_color: Color::from_rgb8(0xEE, 0xEE, 0xEE),
..button::Style::default()
}
}
fn hovered(&self) -> button::Style {
button::Style {
text_color: Color::WHITE,
shadow_offset: Vector::new(1.0, 2.0),
..self.active()
}
}
}
}