Skip to content

Commit 798f72b

Browse files
committed
主题
1 parent 2cb7d75 commit 798f72b

4 files changed

Lines changed: 255 additions & 68 deletions

File tree

src/i18n.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,30 @@ impl Texts {
212212
}
213213
}
214214

215+
#[cfg(target_os = "macos")]
216+
pub const fn settings_theme_label(&self) -> &'static str {
217+
match self.lang {
218+
Language::En => "Theme Appearance:",
219+
Language::Zh => "主题外观:",
220+
}
221+
}
222+
223+
#[cfg(target_os = "macos")]
224+
pub const fn theme_dark(&self) -> &'static str {
225+
match self.lang {
226+
Language::En => "Dark Theme",
227+
Language::Zh => "暗色主题",
228+
}
229+
}
230+
231+
#[cfg(target_os = "macos")]
232+
pub const fn theme_light(&self) -> &'static str {
233+
match self.lang {
234+
Language::En => "Light Theme",
235+
Language::Zh => "浅色主题",
236+
}
237+
}
238+
215239
#[cfg(target_os = "macos")]
216240
pub const fn ok_button(&self) -> &'static str {
217241
match self.lang {

src/macos/config.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,22 @@ use objc2_foundation::{NSString, NSUserDefaults};
99

1010
use crate::i18n::{Language, LanguagePreference};
1111

12+
/// 界面主题外观
13+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
14+
pub enum Theme {
15+
#[default]
16+
Dark = 0,
17+
Light = 1,
18+
}
19+
1220
/// 应用配置
1321
#[derive(Clone, Debug)]
1422
pub struct Config {
1523
pub interval_minutes: u64,
1624
pub break_seconds: u64,
1725
pub language: LanguagePreference,
1826
pub allow_skip_break: bool,
27+
pub theme: Theme,
1928
}
2029

2130
impl Config {
@@ -32,6 +41,7 @@ impl Config {
3241
const KEY_BREAK_SECONDS: &'static str = "restgap.break_seconds";
3342
const KEY_LANGUAGE: &'static str = "restgap.language";
3443
const KEY_ALLOW_SKIP_BREAK: &'static str = "restgap.allow_skip_break";
44+
const KEY_THEME: &'static str = "restgap.theme";
3545

3646
const LEGACY_KEY_INTERVAL_MINUTES: &'static str = "restp.interval_minutes";
3747
const LEGACY_KEY_BREAK_SECONDS: &'static str = "restp.break_seconds";
@@ -44,6 +54,7 @@ impl Config {
4454
let break_key = NSString::from_str(Self::KEY_BREAK_SECONDS);
4555
let language_key = NSString::from_str(Self::KEY_LANGUAGE);
4656
let allow_skip_key = NSString::from_str(Self::KEY_ALLOW_SKIP_BREAK);
57+
let theme_key = NSString::from_str(Self::KEY_THEME);
4758

4859
let legacy_interval_key = NSString::from_str(Self::LEGACY_KEY_INTERVAL_MINUTES);
4960
let legacy_break_key = NSString::from_str(Self::LEGACY_KEY_BREAK_SECONDS);
@@ -52,6 +63,7 @@ impl Config {
5263
let break_raw = defaults.integerForKey(&break_key);
5364
let language_raw = defaults.integerForKey(&language_key);
5465
let allow_skip_break = defaults.boolForKey(&allow_skip_key);
66+
let theme_raw = defaults.integerForKey(&theme_key);
5567

5668
let interval_raw = if interval_raw <= 0 {
5769
defaults.integerForKey(&legacy_interval_key)
@@ -82,6 +94,11 @@ impl Config {
8294
_ => LanguagePreference::Auto,
8395
};
8496

97+
let theme = match theme_raw {
98+
1 => Theme::Light,
99+
_ => Theme::Dark,
100+
};
101+
85102
Self {
86103
interval_minutes: clamp_u64(
87104
interval_minutes,
@@ -95,6 +112,7 @@ impl Config {
95112
),
96113
language,
97114
allow_skip_break,
115+
theme,
98116
}
99117
}
100118

@@ -105,6 +123,7 @@ impl Config {
105123
let break_key = NSString::from_str(Self::KEY_BREAK_SECONDS);
106124
let language_key = NSString::from_str(Self::KEY_LANGUAGE);
107125
let allow_skip_key = NSString::from_str(Self::KEY_ALLOW_SKIP_BREAK);
126+
let theme_key = NSString::from_str(Self::KEY_THEME);
108127

109128
let interval_minutes = NSInteger::try_from(self.interval_minutes).unwrap_or(NSInteger::MAX);
110129
let break_seconds = NSInteger::try_from(self.break_seconds).unwrap_or(NSInteger::MAX);
@@ -119,6 +138,12 @@ impl Config {
119138
};
120139
defaults.setInteger_forKey(language_raw, &language_key);
121140
defaults.setBool_forKey(self.allow_skip_break, &allow_skip_key);
141+
142+
let theme_raw = match self.theme {
143+
Theme::Dark => 0,
144+
Theme::Light => 1,
145+
};
146+
defaults.setInteger_forKey(theme_raw, &theme_key);
122147
}
123148

124149
pub fn effective_language(&self) -> Language {
@@ -155,6 +180,7 @@ impl Default for Config {
155180
break_seconds: Self::DEFAULT_BREAK_SECONDS,
156181
language: LanguagePreference::Auto,
157182
allow_skip_break: false,
183+
theme: Theme::Dark,
158184
}
159185
}
160186
}
@@ -182,6 +208,7 @@ mod tests {
182208
assert_eq!(config.break_seconds, Config::DEFAULT_BREAK_SECONDS);
183209
assert_eq!(config.language, LanguagePreference::Auto);
184210
assert!(!config.allow_skip_break);
211+
assert_eq!(config.theme, Theme::Dark);
185212
}
186213

187214
#[test]
@@ -191,6 +218,7 @@ mod tests {
191218
break_seconds: 120,
192219
language: LanguagePreference::Auto,
193220
allow_skip_break: false,
221+
theme: Theme::Dark,
194222
};
195223
assert_eq!(config.work_interval(), Duration::from_secs(1800));
196224
}
@@ -202,6 +230,7 @@ mod tests {
202230
break_seconds: 120,
203231
language: LanguagePreference::Auto,
204232
allow_skip_break: false,
233+
theme: Theme::Dark,
205234
};
206235
assert_eq!(config.break_duration(), Duration::from_secs(120));
207236
}

0 commit comments

Comments
 (0)