Skip to content

Commit 039f8f9

Browse files
committed
feat: connection window with a tree view and stuff
1 parent c19f14d commit 039f8f9

6 files changed

Lines changed: 276 additions & 24 deletions

File tree

‎player/src/gui.rs‎

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ use protocol::Track;
2121
use protocol::TrackArt;
2222

2323
pub mod bitrate_bar;
24+
pub mod connection_window;
2425
pub mod main_window;
2526
pub mod marquee_label;
2627
pub mod queue_window;
2728
pub mod visualizer;
28-
pub mod connection_window;
29+
30+
use crate::Connection;
2931

3032
use self::connection_window::ConnectionWindow;
3133
use self::main_window::*;
@@ -40,8 +42,9 @@ pub enum UIEvent {
4042
BtnNext,
4143
BtnPrev,
4244
BtnQueue,
43-
BtnConnect,
45+
BtnOpenConnectionDialog,
4446
Connect(String),
47+
JoinRoom(usize),
4548
VolumeSlider(f32),
4649
VolumeUp,
4750
VolumeDown,
@@ -55,6 +58,7 @@ pub enum UIEvent {
5558
Quit,
5659

5760
Update(UIUpdateEvent),
61+
ConnectionDlg(ConnectionDlgEvent),
5862
}
5963

6064
#[derive(Debug, Clone)]
@@ -63,6 +67,8 @@ pub enum UIUpdateEvent {
6367
UpdateUserList(HashMap<String, String>),
6468
UpdateRoomName(Option<String>),
6569
UpdateQueue(Option<Track>, VecDeque<Track>),
70+
UpdateConnectionTree(Vec<self::connection_window::Server>),
71+
UpdateConnectionTreePartial(self::connection_window::Server),
6672
Status(String),
6773
Visualizer([u8; 14]),
6874
Buffer(u8),
@@ -71,6 +77,14 @@ pub enum UIUpdateEvent {
7177
Periodic,
7278
}
7379

80+
#[derive(Debug, Clone)]
81+
pub enum ConnectionDlgEvent {
82+
BtnConnect,
83+
BtnNewRoom,
84+
BtnRefresh,
85+
AddServer(String),
86+
}
87+
7488
#[derive(Default)]
7589
struct DragState {
7690
x: i32,
@@ -165,7 +179,7 @@ impl UIThread {
165179

166180
gui,
167181
queue_gui,
168-
connection_gui
182+
connection_gui,
169183
}
170184
}
171185

@@ -225,7 +239,10 @@ impl UIThread {
225239
}
226240
UIUpdateEvent::UpdateUserList(val) => {
227241
self.gui.users.clear();
228-
self.gui.users.add(&format!("[{}]", self.room_name.clone().unwrap_or("no room".into())));
242+
self.gui.users.add(&format!(
243+
"[{}]",
244+
self.room_name.clone().unwrap_or("no room".into())
245+
));
229246
for (id, name) in val.iter() {
230247
let line = if id == &self.my_id {
231248
format!("@b* you")
@@ -308,10 +325,47 @@ impl UIThread {
308325
self.queue_gui.queue_browser.size(),
309326
));
310327
}
328+
UIUpdateEvent::UpdateConnectionTree(list) => {
329+
self.connection_gui.populate(list);
330+
}
331+
UIUpdateEvent::UpdateConnectionTreePartial(server) => {
332+
self.connection_gui.update_just_one_server(server);
333+
}
311334
_ => {
312335
dbg!(evt);
313336
}
314337
},
338+
UIEvent::ConnectionDlg(evt) => match evt {
339+
ConnectionDlgEvent::BtnConnect => {
340+
if let Some(item) = self.connection_gui.tree.first_selected_item() {
341+
let (maybe_room_id, server_addr): (Option<usize>, String) =
342+
match item.depth() {
343+
1 => unsafe {
344+
// server selected
345+
(
346+
None,
347+
item.user_data().unwrap(),
348+
)
349+
},
350+
2 => unsafe {
351+
// room selected
352+
(
353+
Some(item.user_data().unwrap()),
354+
item.parent().unwrap().user_data().unwrap(),
355+
)
356+
},
357+
_ => unreachable!(),
358+
};
359+
360+
self.tx.send(UIEvent::Connect(server_addr)).unwrap();
361+
if let Some(room_id) = maybe_room_id {
362+
self.tx.send(UIEvent::JoinRoom(room_id)).unwrap();
363+
}
364+
}
365+
}
366+
ConnectionDlgEvent::AddServer(_) => todo!(),
367+
_ => {}
368+
},
315369
UIEvent::BtnPlay => {
316370
// play button behaviour:
317371
// if stopped and queue is empty, ask for file
@@ -325,8 +379,9 @@ impl UIThread {
325379
UIEvent::BtnNext => {}
326380
UIEvent::BtnPrev => {}
327381

328-
UIEvent::BtnConnect => {
382+
UIEvent::BtnOpenConnectionDialog => {
329383
self.connection_gui.main_win.show();
384+
self.tx.send(UIEvent::ConnectionDlg(ConnectionDlgEvent::BtnRefresh)).unwrap();
330385
}
331386
UIEvent::BtnQueue => {
332387
self.queue_gui.main_win.show();

‎player/src/gui/connection_window.rs‎

Lines changed: 118 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,146 @@ use fltk::enums::*;
55
use fltk::group::Group;
66
use fltk::input::Input;
77
use fltk::prelude::*;
8+
use fltk::tree::Tree;
9+
use fltk::tree::TreeItem;
810
use fltk::window::*;
911

1012
use super::add_bar;
13+
use super::ConnectionDlgEvent;
1114
use super::UIEvent;
1215

1316
pub struct ConnectionWindow {
1417
pub main_win: Window,
18+
pub tree: Tree,
1519
}
1620

1721
impl ConnectionWindow {
1822
pub fn make_window(s: Sender<UIEvent>) -> Self {
19-
let mut main_win = Window::new(100, 100, 300, 200, "queue");
23+
let mut main_win = Window::new(100, 100, 330, 300, "queue");
2024
main_win.set_frame(FrameType::UpBox);
2125

22-
add_bar(&mut main_win, s.clone(), UIEvent::HideConnectionWindow, "Connection Dialog");
26+
add_bar(
27+
&mut main_win,
28+
s.clone(),
29+
UIEvent::HideConnectionWindow,
30+
"Connect to Server",
31+
);
2332
main_win.set_border(false);
2433

25-
// very temp
34+
/* // very temp
2635
let mut addr_bar = Input::new(10,40,150,25,"Address").with_align(Align::TopLeft);
2736
let tmp = s.clone();
2837
addr_bar.set_trigger(CallbackTrigger::EnterKey);
2938
addr_bar.set_callback(move |bar| {
3039
tmp.send(UIEvent::Connect(bar.value()));
40+
}); */
41+
42+
let mut btn_connect = Button::new(235, 32, 85, 25, "Connect");
43+
btn_connect.emit(
44+
s.clone(),
45+
UIEvent::ConnectionDlg(ConnectionDlgEvent::BtnConnect),
46+
);
47+
main_win.add(&btn_connect);
48+
49+
let mut btn_new_room = Button::new(0, 0, 85, 25, "New Room").below_of(&btn_connect, 4);
50+
btn_new_room.emit(
51+
s.clone(),
52+
UIEvent::ConnectionDlg(ConnectionDlgEvent::BtnNewRoom),
53+
);
54+
main_win.add(&btn_new_room);
55+
56+
let mut btn_refresh = Button::new(0, 0, 85, 25, "Refresh").below_of(&btn_new_room, 4);
57+
btn_refresh.emit(
58+
s.clone(),
59+
UIEvent::ConnectionDlg(ConnectionDlgEvent::BtnRefresh),
60+
);
61+
main_win.add(&btn_refresh);
62+
63+
let mut tree = Tree::default().with_size(210, 230).with_pos(10, 32);
64+
tree.set_show_root(false);
65+
66+
tree.add("unpopulated");
67+
68+
tree.set_trigger(CallbackTrigger::Release);
69+
tree.set_callback(move |t| {
70+
if let Some(mut selected) = t.first_selected_item() {
71+
if selected.depth() == 3 {
72+
selected.deselect();
73+
selected.parent().unwrap().select_toggle();
74+
}
75+
btn_connect.activate();
76+
} else {
77+
btn_connect.deactivate();
78+
}
3179
});
80+
main_win.add(&tree);
81+
82+
main_win.end();
83+
84+
Self { main_win, tree }
85+
}
3286

33-
Self {
34-
main_win
87+
pub fn populate(&mut self, list: Vec<Server>) {
88+
let tree = &mut self.tree;
89+
tree.clear();
90+
tree.begin();
91+
for server in list {
92+
let mut item = tree.add(&server.name).unwrap();
93+
item.set_user_data(server.addr);
94+
if let Some(rooms) = &server.rooms {
95+
for room in rooms {
96+
let line = &format!("{}/{}", server.name, room.id);
97+
if let Some(mut sub_item) = tree.add(&line) {
98+
sub_item.set_user_data(room.id);
99+
sub_item.close();
100+
for user in &room.user_names {
101+
tree.add(&format!("{}/{}", line, user));
102+
}
103+
let actual_line = &format!("{} ({})", room.name, room.user_names.len());
104+
sub_item.set_label(actual_line);
105+
}
106+
}
107+
} else {
108+
tree.add(&format!("{}/(unable to connect)", server.name));
109+
}
35110
}
111+
tree.end();
112+
tree.redraw();
36113
}
114+
115+
pub fn update_just_one_server(&mut self, server: Server) {
116+
let tree = &mut self.tree;
117+
if let Some(mut item) = tree.find_item(&server.name) {
118+
item.clear_children();
119+
120+
if let Some(rooms) = &server.rooms {
121+
for room in rooms {
122+
let line = &format!("{}/{}", server.name, room.id);
123+
if let Some(mut sub_item) = tree.add(&line) {
124+
sub_item.set_user_data(room.id);
125+
sub_item.close();
126+
for user in &room.user_names {
127+
tree.add(&format!("{}/{}", line, user));
128+
}
129+
let actual_line = &format!("{} ({})", room.name, room.user_names.len());
130+
sub_item.set_label(actual_line);
131+
}
132+
}
133+
} else {
134+
eprintln!("uhhhhhh should never happen,,,,,,,,,,,");
135+
}
136+
137+
} else {
138+
println!("couldn't find {} ????", server.name);
139+
}
140+
tree.redraw();
141+
}
142+
143+
}
144+
145+
#[derive(Debug, Clone)]
146+
pub struct Server {
147+
pub name: String,
148+
pub addr: String,
149+
pub rooms: Option<Vec<protocol::RoomListing>>,
37150
}

‎player/src/gui/main_window.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl MainWindow {
178178
main_win.add(&users);
179179

180180
let mut btn_connect = Button::new(main_win.width() - 85 - 46, 26, 24, 24, "Cn");
181-
btn_connect.emit(s.clone(), UIEvent::BtnConnect);
181+
btn_connect.emit(s.clone(), UIEvent::BtnOpenConnectionDialog);
182182
main_win.add(&btn_connect);
183183

184184
let mut btn_queue = Button::new(main_win.width() - 85 - 46, 26 + 24 + 2, 24, 24, "Qu");

0 commit comments

Comments
 (0)