-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsettings_window.rs
100 lines (93 loc) · 3.88 KB
/
settings_window.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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,
#[cfg(feature = "self_update")] new_release: &mut Option<Release>,
settings_window_open: &mut bool,
update_text: &mut String,
) -> Option<InnerResponse<Option<()>>> {
egui::Window::new("Settings")
.fixed_size(Vec2 { x: 600.0, y: 200.0 })
.anchor(Align2::CENTER_CENTER, Vec2 { x: 0.0, y: 0.0 })
.collapsible(false)
.show(ctx, |ui| {
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();
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();
}
let current_version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap();
ui.label(format!("Current version: {}", current_version));
ui.end_row();
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");
}
});
ui.label(update_text.clone());
ui.horizontal(|ui| {
ui.horizontal(|ui| {
if !update_text.is_empty() {
ui.disable();
};
if ui.button("Exit Settings").clicked() {
*settings_window_open = false;
*update_text = "".to_string();
}
});
#[cfg(feature = "self_update")]
if !update_text.is_empty() && ui.button("Restart").clicked() {
restart();
ctx.request_repaint(); // Optional: Request repaint for immediate feedback
ui.ctx().send_viewport_cmd(egui::ViewportCommand::Close);
}
});
})
}