1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Rust】Command Launcher

Last updated at Posted at 2025-04-09

GitHub

ScreenShot

Command-Launcher.gif

Motive

就職活動が全く順調ではないため、息抜きに.

Use Case

シェルを開いて、毎回コマンドやスクリプトを実行したりするのって、意外とめんどくさいので、その場面を想定しました。

Tech Stack

  • Rust
  • Tauri 2.0

Difficulty

新しいウィンドウを開くことと、開いたあとにDOMを操作する箇所に躓きました.
0.5秒のスリープを入れたら、新しいウィンドウのスクリプトが実行されたのですが、それだと0.6秒以上かかったら、実行されないんですよね...
perplexityのお力を借りて、なんとか解決しました。
新しいウィンドウでロード->emit->Rustでemitを検知といった感じですね.

confirmation.ts
// ウィンドウのロード完了を通知
// ウィンドウのロードを完了を待って、#commandの書き換えを行うため
window.addEventListener("DOMContentLoaded", () => {
  emit(
    "confirmation-window-loaded", 
    "confirmation"
  );
});
lib.rs
// confirmationウィンドウを開く
#[tauri::command]
async fn open_confirmation_window(
    app: tauri::AppHandle,
    command: String,
) -> tauri::Result<()> {
    let new_window = WebviewWindowBuilder::new(
        &app,
        "confirmation",
        WebviewUrl::App("confirmation.html".into()),
    )
    .inner_size(600.0, 160.0)
    .center()
    .build()?;

    // ウィンドウのロード完了を受信
    // ウィンドウのロードを完了を待って、#commandの書き換えを行うため
    // フラグを Box でラップして可変性を持たせる
    let has_processed = std::sync::Arc::new(std::sync::Mutex::new(false));
    let has_processed_clone = has_processed.clone();

    app.listen("confirmation-window-loaded", move |event| {
        let mut flag = has_processed_clone.lock().unwrap();
        if *flag {
            return;
        }
        let payload = event.payload();
        if payload == "\"confirmation\"" {
            new_window.emit("update-command", &command).unwrap();
            *flag = true;
        }
    });
    
    Ok(())
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?