This commit is contained in:
王一嘉
2025-07-28 14:24:52 +08:00
commit 0e6011727b
43 changed files with 7977 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

5596
src-tauri/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

31
src-tauri/Cargo.toml Normal file
View File

@@ -0,0 +1,31 @@
[package]
name = "doze-translate"
version = "0.1.0"
description = "A Translate App"
authors = ["doze"]
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 = "doze_translate_lib"
crate-type = ["staticlib", "cdylib", "rlib"]
[build-dependencies]
tauri-build = { version = "2", features = [] }
[dependencies]
ollama-rs = { version = "0.3.2", features = ["stream"] }
tauri = { version = "2", features = [] }
tauri-plugin-opener = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
futures = "0.3.31"
tokio = "1.46.1"
clipboard = "0.5.0"
[target."cfg(target_os = \"macos\")".dependencies]
cocoa = "0.26.1"

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"
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 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: 903 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

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.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

86
src-tauri/src/lib.rs Normal file
View File

@@ -0,0 +1,86 @@
use futures::StreamExt;
use ollama_rs::generation::completion::{request::GenerationRequest};
use ollama_rs::Ollama;
use tauri::{AppHandle, TitleBarStyle, WebviewUrl, WebviewWindowBuilder, Emitter};
use clipboard::{ClipboardContext, ClipboardProvider};
#[tauri::command]
async fn translate_steam(content: &str, app_handle: AppHandle) -> Result<String, ()> {
let model = "gemma2:2b".to_string();
let prompt = format!(
"你是一个专业翻译模型,任务是将给定的文本翻译成目标语言。请严格遵循以下规则:\n\
只返回翻译结果,不要解释、不要问候、不要添加任何额外内容。\n\
如果输入是英文,翻译成中文;如果是中文,翻译成英文。\n\
示例1\n\
输入: 'Good morning.'\n\
输出: '早上好。'\n\
示例2\n\
输入: '这本书很有趣。'\n\
输出: 'This book is very interesting.'\n\
现在请翻译:\n\
输入: '{}'\n\
输出:",
content
);
let ollama = Ollama::new("http://localhost".to_string(), 11434);
let mut stream = ollama.generate_stream(GenerationRequest::new(model, prompt)).await.unwrap();
while let Some(res) = stream.next().await {
let responses = res.unwrap();
for resp in responses {
app_handle.emit("translate_steam" ,resp.response).unwrap();
}
}
Ok("完成".to_string())
}
#[tauri::command]
fn copy(content: &str) {
let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
ctx.set_contents(content.to_string()).unwrap();
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.setup(|app| {
let win_builder =
WebviewWindowBuilder::new(app, "main", WebviewUrl::default())
.title("Transparent Titlebar Window")
.inner_size(700.0, 300.0);
// 仅在 macOS 时设置透明标题栏
#[cfg(target_os = "macos")]
let win_builder = win_builder.title_bar_style(TitleBarStyle::Transparent);
let window = win_builder.build().unwrap();
// 仅在构建 macOS 时设置背景颜色
#[cfg(target_os = "macos")]
{
use cocoa::appkit::{NSColor, NSWindow};
use cocoa::base::{id, nil};
let ns_window = window.ns_window().unwrap() as id;
unsafe {
let bg_color = NSColor::colorWithRed_green_blue_alpha_(
nil,
255.0 / 255.0,
255.0 / 255.0,
255.0 / 255.0,
1.0,
);
ns_window.setBackgroundColor_(bg_color);
}
}
Ok(())
})
.plugin(tauri_plugin_opener::init())
.invoke_handler(tauri::generate_handler![translate_steam, copy])
.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() {
doze_translate_lib::run()
}

25
src-tauri/tauri.conf.json Normal file
View File

@@ -0,0 +1,25 @@
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "doze-translate",
"version": "0.1.0",
"identifier": "com.tsevei.top",
"build": {
"beforeDevCommand": "deno task dev",
"devUrl": "http://localhost:1420",
"beforeBuildCommand": "deno task build",
"frontendDist": "../dist"
},
"app": {
"windows": [],
"security": {
"csp": null
}
},
"bundle": {
"active": true,
"targets": "all",
"icon": [
"icons/icon.icns"
]
}
}