refactor(stage-tamagotchi): group into app/

This commit is contained in:
Neko Ayaka
2025-06-30 14:30:18 +08:00
parent ad0d4e97d2
commit 0f7505a382
7 changed files with 53 additions and 85 deletions
@@ -1,7 +1,7 @@
use log::debug;
use tauri::Manager;
use crate::app_windows;
use crate::app::windows::{chat, settings};
#[tauri::command]
pub async fn open_settings_window(app: tauri::AppHandle) -> Result<(), tauri::Error> {
@@ -11,7 +11,7 @@ pub async fn open_settings_window(app: tauri::AppHandle) -> Result<(), tauri::Er
return Ok(());
}
app_windows::settings::new_settings_window(&app)?;
settings::new_settings_window(&app)?;
Ok(())
}
@@ -23,7 +23,7 @@ pub async fn open_chat_window(app: tauri::AppHandle) -> Result<(), tauri::Error>
return Ok(());
}
app_windows::chat::new_chat_window(&app)?;
chat::new_chat_window(&app)?;
Ok(())
}
@@ -0,0 +1,2 @@
pub mod commands;
pub mod windows;
@@ -3,9 +3,9 @@ use std::path::Path;
use anyhow::{Ok, Result};
#[cfg(target_os = "macos")]
use tauri::TitleBarStyle;
use tauri::{WebviewUrl, WebviewWindowBuilder};
use tauri::{Runtime, WebviewUrl, WebviewWindowBuilder};
pub fn new_chat_window(app: &tauri::AppHandle) -> Result<tauri::WebviewWindow> {
pub fn new_chat_window<R: Runtime>(app: &tauri::AppHandle<R>) -> Result<tauri::WebviewWindow<R>> {
let mut builder = WebviewWindowBuilder::new(
app,
"chat",
@@ -3,9 +3,11 @@ use std::path::Path;
use anyhow::{Ok, Result};
#[cfg(target_os = "macos")]
use tauri::TitleBarStyle;
use tauri::{WebviewUrl, WebviewWindowBuilder};
use tauri::{Runtime, WebviewUrl, WebviewWindowBuilder};
pub fn new_settings_window(app: &tauri::AppHandle) -> Result<tauri::WebviewWindow> {
pub fn new_settings_window<R: Runtime>(
app: &tauri::AppHandle<R>
) -> Result<tauri::WebviewWindow<R>> {
let mut builder = WebviewWindowBuilder::new(
app,
"settings",
+7 -78
View File
@@ -2,17 +2,14 @@ use log::info;
use tauri::{
Emitter,
Manager,
RunEvent,
WebviewUrl,
WebviewWindowBuilder,
menu::{Menu, MenuItem},
tray::TrayIconBuilder,
};
use tauri_plugin_prevent_default::Flags;
use tauri_plugin_window_state::{AppHandleExt, WindowExt};
mod app_windows;
mod commands;
mod app;
mod plugins;
mod whisper;
@@ -42,42 +39,6 @@ async fn load_models(window: tauri::Window) -> Result<(), String> {
)
}
#[tauri::command]
async fn open_route_in_window(
window: tauri::Window,
route: String,
window_label: String,
) -> std::result::Result<(), String> {
let app = window.app_handle();
let target_window = match window_label.as_str() {
"chat" => match app.get_webview_window("chat") {
Some(window) => window,
None => app_windows::chat::new_chat_window(app)
.map_err(|e| format!("Failed to create chat window: {}", e))?,
},
"settings" => match app.get_webview_window("settings") {
Some(window) => window,
None => app_windows::settings::new_settings_window(app)
.map_err(|e| format!("Failed to create settings window: {}", e))?,
},
_ => {
return Err(format!("Unknown window label: {}", window_label));
},
};
let mut current_url = target_window
.url()
.map_err(|e| format!("Failed to get current URL: {}", e))?;
let route: String = "/".to_string() + route.trim_start_matches('/');
current_url.set_fragment(Some(route.to_string().as_str()));
let _ = target_window.show();
let _ = target_window.navigate(current_url);
Ok(())
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
#[allow(clippy::missing_panics_doc)]
pub fn run() {
@@ -110,7 +71,7 @@ pub fn run() {
builder = builder.title_bar_style(tauri::TitleBarStyle::Transparent);
}
let window = builder.build().unwrap();
let _ = builder.build().unwrap();
#[cfg(target_os = "macos")]
{
@@ -125,13 +86,6 @@ pub fn run() {
)?;
}
window
.restore_state(tauri_plugin_window_state::StateFlags::all())
.map_err(|e| format!("Failed to restore window state: {}", e))
.unwrap_or_else(|err| {
info!("Failed to restore window state: {}", err);
});
// TODO: i18n
let quit_item = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?;
let settings_item = MenuItem::with_id(app, "settings", "Settings", true, None::<&str>)?;
@@ -164,7 +118,7 @@ pub fn run() {
return;
}
app_windows::settings::new_settings_window(app).unwrap();
app::windows::settings::new_settings_window(app).unwrap();
}
"hide" => {
let window = app.get_webview_window("main");
@@ -194,38 +148,13 @@ pub fn run() {
Ok(())
})
.invoke_handler(tauri::generate_handler![
commands::open_settings_window,
commands::open_chat_window,
commands::debug_println,
app::commands::open_settings_window,
app::commands::open_chat_window,
app::commands::debug_println,
load_models,
open_route_in_window,
])
.build(tauri::generate_context!())
.expect("error while building tauri application")
.run(|app, event| match event {
RunEvent::Exit { .. } => {
info!("Exiting app");
info!("Exited app");
// Save window state on exit
app
.save_window_state(tauri_plugin_window_state::StateFlags::all())
.map_err(|e| format!("Failed to save window state: {}", e))
.unwrap_or_else(|err| {
info!("Failed to save window state on exit: {}", err);
});
},
RunEvent::ExitRequested { .. } => {
info!("Requested Exiting app");
info!("Requested Exited app");
app
.save_window_state(tauri_plugin_window_state::StateFlags::all())
.map_err(|e| format!("Failed to save window state: {}", e))
.unwrap_or_else(|err| {
info!("Failed to save window state on exit: {}", err);
});
},
_ => {},
})
.run(|_, _| {});
}
@@ -2,6 +2,7 @@ use log::info;
use tauri::{
Manager,
Result,
RunEvent,
Runtime,
Window,
plugin::{Builder, TauriPlugin},
@@ -40,5 +41,39 @@ pub async fn restore<R: Runtime>(window: Window<R>) -> Result<()> {
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("proj-airi-tauri-plugin-window-persistence")
.invoke_handler(tauri::generate_handler![save, restore,])
.on_window_ready(|window| {
window
.restore_state(tauri_plugin_window_state::StateFlags::all())
.map_err(|e| format!("Failed to restore window state: {}", e))
.unwrap_or_else(|err| {
info!("Failed to restore window state: {}", err);
});
})
.on_event(|app, event| match event {
RunEvent::Exit { .. } => {
info!("Exiting app");
info!("Exited app");
// Save window state on exit
app
.save_window_state(tauri_plugin_window_state::StateFlags::all())
.map_err(|e| format!("Failed to save window state: {}", e))
.unwrap_or_else(|err| {
info!("Failed to save window state on exit: {}", err);
});
},
RunEvent::ExitRequested { .. } => {
info!("Requested Exiting app");
info!("Requested Exited app");
app
.save_window_state(tauri_plugin_window_state::StateFlags::all())
.map_err(|e| format!("Failed to save window state: {}", e))
.unwrap_or_else(|err| {
info!("Failed to save window state on exit: {}", err);
});
},
_ => {},
})
.build()
}