feat: initialize Tauri backend with SQLite database
This commit is contained in:
106
src-tauri/src/lib.rs
Normal file
106
src-tauri/src/lib.rs
Normal file
@@ -0,0 +1,106 @@
|
||||
use rusqlite::Connection;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Mutex;
|
||||
use tauri::{Manager, State};
|
||||
use std::path::PathBuf;
|
||||
|
||||
mod database;
|
||||
mod commands;
|
||||
|
||||
pub struct AppState {
|
||||
pub db: Mutex<Connection>,
|
||||
}
|
||||
|
||||
fn get_data_dir() -> PathBuf {
|
||||
let exe_path = std::env::current_exe().unwrap();
|
||||
let exe_dir = exe_path.parent().unwrap();
|
||||
let data_dir = exe_dir.join("data");
|
||||
std::fs::create_dir_all(&data_dir).ok();
|
||||
data_dir
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
env_logger::init();
|
||||
|
||||
let data_dir = get_data_dir();
|
||||
let db_path = data_dir.join("timetracker.db");
|
||||
|
||||
let conn = Connection::open(&db_path).expect("Failed to open database");
|
||||
database::init_db(&conn).expect("Failed to initialize database");
|
||||
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.plugin(tauri_plugin_fs::init())
|
||||
.plugin(tauri_plugin_notification::init())
|
||||
.manage(AppState { db: Mutex::new(conn) })
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
commands::get_clients,
|
||||
commands::create_client,
|
||||
commands::update_client,
|
||||
commands::delete_client,
|
||||
commands::get_projects,
|
||||
commands::create_project,
|
||||
commands::update_project,
|
||||
commands::delete_project,
|
||||
commands::get_tasks,
|
||||
commands::create_task,
|
||||
commands::delete_task,
|
||||
commands::get_time_entries,
|
||||
commands::create_time_entry,
|
||||
commands::update_time_entry,
|
||||
commands::delete_time_entry,
|
||||
commands::get_reports,
|
||||
commands::create_invoice,
|
||||
commands::get_invoices,
|
||||
commands::get_settings,
|
||||
commands::update_settings,
|
||||
])
|
||||
.setup(|app| {
|
||||
#[cfg(desktop)]
|
||||
{
|
||||
use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent};
|
||||
use tauri::menu::{Menu, MenuItem};
|
||||
|
||||
let quit = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?;
|
||||
let show = MenuItem::with_id(app, "show", "Show Window", true, None::<&str>)?;
|
||||
let menu = Menu::with_items(app, &[&show, &quit])?;
|
||||
|
||||
let _tray = TrayIconBuilder::new()
|
||||
.menu(&menu)
|
||||
.menu_on_left_click(false)
|
||||
.on_menu_event(|app, event| {
|
||||
match event.id.as_ref() {
|
||||
"quit" => {
|
||||
app.exit(0);
|
||||
}
|
||||
"show" => {
|
||||
if let Some(window) = app.get_webview_window("main") {
|
||||
window.show().ok();
|
||||
window.set_focus().ok();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
})
|
||||
.on_tray_icon_event(|tray, event| {
|
||||
if let TrayIconEvent::Click {
|
||||
button: MouseButton::Left,
|
||||
button_state: MouseButtonState::Up,
|
||||
..
|
||||
} = event {
|
||||
let app = tray.app_handle();
|
||||
if let Some(window) = app.get_webview_window("main") {
|
||||
window.show().ok();
|
||||
window.set_focus().ok();
|
||||
}
|
||||
}
|
||||
})
|
||||
.build(app)?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
Reference in New Issue
Block a user