Add multi-theme system with random theme selection on app startup
7
src-tauri/.gitignore
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
|
||||
# Generated by Tauri
|
||||
# will have schema files for capabilities auto-completion
|
||||
/gen/schemas
|
||||
5101
src-tauri/Cargo.lock
generated
Normal file
26
src-tauri/Cargo.toml
Normal file
@@ -0,0 +1,26 @@
|
||||
[package]
|
||||
name = "countdown"
|
||||
version = "0.1.0"
|
||||
description = "A Countdown app"
|
||||
authors = ["sangge"]
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[lib]
|
||||
# The `_lib` suffix may seem redundant but it is necessary
|
||||
# to make the lib name unique and wouldn't conflict with the bin name.
|
||||
# This seems to be only an issue on Windows, see https://github.com/rust-lang/cargo/issues/8519
|
||||
name = "countdown_lib"
|
||||
crate-type = ["staticlib", "cdylib", "rlib"]
|
||||
|
||||
[build-dependencies]
|
||||
tauri-build = { version = "2", features = [] }
|
||||
|
||||
[dependencies]
|
||||
rusqlite = { version = "0.33.0", features = ["bundled"] }
|
||||
tauri = { version = "2", features = [] }
|
||||
tauri-plugin-opener = "2"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
chrono = "0.4.39"
|
||||
3
src-tauri/build.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
tauri_build::build()
|
||||
}
|
||||
10
src-tauri/capabilities/default.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"$schema": "../gen/schemas/desktop-schema.json",
|
||||
"identifier": "default",
|
||||
"description": "Capability for the main window",
|
||||
"windows": ["main"],
|
||||
"permissions": [
|
||||
"core:default",
|
||||
"opener:default"
|
||||
]
|
||||
}
|
||||
BIN
src-tauri/icons/128x128.png
Normal file
|
After Width: | Height: | Size: 1.8 KiB |
BIN
src-tauri/icons/128x128@2x.png
Normal file
|
After Width: | Height: | Size: 4.1 KiB |
BIN
src-tauri/icons/32x32.png
Normal file
|
After Width: | Height: | Size: 482 B |
BIN
src-tauri/icons/4096x4096.png
Normal file
|
After Width: | Height: | Size: 118 KiB |
BIN
src-tauri/icons/Square107x107Logo.png
Normal file
|
After Width: | Height: | Size: 2.8 KiB |
BIN
src-tauri/icons/Square142x142Logo.png
Normal file
|
After Width: | Height: | Size: 3.8 KiB |
BIN
src-tauri/icons/Square150x150Logo.png
Normal file
|
After Width: | Height: | Size: 3.9 KiB |
BIN
src-tauri/icons/Square284x284Logo.png
Normal file
|
After Width: | Height: | Size: 7.6 KiB |
BIN
src-tauri/icons/Square30x30Logo.png
Normal file
|
After Width: | Height: | Size: 447 B |
BIN
src-tauri/icons/Square310x310Logo.png
Normal file
|
After Width: | Height: | Size: 8.4 KiB |
BIN
src-tauri/icons/Square44x44Logo.png
Normal file
|
After Width: | Height: | Size: 509 B |
BIN
src-tauri/icons/Square71x71Logo.png
Normal file
|
After Width: | Height: | Size: 755 B |
BIN
src-tauri/icons/Square89x89Logo.png
Normal file
|
After Width: | Height: | Size: 2.4 KiB |
BIN
src-tauri/icons/StoreLogo.png
Normal file
|
After Width: | Height: | Size: 1.5 KiB |
BIN
src-tauri/icons/icon.icns
Normal file
BIN
src-tauri/icons/icon.ico
Normal file
|
After Width: | Height: | Size: 103 KiB |
BIN
src-tauri/icons/icon.png
Normal file
|
After Width: | Height: | Size: 7.4 KiB |
174
src-tauri/src/lib.rs
Normal file
@@ -0,0 +1,174 @@
|
||||
use chrono::{Local, NaiveDate};
|
||||
use rusqlite::{Connection, Result};
|
||||
use serde::Serialize;
|
||||
use std::fs;
|
||||
use tauri::Manager;
|
||||
|
||||
fn init_database(app: &tauri::App) -> Result<(), Box<dyn std::error::Error>> {
|
||||
// 获取应用数据目录
|
||||
let app_dir = app.path().app_data_dir()?;
|
||||
|
||||
// 确保目录存在
|
||||
fs::create_dir_all(&app_dir)?;
|
||||
|
||||
// 构建数据库文件路径
|
||||
let db_path = app_dir.join("anniversaries.db");
|
||||
|
||||
// 打开数据库连接
|
||||
let conn = Connection::open(db_path)?;
|
||||
|
||||
// 创建表
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS anniversaries (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT NOT NULL,
|
||||
start_date TEXT NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)",
|
||||
[],
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Anniversary {
|
||||
id: i32,
|
||||
title: String,
|
||||
start_date: String,
|
||||
days: i64,
|
||||
}
|
||||
#[tauri::command]
|
||||
async fn get_anniversaries(app_handle: tauri::AppHandle) -> Result<Vec<Anniversary>, String> {
|
||||
// 返回主页数据
|
||||
let app_dir = app_handle
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.map_err(|e| e.to_string())?;
|
||||
let db_path = app_dir.join("anniversaries.db");
|
||||
let conn = Connection::open(db_path).map_err(|e| e.to_string())?;
|
||||
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT id, title, start_date FROM anniversaries ORDER BY created_at DESC")
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let today = Local::now().date_naive();
|
||||
|
||||
let anniversaries = stmt
|
||||
.query_map([], |row| {
|
||||
let id: i32 = row.get(0)?;
|
||||
let title: String = row.get(1)?;
|
||||
let start_date: String = row.get(2)?;
|
||||
|
||||
// 计算天数
|
||||
let start = NaiveDate::parse_from_str(&start_date, "%Y-%m-%d")
|
||||
.map_err(|e| rusqlite::Error::InvalidParameterName(e.to_string()))?;
|
||||
let days = (today - start).num_days();
|
||||
|
||||
Ok(Anniversary {
|
||||
id,
|
||||
title,
|
||||
start_date,
|
||||
days,
|
||||
})
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let result: Result<Vec<_>, _> = anniversaries.collect();
|
||||
result.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn get_anniversary_by_id(
|
||||
app_handle: tauri::AppHandle,
|
||||
id: i32,
|
||||
) -> Result<Anniversary, String> {
|
||||
// 返回详情页数据
|
||||
let app_dir = app_handle
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.map_err(|e| e.to_string())?;
|
||||
let db_path = app_dir.join("anniversaries.db");
|
||||
let conn = Connection::open(db_path).map_err(|e| e.to_string())?;
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT title, start_date FROM anniversaries WHERE id = ?")
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let today = Local::now().date_naive();
|
||||
|
||||
let result = stmt
|
||||
.query_row([id], |row| {
|
||||
let title: String = row.get(0)?;
|
||||
let start_date: String = row.get(1)?;
|
||||
|
||||
// 计算天数
|
||||
let start = NaiveDate::parse_from_str(&start_date, "%Y-%m-%d")
|
||||
.map_err(|e| rusqlite::Error::InvalidParameterName(e.to_string()))?;
|
||||
let days = (today - start).num_days();
|
||||
|
||||
Ok(Anniversary {
|
||||
id,
|
||||
title,
|
||||
start_date,
|
||||
days,
|
||||
})
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn delete_anniversary(app_handle: tauri::AppHandle, id: i64) -> Result<(), String> {
|
||||
// 详情页删除
|
||||
let app_dir = app_handle
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.map_err(|e| e.to_string())?;
|
||||
let db_path = app_dir.join("anniversaries.db");
|
||||
let conn = Connection::open(db_path).map_err(|e| e.to_string())?;
|
||||
conn.execute("DELETE FROM anniversaries WHERE id = ?", [id])
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn add_anniversary(
|
||||
app_handle: tauri::AppHandle,
|
||||
title: String,
|
||||
start_date: String,
|
||||
) -> Result<(), String> {
|
||||
// 主页添加
|
||||
let app_dir = app_handle
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.map_err(|e| e.to_string())?;
|
||||
let db_path = app_dir.join("anniversaries.db");
|
||||
let conn = Connection::open(db_path).map_err(|e| e.to_string())?;
|
||||
conn.execute(
|
||||
"INSERT INTO anniversaries (title, start_date) VALUES (?1, ?2)",
|
||||
[&title, &start_date],
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.setup(|app| {
|
||||
init_database(app).expect("failed to initialize database");
|
||||
Ok(())
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
get_anniversaries,
|
||||
get_anniversary_by_id,
|
||||
add_anniversary,
|
||||
delete_anniversary,
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
6
src-tauri/src/main.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
fn main() {
|
||||
countdown_lib::run()
|
||||
}
|
||||
36
src-tauri/tauri.conf.json
Normal file
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"$schema": "https://schema.tauri.app/config/2",
|
||||
"productName": "countdown",
|
||||
"version": "0.1.0",
|
||||
"identifier": "com.countdown.app",
|
||||
"build": {
|
||||
"beforeDevCommand": "pnpm dev",
|
||||
"devUrl": "http://localhost:1420",
|
||||
"beforeBuildCommand": "pnpm build",
|
||||
"frontendDist": "../dist"
|
||||
},
|
||||
"app": {
|
||||
"windows": [
|
||||
{
|
||||
"title": "countdown",
|
||||
"width": 800,
|
||||
"height": 600
|
||||
}
|
||||
],
|
||||
"security": {
|
||||
"csp": "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'"
|
||||
}
|
||||
},
|
||||
"bundle": {
|
||||
"active": true,
|
||||
"targets": ["deb", "rpm", "appimage", "nsis", "msi", "dmg", "app"],
|
||||
"icon": [
|
||||
"icons/32x32.png",
|
||||
"icons/128x128.png",
|
||||
"icons/128x128@2x.png",
|
||||
"icons/icon.png",
|
||||
"icons/icon.icns",
|
||||
"icons/icon.ico"
|
||||
]
|
||||
}
|
||||
}
|
||||