summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/download_progress/Cargo.toml2
-rw-r--r--examples/download_progress/src/download.rs14
-rw-r--r--examples/editor/src/main.rs39
-rw-r--r--examples/multi_window/src/main.rs16
-rw-r--r--examples/pokedex/Cargo.toml2
-rw-r--r--examples/pokedex/src/main.rs14
-rw-r--r--examples/slider/Cargo.toml1
-rw-r--r--examples/slider/src/main.rs43
-rw-r--r--examples/todos/src/main.rs13
-rw-r--r--examples/websocket/src/echo.rs102
-rw-r--r--examples/websocket/src/main.rs29
11 files changed, 131 insertions, 144 deletions
diff --git a/examples/download_progress/Cargo.toml b/examples/download_progress/Cargo.toml
index 18a49f66..f78df529 100644
--- a/examples/download_progress/Cargo.toml
+++ b/examples/download_progress/Cargo.toml
@@ -10,6 +10,6 @@ iced.workspace = true
iced.features = ["tokio"]
[dependencies.reqwest]
-version = "0.11"
+version = "0.12"
default-features = false
features = ["rustls-tls"]
diff --git a/examples/download_progress/src/download.rs b/examples/download_progress/src/download.rs
index d6cc1e24..bdf57290 100644
--- a/examples/download_progress/src/download.rs
+++ b/examples/download_progress/src/download.rs
@@ -1,4 +1,5 @@
-use iced::subscription;
+use iced::futures;
+use iced::Subscription;
use std::hash::Hash;
@@ -7,9 +8,14 @@ pub fn file<I: 'static + Hash + Copy + Send + Sync, T: ToString>(
id: I,
url: T,
) -> iced::Subscription<(I, Progress)> {
- subscription::unfold(id, State::Ready(url.to_string()), move |state| {
- download(id, state)
- })
+ Subscription::run_with_id(
+ id,
+ futures::stream::unfold(State::Ready(url.to_string()), move |state| {
+ use iced::futures::FutureExt;
+
+ download(id, state).map(Some)
+ }),
+ )
}
async fn download<I: Copy>(id: I, state: State) -> ((I, Progress), State) {
diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs
index bed9d94a..ce3c478d 100644
--- a/examples/editor/src/main.rs
+++ b/examples/editor/src/main.rs
@@ -13,12 +13,11 @@ use std::sync::Arc;
pub fn main() -> iced::Result {
iced::application("Editor - Iced", Editor::update, Editor::view)
- .load(Editor::load)
.subscription(Editor::subscription)
.theme(Editor::theme)
.font(include_bytes!("../fonts/icons.ttf").as_slice())
.default_font(Font::MONOSPACE)
- .run()
+ .run_with(Editor::new)
}
struct Editor {
@@ -41,20 +40,22 @@ enum Message {
}
impl Editor {
- fn new() -> Self {
- Self {
- file: None,
- content: text_editor::Content::new(),
- theme: highlighter::Theme::SolarizedDark,
- is_loading: true,
- is_dirty: false,
- }
- }
-
- fn load() -> Task<Message> {
- Task::perform(
- load_file(format!("{}/src/main.rs", env!("CARGO_MANIFEST_DIR"))),
- Message::FileOpened,
+ fn new() -> (Self, Task<Message>) {
+ (
+ Self {
+ file: None,
+ content: text_editor::Content::new(),
+ theme: highlighter::Theme::SolarizedDark,
+ is_loading: true,
+ is_dirty: false,
+ },
+ Task::perform(
+ load_file(format!(
+ "{}/src/main.rs",
+ env!("CARGO_MANIFEST_DIR")
+ )),
+ Message::FileOpened,
+ ),
)
}
@@ -214,12 +215,6 @@ impl Editor {
}
}
-impl Default for Editor {
- fn default() -> Self {
- Self::new()
- }
-}
-
#[derive(Debug, Clone)]
pub enum Error {
DialogClosed,
diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs
index 98e753ab..460ca3b5 100644
--- a/examples/multi_window/src/main.rs
+++ b/examples/multi_window/src/main.rs
@@ -9,16 +9,12 @@ use std::collections::BTreeMap;
fn main() -> iced::Result {
iced::daemon(Example::title, Example::update, Example::view)
- .load(|| {
- window::open(window::Settings::default()).map(Message::WindowOpened)
- })
.subscription(Example::subscription)
.theme(Example::theme)
.scale_factor(Example::scale_factor)
- .run()
+ .run_with(Example::new)
}
-#[derive(Default)]
struct Example {
windows: BTreeMap<window::Id, Window>,
}
@@ -43,6 +39,16 @@ enum Message {
}
impl Example {
+ fn new() -> (Self, Task<Message>) {
+ (
+ Self {
+ windows: BTreeMap::new(),
+ },
+ window::open(window::Settings::default())
+ .map(Message::WindowOpened),
+ )
+ }
+
fn title(&self, window: window::Id) -> String {
self.windows
.get(&window)
diff --git a/examples/pokedex/Cargo.toml b/examples/pokedex/Cargo.toml
index bf7e1e35..1a6d5445 100644
--- a/examples/pokedex/Cargo.toml
+++ b/examples/pokedex/Cargo.toml
@@ -16,7 +16,7 @@ version = "1.0"
features = ["derive"]
[dependencies.reqwest]
-version = "0.11"
+version = "0.12"
default-features = false
features = ["json", "rustls-tls"]
diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs
index b22ffe7f..7414ae54 100644
--- a/examples/pokedex/src/main.rs
+++ b/examples/pokedex/src/main.rs
@@ -4,17 +4,13 @@ use iced::{Alignment, Element, Length, Task};
pub fn main() -> iced::Result {
iced::application(Pokedex::title, Pokedex::update, Pokedex::view)
- .load(Pokedex::search)
- .run()
+ .run_with(Pokedex::new)
}
-#[derive(Debug, Default)]
+#[derive(Debug)]
enum Pokedex {
- #[default]
Loading,
- Loaded {
- pokemon: Pokemon,
- },
+ Loaded { pokemon: Pokemon },
Errored,
}
@@ -25,6 +21,10 @@ enum Message {
}
impl Pokedex {
+ fn new() -> (Self, Task<Message>) {
+ (Self::Loading, Self::search())
+ }
+
fn search() -> Task<Message> {
Task::perform(Pokemon::search(), Message::PokemonFound)
}
diff --git a/examples/slider/Cargo.toml b/examples/slider/Cargo.toml
index fad8916e..05e74d2c 100644
--- a/examples/slider/Cargo.toml
+++ b/examples/slider/Cargo.toml
@@ -7,3 +7,4 @@ publish = false
[dependencies]
iced.workspace = true
+iced.features = ["svg"]
diff --git a/examples/slider/src/main.rs b/examples/slider/src/main.rs
index 0b4c29aa..fd312763 100644
--- a/examples/slider/src/main.rs
+++ b/examples/slider/src/main.rs
@@ -1,5 +1,5 @@
-use iced::widget::{center, column, container, slider, text, vertical_slider};
-use iced::{Element, Length};
+use iced::widget::{column, container, iced, slider, text, vertical_slider};
+use iced::{Alignment, Element, Length};
pub fn main() -> iced::Result {
iced::run("Slider - Iced", Slider::update, Slider::view)
@@ -12,19 +12,11 @@ pub enum Message {
pub struct Slider {
value: u8,
- default: u8,
- step: u8,
- shift_step: u8,
}
impl Slider {
fn new() -> Self {
- Slider {
- value: 50,
- default: 50,
- step: 5,
- shift_step: 1,
- }
+ Slider { value: 50 }
}
fn update(&mut self, message: Message) {
@@ -37,32 +29,27 @@ impl Slider {
fn view(&self) -> Element<Message> {
let h_slider = container(
- slider(0..=100, self.value, Message::SliderChanged)
- .default(self.default)
- .step(self.step)
- .shift_step(self.shift_step),
+ slider(1..=100, self.value, Message::SliderChanged)
+ .default(50)
+ .shift_step(5),
)
.width(250);
let v_slider = container(
- vertical_slider(0..=100, self.value, Message::SliderChanged)
- .default(self.default)
- .step(self.step)
- .shift_step(self.shift_step),
+ vertical_slider(1..=100, self.value, Message::SliderChanged)
+ .default(50)
+ .shift_step(5),
)
.height(200);
let text = text(self.value);
- center(
- column![
- container(v_slider).center_x(Length::Fill),
- container(h_slider).center_x(Length::Fill),
- container(text).center_x(Length::Fill)
- ]
- .spacing(25),
- )
- .into()
+ column![v_slider, h_slider, text, iced(self.value as f32),]
+ .width(Length::Fill)
+ .align_items(Alignment::Center)
+ .spacing(20)
+ .padding(20)
+ .into()
}
}
diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs
index 6ed50d31..b34f71ce 100644
--- a/examples/todos/src/main.rs
+++ b/examples/todos/src/main.rs
@@ -18,16 +18,14 @@ pub fn main() -> iced::Result {
tracing_subscriber::fmt::init();
iced::application(Todos::title, Todos::update, Todos::view)
- .load(Todos::load)
.subscription(Todos::subscription)
.font(include_bytes!("../fonts/icons.ttf").as_slice())
.window_size((500.0, 800.0))
- .run()
+ .run_with(Todos::new)
}
-#[derive(Default, Debug)]
+#[derive(Debug)]
enum Todos {
- #[default]
Loading,
Loaded(State),
}
@@ -54,8 +52,11 @@ enum Message {
}
impl Todos {
- fn load() -> Command<Message> {
- Command::perform(SavedState::load(), Message::Loaded)
+ fn new() -> (Self, Command<Message>) {
+ (
+ Self::Loading,
+ Command::perform(SavedState::load(), Message::Loaded),
+ )
}
fn title(&self) -> String {
diff --git a/examples/websocket/src/echo.rs b/examples/websocket/src/echo.rs
index cd32cb66..14652936 100644
--- a/examples/websocket/src/echo.rs
+++ b/examples/websocket/src/echo.rs
@@ -1,87 +1,79 @@
pub mod server;
use iced::futures;
-use iced::subscription::{self, Subscription};
+use iced::stream;
use iced::widget::text;
use futures::channel::mpsc;
use futures::sink::SinkExt;
-use futures::stream::StreamExt;
+use futures::stream::{Stream, StreamExt};
use async_tungstenite::tungstenite;
use std::fmt;
-pub fn connect() -> Subscription<Event> {
- struct Connect;
+pub fn connect() -> impl Stream<Item = Event> {
+ stream::channel(100, |mut output| async move {
+ let mut state = State::Disconnected;
- subscription::channel(
- std::any::TypeId::of::<Connect>(),
- 100,
- |mut output| async move {
- let mut state = State::Disconnected;
+ loop {
+ match &mut state {
+ State::Disconnected => {
+ const ECHO_SERVER: &str = "ws://127.0.0.1:3030";
- loop {
- match &mut state {
- State::Disconnected => {
- const ECHO_SERVER: &str = "ws://127.0.0.1:3030";
-
- match async_tungstenite::tokio::connect_async(
- ECHO_SERVER,
- )
+ match async_tungstenite::tokio::connect_async(ECHO_SERVER)
.await
- {
- Ok((websocket, _)) => {
- let (sender, receiver) = mpsc::channel(100);
-
- let _ = output
- .send(Event::Connected(Connection(sender)))
- .await;
+ {
+ Ok((websocket, _)) => {
+ let (sender, receiver) = mpsc::channel(100);
- state = State::Connected(websocket, receiver);
- }
- Err(_) => {
- tokio::time::sleep(
- tokio::time::Duration::from_secs(1),
- )
+ let _ = output
+ .send(Event::Connected(Connection(sender)))
.await;
- let _ = output.send(Event::Disconnected).await;
- }
+ state = State::Connected(websocket, receiver);
+ }
+ Err(_) => {
+ tokio::time::sleep(
+ tokio::time::Duration::from_secs(1),
+ )
+ .await;
+
+ let _ = output.send(Event::Disconnected).await;
}
}
- State::Connected(websocket, input) => {
- let mut fused_websocket = websocket.by_ref().fuse();
-
- futures::select! {
- received = fused_websocket.select_next_some() => {
- match received {
- Ok(tungstenite::Message::Text(message)) => {
- let _ = output.send(Event::MessageReceived(Message::User(message))).await;
- }
- Err(_) => {
- let _ = output.send(Event::Disconnected).await;
-
- state = State::Disconnected;
- }
- Ok(_) => continue,
+ }
+ State::Connected(websocket, input) => {
+ let mut fused_websocket = websocket.by_ref().fuse();
+
+ futures::select! {
+ received = fused_websocket.select_next_some() => {
+ match received {
+ Ok(tungstenite::Message::Text(message)) => {
+ let _ = output.send(Event::MessageReceived(Message::User(message))).await;
}
- }
-
- message = input.select_next_some() => {
- let result = websocket.send(tungstenite::Message::Text(message.to_string())).await;
-
- if result.is_err() {
+ Err(_) => {
let _ = output.send(Event::Disconnected).await;
state = State::Disconnected;
}
+ Ok(_) => continue,
+ }
+ }
+
+ message = input.select_next_some() => {
+ let result = websocket.send(tungstenite::Message::Text(message.to_string())).await;
+
+ if result.is_err() {
+ let _ = output.send(Event::Disconnected).await;
+
+ state = State::Disconnected;
}
}
}
}
}
- },
- )
+ }
+ })
}
#[derive(Debug)]
diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs
index 8422ce16..d8246436 100644
--- a/examples/websocket/src/main.rs
+++ b/examples/websocket/src/main.rs
@@ -9,12 +9,10 @@ use once_cell::sync::Lazy;
pub fn main() -> iced::Result {
iced::application("WebSocket - Iced", WebSocket::update, WebSocket::view)
- .load(WebSocket::load)
.subscription(WebSocket::subscription)
- .run()
+ .run_with(WebSocket::new)
}
-#[derive(Default)]
struct WebSocket {
messages: Vec<echo::Message>,
new_message: String,
@@ -30,11 +28,18 @@ enum Message {
}
impl WebSocket {
- fn load() -> Task<Message> {
- Task::batch([
- Task::perform(echo::server::run(), |_| Message::Server),
- widget::focus_next(),
- ])
+ fn new() -> (Self, Task<Message>) {
+ (
+ Self {
+ messages: Vec::new(),
+ new_message: String::new(),
+ state: State::Disconnected,
+ },
+ Task::batch([
+ Task::perform(echo::server::run(), |_| Message::Server),
+ widget::focus_next(),
+ ]),
+ )
}
fn update(&mut self, message: Message) -> Task<Message> {
@@ -83,7 +88,7 @@ impl WebSocket {
}
fn subscription(&self) -> Subscription<Message> {
- echo::connect().map(Message::Echo)
+ Subscription::run(echo::connect).map(Message::Echo)
}
fn view(&self) -> Element<Message> {
@@ -140,10 +145,4 @@ enum State {
Connected(echo::Connection),
}
-impl Default for State {
- fn default() -> Self {
- Self::Disconnected
- }
-}
-
static MESSAGE_LOG: Lazy<scrollable::Id> = Lazy::new(scrollable::Id::unique);