🎨 (app) 用 PlatformSpecific 代替到处判断系统
以往需要跨平台不同值时,我们都是到处进行 cfg-target 判断。 由于代码片段都是重复的,因此这个 commit 把所有相关的跨平台结构 全部整合在一个结构 (PlatformSpecific) 内,这样除了更容易制造 规范的接口,还可以提升维护的效率(而不用到处修改相关的判断逻辑)。
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user