|
|
@@ -37,6 +37,8 @@ export struct SettingPage {
|
|
|
@State tempCoverApiUrl: string = ''
|
|
|
@State coverApiUrl: string = PreferencesUtil.getStringSync('COVER_API', '')
|
|
|
@State apiDialogType: 'lyric' | 'cover' = 'lyric'
|
|
|
+ @State themeMode: number = 0 // 0: 跟随系统, 1: 浅色, 2: 深色
|
|
|
+ static readonly THEME_MODE: string = 'themeMode';
|
|
|
static readonly SORT_TYPE: string = 'musicSortType';
|
|
|
@StorageProp('currentColorMode') @Watch('onColorModeChange') currentMode: number = ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT;
|
|
|
@StorageLink('isDarkMode') isDarkMode: boolean = false // 是否启用深色模式
|
|
|
@@ -149,6 +151,8 @@ export struct SettingPage {
|
|
|
const savedDarkMode = PreferencesUtil.getBooleanSync(SettingPage.IS_DARK_MODE, false);
|
|
|
this.isDarkMode = savedDarkMode;
|
|
|
this.currentMode = savedDarkMode ? ConfigurationConstant.ColorMode.COLOR_MODE_DARK : ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT;
|
|
|
+ this.themeMode = PreferencesUtil.getNumberSync(SettingPage.THEME_MODE, 0)
|
|
|
+ this.applyThemeMode(this.themeMode)
|
|
|
AppStorage.setOrCreate('currentColorMode', this.currentMode);
|
|
|
AppStorage.setOrCreate('isDarkMode', this.isDarkMode);
|
|
|
}
|
|
|
@@ -161,38 +165,25 @@ export struct SettingPage {
|
|
|
PreferencesUtil.putSync(SettingPage.IS_DARK_MODE, this.isDarkMode);
|
|
|
}
|
|
|
|
|
|
- // 切换深色模式
|
|
|
- async toggleDarkMode(checked: boolean) {
|
|
|
- try {
|
|
|
- const newMode = checked ? ConfigurationConstant.ColorMode.COLOR_MODE_DARK : ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT;
|
|
|
-
|
|
|
- // 更新 AppStorage 中的颜色模式
|
|
|
- AppStorage.setOrCreate('currentColorMode', newMode);
|
|
|
- AppStorage.setOrCreate('isDarkMode', checked);
|
|
|
-
|
|
|
- // 保存深色模式设置到持久化存储
|
|
|
- PreferencesUtil.putSync(SettingPage.IS_DARK_MODE, checked);
|
|
|
-
|
|
|
- // 通知系统更新颜色模式
|
|
|
- const context = getContext(this) as common.UIAbilityContext;
|
|
|
- context.getApplicationContext().setColorMode(newMode);
|
|
|
-
|
|
|
- // 显示提示
|
|
|
- this.getUIContext().getPromptAction().showToast({
|
|
|
- message: checked ? '已切换到深色模式' : '已切换到浅色模式',
|
|
|
- duration: 2000,
|
|
|
- showMode: promptAction.ToastShowMode.TOP_MOST,
|
|
|
- bottom: 85
|
|
|
- });
|
|
|
- } catch (err) {
|
|
|
- console.error('Failed to update color mode:', err);
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
// 组件生命周期
|
|
|
aboutToDisappear() {
|
|
|
// 无需处理apiDialogController
|
|
|
}
|
|
|
+ applyThemeMode(mode: number) {
|
|
|
+ let colorMode = ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET
|
|
|
+ if (mode === 1) {
|
|
|
+ colorMode = ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT
|
|
|
+ } else if (mode === 2) {
|
|
|
+ colorMode = ConfigurationConstant.ColorMode.COLOR_MODE_DARK
|
|
|
+ }
|
|
|
+ AppStorage.setOrCreate('currentColorMode', colorMode)
|
|
|
+ AppStorage.setOrCreate('isDarkMode', colorMode === ConfigurationConstant.ColorMode.COLOR_MODE_DARK)
|
|
|
+ PreferencesUtil.putSync(SettingPage.THEME_MODE, mode)
|
|
|
+ // 只有强制浅色/深色时才主动设置
|
|
|
+ const context = getContext(this) as common.UIAbilityContext
|
|
|
+ context.getApplicationContext().setColorMode(colorMode)
|
|
|
+ }
|
|
|
|
|
|
build() {
|
|
|
Column() {
|
|
|
@@ -218,22 +209,51 @@ export struct SettingPage {
|
|
|
.height(48)
|
|
|
Line().width('100%').height(0.3).backgroundColor($r('app.color.index_tab_unselected_font_color'))
|
|
|
// 深色模式
|
|
|
+ // Row() {
|
|
|
+ // Text('深色模式')
|
|
|
+ // .margin({ left: 18 })
|
|
|
+ // .fontSize(15)
|
|
|
+ // .fontColor(Color.Gray)
|
|
|
+ // .fontWeight(480)
|
|
|
+ // .layoutWeight(1)
|
|
|
+ // Toggle({ type: ToggleType.Switch, isOn: this.isDarkMode })
|
|
|
+ // .selectedColor($r('app.color.tab_item_bg'))
|
|
|
+ // .switchPointColor(Color.White)
|
|
|
+ // .margin({ right: 18 })
|
|
|
+ // .onChange((checked: boolean) => {
|
|
|
+ // this.toggleDarkMode(checked);
|
|
|
+ // })
|
|
|
+ // .width(50)
|
|
|
+ // .height(30);
|
|
|
+ // }
|
|
|
+ // .height(55)
|
|
|
Row() {
|
|
|
- Text('深色模式')
|
|
|
+ Text('主题模式')
|
|
|
.margin({ left: 18 })
|
|
|
.fontSize(15)
|
|
|
.fontColor(Color.Gray)
|
|
|
.fontWeight(480)
|
|
|
.layoutWeight(1)
|
|
|
- Toggle({ type: ToggleType.Switch, isOn: this.isDarkMode })
|
|
|
- .selectedColor($r('app.color.tab_item_bg'))
|
|
|
- .switchPointColor(Color.White)
|
|
|
+ Select([
|
|
|
+ { value: '跟随系统' },
|
|
|
+ { value: '浅色' },
|
|
|
+ { value: '深色' }
|
|
|
+ ])
|
|
|
+ .font({ size: 15, weight: FontWeight.Medium })
|
|
|
+ .fontColor(Color.Gray)
|
|
|
.margin({ right: 18 })
|
|
|
- .onChange((checked: boolean) => {
|
|
|
- this.toggleDarkMode(checked);
|
|
|
+ .selected(this.themeMode)
|
|
|
+ .value(['跟随系统', '浅色', '深色'][this.themeMode])
|
|
|
+ .onSelect((_index: number) => {
|
|
|
+ this.themeMode = _index
|
|
|
+ this.applyThemeMode(_index)
|
|
|
+ this.getUIContext().getPromptAction().showToast({
|
|
|
+ message: ['已切换为跟随系统', '已切换为浅色', '已切换为深色'][_index],
|
|
|
+ duration: 2000,
|
|
|
+ showMode: promptAction.ToastShowMode.TOP_MOST,
|
|
|
+ bottom: 85
|
|
|
+ })
|
|
|
})
|
|
|
- .width(50)
|
|
|
- .height(30);
|
|
|
}
|
|
|
.height(55)
|
|
|
|