Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add file dialog when no files specified. #129

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions bins/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bins/ayaka-gui/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ayaka-model = { workspace = true }
flexi_logger = { workspace = true }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.2", features = ["cli", "window-all"] }
tauri = { version = "1.2", features = ["cli", "dialog-open", "window-all"] }
tauri-plugin-window-state = "0.1"
axum = { version = "0.6", default-features = false, features = [
"http1",
Expand Down
35 changes: 23 additions & 12 deletions bins/ayaka-gui/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ use flexi_logger::{FileSpec, LogSpecification, Logger};
use serde::{Deserialize, Serialize};
use settings::*;
use std::{
borrow::Cow,
collections::{HashMap, HashSet},
fmt::Display,
net::TcpListener,
path::PathBuf,
pin::pin,
};
use tauri::{
async_runtime::RwLock, command, utils::config::AppUrl, AppHandle, Manager, PathResolver, State,
WindowUrl,
api::dialog::blocking::FileDialogBuilder, async_runtime::RwLock, command,
utils::config::AppUrl, AppHandle, Manager, PathResolver, State, WindowUrl,
};

type CommandResult<T> = Result<T, CommandError>;
Expand Down Expand Up @@ -55,13 +57,13 @@ fn ayaka_version() -> &'static str {
}

struct Storage {
config: Vec<String>,
config: Vec<PathBuf>,
dist_port: u16,
model: RwLock<GameViewModel<FileSettingsManager>>,
}

impl Storage {
pub fn new(resolver: &PathResolver, config: Vec<String>, dist_port: u16) -> Self {
pub fn new(resolver: &PathResolver, config: Vec<PathBuf>, dist_port: u16) -> Self {
let manager = FileSettingsManager::new(resolver);
Self {
config,
Expand Down Expand Up @@ -95,10 +97,19 @@ fn dist_port(storage: State<Storage>) -> u16 {

#[command]
async fn open_game(handle: AppHandle, storage: State<'_, Storage>) -> CommandResult<()> {
let config = &storage.config;
let config = if storage.config.is_empty() {
Cow::Owned(
FileDialogBuilder::new()
.add_filter("Ayaka package", &["ayapack"])
.pick_files()
.unwrap_or_default(),
)
} else {
Cow::Borrowed(&storage.config)
};
let mut model = storage.model.write().await;
{
let context = model.open_game(config, FrontendType::Html);
let context = model.open_game(&config, FrontendType::Html);
let mut context = pin!(context);
while let Some(status) = context.next().await {
handle.emit_all("ayaka://open_status", status)?;
Expand Down Expand Up @@ -289,11 +300,11 @@ fn main() -> Result<()> {

let matches = app.get_cli_matches()?;
let config = match &matches.args["config"].value {
Value::String(s) => vec![s.to_string()],
Value::String(s) => vec![PathBuf::from(s)],
Value::Array(arr) => arr
.iter()
.filter_map(|v| v.as_str())
.map(|s| s.to_string())
.map(PathBuf::from)
.collect::<Vec<_>>(),
_ => {
let current = std::env::current_exe()?;
Expand All @@ -311,13 +322,13 @@ fn main() -> Result<()> {
.filter(|p| p.exists()),
);
} else {
paths.push(current.join("config.yaml"));
let current_config = current.join("config.yaml");
if current_config.exists() {
paths.push(current_config);
}
}

paths
.into_iter()
.map(|p| p.to_string_lossy().into_owned())
.collect()
}
};
app.manage(Storage::new(&resolver, config, port));
Expand Down
4 changes: 4 additions & 0 deletions bins/ayaka-gui/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"all": false,
"window": {
"all": true
},
"dialog": {
"all": false,
"open": true
}
},
"bundle": {
Expand Down