Prompt popup implementation

This commit is contained in:
2026-02-02 20:34:32 +02:00
parent 6416d7c347
commit b5ae7c8550
6 changed files with 97 additions and 29 deletions

View File

@@ -3,10 +3,10 @@
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tauri::{Manager, State, WindowEvent}; use tauri::{Emitter, Manager, State, WindowEvent};
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState}; use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState};
use shared::ai::{PromptRequest, ai_daemon_client::AiDaemonClient}; use shared::ai::{ai_daemon_client::AiDaemonClient, PromptRequest};
struct AppState { struct AppState {
grpc_client: Mutex<AiDaemonClient<tonic::transport::Channel>>, grpc_client: Mutex<AiDaemonClient<tonic::transport::Channel>>,
@@ -22,6 +22,7 @@ fn toggle_popup(app_handle: tauri::AppHandle) {
} else { } else {
window.show().unwrap(); window.show().unwrap();
window.set_focus().unwrap(); window.set_focus().unwrap();
let _ = window.emit("window-focused", ());
} }
} }
None => { None => {
@@ -32,6 +33,7 @@ fn toggle_popup(app_handle: tauri::AppHandle) {
#[tauri::command] #[tauri::command]
async fn prompt_llm(state: State<'_, AppState>, prompt: String) -> Result<String, String> { async fn prompt_llm(state: State<'_, AppState>, prompt: String) -> Result<String, String> {
println!(">>>> {}", prompt);
let mut client = state.grpc_client.lock().await; let mut client = state.grpc_client.lock().await;
let request = tonic::Request::new(PromptRequest { prompt }); let request = tonic::Request::new(PromptRequest { prompt });
match client.prompt(request).await { match client.prompt(request).await {
@@ -42,7 +44,6 @@ async fn prompt_llm(state: State<'_, AppState>, prompt: String) -> Result<String
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let channel = tonic::transport::Channel::from_static("http://[::1]:50051") let channel = tonic::transport::Channel::from_static("http://[::1]:50051")
.connect() .connect()
.await .await
@@ -51,7 +52,9 @@ async fn main() {
let client = AiDaemonClient::new(channel); let client = AiDaemonClient::new(channel);
tauri::Builder::default() tauri::Builder::default()
.manage(AppState { grpc_client: Mutex::new(client) }) .manage(AppState {
grpc_client: Mutex::new(client),
})
.plugin(tauri_plugin_global_shortcut::Builder::new().build()) .plugin(tauri_plugin_global_shortcut::Builder::new().build())
.invoke_handler(tauri::generate_handler![toggle_popup, prompt_llm]) .invoke_handler(tauri::generate_handler![toggle_popup, prompt_llm])
.setup(|app| { .setup(|app| {
@@ -66,7 +69,8 @@ async fn main() {
}) })
} }
let shortcut = Shortcut::new(Some(Modifiers::META), Code::Space); let shortcut = Shortcut::new(Some(Modifiers::META), Code::Space);
app.global_shortcut().on_shortcut(shortcut, move |app, _shortcut, event| { app.global_shortcut()
.on_shortcut(shortcut, move |app, _shortcut, event| {
if event.state() == ShortcutState::Pressed { if event.state() == ShortcutState::Pressed {
toggle_popup(app.clone()); toggle_popup(app.clone());
} }

View File

@@ -23,7 +23,7 @@
"title": "AI Quick Action", "title": "AI Quick Action",
"url": "/popup", "url": "/popup",
"width": 600, "width": 600,
"height": 100, "height": 260,
"decorations": false, "decorations": false,
"transparent": true, "transparent": true,
"alwaysOnTop": true, "alwaysOnTop": true,

View File

@@ -1,15 +1,10 @@
use crate::bridge::invoke;
use crate::popup::Popup;
use leptos::{prelude::*, reactive::spawn_local}; use leptos::{prelude::*, reactive::spawn_local};
use leptos_router::{ use leptos_router::{
components::{Route, Router, Routes}, components::{Route, Router, Routes},
path, path,
}; };
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = ["window", "__TAURI__", "core"])]
async fn invoke(cmd: &str, args: JsValue) -> JsValue;
}
#[component] #[component]
pub fn App() -> impl IntoView { pub fn App() -> impl IntoView {
@@ -33,7 +28,9 @@ fn Dashboard() -> impl IntoView {
}; };
let prompt = |_ev: leptos::ev::MouseEvent| { let prompt = |_ev: leptos::ev::MouseEvent| {
spawn_local(async { spawn_local(async {
let prompt = serde_wasm_bindgen::to_value(&serde_json::json!({"prompt": "jee juu juu"})).unwrap(); let prompt =
serde_wasm_bindgen::to_value(&serde_json::json!({"prompt": "jee juu juu"}))
.unwrap();
invoke("prompt_llm", prompt).await; invoke("prompt_llm", prompt).await;
}); });
}; };
@@ -45,13 +42,3 @@ fn Dashboard() -> impl IntoView {
</main> </main>
} }
} }
#[component]
fn Popup() -> impl IntoView {
view! {
<main class="window-shell rounded-container">
<h3>"AI quick action"</h3>
<input type="text" placeholder="Ask Gordon AI" autofocus />
</main>
}
}

6
frontend/src/bridge.rs Normal file
View File

@@ -0,0 +1,6 @@
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = ["window", "__TAURI__", "core"])]
pub async fn invoke(cmd: &str, args: JsValue) -> JsValue;
}

View File

@@ -1,4 +1,6 @@
mod app; mod app;
mod bridge;
mod popup;
use app::*; use app::*;
use leptos::prelude::*; use leptos::prelude::*;

69
frontend/src/popup.rs Normal file
View File

@@ -0,0 +1,69 @@
use crate::bridge::invoke;
use leptos::{html::Input, prelude::*};
use wasm_bindgen::{prelude::Closure, JsCast};
#[component]
pub fn Popup() -> impl IntoView {
let prompt_input_ref = NodeRef::<Input>::new();
let (prompt_text, set_prompt_text) = signal(String::new());
let (prompt_result, set_prompt_result) = signal(String::new());
let prompt_action = Action::new_local(|prompt: &String| {
let prompt = prompt.clone();
async move {
let response = invoke(
"prompt_llm",
serde_wasm_bindgen::to_value(&serde_json::json!({"prompt": prompt})).unwrap(),
)
.await;
let result: String = serde_wasm_bindgen::from_value(response).unwrap();
result
}
});
Effect::new(move |_| {
if let Some(result) = prompt_action.value().get() {
set_prompt_result.set(result)
}
});
Effect::new(move |_| {
let Some(input_el) = prompt_input_ref.get() else {
return;
};
let handle_focus = Closure::<dyn FnMut()>::new(move || {
let _ = input_el.focus();
set_prompt_text.update(|s| *s = "".to_string());
});
window()
.add_event_listener_with_callback("focus", handle_focus.as_ref().unchecked_ref())
.unwrap();
handle_focus.forget();
});
view! {
<main class="window-shell rounded-container">
<h3>"AI quick action"</h3>
<input
type="text"
node_ref=prompt_input_ref
placeholder="Ask Gordon AI"
autofocus
on:input=move |ev| set_prompt_text.set(event_target_value(&ev))
on:keydown=move |ev| {
if ev.key() == "Enter" {
prompt_action.dispatch(prompt_text.get());
//set_prompt_result.update(|s| *s = prompt_action.value());
set_prompt_text.update(|s| *s = "".to_string());
}
}
prop:value=prompt_text
/>
<div class="response-area">
<p>{move || prompt_result.get()}</p>
</div>
</main>
}
}