1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
mod echo;
use iced::widget::{
self, button, center, column, row, scrollable, text, text_input,
};
use iced::{color, Center, Element, Fill, Subscription, Task};
use std::sync::LazyLock;
pub fn main() -> iced::Result {
iced::application("WebSocket - Iced", WebSocket::update, WebSocket::view)
.subscription(WebSocket::subscription)
.run_with(WebSocket::new)
}
struct WebSocket {
messages: Vec<echo::Message>,
new_message: String,
state: State,
}
#[derive(Debug, Clone)]
enum Message {
NewMessageChanged(String),
Send(echo::Message),
Echo(echo::Event),
Server,
}
impl WebSocket {
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> {
match message {
Message::NewMessageChanged(new_message) => {
self.new_message = new_message;
Task::none()
}
Message::Send(message) => match &mut self.state {
State::Connected(connection) => {
self.new_message.clear();
connection.send(message);
Task::none()
}
State::Disconnected => Task::none(),
},
Message::Echo(event) => match event {
echo::Event::Connected(connection) => {
self.state = State::Connected(connection);
self.messages.push(echo::Message::connected());
Task::none()
}
echo::Event::Disconnected => {
self.state = State::Disconnected;
self.messages.push(echo::Message::disconnected());
Task::none()
}
echo::Event::MessageReceived(message) => {
self.messages.push(message);
scrollable::snap_to(
MESSAGE_LOG.clone(),
scrollable::RelativeOffset::END,
)
}
},
Message::Server => Task::none(),
}
}
fn subscription(&self) -> Subscription<Message> {
Subscription::run(echo::connect).map(Message::Echo)
}
fn view(&self) -> Element<Message> {
let message_log: Element<_> = if self.messages.is_empty() {
center(
text("Your messages will appear here...")
.color(color!(0x888888)),
)
.into()
} else {
scrollable(
column(self.messages.iter().map(text).map(Element::from))
.spacing(10),
)
.id(MESSAGE_LOG.clone())
.height(Fill)
.into()
};
let new_message_input = {
let mut input = text_input("Type a message...", &self.new_message)
.on_input(Message::NewMessageChanged)
.padding(10);
let mut button = button(text("Send").height(40).align_y(Center))
.padding([0, 20]);
if matches!(self.state, State::Connected(_)) {
if let Some(message) = echo::Message::new(&self.new_message) {
input = input.on_submit(Message::Send(message.clone()));
button = button.on_press(Message::Send(message));
}
}
row![input, button].spacing(10).align_y(Center)
};
column![message_log, new_message_input]
.height(Fill)
.padding(20)
.spacing(10)
.into()
}
}
enum State {
Disconnected,
Connected(echo::Connection),
}
static MESSAGE_LOG: LazyLock<scrollable::Id> =
LazyLock::new(scrollable::Id::unique);
|