@@ -3,7 +3,7 @@ use crate::{
33 theme:: { self , log_colors} ,
44} ;
55use alvr_packets:: ClientListAction ;
6- use alvr_session:: SessionDesc ;
6+ use alvr_session:: { ClientConnectionDesc , SessionDesc } ;
77use eframe:: {
88 egui:: { Frame , Grid , Layout , RichText , TextEdit , Ui , Window } ,
99 emath:: { Align , Align2 } ,
@@ -18,23 +18,38 @@ struct EditPopupState {
1818}
1919
2020pub struct ConnectionsTab {
21+ new_clients : Option < Vec < ( String , ClientConnectionDesc ) > > ,
22+ trusted_clients : Option < Vec < ( String , ClientConnectionDesc ) > > ,
2123 edit_popup_state : Option < EditPopupState > ,
2224}
2325
2426impl ConnectionsTab {
2527 pub fn new ( ) -> Self {
2628 Self {
29+ new_clients : None ,
30+ trusted_clients : None ,
2731 edit_popup_state : None ,
2832 }
2933 }
3034
31- pub fn ui (
32- & mut self ,
33- ui : & mut Ui ,
34- session : & SessionDesc ,
35- connected_to_server : bool ,
36- ) -> Option < ServerRequest > {
37- let mut response = None ;
35+ pub fn update_client_list ( & mut self , session : & SessionDesc ) {
36+ let ( trusted_clients, untrusted_clients) =
37+ session
38+ . client_connections
39+ . clone ( )
40+ . into_iter ( )
41+ . partition :: < Vec < _ > , _ > ( |( _, data) | data. trusted ) ;
42+
43+ self . trusted_clients = Some ( trusted_clients) ;
44+ self . new_clients = Some ( untrusted_clients) ;
45+ }
46+
47+ pub fn ui ( & mut self , ui : & mut Ui , connected_to_server : bool ) -> Vec < ServerRequest > {
48+ let mut requests = vec ! [ ] ;
49+
50+ if self . new_clients . is_none ( ) {
51+ requests. push ( ServerRequest :: GetSession ) ;
52+ }
3853
3954 if !connected_to_server {
4055 Frame :: group ( ui. style ( ) )
@@ -61,91 +76,90 @@ impl ConnectionsTab {
6176 } ) ;
6277 }
6378
64- // Get the different types of clients from the session
65- let ( trusted_clients, untrusted_clients) = session
66- . client_connections
67- . iter ( )
68- . partition :: < Vec < _ > , _ > ( |( _, data) | data. trusted ) ;
69-
7079 ui. vertical_centered_justified ( |ui| {
71- Frame :: group ( ui. style ( ) )
72- . fill ( theme:: SECTION_BG )
73- . show ( ui, |ui| {
74- ui. vertical_centered_justified ( |ui| {
75- ui. add_space ( 5.0 ) ;
76- ui. heading ( "New clients" ) ;
77- } ) ;
80+ if let Some ( clients) = & self . new_clients {
81+ Frame :: group ( ui. style ( ) )
82+ . fill ( theme:: SECTION_BG )
83+ . show ( ui, |ui| {
84+ ui. vertical_centered_justified ( |ui| {
85+ ui. add_space ( 5.0 ) ;
86+ ui. heading ( "New clients" ) ;
87+ } ) ;
7888
79- Grid :: new ( 1 ) . num_columns ( 2 ) . show ( ui, |ui| {
80- for ( hostname, _) in untrusted_clients {
81- ui. horizontal ( |ui| {
82- ui. add_space ( 10.0 ) ;
83- ui. label ( hostname) ;
84- } ) ;
85- ui. with_layout ( Layout :: right_to_left ( Align :: Center ) , |ui| {
86- if ui. button ( "Trust" ) . clicked ( ) {
87- response = Some ( ServerRequest :: UpdateClientList {
88- hostname : hostname. clone ( ) ,
89- action : ClientListAction :: Trust ,
90- } ) ;
91- } ;
92- } ) ;
93- ui. end_row ( ) ;
94- }
95- } )
96- } ) ;
89+ Grid :: new ( 1 ) . num_columns ( 2 ) . show ( ui, |ui| {
90+ for ( hostname, _) in clients {
91+ ui. horizontal ( |ui| {
92+ ui. add_space ( 10.0 ) ;
93+ ui. label ( hostname) ;
94+ } ) ;
95+ ui. with_layout ( Layout :: right_to_left ( Align :: Center ) , |ui| {
96+ if ui. button ( "Trust" ) . clicked ( ) {
97+ requests. push ( ServerRequest :: UpdateClientList {
98+ hostname : hostname. clone ( ) ,
99+ action : ClientListAction :: Trust ,
100+ } ) ;
101+ } ;
102+ } ) ;
103+ ui. end_row ( ) ;
104+ }
105+ } )
106+ } ) ;
107+ }
97108
98109 ui. add_space ( 10.0 ) ;
99110
100- Frame :: group ( ui. style ( ) )
101- . fill ( theme:: SECTION_BG )
102- . show ( ui, |ui| {
103- ui. vertical_centered_justified ( |ui| {
104- ui. add_space ( 5.0 ) ;
105- ui. heading ( "Trusted clients" ) ;
106- } ) ;
111+ if let Some ( clients) = & self . trusted_clients {
112+ Frame :: group ( ui. style ( ) )
113+ . fill ( theme:: SECTION_BG )
114+ . show ( ui, |ui| {
115+ ui. vertical_centered_justified ( |ui| {
116+ ui. add_space ( 5.0 ) ;
117+ ui. heading ( "Trusted clients" ) ;
118+ } ) ;
107119
108- Grid :: new ( 2 ) . num_columns ( 2 ) . show ( ui, |ui| {
109- for ( hostname, data) in trusted_clients {
110- ui. horizontal ( |ui| {
111- ui. add_space ( 10.0 ) ;
112- ui. label ( format ! (
113- "{hostname}: {} ({})" ,
114- data. current_ip. unwrap_or( IpAddr :: V4 ( Ipv4Addr :: UNSPECIFIED ) ) ,
115- data. display_name
116- ) ) ;
117- } ) ;
118- ui. with_layout ( Layout :: right_to_left ( Align :: Center ) , |ui| {
119- if ui. button ( "Remove" ) . clicked ( ) {
120- response = Some ( ServerRequest :: UpdateClientList {
121- hostname : hostname. clone ( ) ,
122- action : ClientListAction :: RemoveEntry ,
123- } ) ;
124- }
125- if ui. button ( "Edit" ) . clicked ( ) {
126- self . edit_popup_state = Some ( EditPopupState {
127- new_client : false ,
128- hostname : hostname. to_owned ( ) ,
129- ips : data
130- . manual_ips
131- . iter ( )
132- . map ( |addr| addr. to_string ( ) )
133- . collect :: < Vec < String > > ( ) ,
134- } ) ;
135- }
120+ Grid :: new ( 2 ) . num_columns ( 2 ) . show ( ui, |ui| {
121+ for ( hostname, data) in clients {
122+ ui. horizontal ( |ui| {
123+ ui. add_space ( 10.0 ) ;
124+ ui. label ( format ! (
125+ "{hostname}: {} ({})" ,
126+ data. current_ip
127+ . unwrap_or( IpAddr :: V4 ( Ipv4Addr :: UNSPECIFIED ) ) ,
128+ data. display_name
129+ ) ) ;
130+ } ) ;
131+ ui. with_layout ( Layout :: right_to_left ( Align :: Center ) , |ui| {
132+ if ui. button ( "Remove" ) . clicked ( ) {
133+ requests. push ( ServerRequest :: UpdateClientList {
134+ hostname : hostname. clone ( ) ,
135+ action : ClientListAction :: RemoveEntry ,
136+ } ) ;
137+ }
138+ if ui. button ( "Edit" ) . clicked ( ) {
139+ self . edit_popup_state = Some ( EditPopupState {
140+ new_client : false ,
141+ hostname : hostname. to_owned ( ) ,
142+ ips : data
143+ . manual_ips
144+ . iter ( )
145+ . map ( |addr| addr. to_string ( ) )
146+ . collect :: < Vec < String > > ( ) ,
147+ } ) ;
148+ }
149+ } ) ;
150+ ui. end_row ( ) ;
151+ }
152+ } ) ;
153+
154+ if ui. button ( "Add client manually" ) . clicked ( ) {
155+ self . edit_popup_state = Some ( EditPopupState {
156+ hostname : "XXXX.client.alvr" . into ( ) ,
157+ new_client : true ,
158+ ips : Vec :: new ( ) ,
136159 } ) ;
137- ui. end_row ( ) ;
138160 }
139161 } ) ;
140-
141- if ui. button ( "Add client manually" ) . clicked ( ) {
142- self . edit_popup_state = Some ( EditPopupState {
143- hostname : "XXXX.client.alvr" . into ( ) ,
144- new_client : true ,
145- ips : Vec :: new ( ) ,
146- } ) ;
147- }
148- } ) ;
162+ }
149163 } ) ;
150164
151165 if let Some ( mut state) = self . edit_popup_state . take ( ) {
@@ -165,7 +179,7 @@ impl ConnectionsTab {
165179 ui[ 1 ] . text_edit_singleline ( address) ;
166180 }
167181 if ui[ 1 ] . button ( "Add new" ) . clicked ( ) {
168- state. ips . push ( "192.168.1.2 " . to_string ( ) ) ;
182+ state. ips . push ( "192.168.X.X " . to_string ( ) ) ;
169183 }
170184 } ) ;
171185 ui. columns ( 2 , |ui| {
@@ -174,16 +188,16 @@ impl ConnectionsTab {
174188 state. ips . iter ( ) . filter_map ( |s| s. parse ( ) . ok ( ) ) . collect ( ) ;
175189
176190 if state. new_client {
177- response = Some ( ServerRequest :: UpdateClientList {
178- hostname : state. hostname . clone ( ) ,
191+ requests . push ( ServerRequest :: UpdateClientList {
192+ hostname : state. hostname ,
179193 action : ClientListAction :: AddIfMissing {
180194 trusted : true ,
181195 manual_ips,
182196 } ,
183197 } ) ;
184198 } else {
185- response = Some ( ServerRequest :: UpdateClientList {
186- hostname : state. hostname . clone ( ) ,
199+ requests . push ( ServerRequest :: UpdateClientList {
200+ hostname : state. hostname ,
187201 action : ClientListAction :: SetManualIps ( manual_ips) ,
188202 } ) ;
189203 }
@@ -194,6 +208,6 @@ impl ConnectionsTab {
194208 } ) ;
195209 }
196210
197- response
211+ requests
198212 }
199213}
0 commit comments