Close open files on exit

This commit is contained in:
hardliner66 2024-10-08 23:21:53 +02:00
parent 7a54e5e40f
commit e8ac6c60f1
1 changed files with 23 additions and 2 deletions

View File

@ -3,6 +3,8 @@
windows_subsystem = "windows" windows_subsystem = "windows"
)] )]
use std::collections::HashMap;
use std::process::Child;
use std::str::FromStr; use std::str::FromStr;
use std::{path::PathBuf, process::Command}; use std::{path::PathBuf, process::Command};
@ -27,6 +29,8 @@ struct LogFile {
#[derive(AppState, Serialize, Deserialize)] #[derive(AppState, Serialize, Deserialize)]
struct State { struct State {
log_history: IndexSet<LogFile>, log_history: IndexSet<LogFile>,
#[serde(skip)]
open_files: HashMap<PathBuf, Child>,
} }
impl State { impl State {
@ -64,7 +68,14 @@ fn update(_app: &mut App, _state: &mut State) {}
fn event(_app: &mut App, _assets: &mut Assets, state: &mut State, evt: Event) { fn event(_app: &mut App, _assets: &mut Assets, state: &mut State, evt: Event) {
match evt { match evt {
Event::Exit => {} Event::Exit => {
for (_, mut child) in state.open_files.drain() {
if let Ok(Some(_)) = child.try_wait() {
continue;
}
_ = child.kill();
}
}
Event::Drop(file) => { Event::Drop(file) => {
if file.name.ends_with(".log") { if file.name.ends_with(".log") {
state.log_history.shift_insert( state.log_history.shift_insert(
@ -88,6 +99,8 @@ fn draw(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut St
egui::SidePanel::left("side_panel") egui::SidePanel::left("side_panel")
.exact_width(app.window().width() as f32) .exact_width(app.window().width() as f32)
.show(ctx, |ui| { .show(ctx, |ui| {
ui.heading("Log History");
ui.separator();
egui::ScrollArea::vertical().show(ui, |ui| { egui::ScrollArea::vertical().show(ui, |ui| {
let mut to_swap = None; let mut to_swap = None;
let mut to_delete = None; let mut to_delete = None;
@ -108,7 +121,14 @@ fn draw(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut St
} }
}); });
if label.clicked() { if label.clicked() {
_ = Command::new("fast_log_viewer.exe").arg(path_text).spawn(); if let Some(child) = state.open_files.get_mut(&log.path) {
if let Ok(Some(_)) = child.try_wait() {
*child = Command::new("fast_log_viewer.exe")
.arg(&path_text)
.spawn()
.unwrap();
}
}
to_swap = Some(i); to_swap = Some(i);
} }
if i < history_len - 1 { if i < history_len - 1 {
@ -154,5 +174,6 @@ fn init(_gfx: &mut Graphics) -> State {
state.unwrap_or_else(|| State { state.unwrap_or_else(|| State {
log_history: IndexSet::new(), log_history: IndexSet::new(),
open_files: HashMap::new(),
}) })
} }