Skip to content

Commit a2086b9

Browse files
committed
implement application restart
1 parent 5a40f4f commit a2086b9

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/gui.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::data::{DataContainer, SerialDirection};
1212
use crate::serial::{clear_serial_settings, save_serial_settings, Device, SerialDevices};
1313
use crate::settings_window::settings_window;
1414
use crate::toggle::toggle;
15+
use crate::update::check_update;
1516
use crate::FileOptions;
1617
use crate::{APP_INFO, PREFS_KEY};
1718
use eframe::egui::panel::Side;
@@ -121,6 +122,7 @@ pub struct MyApp {
121122
information_panel: InformationPanel,
122123
file_opened: bool,
123124
settings_window_open: bool,
125+
update_text: String,
124126
gui_conf: GuiSettingsContainer,
125127
device_lock: Arc<RwLock<Device>>,
126128
devices_lock: Arc<RwLock<Vec<String>>>,
@@ -243,6 +245,7 @@ impl MyApp {
243245
file_opened: false,
244246
new_release: None,
245247
settings_window_open: false,
248+
update_text: "".to_string(),
246249
}
247250
}
248251

@@ -882,6 +885,7 @@ impl MyApp {
882885
.button(format!("{} Settings", egui_phosphor::regular::GEAR_FINE))
883886
.clicked()
884887
{
888+
self.new_release = check_update();
885889
self.settings_window_open = true;
886890
}
887891
if self.settings_window_open {
@@ -890,6 +894,7 @@ impl MyApp {
890894
&mut self.gui_conf,
891895
&mut self.new_release,
892896
&mut self.settings_window_open,
897+
&mut self.update_text,
893898
);
894899
}
895900

src/settings_window.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::gui::GuiSettingsContainer;
2-
use crate::update::{check_update, update};
2+
use crate::update::{check_update, restart_application, update};
33
use eframe::egui;
44
use eframe::egui::{Align2, InnerResponse, Vec2, Visuals};
55
use egui_theme_switch::ThemeSwitch;
@@ -11,6 +11,7 @@ pub fn settings_window(
1111
gui_conf: &mut GuiSettingsContainer,
1212
new_release: &mut Option<Release>,
1313
settings_window_open: &mut bool,
14+
update_text: &mut String,
1415
) -> Option<InnerResponse<Option<()>>> {
1516
egui::Window::new("Settings")
1617
.fixed_size(Vec2 { x: 600.0, y: 200.0 })
@@ -44,6 +45,7 @@ pub fn settings_window(
4445
if ui.button("Update").clicked() {
4546
if let Ok(()) = update(r.clone()) {
4647
*new_release = None;
48+
*update_text = "Update done. Please Restart Application.".to_string();
4749
}
4850
}
4951
} else {
@@ -55,10 +57,19 @@ pub fn settings_window(
5557
});
5658
ui.label("No new update");
5759
}
60+
});
61+
ui.label(update_text.clone());
5862

59-
ui.end_row();
63+
ui.horizontal(|ui| {
6064
if ui.button("Exit Settings").clicked() {
6165
*settings_window_open = false;
66+
*update_text = "".to_string();
67+
}
68+
69+
if !update_text.is_empty() && ui.button("Restart").clicked() {
70+
let _ = restart_application();
71+
ctx.request_repaint(); // Optional: Request repaint for immediate feedback
72+
ui.ctx().send_viewport_cmd(egui::ViewportCommand::Close);
6273
}
6374
});
6475
})

src/update.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use self_update::update::Release;
33
use semver::Version;
44
use std::fs::File;
55
use std::path::Path;
6+
use std::process::{Command, ExitStatus};
67
use std::{env, fs, io};
78
use zip::ZipArchive;
89

@@ -11,6 +12,17 @@ const REPO_NAME: &str = "serial-monitor-rust";
1112

1213
const MACOS_APP_NAME: &str = "Serial Monitor.app";
1314

15+
pub fn restart_application() -> std::io::Result<ExitStatus> {
16+
// Get the current executable path
17+
let current_exe = std::env::current_exe().expect("Failed to get current executable path");
18+
19+
// Launch a new instance of the application
20+
Command::new(current_exe)
21+
.spawn()
22+
.expect("Failed to restart application")
23+
.wait()
24+
}
25+
1426
fn extract_zip(tmp_archive_path: &Path, tmp_archive_dir: &Path) -> io::Result<()> {
1527
// Open the zip file
1628
let file = File::open(tmp_archive_path)?;

0 commit comments

Comments
 (0)