✨ 重构一下代码
This commit is contained in:
@@ -44,6 +44,7 @@ impl PakeConfig {
|
||||
menu_status
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
pub fn show_system_tray(&self) -> bool {
|
||||
#[cfg(target_os = "macos")]
|
||||
let tray_status = self.system_tray.macos;
|
||||
@@ -1,13 +1,6 @@
|
||||
#[cfg(target_os = "macos")]
|
||||
use tauri::MenuItem;
|
||||
|
||||
use tauri::{
|
||||
App, Config, CustomMenuItem, Menu, Submenu, TitleBarStyle, Window, WindowBuilder,
|
||||
WindowMenuEvent, WindowUrl,
|
||||
};
|
||||
|
||||
mod pake;
|
||||
use pake::PakeConfig;
|
||||
use tauri::{CustomMenuItem, Menu, Submenu, WindowMenuEvent};
|
||||
|
||||
pub fn get_menu() -> Menu {
|
||||
// first menu
|
||||
@@ -51,17 +44,6 @@ pub fn menu_event_handle(event: WindowMenuEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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)
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "linux", target_os = "windows"))]
|
||||
pub fn get_system_tray(show_menu: bool) -> SystemTray {
|
||||
let hide_app = CustomMenuItem::new("hide_app".to_string(), "Hide App");
|
||||
@@ -127,67 +109,3 @@ pub fn system_tray_handle(app: &tauri::AppHandle, event: tauri::SystemTrayEvent)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn get_data_dir(_tauri_config: Config) -> std::path::PathBuf {
|
||||
#[cfg(any(target_os = "linux", target_os = "windows"))]
|
||||
let data_dir = {
|
||||
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
|
||||
};
|
||||
#[cfg(target_os = "macos")]
|
||||
let data_dir = std::path::PathBuf::new();
|
||||
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::App(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)
|
||||
//用于隐藏头部
|
||||
.title_bar_style(if window_config.transparent {
|
||||
TitleBarStyle::Overlay
|
||||
} else {
|
||||
TitleBarStyle::Visible
|
||||
})
|
||||
.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()
|
||||
}
|
||||
4
src-tauri/src/app/mod.rs
Normal file
4
src-tauri/src/app/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
pub mod menu;
|
||||
pub mod window;
|
||||
pub mod config;
|
||||
pub mod invoke;
|
||||
43
src-tauri/src/app/window.rs
Normal file
43
src-tauri/src/app/window.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use crate::app::config::PakeConfig;
|
||||
use tauri::{App, TitleBarStyle, Window, WindowBuilder, WindowUrl};
|
||||
|
||||
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::App(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)
|
||||
//用于隐藏头部
|
||||
.title_bar_style(if window_config.transparent {
|
||||
TitleBarStyle::Overlay
|
||||
} else {
|
||||
TitleBarStyle::Visible
|
||||
})
|
||||
.inner_size(window_config.width, window_config.height)
|
||||
.initialization_script(include_str!("../inject.js"));
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
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!("../inject.js"))
|
||||
};
|
||||
window.build().unwrap()
|
||||
}
|
||||
@@ -3,17 +3,33 @@
|
||||
windows_subsystem = "windows"
|
||||
)]
|
||||
|
||||
use app::{get_data_dir, get_menu, get_pake_config, get_window, menu_event_handle};
|
||||
pub mod cmd;
|
||||
mod app;
|
||||
mod util;
|
||||
|
||||
use app::{invoke, menu, window};
|
||||
use invoke::{drag_window, fullscreen, open_browser};
|
||||
use menu::{get_menu, menu_event_handle};
|
||||
use util::{get_data_dir, get_pake_config};
|
||||
use window::get_window;
|
||||
|
||||
pub fn run_app() {
|
||||
let (pake_config, tauri_config) = get_pake_config();
|
||||
let show_menu = pake_config.show_menu();
|
||||
let menu = get_menu();
|
||||
let data_dir = get_data_dir(tauri_config);
|
||||
#[cfg(any(target_os = "linux", target_os = "windows"))]
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
let tauri_app = if show_menu {
|
||||
tauri::Builder::default()
|
||||
.menu(menu)
|
||||
.on_menu_event(menu_event_handle)
|
||||
} else {
|
||||
tauri::Builder::default()
|
||||
};
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
let tauri_app = {
|
||||
use app::{get_system_tray, system_tray_handle};
|
||||
use pake::{get_system_tray, system_tray_handle};
|
||||
|
||||
let show_system_tray = pake_config.show_system_tray();
|
||||
let system_tray = get_system_tray(show_menu);
|
||||
@@ -35,18 +51,14 @@ pub fn run_app() {
|
||||
tauri::Builder::default()
|
||||
};
|
||||
};
|
||||
#[cfg(target_os = "macos")]
|
||||
let tauri_app = if show_menu {
|
||||
tauri::Builder::default()
|
||||
.menu(menu)
|
||||
.on_menu_event(menu_event_handle)
|
||||
} else {
|
||||
tauri::Builder::default()
|
||||
};
|
||||
|
||||
tauri_app
|
||||
.plugin(tauri_plugin_window_state::Builder::default().build())
|
||||
.invoke_handler(tauri::generate_handler![cmd::drag_window, cmd::fullscreen])
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
drag_window,
|
||||
fullscreen,
|
||||
open_browser
|
||||
])
|
||||
.setup(|app| {
|
||||
let _window = get_window(app, pake_config, data_dir);
|
||||
#[cfg(feature = "devtools")]
|
||||
|
||||
36
src-tauri/src/util.rs
Normal file
36
src-tauri/src/util.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use crate::app::config::PakeConfig;
|
||||
use tauri::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);
|
||||
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)
|
||||
}
|
||||
|
||||
pub fn get_data_dir(_tauri_config: Config) -> std::path::PathBuf {
|
||||
#[cfg(any(target_os = "linux", target_os = "windows"))]
|
||||
let data_dir = {
|
||||
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
|
||||
};
|
||||
#[cfg(target_os = "macos")]
|
||||
let data_dir = std::path::PathBuf::new();
|
||||
data_dir
|
||||
}
|
||||
Reference in New Issue
Block a user