Skip to content

Commit b94535b

Browse files
committed
feat: update connection dialog better
1 parent ed23b63 commit b94535b

4 files changed

Lines changed: 105 additions & 39 deletions

File tree

‎player/src/connection.rs‎

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@ use futures::StreamExt;
88
use tokio::{
99
net::TcpStream,
1010
sync::{mpsc, oneshot, RwLock},
11-
task::block_in_place,
1211
time::timeout,
1312
};
1413

15-
use protocol::{
16-
network::FrameStream, AudioData, Message, Notification, Request, Response, RoomListing,
17-
RoomOptions, Track,
18-
};
14+
use protocol::{network::FrameStream, RoomListing};
15+
use protocol::{AudioData, Message, Notification, Request, Response, RoomOptions, Track};
1916

2017
use crate::{
2118
audio::{AudioCommand, AudioStatusRx, AudioThread, AudioThreadHandle},
22-
gui::{UIThreadHandle, UIUpdateEvent},
19+
gui::{connection_window::Server, UIThreadHandle, UIUpdateEvent},
2320
transmit::{TransmitCommand, TransmitThread, TransmitThreadHandle},
2421
AudioStatus, ConnectionState, RoomState, State,
2522
};
@@ -281,7 +278,7 @@ impl ConnectionActor {
281278

282279
// stream disconnected
283280
None => {
284-
dbg!("stream disconnected");
281+
println!("stream disconnected");
285282
break;
286283
},
287284
}
@@ -367,11 +364,7 @@ impl ConnectionActor {
367364
.connection
368365
.as_mut()
369366
.unwrap()
370-
{
371-
let mut state = self.state.write().await;
372-
let room = state.connection.as_mut().unwrap().room.as_mut().unwrap();
373-
room.is_synced = true;
374-
}
367+
.room
375368
.as_mut()
376369
.unwrap();
377370
room_write.is_synced = true;
@@ -386,7 +379,11 @@ impl ConnectionActor {
386379
.unwrap();
387380
}
388381
protocol::AudioData::Start => {
389-
self.is_synced = true;
382+
{
383+
let mut state = self.state.write().await;
384+
let room = state.connection.as_mut().unwrap().room.as_mut().unwrap();
385+
room.is_synced = true;
386+
}
390387
self.audio.send(AudioCommand::AudioData(data)).unwrap();
391388
}
392389
_ => {
@@ -469,6 +466,7 @@ impl ConnectionActor {
469466
name: name.into(),
470467
addr: conn.addr.clone(),
471468
rooms: Some(list),
469+
tried: true,
472470
};
473471
self.ui
474472
.update(UIUpdateEvent::UpdateConnectionTreePartial(s));
@@ -477,6 +475,24 @@ impl ConnectionActor {
477475
}
478476
}
479477

478+
pub async fn query_server(name: &str, addr: &str) -> Server {
479+
if let Ok(rooms) = query_room_list(addr).await {
480+
Server {
481+
name: name.into(),
482+
addr: addr.into(),
483+
rooms: Some(rooms),
484+
tried: true,
485+
}
486+
} else {
487+
Server {
488+
name: name.into(),
489+
addr: addr.into(),
490+
rooms: None,
491+
tried: true,
492+
}
493+
}
494+
}
495+
480496
pub async fn query_room_list(addr: &str) -> Result<Vec<RoomListing>, Box<dyn Error + Send + Sync>> {
481497
let duration = tokio::time::Duration::from_secs(1);
482498

‎player/src/gui.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub enum UIUpdateEvent {
6565
QueueChanged,
6666
UpdateConnectionTree(Vec<self::connection_window::Server>),
6767
UpdateConnectionTreePartial(self::connection_window::Server),
68+
ConnectionChanged,
6869
Status,
6970
Visualizer([u8; 14]),
7071
Buffer(u8),
@@ -417,6 +418,9 @@ impl UIThread {
417418
UIUpdateEvent::UpdateConnectionTreePartial(server) => {
418419
self.connection_gui.update_just_one_server(server);
419420
}
421+
UIUpdateEvent::ConnectionChanged => {
422+
self.connection_gui.update_connected();
423+
}
420424
_ => {
421425
dbg!(evt);
422426
}

‎player/src/gui/connection_window.rs‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,25 @@ impl ConnectionWindow {
146146
tree.redraw();
147147
}
148148

149+
pub fn update_connected(&mut self) {
150+
let state = self.state.blocking_read();
151+
let tree = &mut self.tree;
152+
for mut item in tree.get_items().unwrap_or_default() {
153+
if item.depth() == 1 {
154+
unsafe {
155+
let addr: String = item.user_data().unwrap();
156+
item.set_label_font(Font::Helvetica);
157+
if let Some(c) = &state.connection {
158+
if c.addr == addr {
159+
item.set_label_font(Font::HelveticaBold);
160+
}
161+
}
162+
}
163+
}
164+
}
165+
tree.redraw();
166+
}
167+
149168
fn populate_server_item(
150169
state: Arc<RwLock<State>>,
151170
tree: &mut Tree,
@@ -169,7 +188,11 @@ impl ConnectionWindow {
169188
}
170189
}
171190
} else {
172-
tree.add(&format!("{}/(unable to connect)", server.addr));
191+
if server.tried {
192+
tree.add(&format!("{}/(unable to connect)", server.addr));
193+
} else {
194+
tree.add(&format!("{}/(querying...)", server.addr));
195+
}
173196
}
174197

175198
// connected indicator
@@ -191,4 +214,5 @@ pub struct Server {
191214
pub name: String,
192215
pub addr: String,
193216
pub rooms: Option<Vec<protocol::RoomListing>>,
217+
pub tried: bool, // we want to list them without showing unable to connect if we didn't try yet
194218
}

‎player/src/main.rs‎

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod audio;
1717
use crate::audio::AudioCommand;
1818

1919
mod gui;
20+
use crate::gui::connection_window::Server;
2021
use crate::gui::{ConnectionDlgEvent, UIEvent, UIThread, UIUpdateEvent};
2122

2223
use protocol::{RoomOptions, Track};
@@ -159,18 +160,22 @@ impl MainThread {
159160
tokio::select! {
160161
// watch for connection exits
161162
Some(()) = maybe_connection_exited(&self.connection) => {
162-
self.disconnect();
163+
self.disconnect().await;
163164
}
164165

165166
// some ui event
166167
Some(event) = self.ui_rx.recv() => {
167168
match event {
168169
UIEvent::Connect(addr) => {
169-
if self.connection.is_none() {
170-
self.connect(addr).await;
171-
if let Some(c) = &mut self.connection {
172-
c.refresh_room_list().unwrap();
170+
if self.connection.is_some() {
171+
let current_addr = self.state.read().await.connection.as_ref().unwrap().addr.clone();
172+
if current_addr != addr {
173+
self.disconnect().await;
174+
175+
self.connect(addr).await;
173176
}
177+
} else {
178+
self.connect(addr).await;
174179
}
175180
}
176181

@@ -183,23 +188,22 @@ impl MainThread {
183188

184189
UIEvent::ConnectionDlg(ConnectionDlgEvent::BtnRefresh) => {
185190
let servers_tmp = vec!["127.0.0.1:8080", "example.com:443"];
186-
let mut servers: Vec<crate::gui::connection_window::Server> = vec![];
187-
// TODO: query everything in ~parallel~
191+
192+
self.ui.update(UIUpdateEvent::UpdateConnectionTree(servers_tmp.iter().map(|s| Server {
193+
addr: s.to_string(),
194+
name: s.to_string().split(":").next().unwrap().into(),
195+
rooms: None,
196+
tried: false,
197+
}).collect()));
198+
188199
for addr in servers_tmp {
189-
let name = addr.split(":").next().unwrap();
190-
let rooms_future = connection::query_room_list(addr);
191-
let mut s = crate::gui::connection_window::Server {
192-
name: name.into(),
193-
addr: addr.into(),
194-
rooms: None,
195-
};
196-
if let Ok(rooms) = rooms_future.await {
197-
s.rooms = Some(rooms);
198-
}
199-
servers.push(s);
200+
let mut ui2 = self.ui.clone();
201+
tokio::spawn(async move {
202+
let name = addr.split(":").next().unwrap();
203+
let s = connection::query_server(name, addr).await;
204+
ui2.update(UIUpdateEvent::UpdateConnectionTreePartial(s));
205+
});
200206
}
201-
println!("sending event: {:?}", servers);
202-
self.ui.update(UIUpdateEvent::UpdateConnectionTree(servers));
203207
},
204208

205209
UIEvent::ConnectionDlg(ConnectionDlgEvent::BtnNewRoom) => {
@@ -291,9 +295,10 @@ impl MainThread {
291295
if s.is_transmitting() {
292296
conn.transmit.send(TransmitCommand::Stop).unwrap();
293297
}
294-
}
295-
UIEvent::Pause => {
296-
298+
}
299+
UIEvent::BtnPause => {
300+
// TODO UNDO ME
301+
self.disconnect().await;
297302
}
298303
UIEvent::Stop => {
299304
// TODO do this obviously
@@ -347,16 +352,33 @@ impl MainThread {
347352
}
348353
}
349354

355+
self.ui.update(UIUpdateEvent::ConnectionChanged);
350356
self.ui.update(UIUpdateEvent::Status);
351357
}
352358

353-
fn disconnect(&mut self) {
359+
async fn disconnect(&mut self) {
354360
// unset the connection handle which drops the thread and starts cleanup
355361
// ""RAII""
356362
self.connection = None;
357363

358-
self.state.blocking_write().connection = None;
364+
let mut state = self.state.write().await;
365+
366+
// query old server's list after we disconnect so it updates properly?
367+
if let Some(c) = &state.connection {
368+
let mut ui2 = self.ui.clone();
369+
let addr = c.addr.clone();
370+
tokio::spawn(async move {
371+
let name = addr.split(":").next().unwrap();
372+
let s = connection::query_server(name, &addr).await;
373+
ui2.update(UIUpdateEvent::UpdateConnectionTreePartial(s));
374+
});
375+
}
376+
377+
state.connection = None;
378+
379+
drop(state);
359380

381+
self.ui.update(UIUpdateEvent::ConnectionChanged);
360382
self.ui.update(UIUpdateEvent::Status);
361383
}
362384
}

0 commit comments

Comments
 (0)