tauriでmain windowのdata directoryを変更する (rust)

Rust

tauriでは起動するwebview毎にdata directoryを変更できます。
しかし、main windowのdata directoryを変更するにはどうしたらよいでしょうか?

WindowBuilder in tauri::window - Rust
AbuilderforawebviewwindowmanagedbyTauri.

このdata_directoryメソッドを呼ぶタイミングは以下のようにwindowを生成するタイミングです。
でもmain windowは手動でインスタンス化しないのでこのタイミングが無いように見えます。

let window =
    tauri::WindowBuilder::new(app, "label", tauri::WindowUrl::App("index.html".into()))
        .data_directory(app_dir(app_handle).into())
        .inner_size(800.0, 600.0)
        .build()?;

これ、実は簡単なんです。
tauri.conf.jsonのtauri.windowsを消してしまえばいいんです。

{
    ...略
    "tauri":{ ...略
        "windows": [略]
    }
}

ここにmain windowの設定を書いていると思いますが、実はこれ消せます。
消したうえでコードからmain windowを生成すれば後は意のままに操れます。

#[async_std::main]
async fn main() {
    tauri::Builder::default()
        .setup(|app| {
            let app_handle = app.handle();
            let window =
                tauri::WindowBuilder::new(app, "label", tauri::WindowUrl::App("index.html".into()))
                    .data_directory(app_dir(app_handle).into())
                    .inner_size(800.0, 600.0)
                    .build()?;
            Ok(())
        })
        .invoke_handler(tauri::generate_handler![greet])
        .run(tauri::generate_context!())
        .expect("error while building tauri application");
}

setupで手動でインスタンス化すればdata_directoryを変更する余地がでてきます。
.data_directory(app_dir(app_handle).into())
の部分は仮ですので、任意のPathBufを渡してください。

ちなみに

ここのsetupを使ってTauriが主に利用するPathの一覧を出力できます。
app_data_dirってどこ?resource_dirってどこ?
windowsだとどこよ?Macは?って混乱したりしませんか?

以下のコードで起動時に必要なPathをprintして再確認しましょう。


#[async_std::main]
async fn main() {
    tauri::Builder::default()
        .setup(|app| {
            setup_handler(app);
            Ok(())
        })
        .invoke_handler(tauri::generate_handler![greet])
        .run(tauri::generate_context!())
        .expect("error while building tauri application");
}

fn setup_handler(app: &mut tauri::App)  {
    let app_handle = app.handle();

    println!("{}", app_handle.path_resolver().resource_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", app_handle.path_resolver().app_config_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", app_handle.path_resolver().app_data_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", app_handle.path_resolver().app_local_data_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", app_handle.path_resolver().app_cache_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", app_handle.path_resolver().app_log_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", tauri::api::path::data_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", tauri::api::path::local_data_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", tauri::api::path::cache_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", tauri::api::path::config_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", tauri::api::path::executable_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", tauri::api::path::public_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", tauri::api::path::runtime_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", tauri::api::path::template_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", tauri::api::path::font_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", tauri::api::path::home_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", tauri::api::path::audio_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", tauri::api::path::desktop_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", tauri::api::path::document_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", tauri::api::path::download_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());
    println!("{}", tauri::api::path::picture_dir().unwrap_or(std::path::PathBuf::new()).to_string_lossy());

    return
}
Rust
スポンサーリンク
Once and Only

コメント