Add multi-theme system with random theme selection on app startup

This commit is contained in:
2025-06-07 22:58:23 +08:00
commit 7e8f95e73e
47 changed files with 7677 additions and 0 deletions

7
src-tauri/.gitignore vendored Normal file
View 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

File diff suppressed because it is too large Load Diff

26
src-tauri/Cargo.toml Normal file
View 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
View File

@@ -0,0 +1,3 @@
fn main() {
tauri_build::build()
}

View 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

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

BIN
src-tauri/icons/32x32.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 482 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 447 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 509 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 755 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
src-tauri/icons/icon.icns Normal file

Binary file not shown.

BIN
src-tauri/icons/icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

BIN
src-tauri/icons/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

174
src-tauri/src/lib.rs Normal file
View 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
View 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
View 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"
]
}
}