Implement Program::load to specify startup Command
This commit is contained in:
parent
5a986897d2
commit
93ae790da1
8 changed files with 122 additions and 77 deletions
|
|
@ -5,8 +5,7 @@ use iced::window;
|
||||||
use iced::{Alignment, Command, Element, Length, Subscription};
|
use iced::{Alignment, Command, Element, Length, Subscription};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::application(Events::new, Events::update, Events::view)
|
iced::application("Events - Iced", Events::update, Events::view)
|
||||||
.title("Events - Iced")
|
|
||||||
.subscription(Events::subscription)
|
.subscription(Events::subscription)
|
||||||
.ignore_close_request()
|
.ignore_close_request()
|
||||||
.run()
|
.run()
|
||||||
|
|
@ -26,10 +25,6 @@ enum Message {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Events {
|
impl Events {
|
||||||
fn new() -> (Events, Command<Message>) {
|
|
||||||
(Events::default(), Command::none())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update(&mut self, message: Message) -> Command<Message> {
|
fn update(&mut self, message: Message) -> Command<Message> {
|
||||||
match message {
|
match message {
|
||||||
Message::EventOccurred(event) if self.enabled => {
|
Message::EventOccurred(event) if self.enabled => {
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,7 @@ use iced::window;
|
||||||
use iced::{Alignment, Command, Element, Length};
|
use iced::{Alignment, Command, Element, Length};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::application(Exit::new, Exit::update, Exit::view)
|
iced::application("Exit - Iced", Exit::update, Exit::view).run()
|
||||||
.title("Exit - Iced")
|
|
||||||
.run()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|
@ -20,10 +18,6 @@ enum Message {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Exit {
|
impl Exit {
|
||||||
fn new() -> (Self, Command<Message>) {
|
|
||||||
(Self::default(), Command::none())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update(&mut self, message: Message) -> Command<Message> {
|
fn update(&mut self, message: Message) -> Command<Message> {
|
||||||
match message {
|
match message {
|
||||||
Message::Confirm => window::close(window::Id::MAIN),
|
Message::Confirm => window::close(window::Id::MAIN),
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,18 @@ use iced::widget::{self, column, container, image, row, text};
|
||||||
use iced::{Alignment, Command, Element, Length};
|
use iced::{Alignment, Command, Element, Length};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::application(Pokedex::new, Pokedex::update, Pokedex::view)
|
iced::application(Pokedex::title, Pokedex::update, Pokedex::view)
|
||||||
.title(Pokedex::title)
|
.load(Pokedex::load)
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Default)]
|
||||||
enum Pokedex {
|
enum Pokedex {
|
||||||
|
#[default]
|
||||||
Loading,
|
Loading,
|
||||||
Loaded { pokemon: Pokemon },
|
Loaded {
|
||||||
|
pokemon: Pokemon,
|
||||||
|
},
|
||||||
Errored,
|
Errored,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -22,11 +25,8 @@ enum Message {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pokedex {
|
impl Pokedex {
|
||||||
fn new() -> (Self, Command<Message>) {
|
fn load() -> Command<Message> {
|
||||||
(
|
Command::perform(Pokemon::search(), Message::PokemonFound)
|
||||||
Pokedex::Loading,
|
|
||||||
Command::perform(Pokemon::search(), Message::PokemonFound),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title(&self) -> String {
|
fn title(&self) -> String {
|
||||||
|
|
|
||||||
|
|
@ -13,12 +13,12 @@ use ::image::ColorType;
|
||||||
fn main() -> iced::Result {
|
fn main() -> iced::Result {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
iced::application(Example::new, Example::update, Example::view)
|
iced::application("Screenshot - Iced", Example::update, Example::view)
|
||||||
.subscription(Example::subscription)
|
.subscription(Example::subscription)
|
||||||
.title("Screenshot - Iced")
|
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
struct Example {
|
struct Example {
|
||||||
screenshot: Option<Screenshot>,
|
screenshot: Option<Screenshot>,
|
||||||
saved_png_path: Option<Result<String, PngError>>,
|
saved_png_path: Option<Result<String, PngError>>,
|
||||||
|
|
@ -44,22 +44,6 @@ enum Message {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Example {
|
impl Example {
|
||||||
fn new() -> (Self, Command<Message>) {
|
|
||||||
(
|
|
||||||
Example {
|
|
||||||
screenshot: None,
|
|
||||||
saved_png_path: None,
|
|
||||||
png_saving: false,
|
|
||||||
crop_error: None,
|
|
||||||
x_input_value: None,
|
|
||||||
y_input_value: None,
|
|
||||||
width_input_value: None,
|
|
||||||
height_input_value: None,
|
|
||||||
},
|
|
||||||
Command::none(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update(&mut self, message: Message) -> Command<Message> {
|
fn update(&mut self, message: Message) -> Command<Message> {
|
||||||
match message {
|
match message {
|
||||||
Message::Screenshot => {
|
Message::Screenshot => {
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,11 @@ static SCROLLABLE_ID: Lazy<scrollable::Id> = Lazy::new(scrollable::Id::unique);
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::application(
|
iced::application(
|
||||||
ScrollableDemo::new,
|
"Scrollable - Iced",
|
||||||
ScrollableDemo::update,
|
ScrollableDemo::update,
|
||||||
ScrollableDemo::view,
|
ScrollableDemo::view,
|
||||||
)
|
)
|
||||||
.theme(ScrollableDemo::theme)
|
.theme(ScrollableDemo::theme)
|
||||||
.title("Scrollable - Iced")
|
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -49,8 +48,7 @@ enum Message {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ScrollableDemo {
|
impl ScrollableDemo {
|
||||||
fn new() -> (Self, Command<Message>) {
|
fn new() -> Self {
|
||||||
(
|
|
||||||
ScrollableDemo {
|
ScrollableDemo {
|
||||||
scrollable_direction: Direction::Vertical,
|
scrollable_direction: Direction::Vertical,
|
||||||
scrollbar_width: 10,
|
scrollbar_width: 10,
|
||||||
|
|
@ -58,9 +56,7 @@ impl ScrollableDemo {
|
||||||
scroller_width: 10,
|
scroller_width: 10,
|
||||||
current_scroll_offset: scrollable::RelativeOffset::START,
|
current_scroll_offset: scrollable::RelativeOffset::START,
|
||||||
alignment: scrollable::Alignment::Start,
|
alignment: scrollable::Alignment::Start,
|
||||||
},
|
}
|
||||||
Command::none(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, message: Message) -> Command<Message> {
|
fn update(&mut self, message: Message) -> Command<Message> {
|
||||||
|
|
@ -339,6 +335,12 @@ impl ScrollableDemo {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for ScrollableDemo {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn progress_bar_custom_style(theme: &Theme) -> progress_bar::Appearance {
|
fn progress_bar_custom_style(theme: &Theme) -> progress_bar::Appearance {
|
||||||
progress_bar::Appearance {
|
progress_bar::Appearance {
|
||||||
background: theme.extended_palette().background.strong.color.into(),
|
background: theme.extended_palette().background.strong.color.into(),
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,23 @@
|
||||||
use iced::widget::{button, column, container, text};
|
use iced::widget::{button, column, container, text};
|
||||||
use iced::{system, Command, Element, Length};
|
use iced::{system, Command, Element, Length};
|
||||||
|
|
||||||
use bytesize::ByteSize;
|
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::application(Example::new, Example::update, Example::view)
|
iced::application(
|
||||||
.title("System Information - Iced")
|
"System Information - Iced",
|
||||||
|
Example::update,
|
||||||
|
Example::view,
|
||||||
|
)
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
#[allow(clippy::large_enum_variant)]
|
#[allow(clippy::large_enum_variant)]
|
||||||
enum Example {
|
enum Example {
|
||||||
|
#[default]
|
||||||
Loading,
|
Loading,
|
||||||
Loaded { information: system::Information },
|
Loaded {
|
||||||
|
information: system::Information,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
|
@ -23,13 +28,6 @@ enum Message {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Example {
|
impl Example {
|
||||||
fn new() -> (Self, Command<Message>) {
|
|
||||||
(
|
|
||||||
Self::Loading,
|
|
||||||
system::fetch_information(Message::InformationReceived),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update(&mut self, message: Message) -> Command<Message> {
|
fn update(&mut self, message: Message) -> Command<Message> {
|
||||||
match message {
|
match message {
|
||||||
Message::Refresh => {
|
Message::Refresh => {
|
||||||
|
|
@ -46,6 +44,8 @@ impl Example {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self) -> Element<Message> {
|
fn view(&self) -> Element<Message> {
|
||||||
|
use bytesize::ByteSize;
|
||||||
|
|
||||||
let content: Element<_> = match self {
|
let content: Element<_> = match self {
|
||||||
Example::Loading => text("Loading...").size(40).into(),
|
Example::Loading => text("Loading...").size(40).into(),
|
||||||
Example::Loaded { information } => {
|
Example::Loaded { information } => {
|
||||||
|
|
|
||||||
|
|
@ -362,7 +362,7 @@ pub type Result = std::result::Result<(), Error>;
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn run<State, Message>(
|
pub fn run<State, Message>(
|
||||||
title: &'static str,
|
title: impl program::Title<State> + 'static,
|
||||||
update: impl Fn(&mut State, Message) + 'static,
|
update: impl Fn(&mut State, Message) + 'static,
|
||||||
view: impl for<'a> program::View<'a, State, Message> + 'static,
|
view: impl for<'a> program::View<'a, State, Message> + 'static,
|
||||||
) -> Result
|
) -> Result
|
||||||
|
|
|
||||||
|
|
@ -143,31 +143,30 @@ where
|
||||||
/// Creates a [`Program`] that can leverage the [`Command`] API for
|
/// Creates a [`Program`] that can leverage the [`Command`] API for
|
||||||
/// concurrent operations.
|
/// concurrent operations.
|
||||||
pub fn application<State, Message>(
|
pub fn application<State, Message>(
|
||||||
new: impl Fn() -> (State, Command<Message>),
|
title: impl Title<State>,
|
||||||
update: impl Fn(&mut State, Message) -> Command<Message>,
|
update: impl Fn(&mut State, Message) -> Command<Message>,
|
||||||
view: impl for<'a> self::View<'a, State, Message>,
|
view: impl for<'a> self::View<'a, State, Message>,
|
||||||
) -> Program<
|
) -> Program<
|
||||||
impl Definition<State = State, Message = Message, Theme = crate::Theme>,
|
impl Definition<State = State, Message = Message, Theme = crate::Theme>,
|
||||||
>
|
>
|
||||||
where
|
where
|
||||||
State: 'static,
|
State: Default + 'static,
|
||||||
Message: Send + std::fmt::Debug,
|
Message: Send + std::fmt::Debug,
|
||||||
{
|
{
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
struct Application<State, Message, New, Update, View> {
|
struct Application<State, Message, Update, View> {
|
||||||
new: New,
|
|
||||||
update: Update,
|
update: Update,
|
||||||
view: View,
|
view: View,
|
||||||
_state: PhantomData<State>,
|
_state: PhantomData<State>,
|
||||||
_message: PhantomData<Message>,
|
_message: PhantomData<Message>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<State, Message, New, Update, View> Definition
|
impl<State, Message, Update, View> Definition
|
||||||
for Application<State, Message, New, Update, View>
|
for Application<State, Message, Update, View>
|
||||||
where
|
where
|
||||||
|
State: Default,
|
||||||
Message: Send + std::fmt::Debug,
|
Message: Send + std::fmt::Debug,
|
||||||
New: Fn() -> (State, Command<Message>),
|
|
||||||
Update: Fn(&mut State, Message) -> Command<Message>,
|
Update: Fn(&mut State, Message) -> Command<Message>,
|
||||||
View: for<'a> self::View<'a, State, Message>,
|
View: for<'a> self::View<'a, State, Message>,
|
||||||
{
|
{
|
||||||
|
|
@ -177,7 +176,7 @@ where
|
||||||
type Executor = executor::Default;
|
type Executor = executor::Default;
|
||||||
|
|
||||||
fn build(&self) -> (Self::State, Command<Self::Message>) {
|
fn build(&self) -> (Self::State, Command<Self::Message>) {
|
||||||
(self.new)()
|
(Self::State::default(), Command::none())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(
|
fn update(
|
||||||
|
|
@ -198,7 +197,6 @@ where
|
||||||
|
|
||||||
Program {
|
Program {
|
||||||
raw: Application {
|
raw: Application {
|
||||||
new,
|
|
||||||
update,
|
update,
|
||||||
view,
|
view,
|
||||||
_state: PhantomData,
|
_state: PhantomData,
|
||||||
|
|
@ -206,6 +204,7 @@ where
|
||||||
},
|
},
|
||||||
settings: Settings::default(),
|
settings: Settings::default(),
|
||||||
}
|
}
|
||||||
|
.title(title)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A fully functioning and configured iced application.
|
/// A fully functioning and configured iced application.
|
||||||
|
|
@ -367,6 +366,19 @@ impl<P: Definition> Program<P> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Runs the [`Command`] produced by the closure at startup.
|
||||||
|
pub fn load(
|
||||||
|
self,
|
||||||
|
f: impl Fn() -> Command<P::Message>,
|
||||||
|
) -> Program<
|
||||||
|
impl Definition<State = P::State, Message = P::Message, Theme = P::Theme>,
|
||||||
|
> {
|
||||||
|
Program {
|
||||||
|
raw: with_load(self.raw, f),
|
||||||
|
settings: self.settings,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Sets the subscription logic of the [`Program`].
|
/// Sets the subscription logic of the [`Program`].
|
||||||
pub fn subscription(
|
pub fn subscription(
|
||||||
self,
|
self,
|
||||||
|
|
@ -500,6 +512,64 @@ fn with_title<P: Definition>(
|
||||||
WithTitle { program, title }
|
WithTitle { program, title }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn with_load<P: Definition>(
|
||||||
|
program: P,
|
||||||
|
f: impl Fn() -> Command<P::Message>,
|
||||||
|
) -> impl Definition<State = P::State, Message = P::Message, Theme = P::Theme> {
|
||||||
|
struct WithLoad<P, F> {
|
||||||
|
program: P,
|
||||||
|
load: F,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<P: Definition, F> Definition for WithLoad<P, F>
|
||||||
|
where
|
||||||
|
F: Fn() -> Command<P::Message>,
|
||||||
|
{
|
||||||
|
type State = P::State;
|
||||||
|
type Message = P::Message;
|
||||||
|
type Theme = P::Theme;
|
||||||
|
type Executor = executor::Default;
|
||||||
|
|
||||||
|
fn build(&self) -> (Self::State, Command<Self::Message>) {
|
||||||
|
let (state, command) = self.program.build();
|
||||||
|
|
||||||
|
(state, Command::batch([command, (self.load)()]))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(
|
||||||
|
&self,
|
||||||
|
state: &mut Self::State,
|
||||||
|
message: Self::Message,
|
||||||
|
) -> Command<Self::Message> {
|
||||||
|
self.program.update(state, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view<'a>(
|
||||||
|
&self,
|
||||||
|
state: &'a Self::State,
|
||||||
|
) -> Element<'a, Self::Message, Self::Theme> {
|
||||||
|
self.program.view(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn title(&self, state: &Self::State) -> String {
|
||||||
|
self.program.title(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn theme(&self, state: &Self::State) -> Self::Theme {
|
||||||
|
self.program.theme(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn subscription(
|
||||||
|
&self,
|
||||||
|
state: &Self::State,
|
||||||
|
) -> Subscription<Self::Message> {
|
||||||
|
self.program.subscription(state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WithLoad { program, load: f }
|
||||||
|
}
|
||||||
|
|
||||||
fn with_subscription<P: Definition>(
|
fn with_subscription<P: Definition>(
|
||||||
program: P,
|
program: P,
|
||||||
f: impl Fn(&P::State) -> Subscription<P::Message>,
|
f: impl Fn(&P::State) -> Subscription<P::Message>,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue