Add theme::Application styling support to Sandbox
This commit is contained in:
parent
bb07d017e8
commit
3514bd1535
2 changed files with 22 additions and 62 deletions
|
|
@ -7,7 +7,7 @@ use iced::theme;
|
||||||
use iced::{
|
use iced::{
|
||||||
Button, Checkbox, Color, Column, Container, ContentFit, Element, Image,
|
Button, Checkbox, Color, Column, Container, ContentFit, Element, Image,
|
||||||
Length, Radio, Row, Sandbox, Scrollable, Settings, Slider, Space, Text,
|
Length, Radio, Row, Sandbox, Scrollable, Settings, Slider, Space, Text,
|
||||||
TextInput, Theme, Toggler,
|
TextInput, Toggler,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
|
|
@ -21,7 +21,6 @@ pub struct Tour {
|
||||||
scroll: scrollable::State,
|
scroll: scrollable::State,
|
||||||
back_button: button::State,
|
back_button: button::State,
|
||||||
next_button: button::State,
|
next_button: button::State,
|
||||||
theme: Theme,
|
|
||||||
debug: bool,
|
debug: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -34,7 +33,6 @@ impl Sandbox for Tour {
|
||||||
scroll: scrollable::State::new(),
|
scroll: scrollable::State::new(),
|
||||||
back_button: button::State::new(),
|
back_button: button::State::new(),
|
||||||
next_button: button::State::new(),
|
next_button: button::State::new(),
|
||||||
theme: Theme::default(),
|
|
||||||
debug: false,
|
debug: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -52,8 +50,7 @@ impl Sandbox for Tour {
|
||||||
self.steps.advance();
|
self.steps.advance();
|
||||||
}
|
}
|
||||||
Message::StepMessage(step_msg) => {
|
Message::StepMessage(step_msg) => {
|
||||||
self.steps
|
self.steps.update(step_msg, &mut self.debug);
|
||||||
.update(step_msg, &mut self.theme, &mut self.debug);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -91,7 +88,7 @@ impl Sandbox for Tour {
|
||||||
.max_width(540)
|
.max_width(540)
|
||||||
.spacing(20)
|
.spacing(20)
|
||||||
.padding(20)
|
.padding(20)
|
||||||
.push(steps.view(self.theme, self.debug).map(Message::StepMessage))
|
.push(steps.view(self.debug).map(Message::StepMessage))
|
||||||
.push(controls)
|
.push(controls)
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
|
|
@ -109,10 +106,6 @@ impl Sandbox for Tour {
|
||||||
.center_y()
|
.center_y()
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn theme(&self) -> Theme {
|
|
||||||
self.theme
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
|
@ -132,7 +125,6 @@ impl Steps {
|
||||||
Steps {
|
Steps {
|
||||||
steps: vec![
|
steps: vec![
|
||||||
Step::Welcome,
|
Step::Welcome,
|
||||||
Step::Theming,
|
|
||||||
Step::Slider {
|
Step::Slider {
|
||||||
state: slider::State::new(),
|
state: slider::State::new(),
|
||||||
value: 50,
|
value: 50,
|
||||||
|
|
@ -170,17 +162,12 @@ impl Steps {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(
|
fn update(&mut self, msg: StepMessage, debug: &mut bool) {
|
||||||
&mut self,
|
self.steps[self.current].update(msg, debug);
|
||||||
msg: StepMessage,
|
|
||||||
theme: &mut Theme,
|
|
||||||
debug: &mut bool,
|
|
||||||
) {
|
|
||||||
self.steps[self.current].update(msg, theme, debug);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&mut self, theme: Theme, debug: bool) -> Element<StepMessage> {
|
fn view(&mut self, debug: bool) -> Element<StepMessage> {
|
||||||
self.steps[self.current].view(theme, debug)
|
self.steps[self.current].view(debug)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn advance(&mut self) {
|
fn advance(&mut self) {
|
||||||
|
|
@ -211,7 +198,6 @@ impl Steps {
|
||||||
|
|
||||||
enum Step {
|
enum Step {
|
||||||
Welcome,
|
Welcome,
|
||||||
Theming,
|
|
||||||
Slider {
|
Slider {
|
||||||
state: slider::State,
|
state: slider::State,
|
||||||
value: u8,
|
value: u8,
|
||||||
|
|
@ -250,7 +236,6 @@ enum Step {
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum StepMessage {
|
pub enum StepMessage {
|
||||||
ThemeSelected(Theme),
|
|
||||||
SliderChanged(u8),
|
SliderChanged(u8),
|
||||||
LayoutChanged(Layout),
|
LayoutChanged(Layout),
|
||||||
SpacingChanged(u16),
|
SpacingChanged(u16),
|
||||||
|
|
@ -266,16 +251,8 @@ pub enum StepMessage {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Step {
|
impl<'a> Step {
|
||||||
fn update(
|
fn update(&mut self, msg: StepMessage, debug: &mut bool) {
|
||||||
&mut self,
|
|
||||||
msg: StepMessage,
|
|
||||||
theme: &mut Theme,
|
|
||||||
debug: &mut bool,
|
|
||||||
) {
|
|
||||||
match msg {
|
match msg {
|
||||||
StepMessage::ThemeSelected(new_theme) => {
|
|
||||||
*theme = new_theme;
|
|
||||||
}
|
|
||||||
StepMessage::DebugToggled(value) => {
|
StepMessage::DebugToggled(value) => {
|
||||||
if let Step::Debugger = self {
|
if let Step::Debugger = self {
|
||||||
*debug = value;
|
*debug = value;
|
||||||
|
|
@ -342,7 +319,6 @@ impl<'a> Step {
|
||||||
fn title(&self) -> &str {
|
fn title(&self) -> &str {
|
||||||
match self {
|
match self {
|
||||||
Step::Welcome => "Welcome",
|
Step::Welcome => "Welcome",
|
||||||
Step::Theming => "Theming",
|
|
||||||
Step::Radio { .. } => "Radio button",
|
Step::Radio { .. } => "Radio button",
|
||||||
Step::Toggler { .. } => "Toggler",
|
Step::Toggler { .. } => "Toggler",
|
||||||
Step::Slider { .. } => "Slider",
|
Step::Slider { .. } => "Slider",
|
||||||
|
|
@ -359,7 +335,6 @@ impl<'a> Step {
|
||||||
fn can_continue(&self) -> bool {
|
fn can_continue(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Step::Welcome => true,
|
Step::Welcome => true,
|
||||||
Step::Theming => true,
|
|
||||||
Step::Radio { selection } => *selection == Some(Language::Rust),
|
Step::Radio { selection } => *selection == Some(Language::Rust),
|
||||||
Step::Toggler { can_continue } => *can_continue,
|
Step::Toggler { can_continue } => *can_continue,
|
||||||
Step::Slider { .. } => true,
|
Step::Slider { .. } => true,
|
||||||
|
|
@ -373,10 +348,9 @@ impl<'a> Step {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&mut self, theme: Theme, debug: bool) -> Element<StepMessage> {
|
fn view(&mut self, debug: bool) -> Element<StepMessage> {
|
||||||
match self {
|
match self {
|
||||||
Step::Welcome => Self::welcome(),
|
Step::Welcome => Self::welcome(),
|
||||||
Step::Theming => Self::theme(theme),
|
|
||||||
Step::Radio { selection } => Self::radio(*selection),
|
Step::Radio { selection } => Self::radio(*selection),
|
||||||
Step::Toggler { can_continue } => Self::toggler(*can_continue),
|
Step::Toggler { can_continue } => Self::toggler(*can_continue),
|
||||||
Step::Slider { state, value } => Self::slider(state, *value),
|
Step::Slider { state, value } => Self::slider(state, *value),
|
||||||
|
|
@ -441,30 +415,6 @@ impl<'a> Step {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn theme(theme: Theme) -> Column<'a, StepMessage> {
|
|
||||||
let light_radio = Radio::new(
|
|
||||||
Theme::Light,
|
|
||||||
"Light",
|
|
||||||
Some(theme),
|
|
||||||
StepMessage::ThemeSelected,
|
|
||||||
);
|
|
||||||
|
|
||||||
let dark_radio = Radio::new(
|
|
||||||
Theme::Dark,
|
|
||||||
"Dark",
|
|
||||||
Some(theme),
|
|
||||||
StepMessage::ThemeSelected,
|
|
||||||
);
|
|
||||||
|
|
||||||
Self::container("Theming")
|
|
||||||
.push(Text::new(
|
|
||||||
"You can easily change the appearance of an application made \
|
|
||||||
with Iced by selecting a different theme!",
|
|
||||||
))
|
|
||||||
.push(light_radio)
|
|
||||||
.push(dark_radio)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn slider(
|
fn slider(
|
||||||
state: &'a mut slider::State,
|
state: &'a mut slider::State,
|
||||||
value: u8,
|
value: u8,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
use crate::{
|
use crate::theme::{self, Theme};
|
||||||
Application, Command, Element, Error, Settings, Subscription, Theme,
|
use crate::{Application, Command, Element, Error, Settings, Subscription};
|
||||||
};
|
|
||||||
|
|
||||||
/// A sandboxed [`Application`].
|
/// A sandboxed [`Application`].
|
||||||
///
|
///
|
||||||
|
|
@ -121,6 +120,13 @@ pub trait Sandbox {
|
||||||
Theme::default()
|
Theme::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the current style variant of [`theme::Application`].
|
||||||
|
///
|
||||||
|
/// By default, it returns [`theme::Application::default`].
|
||||||
|
fn style(&self) -> theme::Application {
|
||||||
|
theme::Application::default()
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the scale factor of the [`Sandbox`].
|
/// Returns the scale factor of the [`Sandbox`].
|
||||||
///
|
///
|
||||||
/// It can be used to dynamically control the size of the UI at runtime
|
/// It can be used to dynamically control the size of the UI at runtime
|
||||||
|
|
@ -186,6 +192,10 @@ where
|
||||||
T::theme(self)
|
T::theme(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn style(&self) -> theme::Application {
|
||||||
|
T::style(self)
|
||||||
|
}
|
||||||
|
|
||||||
fn subscription(&self) -> Subscription<T::Message> {
|
fn subscription(&self) -> Subscription<T::Message> {
|
||||||
Subscription::none()
|
Subscription::none()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue