Skip to content

Commit

Permalink
introduce self-update as a feature
Browse files Browse the repository at this point in the history
  • Loading branch information
hacknus committed Jan 22, 2025
1 parent 99796a0 commit 76d1444
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 46 deletions.
12 changes: 8 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ regex = "1"
serde = { version = "1.0", features = ["derive"] }
serialport = { version = "4.7", features = ["serde"] }
log = "0.4"
self_update = { git = "https://github.com/hacknus/self_update", features = ["archive-zip", "compression-zip-deflate"] }
tempfile = "3.15"
reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "rustls-tls", "http2"] }
semver = "1.0.24"
self_update = { git = "https://github.com/hacknus/self_update", features = ["archive-zip", "compression-zip-deflate"], optional = true }
tempfile = { version = "3.15", optional = true }
reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "rustls-tls", "http2"], optional = true }
semver = { version = "1.0.24", optional = true }

[features]
default = ["self_update"]
self_update = ["dep:self_update", "tempfile", "reqwest", "semver"]

[build-dependencies]
regex = "1.11"
Expand Down
10 changes: 9 additions & 1 deletion src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::data::{DataContainer, SerialDirection};
use crate::serial::{clear_serial_settings, save_serial_settings, Device, SerialDevices};
use crate::settings_window::settings_window;
use crate::toggle::toggle;
#[cfg(feature = "self_update")]
use crate::update::check_update;
use crate::FileOptions;
use crate::{APP_INFO, PREFERENCES_KEY};
Expand All @@ -25,6 +26,7 @@ use egui_file_dialog::information_panel::InformationPanel;
use egui_file_dialog::FileDialog;
use egui_plot::{log_grid_spacer, GridMark, Legend, Line, Plot, PlotPoint, PlotPoints};
use preferences::Preferences;
#[cfg(feature = "self_update")]
use self_update::update::Release;
use serde::{Deserialize, Serialize};
use serialport::{DataBits, FlowControl, Parity, StopBits};
Expand Down Expand Up @@ -146,6 +148,7 @@ pub struct MyApp {
show_warning_window: WindowFeedback,
do_not_show_clear_warning: bool,
init: bool,
#[cfg(feature = "self_update")]
new_release: Option<Release>,
}

Expand Down Expand Up @@ -243,6 +246,7 @@ impl MyApp {
init: false,
show_color_window: ColorWindow::NoShow,
file_opened: false,
#[cfg(feature = "self_update")]
new_release: None,
settings_window_open: false,
update_text: "".to_string(),
Expand Down Expand Up @@ -885,13 +889,17 @@ impl MyApp {
.button(format!("{} Settings", egui_phosphor::regular::GEAR_FINE))
.clicked()
{
self.new_release = check_update();
#[cfg(feature = "self_update")]
{
self.new_release = check_update();
}
self.settings_window_open = true;
}
if self.settings_window_open {
settings_window(
ui.ctx(),
&mut self.gui_conf,
#[cfg(feature = "self_update")]
&mut self.new_release,
&mut self.settings_window_open,
&mut self.update_text,
Expand Down
93 changes: 52 additions & 41 deletions src/settings_window.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use crate::gui::GuiSettingsContainer;
#[cfg(feature = "self_update")]
use crate::update::{check_update, update};
use eframe::egui;
use eframe::egui::{Align2, InnerResponse, Vec2, Visuals};
use egui_theme_switch::ThemeSwitch;
#[cfg(feature = "self_update")]
use self_update::restart::restart;
#[cfg(feature = "self_update")]
use self_update::update::Release;
#[cfg(feature = "self_update")]
use semver::Version;

pub fn settings_window(
ctx: &egui::Context,
gui_conf: &mut GuiSettingsContainer,
new_release: &mut Option<Release>,
#[cfg(feature = "self_update")] new_release: &mut Option<Release>,
settings_window_open: &mut bool,
update_text: &mut String,
) -> Option<InnerResponse<Option<()>>> {
Expand All @@ -19,53 +23,59 @@ pub fn settings_window(
.anchor(Align2::CENTER_CENTER, Vec2 { x: 0.0, y: 0.0 })
.collapsible(false)
.show(ctx, |ui| {
egui::Grid::new("settings").striped(true).show(ui, |ui| {
if ui
.add(ThemeSwitch::new(&mut gui_conf.theme_preference))
.changed()
{
ui.ctx().set_theme(gui_conf.theme_preference);
};
gui_conf.dark_mode = ui.visuals() == &Visuals::dark();

ui.end_row();
ui.end_row();

if ui.button("Check for Updates").clicked() {
*new_release = check_update();
}
egui::Grid::new("theme settings")
.striped(true)
.show(ui, |ui| {
if ui
.add(ThemeSwitch::new(&mut gui_conf.theme_preference))
.changed()
{
ui.ctx().set_theme(gui_conf.theme_preference);
};
gui_conf.dark_mode = ui.visuals() == &Visuals::dark();

let current_version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap();
ui.label(format!("Current version: {}", current_version));
ui.end_row();
ui.end_row();
});
#[cfg(feature = "self_update")]
egui::Grid::new("update settings")
.striped(true)
.show(ui, |ui| {
if ui.button("Check for Updates").clicked() {
*new_release = check_update();
}

ui.end_row();
let current_version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap();
ui.label(format!("Current version: {}", current_version));

if let Some(r) = &new_release {
ui.label(format!("New release: {}", r.version));
ui.end_row();
if ui.button("Update").clicked() {
match update(r.clone()) {
Ok(_) => {
log::info!("Update done. {} >> {}", current_version, r.version);
*new_release = None;
*update_text =
"Update done. Please Restart Application.".to_string();
}
Err(err) => {
log::error!("{}", err);

if let Some(r) = &new_release {
ui.label(format!("New release: {}", r.version));
ui.end_row();
if ui.button("Update").clicked() {
match update(r.clone()) {
Ok(_) => {
log::info!("Update done. {} >> {}", current_version, r.version);
*new_release = None;
*update_text =
"Update done. Please Restart Application.".to_string();
}
Err(err) => {
log::error!("{}", err);
}
}
}
} else {
ui.label("");
ui.end_row();
ui.horizontal(|ui| {
ui.disable();
let _ = ui.button("Update");
});
ui.label("You have the latest version");
}
} else {
ui.label("");
ui.end_row();
ui.horizontal(|ui| {
ui.disable();
let _ = ui.button("Update");
});
ui.label("You have the latest version");
}
});
});
ui.label(update_text.clone());

ui.horizontal(|ui| {
Expand All @@ -79,6 +89,7 @@ pub fn settings_window(
}
});

#[cfg(feature = "self_update")]
if !update_text.is_empty() && ui.button("Restart").clicked() {
restart();
ctx.request_repaint(); // Optional: Request repaint for immediate feedback
Expand Down
2 changes: 2 additions & 0 deletions src/update.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "self_update")]

use self_update::self_replace;
use self_update::update::Release;
use semver::Version;
Expand Down

0 comments on commit 76d1444

Please sign in to comment.