Merge pull request #1393 from iced-rs/deprecate-stateful-widgets

Replace stateful widgets with the new `iced_pure` API
This commit is contained in:
Héctor Ramón 2022-08-06 00:32:57 +02:00 committed by GitHub
commit 1923dbf7f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
160 changed files with 4612 additions and 14928 deletions

View file

@ -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"

View file

@ -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),
}
}
}

View file

@ -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();
}