Renamed frontend directory
This commit is contained in:
78
frontend/src-tauri/src/main.rs
Normal file
78
frontend/src-tauri/src/main.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use tauri::{Manager, State, WindowEvent};
|
||||
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState};
|
||||
|
||||
use shared::ai::{PromptRequest, ai_daemon_client::AiDaemonClient};
|
||||
|
||||
struct AppState {
|
||||
grpc_client: Mutex<AiDaemonClient<tonic::transport::Channel>>,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn toggle_popup(app_handle: tauri::AppHandle) {
|
||||
match app_handle.get_webview_window("popup") {
|
||||
Some(window) => {
|
||||
let is_visible = window.is_visible().unwrap_or(false);
|
||||
if is_visible {
|
||||
window.hide().unwrap();
|
||||
} else {
|
||||
window.show().unwrap();
|
||||
window.set_focus().unwrap();
|
||||
}
|
||||
}
|
||||
None => {
|
||||
println!("ERROR: Window with label 'popup' not found!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn prompt_llm(state: State<'_, AppState>, prompt: String) -> Result<String, String> {
|
||||
let mut client= state.grpc_client.lock().await;
|
||||
let request = tonic::Request::new(PromptRequest { prompt });
|
||||
match client.prompt(request).await {
|
||||
Ok(response) => Ok(response.into_inner().response),
|
||||
Err(e) => Err(format!("gRPC error: {}", e)),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
||||
let channel = tonic::transport::Channel::from_static("http://[::1]:50051")
|
||||
.connect()
|
||||
.await
|
||||
.expect("Could not connect to daemon!");
|
||||
|
||||
let client = AiDaemonClient::new(channel);
|
||||
|
||||
tauri::Builder::default()
|
||||
.manage(AppState { grpc_client: Mutex::new(client) })
|
||||
.plugin(tauri_plugin_global_shortcut::Builder::new().build())
|
||||
.invoke_handler(tauri::generate_handler![toggle_popup, prompt_llm])
|
||||
.setup(|app| {
|
||||
if let Some(window) = app.get_webview_window("popup") {
|
||||
let w = window.clone();
|
||||
window.on_window_event(move |event| {
|
||||
if let WindowEvent::Focused(focuced) = event {
|
||||
if !focuced {
|
||||
w.hide().unwrap();
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
let shortcut = Shortcut::new(Some(Modifiers::META), Code::Space);
|
||||
app.global_shortcut().on_shortcut(shortcut, move |app, _shortcut, event| {
|
||||
if event.state() == ShortcutState::Pressed {
|
||||
toggle_popup(app.clone());
|
||||
}
|
||||
})?;
|
||||
Ok(())
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
Reference in New Issue
Block a user