Skip to content

Commit 245bfd9

Browse files
committed
using close event from slint to force exit the app
1 parent 3042818 commit 245bfd9

File tree

4 files changed

+11
-28
lines changed

4 files changed

+11
-28
lines changed

src/event_handler/debug.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::{collections::HashMap, process::exit, rc::Rc, time::Duration};
1+
use std::{collections::HashMap, rc::Rc, time::Duration};
22

33
use crate::slint_generatedAppWindow::{raw_can, AppWindow};
4-
use slint::{ComponentHandle, Model, SharedString, VecModel, Weak};
4+
use slint::{Model, SharedString, VecModel, Weak};
55
use socketcan::{CanSocket, EmbeddedFrame, Frame, Socket};
66
pub struct DebugHandler<'a> {
77
#[cfg(target_os = "linux")]
@@ -14,14 +14,6 @@ pub struct DebugHandler<'a> {
1414
}
1515

1616
impl<'a> DebugHandler<'a> {
17-
pub fn check_to_kill_thread(&self) {
18-
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
19-
if !ui.window().is_visible() {
20-
exit(1);
21-
}
22-
});
23-
}
24-
2517
pub fn run(&mut self) {
2618
let can_socket = CanSocket::open(self.iface).unwrap();
2719
if let Ok(frame) = can_socket.read_frame() {

src/event_handler/init.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::slint_generatedAppWindow::{socket_info, AppWindow};
22
#[cfg(target_os = "windows")]
33
use pcan_basic::hw::attached_channels as available_interfaces;
4-
use slint::{ComponentHandle, ModelRc, SharedString, VecModel, Weak};
4+
use slint::{ModelRc, SharedString, VecModel, Weak};
55
#[cfg(target_os = "linux")]
66
use socketcan::available_interfaces;
7-
use std::{process::exit, time::Duration};
7+
use std::time::Duration;
88
pub struct Init<'a> {
99
pub ui_handle: &'a Weak<AppWindow>,
1010
}
@@ -95,11 +95,6 @@ impl<'a> Init<'a> {
9595
});
9696
}
9797
};
98-
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
99-
if !ui.window().is_visible() {
100-
exit(1);
101-
}
102-
});
10398
std::thread::sleep(Duration::from_millis(50));
10499
}
105100
}

src/event_handler/view.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ use can_dbc::DBC;
22
use chrono::Utc;
33
#[cfg(target_os = "windows")]
44
use pcan_basic::socket::usb::UsbCanSocket;
5-
use slint::{ComponentHandle, Model, ModelRc, SharedString, VecModel, Weak};
5+
use slint::{Model, ModelRc, SharedString, VecModel, Weak};
66
#[cfg(target_os = "linux")]
77
use socketcan::{CanInterface, CanSocket, EmbeddedFrame, Frame, Socket};
88
use std::{
99
collections::HashMap,
1010
fmt::Write,
11-
process::exit,
1211
rc::Rc,
1312
sync::{mpsc::Receiver, Arc, Mutex},
1413
thread::sleep,
@@ -30,13 +29,6 @@ static mut NEW_DBC_CHECK: bool = false;
3029
use super::{EVEN_COLOR, ODD_COLOR};
3130

3231
impl<'a> ViewHandler<'a> {
33-
pub fn check_to_kill_thread(&self) {
34-
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
35-
if !ui.window().is_visible() {
36-
exit(1);
37-
}
38-
});
39-
}
4032
pub fn process_can_messages(&mut self) {
4133
if let Ok(dbc) = self.mspc_rx.lock().unwrap().try_recv() {
4234
#[cfg(target_os = "linux")]
@@ -76,7 +68,6 @@ impl<'a> ViewHandler<'a> {
7668
let mut start_bus_load = Instant::now();
7769
let mut total_bits = 0;
7870
loop {
79-
self.check_to_kill_thread();
8071
let bus_state = match can_if.state().unwrap().unwrap() {
8172
socketcan::nl::CanState::ErrorActive => "ERR_ACTIVE",
8273
socketcan::nl::CanState::ErrorWarning => "ERR_WARNING",

src/main.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ async fn main() -> io::Result<()> {
118118
};
119119
loop {
120120
can_handler.run();
121-
can_handler.check_to_kill_thread();
122121
}
123122
}
124123
});
@@ -144,6 +143,12 @@ async fn main() -> io::Result<()> {
144143
};
145144
packet_filter.process_filter();
146145
});
146+
147+
ui.window().on_close_requested(|| {
148+
println!("Closing the application...");
149+
std::process::exit(0);
150+
});
151+
147152
ui.run().unwrap();
148153
Ok(())
149154
}

0 commit comments

Comments
 (0)