Skip to content

Commit 2f22fc8

Browse files
committed
refactor show_settings_dialog for non-blocking reason
1 parent 6fc3455 commit 2f22fc8

File tree

3 files changed

+31
-34
lines changed

3 files changed

+31
-34
lines changed

src/main.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use crate::content_table::refresh_table;
55
use fltk::{
6-
app, dialog,
6+
dialog,
77
enums::{Event, Shortcut},
88
menu::{MenuBar, MenuFlag},
99
prelude::{GroupExt, MenuExt, WidgetBase, WidgetExt, WindowExt},
@@ -40,7 +40,7 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
4040
log::set_max_level(log::LevelFilter::Debug);
4141

4242
let state = states_manager::load_app_state();
43-
let tun2proxy_enable = state.system_settings.clone().unwrap_or_default().tun2proxy_enable.unwrap_or(true);
43+
let tun2proxy_enable = state.system_settings.clone().unwrap_or_default().tun2proxy_enable.unwrap_or(false);
4444
let state = Rc::new(RefCell::new(state));
4545

4646
if tun2proxy_enable && !run_as::is_elevated() {
@@ -51,7 +51,7 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
5151
let remote_nodes = Rc::new(RefCell::new(state.borrow().remote_nodes.clone()));
5252
let current_node_index = Rc::new(RefCell::new(state.borrow().current_node_index));
5353

54-
let app = app::App::default();
54+
let _app = ::fltk::app::App::default();
5555

5656
let ws = state.borrow().window.clone();
5757
let title = format!("OverTLS clients manager for {}", util::host_os_name());
@@ -63,24 +63,12 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
6363

6464
refresh_table(&mut table, &mut win, remote_nodes.borrow().len());
6565

66+
let (settings_tx, settings_rx) = std::sync::mpsc::channel();
6667
let w = win.clone();
6768
let state_clone = state.clone();
6869
menubar.add("&File/Settings", Shortcut::None, MenuFlag::MenuDivider, move |_m| {
6970
let settings = state_clone.borrow().system_settings.clone().unwrap_or_default();
70-
if let Some(new_settings) = settings_dialog::show_settings_dialog(&w, &settings) {
71-
let tun2proxy_enable = new_settings.tun2proxy_enable.unwrap_or_default();
72-
state_clone.borrow_mut().system_settings = Some(new_settings);
73-
if tun2proxy_enable && !run_as::is_elevated() {
74-
if let Ok(status) = core::restart_as_admin() {
75-
log::debug!("Restarted as admin with status code {status}, exiting current instance.");
76-
app::quit();
77-
} else {
78-
let x = w.x() + (w.width() - COMMON_DLG_W) / 2;
79-
let y = w.y() + (w.height() - COMMON_DLG_H) / 2;
80-
dialog::alert(x, y, "Failed to restart as admin.");
81-
}
82-
}
83-
}
71+
settings_dialog::show_settings_dialog(&w, &settings, settings_tx.clone());
8472
});
8573

8674
let remote_nodes_clone = remote_nodes.clone();
@@ -232,7 +220,7 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
232220
}
233221

234222
menubar.add("&File/Quit\t", Shortcut::Ctrl | 'q', MenuFlag::Normal, move |_| {
235-
app::quit();
223+
::fltk::app::quit();
236224
});
237225

238226
// --- Edit menu group: View Details ---
@@ -333,7 +321,7 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
333321
return;
334322
};
335323
if let Ok(text) = &node.generate_ssr_url() {
336-
app::copy(text);
324+
::fltk::app::copy(text);
337325
let name = node.remarks.clone().unwrap_or_default();
338326
dialog::message(x, y, &format!("Node '{name}'s URL copied to clipboard"));
339327
} else {
@@ -433,17 +421,34 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
433421
}
434422
});
435423

436-
while app.wait() {
424+
while ::fltk::app::wait() {
437425
// Handle tray menu events
438426
while let Ok(event) = tray_icon::menu::MenuEvent::receiver().try_recv() {
439427
if event.id == show_item.id() {
440428
win.show();
441429
} else if event.id == quit_item.id() {
442-
app::quit();
430+
::fltk::app::quit();
443431
break;
444432
}
445433
}
446434

435+
// Deal with settings dialog results
436+
while let Ok(new_settings) = settings_rx.try_recv() {
437+
let tun2proxy_enable = new_settings.tun2proxy_enable.unwrap_or_default();
438+
state.borrow_mut().system_settings = Some(new_settings);
439+
if tun2proxy_enable && !run_as::is_elevated() {
440+
if let Ok(status) = core::restart_as_admin() {
441+
log::debug!("Restarted as admin with status code {status}, exiting current instance.");
442+
::fltk::app::quit();
443+
} else {
444+
let x = win.x() + (win.width() - COMMON_DLG_W) / 2;
445+
let y = win.y() + (win.height() - COMMON_DLG_H) / 2;
446+
dialog::alert(x, y, "Failed to restart as admin.");
447+
}
448+
}
449+
log::info!("Settings updated via channel");
450+
}
451+
447452
// Append logs from the queue to the Terminal
448453
if let Ok(mut logs) = log_queue.lock() {
449454
for msg in logs.drain(..) {

src/settings_dialog.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ macro_rules! add_row_spin {
8686
}};
8787
}
8888

89-
pub fn show_settings_dialog(win: &Window, system_settings: &SystemSettings) -> Option<SystemSettings> {
89+
/// Pop up the settings dialog, and send the result via channel to avoid idle closure accumulation.
90+
pub fn show_settings_dialog(win: &Window, system_settings: &SystemSettings, tx: std::sync::mpsc::Sender<SystemSettings>) {
9091
let dialog_w = 600;
9192
let dialog_h = 320;
9293
let x = win.x() + (win.width() - dialog_w) / 2;
@@ -143,9 +144,6 @@ pub fn show_settings_dialog(win: &Window, system_settings: &SystemSettings) -> O
143144
remote_dns_address.set_value(tun2proxy_cfg.dns_addr.to_string().as_str());
144145
dns_strategy.set_value(tun2proxy_dns_strategy_index(tun2proxy_cfg.dns) as i32);
145146

146-
let result = std::rc::Rc::new(std::cell::RefCell::new(None));
147-
let result_cb = result.clone();
148-
149147
let mut submit_btn = Button::new(dialog_w / 2 - 60, dialog_h - 45, 120, 35, "Submit");
150148
dlg.end();
151149
dlg.show();
@@ -193,13 +191,7 @@ pub fn show_settings_dialog(win: &Window, system_settings: &SystemSettings) -> O
193191
tun2proxy_enable: Some(tun2proxy_enable_val),
194192
tun2proxy: tun2proxy_cfg,
195193
};
196-
*result_cb.borrow_mut() = Some(new_settings);
194+
let _ = tx.send(new_settings);
197195
dlg_cb.hide();
198196
});
199-
200-
while dlg.visible() {
201-
fltk::app::wait();
202-
}
203-
204-
result.borrow().clone()
205197
}

src/states_manager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl WindowState {
3030
}
3131
}
3232

33-
#[derive(Clone, Serialize, Deserialize)]
33+
#[derive(Debug, Clone, Serialize, Deserialize)]
3434
pub struct SystemSettings {
3535
pub listen_host: String,
3636
pub listen_port: u16,
@@ -52,7 +52,7 @@ impl Default for SystemSettings {
5252
fn default() -> Self {
5353
SystemSettings {
5454
listen_host: "127.0.0.1".into(),
55-
listen_port: 1080,
55+
listen_port: 5080,
5656
listen_user: None,
5757
listen_password: None,
5858
pool_max_size: 100,

0 commit comments

Comments
 (0)