Merge pull request #380 from tw93/platform-specific-improve

🎨 (app) 用 PlatformSpecific 代替到处判断系统
This commit is contained in:
Tw93
2023-04-06 10:32:37 +08:00
committed by GitHub
2 changed files with 31 additions and 30 deletions

View File

@@ -12,19 +12,37 @@ pub struct WindowConfig {
}
#[derive(Debug, Deserialize)]
pub struct UserAgent {
pub macos: String,
pub linux: String,
pub windows: String,
pub struct PlatformSpecific<T> {
pub macos: T,
pub linux: T,
pub windows: T,
}
#[derive(Debug, Deserialize)]
pub struct FunctionON {
pub macos: bool,
pub linux: bool,
pub windows: bool,
impl<T> PlatformSpecific<T> {
pub const fn get(&self) -> &T {
#[cfg(target_os = "macos")]
let platform = &self.macos;
#[cfg(target_os = "linux")]
let platform = &self.linux;
#[cfg(target_os = "windows")]
let platform = &self.windows;
platform
}
}
impl<T> PlatformSpecific<T>
where
T: Copy,
{
pub const fn copied(&self) -> T {
*self.get()
}
}
pub type UserAgent = PlatformSpecific<String>;
pub type FunctionON = PlatformSpecific<bool>;
#[derive(Debug, Deserialize)]
pub struct PakeConfig {
pub windows: Vec<WindowConfig>,
@@ -35,23 +53,11 @@ pub struct PakeConfig {
impl PakeConfig {
pub fn show_menu(&self) -> bool {
#[cfg(target_os = "macos")]
let menu_status = self.menu.macos;
#[cfg(target_os = "linux")]
let menu_status = self.menu.linux;
#[cfg(target_os = "windows")]
let menu_status = self.menu.windows;
menu_status
self.menu.copied()
}
#[cfg(not(target_os = "macos"))]
pub fn show_system_tray(&self) -> bool {
#[cfg(target_os = "macos")]
let tray_status = self.system_tray.macos;
#[cfg(target_os = "linux")]
let tray_status = self.system_tray.linux;
#[cfg(target_os = "windows")]
let tray_status = self.system_tray.windows;
tray_status
self.menu.copied()
}
}

View File

@@ -11,17 +11,12 @@ pub fn get_window(app: &mut App, config: PakeConfig, _data_dir: PathBuf) -> Wind
.first()
.expect("At least one window configuration is required");
#[cfg(target_os = "macos")]
let user_agent = config.user_agent.macos.as_str();
#[cfg(target_os = "linux")]
let user_agent = config.user_agent.linux.as_str();
#[cfg(target_os = "windows")]
let user_agent = config.user_agent.windows.as_str();
let user_agent = config.user_agent.get();
let url = match window_config.url_type.as_str() {
"web" => WindowUrl::App(window_config.url.parse().unwrap()),
"local" => WindowUrl::App(PathBuf::from(&window_config.url)),
_ => panic!("url type only can be web or local"),
_ => panic!("url type can only be web or local"),
};
let mut window_builder = WindowBuilder::new(app, "pake", url)