summaryrefslogtreecommitdiffstats
path: root/examples/websocket
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2022-08-06 00:32:57 +0200
committerLibravatar GitHub <noreply@github.com>2022-08-06 00:32:57 +0200
commit1923dbf7f0769d55e5283f572fde0ce752e28b86 (patch)
tree7be9b36f941f6e13ddc8884f715c04555b1e77db /examples/websocket
parent1b4f38c71f6e05e26599ee75ea9c91dde96e71ae (diff)
parentc23ed7e4a0a2b62a0d7cabe6e35d7323eac543d2 (diff)
downloadiced-1923dbf7f0769d55e5283f572fde0ce752e28b86.tar.gz
iced-1923dbf7f0769d55e5283f572fde0ce752e28b86.tar.bz2
iced-1923dbf7f0769d55e5283f572fde0ce752e28b86.zip
Merge pull request #1393 from iced-rs/deprecate-stateful-widgets
Replace stateful widgets with the new `iced_pure` API
Diffstat (limited to 'examples/websocket')
-rw-r--r--examples/websocket/Cargo.toml1
-rw-r--r--examples/websocket/src/echo.rs15
-rw-r--r--examples/websocket/src/main.rs77
3 files changed, 51 insertions, 42 deletions
diff --git a/examples/websocket/Cargo.toml b/examples/websocket/Cargo.toml
index 327b8324..3981f699 100644
--- a/examples/websocket/Cargo.toml
+++ b/examples/websocket/Cargo.toml
@@ -9,6 +9,7 @@ publish = false
iced = { path = "../..", features = ["tokio", "debug"] }
iced_native = { path = "../../native" }
iced_futures = { path = "../../futures" }
+lazy_static = "1.4"
[dependencies.async-tungstenite]
version = "0.16"
diff --git a/examples/websocket/src/echo.rs b/examples/websocket/src/echo.rs
index 88321880..ae65e064 100644
--- a/examples/websocket/src/echo.rs
+++ b/examples/websocket/src/echo.rs
@@ -8,6 +8,7 @@ use futures::sink::SinkExt;
use futures::stream::StreamExt;
use async_tungstenite::tungstenite;
+use std::fmt;
pub fn connect() -> Subscription<Event> {
struct Connect;
@@ -63,7 +64,7 @@ pub fn connect() -> Subscription<Event> {
}
message = input.select_next_some() => {
- let result = websocket.send(tungstenite::Message::Text(String::from(message))).await;
+ let result = websocket.send(tungstenite::Message::Text(message.to_string())).await;
if result.is_ok() {
(None, State::Connected(websocket, input))
@@ -133,14 +134,14 @@ impl Message {
}
}
-impl From<Message> for String {
- fn from(message: Message) -> Self {
- match message {
- Message::Connected => String::from("Connected successfully!"),
+impl fmt::Display for Message {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Message::Connected => write!(f, "Connected successfully!"),
Message::Disconnected => {
- String::from("Connection lost... Retrying...")
+ write!(f, "Connection lost... Retrying...")
}
- Message::User(message) => message,
+ Message::User(message) => write!(f, "{}", message),
}
}
}
diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs
index 64addc8f..3902e04c 100644
--- a/examples/websocket/src/main.rs
+++ b/examples/websocket/src/main.rs
@@ -1,13 +1,12 @@
mod echo;
use iced::alignment::{self, Alignment};
-use iced::button::{self, Button};
use iced::executor;
-use iced::scrollable::{self, Scrollable};
-use iced::text_input::{self, TextInput};
+use iced::widget::{
+ button, column, container, row, scrollable, text, text_input, Column,
+};
use iced::{
- Application, Color, Column, Command, Container, Element, Length, Row,
- Settings, Subscription, Text, Theme,
+ Application, Color, Command, Element, Length, Settings, Subscription, Theme,
};
pub fn main() -> iced::Result {
@@ -17,10 +16,7 @@ pub fn main() -> iced::Result {
#[derive(Default)]
struct WebSocket {
messages: Vec<echo::Message>,
- message_log: scrollable::State,
new_message: String,
- new_message_state: text_input::State,
- new_message_button: button::State,
state: State,
}
@@ -53,45 +49,52 @@ impl Application for WebSocket {
match message {
Message::NewMessageChanged(new_message) => {
self.new_message = new_message;
+
+ Command::none()
}
Message::Send(message) => match &mut self.state {
State::Connected(connection) => {
self.new_message.clear();
connection.send(message);
+
+ Command::none()
}
- State::Disconnected => {}
+ State::Disconnected => Command::none(),
},
Message::Echo(event) => match event {
echo::Event::Connected(connection) => {
self.state = State::Connected(connection);
self.messages.push(echo::Message::connected());
+
+ Command::none()
}
echo::Event::Disconnected => {
self.state = State::Disconnected;
self.messages.push(echo::Message::disconnected());
+
+ Command::none()
}
echo::Event::MessageReceived(message) => {
self.messages.push(message);
- self.message_log.snap_to(1.0);
+
+ scrollable::snap_to(MESSAGE_LOG.clone(), 1.0)
}
},
- Message::Server => {}
+ Message::Server => Command::none(),
}
-
- Command::none()
}
fn subscription(&self) -> Subscription<Message> {
echo::connect().map(Message::Echo)
}
- fn view(&mut self) -> Element<Message> {
- let message_log = if self.messages.is_empty() {
- Container::new(
- Text::new("Your messages will appear here...")
+ fn view(&self) -> Element<Message> {
+ let message_log: Element<_> = if self.messages.is_empty() {
+ container(
+ text("Your messages will appear here...")
.style(Color::from_rgb8(0x88, 0x88, 0x88)),
)
.width(Length::Fill)
@@ -100,31 +103,33 @@ impl Application for WebSocket {
.center_y()
.into()
} else {
- self.messages
- .iter()
- .cloned()
- .fold(
- Scrollable::new(&mut self.message_log),
- |scrollable, message| scrollable.push(Text::new(message)),
+ scrollable(
+ Column::with_children(
+ self.messages
+ .iter()
+ .cloned()
+ .map(text)
+ .map(Element::from)
+ .collect(),
)
.width(Length::Fill)
- .height(Length::Fill)
- .spacing(10)
- .into()
+ .spacing(10),
+ )
+ .id(MESSAGE_LOG.clone())
+ .height(Length::Fill)
+ .into()
};
let new_message_input = {
- let mut input = TextInput::new(
- &mut self.new_message_state,
+ let mut input = text_input(
"Type a message...",
&self.new_message,
Message::NewMessageChanged,
)
.padding(10);
- let mut button = Button::new(
- &mut self.new_message_button,
- Text::new("Send")
+ let mut button = button(
+ text("Send")
.height(Length::Fill)
.vertical_alignment(alignment::Vertical::Center),
)
@@ -137,12 +142,10 @@ impl Application for WebSocket {
}
}
- Row::with_children(vec![input.into(), button.into()])
- .spacing(10)
- .align_items(Alignment::Fill)
+ row![input, button].spacing(10).align_items(Alignment::Fill)
};
- Column::with_children(vec![message_log, new_message_input.into()])
+ column![message_log, new_message_input]
.width(Length::Fill)
.height(Length::Fill)
.padding(20)
@@ -161,3 +164,7 @@ impl Default for State {
Self::Disconnected
}
}
+
+lazy_static::lazy_static! {
+ static ref MESSAGE_LOG: scrollable::Id = scrollable::Id::unique();
+}