Skip to content

Commit

Permalink
Add FileSelection Widget
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKnauth committed Dec 19, 2023
1 parent 958e95c commit 688763e
Showing 1 changed file with 58 additions and 9 deletions.
67 changes: 58 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
fmt,
fs::{self, File},
io::Write,
path::PathBuf,
path::{Path, PathBuf},
sync::{
atomic::{AtomicU64, AtomicUsize},
Arc, Mutex, RwLock,
Expand All @@ -30,6 +30,7 @@ use egui_file::FileDialog;
use egui_plot::{Bar, BarChart, Legend, Plot, VLine};
use hdrhistogram::Histogram;
use indexmap::IndexMap;
use livesplit_auto_splitting::wasi_path::{path_to_wasi, wasi_to_path};
use livesplit_auto_splitting::{
settings, time, AutoSplitter, CompiledAutoSplitter, Config, ExecutionGuard, Runtime, Timer,
TimerState,
Expand Down Expand Up @@ -278,13 +279,19 @@ struct AppState {
module_modified_time: Option<SystemTime>,
script_modified_time: Option<SystemTime>,
optimize: bool,
open_file_dialog: Option<(FileDialog, bool)>,
open_file_dialog: Option<(FileDialog, FileDialogInfo)>,
module: Option<CompiledAutoSplitter>,
shared_state: Arc<SharedState>,
timer: DebuggerTimer,
runtime: livesplit_auto_splitting::Runtime,
}

enum FileDialogInfo {
WASM,
Script,
SettingsWidget(Arc<str>),
}

struct TabViewer<'a> {
state: &'a mut AppState,
}
Expand All @@ -309,7 +316,7 @@ impl egui_dock::TabViewer for TabViewer<'_> {
if ui.button("Open").clicked() {
let mut dialog = FileDialog::open_file(self.state.path.clone());
dialog.open();
self.state.open_file_dialog = Some((dialog, true));
self.state.open_file_dialog = Some((dialog, FileDialogInfo::WASM));
}
if let Some(auto_splitter) = &*self.state.shared_state.auto_splitter.load() {
if ui.button("Restart").clicked() {
Expand All @@ -330,7 +337,7 @@ impl egui_dock::TabViewer for TabViewer<'_> {
let mut dialog =
FileDialog::open_file(self.state.script_path.clone());
dialog.open();
self.state.open_file_dialog = Some((dialog, false));
self.state.open_file_dialog = Some((dialog, FileDialogInfo::Script));
}
if self.state.shared_state.auto_splitter.load().is_some() {
if let Some(script_path) = &self.state.script_path {
Expand Down Expand Up @@ -607,6 +614,30 @@ impl egui_dock::TabViewer for TabViewer<'_> {
}
}
}
settings::WidgetKind::FileSelection { ref filter } => {
ui.add_space(spacing);
let settings_map = runtime.settings_map();
let current_path: Option<PathBuf> =
match settings_map.get(&setting.key) {
Some(settings::Value::String(path)) => wasi_to_path(path),
_ => None,
};
let filter_pieces: Vec<String> =
filter.split('*').map(String::from).collect();
if ui.button(&*setting.description).clicked() {
let mut dialog = FileDialog::open_file(current_path).filter(
Box::new(move |p: &Path| {
let s = p.to_string_lossy();
filter_pieces.iter().all(|f| s.contains(f))
}),
);
dialog.open();
self.state.open_file_dialog = Some((
dialog,
FileDialogInfo::SettingsWidget(setting.key.clone()),
));
}
}
});
ui.end_row();
}
Expand Down Expand Up @@ -800,13 +831,31 @@ impl App for Debugger {
}
}

if let Some((dialog, is_wasm)) = &mut self.state.open_file_dialog {
if let Some((dialog, info)) = &mut self.state.open_file_dialog {
if dialog.show(ctx).selected() {
if let Some(file) = dialog.path().map(ToOwned::to_owned) {
if *is_wasm {
self.state.load(Load::File(file));
} else {
self.state.set_script_path(file);
match info {
FileDialogInfo::WASM => self.state.load(Load::File(file)),
FileDialogInfo::Script => self.state.set_script_path(file),
FileDialogInfo::SettingsWidget(key) => {
if let Some(s) = path_to_wasi(&file) {
if let Some(runtime) =
&*self.state.shared_state.runtime.read().unwrap()
{
loop {
let old = runtime.settings_map();
let mut new = old.clone();
new.insert(
key.clone(),
settings::Value::String(s.as_ref().into()),
);
if runtime.set_settings_map_if_unchanged(&old, new) {
break;
}
}
}
}
}
}
}
}
Expand Down

0 comments on commit 688763e

Please sign in to comment.