feat: Enhance application with logging, configuration management, and system tray support
first working gui windows with configs
This commit is contained in:
@@ -1,81 +1,60 @@
|
||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
use tauri::{
|
||||
menu::{Menu, MenuItem},
|
||||
tray::{MouseButton, TrayIconBuilder, TrayIconEvent},
|
||||
Manager,
|
||||
};
|
||||
use std::sync::Mutex;
|
||||
mod utilities;
|
||||
mod controllers;
|
||||
mod viewers;
|
||||
|
||||
// Placeholder for application state
|
||||
struct AppState {
|
||||
// Exempel: Spara status för ollama connection här
|
||||
ollama_ready: Mutex<bool>,
|
||||
}
|
||||
use log::info;
|
||||
use utilities::config::load_config;
|
||||
use utilities::logging::setup_logging;
|
||||
use controllers::app_state::init_state;
|
||||
use controllers::settings::{get_settings, save_settings};
|
||||
use controllers::greet::greet;
|
||||
use viewers::tray::setup_tray;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Initiera loggning
|
||||
env_logger::init();
|
||||
// 1. Load configuration
|
||||
let config = match load_config() {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to load config: {}", e);
|
||||
// Default config fallback logic is inside load_config, but if file IO fails we might get here.
|
||||
// We can try to proceed with default logging if possible, but for now just print to stderr.
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// 2. Init logging
|
||||
if let Err(e) = setup_logging(&config) {
|
||||
eprintln!("Failed to setup logging: {}", e);
|
||||
}
|
||||
|
||||
info!("Application started");
|
||||
|
||||
tauri::Builder::default()
|
||||
.invoke_handler(tauri::generate_handler![get_settings, save_settings, greet])
|
||||
.setup(|app| {
|
||||
// Setup Application State
|
||||
app.manage(AppState {
|
||||
ollama_ready: Mutex::new(false),
|
||||
});
|
||||
|
||||
// --- System Tray Setup ---
|
||||
info!("Setting up application...");
|
||||
|
||||
// Skapa menyalternativ
|
||||
let quit_i = MenuItem::with_id(app, "quit", "Avsluta", true, None::<&str>)?;
|
||||
let settings_i = MenuItem::with_id(app, "settings", "Inställningar", true, None::<&str>)?;
|
||||
|
||||
// Skapa menyn
|
||||
let menu = Menu::with_items(app, &[&settings_i, &quit_i])?;
|
||||
// 3. Init State
|
||||
init_state(app);
|
||||
|
||||
// Bygg Tray-ikonen
|
||||
let _tray = TrayIconBuilder::new()
|
||||
.icon(app.default_window_icon().unwrap().clone())
|
||||
.menu(&menu)
|
||||
.show_menu_on_left_click(false)
|
||||
.on_menu_event(|app, event| {
|
||||
match event.id.as_ref() {
|
||||
"quit" => {
|
||||
println!("Avslutar applikationen...");
|
||||
app.exit(0);
|
||||
}
|
||||
"settings" => {
|
||||
println!("Öppnar inställningar (Placeholder)...");
|
||||
// Här kan du öppna ditt inställningsfönster:
|
||||
// if let Some(window) = app.get_webview_window("main") {
|
||||
// window.show().unwrap();
|
||||
// window.set_focus().unwrap();
|
||||
// }
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
})
|
||||
.on_tray_icon_event(|tray, event| {
|
||||
match event {
|
||||
TrayIconEvent::Click {
|
||||
button: MouseButton::Left,
|
||||
..
|
||||
} => {
|
||||
let app = tray.app_handle();
|
||||
if let Some(window) = app.get_webview_window("main") {
|
||||
let _ = window.show();
|
||||
let _ = window.set_focus();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
})
|
||||
.build(app)?;
|
||||
// 4. Setup Tray
|
||||
setup_tray(app)?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.on_window_event(|window, event| {
|
||||
if let tauri::WindowEvent::CloseRequested { api, .. } = event {
|
||||
// Prevent the window from closing (destroying)
|
||||
// Instead, hide it. This keeps the app running in the tray.
|
||||
window.hide().unwrap();
|
||||
api.prevent_close();
|
||||
}
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user