Refactor file picker

This commit is contained in:
Dustin Carlino 2021-05-16 10:51:03 -07:00
parent 4f05d818b9
commit d6539f7449
8 changed files with 48 additions and 40 deletions

2
Cargo.lock generated
View File

@ -1333,7 +1333,6 @@ dependencies = [
"popdat",
"rand",
"rand_xorshift",
"rfd",
"serde",
"serde_json",
"sim",
@ -2303,6 +2302,7 @@ dependencies = [
"js-sys",
"log",
"map_model",
"rfd",
"serde",
"sim",
"subprocess",

View File

@ -43,7 +43,6 @@ petname = "1.1.0"
popdat = { path = "../popdat" }
rand = "0.8.3"
rand_xorshift = "0.3.0"
rfd = "0.2.2"
serde = "1.0.123"
serde_json = "1.0.61"
svg_face = "0.1.3"

View File

@ -764,6 +764,7 @@ pub struct SessionState {
pub tutorial: Option<TutorialState>,
pub high_scores: BTreeMap<GameplayMode, Vec<HighScore>>,
pub info_panel_tab: BTreeMap<&'static str, &'static str>,
pub last_gmns_timing_csv: Option<String>,
}
impl SessionState {
@ -778,6 +779,7 @@ impl SessionState {
"person" => "trips",
"bus" => "status",
},
last_gmns_timing_csv: None,
}
}
}

View File

@ -8,8 +8,7 @@ use abstutil::{prettyprint_usize, Parallelism, Timer};
use geom::{Circle, Distance, PolyLine, Polygon, Pt2D, Ring};
use kml::{ExtraShape, ExtraShapes};
use map_gui::colors::ColorScheme;
use map_gui::load::FutureLoader;
use map_gui::tools::PopupMsg;
use map_gui::tools::{FilePicker, PopupMsg};
use map_model::BuildingID;
use widgetry::{
lctrl, Choice, Color, Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Key, Line,
@ -403,24 +402,9 @@ fn make_query(app: &App, objects: &[Object], query: &str) -> (GeomBatch, usize)
}
fn pick_file(ctx: &mut EventCtx, app: &App) -> Transition {
let (_, outer_progress_rx) = futures_channel::mpsc::channel(1);
let (_, inner_progress_rx) = futures_channel::mpsc::channel(1);
let start_dir = app.primary.map.get_city_name().input_path("");
Transition::Push(FutureLoader::<App, Option<String>>::new_state(
Transition::Push(FilePicker::new_state(
ctx,
Box::pin(async move {
let result = rfd::AsyncFileDialog::new()
.set_directory(&start_dir)
.pick_file()
.await
.map(|x| x.path().display().to_string());
let wrap: Box<dyn Send + FnOnce(&App) -> Option<String>> =
Box::new(move |_: &App| result);
Ok(wrap)
}),
outer_progress_rx,
inner_progress_rx,
"Waiting for a file to be chosen",
Some(app.primary.map.get_city_name().input_path("")),
Box::new(|ctx, app, maybe_path| {
if let Ok(Some(path)) = maybe_path {
Transition::Multi(vec![

View File

@ -1,30 +1,16 @@
// TODO This doesn't really belong in gameplay/freeform
use map_gui::load::FutureLoader;
use map_gui::tools::{find_exe, RunCommand};
use map_gui::tools::{find_exe, FilePicker, RunCommand};
use widgetry::EventCtx;
use crate::app::{App, Transition};
use crate::app::Transition;
use crate::sandbox::gameplay::GameplayMode;
use crate::sandbox::SandboxMode;
pub fn import(ctx: &mut EventCtx) -> Transition {
let (_, outer_progress_rx) = futures_channel::mpsc::channel(1);
let (_, inner_progress_rx) = futures_channel::mpsc::channel(1);
Transition::Push(FutureLoader::<App, Option<String>>::new_state(
Transition::Push(FilePicker::new_state(
ctx,
Box::pin(async {
let result = rfd::AsyncFileDialog::new()
.pick_file()
.await
.map(|x| x.path().display().to_string());
let wrap: Box<dyn Send + FnOnce(&App) -> Option<String>> =
Box::new(move |_: &App| result);
Ok(wrap)
}),
outer_progress_rx,
inner_progress_rx,
"Waiting for a file to be chosen",
None,
Box::new(|ctx, app, maybe_path| {
if let Ok(Some(path)) = maybe_path {
Transition::Replace(RunCommand::new_state(

View File

@ -27,6 +27,7 @@ instant = "0.1.7"
js-sys = { version = "0.3.47", optional = true }
log = "0.4.14"
map_model = { path = "../map_model" }
rfd = "0.2.2"
serde = "1.0.123"
sim = { path = "../sim" }
subprocess = { git = "https://github.com/hniksic/rust-subprocess", optional = true }

View File

@ -12,7 +12,7 @@ pub use self::icons::{goal_marker, start_marker};
pub use self::minimap::{Minimap, MinimapControls};
pub use self::navigate::Navigator;
pub use self::turn_explorer::TurnExplorer;
pub use self::ui::{ChooseSomething, PopupMsg, PromptInput};
pub use self::ui::{ChooseSomething, FilePicker, PopupMsg, PromptInput};
pub use self::url::URLManager;
use crate::AppLike;

View File

@ -1,10 +1,13 @@
//! Generic UI tools. Some of this should perhaps be lifted to widgetry.
use anyhow::Result;
use widgetry::{
hotkeys, Choice, DrawBaselayer, Drawable, EventCtx, GfxCtx, Key, Line, Menu, Outcome, Panel,
State, Text, TextBox, Transition, Widget,
};
use crate::load::FutureLoader;
use crate::tools::grey_out_map;
use crate::AppLike;
@ -209,3 +212,36 @@ impl<A: AppLike> State<A> for PopupMsg {
}
}
}
pub struct FilePicker;
impl FilePicker {
pub fn new_state<A: 'static + AppLike>(
ctx: &mut EventCtx,
start_dir: Option<String>,
on_load: Box<dyn FnOnce(&mut EventCtx, &mut A, Result<Option<String>>) -> Transition<A>>,
) -> Box<dyn State<A>> {
let (_, outer_progress_rx) = futures_channel::mpsc::channel(1);
let (_, inner_progress_rx) = futures_channel::mpsc::channel(1);
FutureLoader::<A, Option<String>>::new_state(
ctx,
Box::pin(async move {
let mut builder = rfd::AsyncFileDialog::new();
if let Some(dir) = start_dir {
builder = builder.set_directory(&dir);
}
let result = builder
.pick_file()
.await
.map(|x| x.path().display().to_string());
let wrap: Box<dyn Send + FnOnce(&A) -> Option<String>> =
Box::new(move |_: &A| result);
Ok(wrap)
}),
outer_progress_rx,
inner_progress_rx,
"Waiting for a file to be chosen",
on_load,
)
}
}