基本完成T
This commit is contained in:
16
dist/about_pake.html
vendored
Normal file
16
dist/about_pake.html
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
<h5>Welcome from Pake!</h5>
|
||||
<p>version: 1.0.1</p>
|
||||
<a href="https://github.com/tw93/Pake" target="_blank">Project link</a><br>
|
||||
<a href="https://github.com/tw93/Pake/discussions" target="_blank">Discussions</a><br>
|
||||
<a href="https://github.com/tw93/Pake/issues" target="_blank">Issues</a><br>
|
||||
<p>LICENSE: MIT</p>
|
||||
</body>
|
||||
</html>
|
||||
1
src-tauri/Cargo.lock
generated
1
src-tauri/Cargo.lock
generated
@@ -50,6 +50,7 @@ dependencies = [
|
||||
"tauri-build",
|
||||
"tauri-plugin-window-state",
|
||||
"tauri-utils",
|
||||
"webkit2gtk",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -22,6 +22,7 @@ image = "0.24.5"
|
||||
home = "0.5"
|
||||
tauri-utils = "1.2.1"
|
||||
tauri-plugin-window-state = { git = "https://github.com/tauri-apps/tauri-plugin-window-state", branch = "dev"}
|
||||
webkit2gtk = "0.18.0"
|
||||
# webbrowser = "0.8.2"
|
||||
# wry = "0.23.4"
|
||||
|
||||
|
||||
@@ -9,5 +9,10 @@
|
||||
"resizable": true,
|
||||
"url_type": "web"
|
||||
}
|
||||
]
|
||||
],
|
||||
"user_agent": {
|
||||
"macos": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15",
|
||||
"linux": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
|
||||
"windows": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
use tauri::MenuItem;
|
||||
|
||||
use tauri::{
|
||||
CustomMenuItem, Menu, Submenu, WindowBuilder, App,
|
||||
CustomMenuItem, Menu, Submenu, WindowBuilder, App, Config,
|
||||
Window, WindowUrl, WindowMenuEvent, window::PlatformWebview,
|
||||
SystemTrayMenu, SystemTray, SystemTrayEvent, Manager
|
||||
};
|
||||
@@ -28,27 +28,31 @@ pub mod pake {
|
||||
}
|
||||
|
||||
|
||||
// #[derive(Debug, Deserialize)]
|
||||
// pub struct SystemTray {
|
||||
// pub icon_path: String,
|
||||
// pub icon_as_template: bool,
|
||||
// }
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct UserAgent {
|
||||
pub macos: String,
|
||||
pub linux: String,
|
||||
pub windows: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct PakeConfig {
|
||||
pub windows: Vec<WindowConfig>,
|
||||
// pub system_tray: SystemTray,
|
||||
pub user_agent: UserAgent,
|
||||
}
|
||||
}
|
||||
|
||||
use pake::PakeConfig;
|
||||
|
||||
pub fn get_pake_config() -> PakeConfig{
|
||||
let config_path = include_str!("../pake.json");
|
||||
let config: PakeConfig = serde_json::from_str(config_path)
|
||||
.expect("failed to parse common config");
|
||||
pub fn get_pake_config() -> (PakeConfig, Config){
|
||||
let pake_config_path = include_str!("../pake.json");
|
||||
let pake_config: PakeConfig = serde_json::from_str(pake_config_path)
|
||||
.expect("failed to parse pake config");
|
||||
// println!("{:#?}", config);
|
||||
config
|
||||
let tauri_config_path = include_str!("../tauri.conf.json");
|
||||
let tauri_config: Config = serde_json::from_str(tauri_config_path)
|
||||
.expect("failed to parse tauri config");
|
||||
(pake_config, tauri_config)
|
||||
}
|
||||
|
||||
|
||||
@@ -118,24 +122,65 @@ pub fn get_menu() -> Menu {
|
||||
}
|
||||
|
||||
|
||||
pub fn get_window(app: & mut App, config: PakeConfig) -> Window {
|
||||
pub fn get_data_dir(tauri_config: Config) -> std::path::PathBuf {
|
||||
let package_name = tauri_config.package.product_name.unwrap();
|
||||
let home_dir = match home::home_dir() {
|
||||
Some(path1) => path1,
|
||||
None => panic!("Error, can't found you home dir!!"),
|
||||
};
|
||||
#[cfg(target_os = "windows")]
|
||||
let data_dir = home_dir.join("AppData").join("Roaming").join(package_name);
|
||||
#[cfg(target_os = "linux")]
|
||||
let data_dir = home_dir.join(".config").join(package_name);
|
||||
if !data_dir.exists() {
|
||||
std::fs::create_dir(&data_dir)
|
||||
.unwrap_or_else(|_| panic!("can't create dir {}", data_dir.display()));
|
||||
}
|
||||
data_dir
|
||||
}
|
||||
|
||||
|
||||
pub fn get_window(app: & mut App, config: PakeConfig, data_dir: std::path::PathBuf) -> Window {
|
||||
let window_config = config.windows.first().unwrap();
|
||||
let user_agent = config.user_agent;
|
||||
let url = match window_config.url_type.as_str() {
|
||||
"web" => WindowUrl::External(window_config.url.parse().unwrap()),
|
||||
"local" => WindowUrl::App(std::path::PathBuf::from(&window_config.url)),
|
||||
_ => panic!("url type only can be web or local"),
|
||||
};
|
||||
#[cfg(target_os = "macos")]
|
||||
let window = WindowBuilder::new(
|
||||
app,
|
||||
"pake",
|
||||
url
|
||||
)
|
||||
.title("")
|
||||
.user_agent(user_agent.macos.as_str())
|
||||
.resizable(window_config.resizable)
|
||||
.fullscreen(window_config.fullscreen)
|
||||
.transparent(window_config.transparent)
|
||||
.inner_size(window_config.width, window_config.height)
|
||||
.initialization_script(include_str!("pake.js"));
|
||||
|
||||
#[cfg(any(target_os = "linux", target_os = "windows"))]
|
||||
let window = {
|
||||
#[cfg(target_os = "linux")]
|
||||
let user_agent = user_agent.linux.as_str();
|
||||
#[cfg(target_os = "windows")]
|
||||
let user_agent = user_agent.windows.as_str();
|
||||
WindowBuilder::new(
|
||||
app,
|
||||
"pake",
|
||||
url
|
||||
)
|
||||
.title("")
|
||||
.data_directory(data_dir)
|
||||
.resizable(window_config.resizable)
|
||||
.fullscreen(window_config.fullscreen)
|
||||
.user_agent(user_agent)
|
||||
.inner_size(window_config.width, window_config.height)
|
||||
.initialization_script(include_str!("pake.js"))
|
||||
};
|
||||
window.build().unwrap()
|
||||
}
|
||||
|
||||
@@ -204,11 +249,13 @@ pub fn menu_event_handle(event: WindowMenuEvent) {
|
||||
|
||||
|
||||
pub fn get_system_tray() -> SystemTray {
|
||||
let debug = CustomMenuItem::new("debug", "Debug");
|
||||
let hide = CustomMenuItem::new("hide".to_string(), "Hide");
|
||||
let show = CustomMenuItem::new("show".to_string(), "Show");
|
||||
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
|
||||
let about = CustomMenuItem::new("about".to_string(), "About");
|
||||
let tray_menu = SystemTrayMenu::new()
|
||||
.add_item(debug)
|
||||
.add_item(hide)
|
||||
.add_item(show)
|
||||
.add_item(quit)
|
||||
@@ -220,6 +267,7 @@ pub fn get_system_tray() -> SystemTray {
|
||||
pub fn system_tray_handle(app: &tauri::AppHandle, event: tauri::SystemTrayEvent) {
|
||||
if let SystemTrayEvent::MenuItemClick { tray_id: _, id, .. } = event {
|
||||
match id.as_str() {
|
||||
"debug" => app.get_window("pake").unwrap().open_devtools(),
|
||||
"hide" => {
|
||||
app.get_window("pake").unwrap().hide().unwrap();
|
||||
},
|
||||
@@ -229,29 +277,70 @@ pub fn system_tray_handle(app: &tauri::AppHandle, event: tauri::SystemTrayEvent)
|
||||
"quit" => {
|
||||
std::process::exit(0);
|
||||
},
|
||||
"about" => {
|
||||
let _about_window = WindowBuilder::new(
|
||||
app,
|
||||
"about",
|
||||
WindowUrl::App(std::path::PathBuf::from("about_pake.html"))
|
||||
)
|
||||
.resizable(true)
|
||||
.title("About")
|
||||
.inner_size(100.0, 100.0)
|
||||
.build()
|
||||
.expect("can't open about!")
|
||||
;
|
||||
}
|
||||
_ => {},
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
pub fn run_app() {
|
||||
let system_tray = get_system_tray();
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
let (pake_config, _) = get_pake_config();
|
||||
let menu = get_menu();
|
||||
tauri::Builder::default()
|
||||
.menu(menu)
|
||||
.on_menu_event(menu_event_handle)
|
||||
.system_tray(system_tray)
|
||||
.on_system_tray_event(system_tray_handle)
|
||||
.plugin(tauri_plugin_window_state::Builder::default().build())
|
||||
.invoke_handler(tauri::generate_handler![])
|
||||
.setup(|app| {
|
||||
let _window = get_window(app, pake_config, std::path::PathBuf::new());
|
||||
Ok(())
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
#[cfg(any(target_os = "linux", target_os = "windows"))]
|
||||
{
|
||||
let (pake_config, tauri_config) = get_pake_config();
|
||||
let data_dir = get_data_dir(tauri_config);
|
||||
// let menu = get_menu();
|
||||
tauri::Builder::default()
|
||||
// .menu(menu)
|
||||
// .on_menu_event(menu_event_handle)
|
||||
.system_tray(system_tray)
|
||||
.on_system_tray_event(system_tray_handle)
|
||||
.plugin(tauri_plugin_window_state::Builder::default().build())
|
||||
.invoke_handler(tauri::generate_handler![])
|
||||
.setup(|app| {
|
||||
let _window = get_window(app, pake_config, data_dir);
|
||||
Ok(())
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let pake_config = get_pake_config();
|
||||
let menu = get_menu();
|
||||
let system_tray = get_system_tray();
|
||||
tauri::Builder::default()
|
||||
.menu(menu)
|
||||
.on_menu_event(menu_event_handle)
|
||||
.system_tray(system_tray)
|
||||
.on_system_tray_event(system_tray_handle)
|
||||
.plugin(tauri_plugin_window_state::Builder::default().build())
|
||||
.invoke_handler(tauri::generate_handler![])
|
||||
.setup(|app| {
|
||||
let _window = get_window(app, pake_config);
|
||||
Ok(())
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
run_app()
|
||||
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
"active": false
|
||||
},
|
||||
"systemTray": {
|
||||
"iconPath": "png/weread_32.ico",
|
||||
"iconPath": "png/weread_512.png",
|
||||
"iconAsTemplate": true
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user