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)] #[derive(Debug, Deserialize)]
pub struct UserAgent { pub struct PlatformSpecific<T> {
pub macos: String, pub macos: T,
pub linux: String, pub linux: T,
pub windows: String, pub windows: T,
} }
#[derive(Debug, Deserialize)] impl<T> PlatformSpecific<T> {
pub struct FunctionON { pub const fn get(&self) -> &T {
pub macos: bool, #[cfg(target_os = "macos")]
pub linux: bool, let platform = &self.macos;
pub windows: bool, #[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)] #[derive(Debug, Deserialize)]
pub struct PakeConfig { pub struct PakeConfig {
pub windows: Vec<WindowConfig>, pub windows: Vec<WindowConfig>,
@@ -35,23 +53,11 @@ pub struct PakeConfig {
impl PakeConfig { impl PakeConfig {
pub fn show_menu(&self) -> bool { pub fn show_menu(&self) -> bool {
#[cfg(target_os = "macos")] self.menu.copied()
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
} }
#[cfg(not(target_os = "macos"))] #[cfg(not(target_os = "macos"))]
pub fn show_system_tray(&self) -> bool { pub fn show_system_tray(&self) -> bool {
#[cfg(target_os = "macos")] self.menu.copied()
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
} }
} }

View File

@@ -11,17 +11,12 @@ pub fn get_window(app: &mut App, config: PakeConfig, _data_dir: PathBuf) -> Wind
.first() .first()
.expect("At least one window configuration is required"); .expect("At least one window configuration is required");
#[cfg(target_os = "macos")] let user_agent = config.user_agent.get();
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 url = match window_config.url_type.as_str() { let url = match window_config.url_type.as_str() {
"web" => WindowUrl::App(window_config.url.parse().unwrap()), "web" => WindowUrl::App(window_config.url.parse().unwrap()),
"local" => WindowUrl::App(PathBuf::from(&window_config.url)), "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) let mut window_builder = WindowBuilder::new(app, "pake", url)