【Rust GUI开发入门】编写一个本地音乐播放器(11. 支持动态明暗主题切换)

本系列教程对应的代码已开源在 Github zeedle

暗色主题 亮色主题

Slint UI内置全局调色板,支持运行时动态切换明暗主题,这通过更改Palette.color-scheme来实现:

  • Palette.color-scheme = ColorScheme.light变为亮色主题
  • Palette.color-scheme = ColorScheme.dark则变为暗色主题

把上述代码放入UI主窗口的function/callback中,通过用户点击按钮触发调用:

slint 复制代码
public function set_light_theme(yes: bool) {
	if (yes) {
		Palette.color-scheme = ColorScheme.light;
	} else {
		Palette.color-scheme = ColorScheme.dark;
	}
}

注意

在Slint UI最新版本(1.13.1)中,ColorScheme无法导出到Rust中使用,因此,在function/callback中实现主题切换是推荐做法,在Rust代码中无法实现该功能,参考如下Github讨论:

How to access/change value of global predefined Palette variable in Rust API ?

相关推荐
Yuyubow16 小时前
gpui step by step 3. 消息传递 EventEmitter
rust
不爱学英文的码字机器17 小时前
[鸿蒙PC命令行移植适配]移植rust三方库tokei到鸿蒙PC的完整实践
华为·rust·harmonyos
EterNity_TiMe_18 小时前
[鸿蒙PC命令行移植适配]移植rust三方库ouch到鸿蒙PC的完整实践
华为·rust·harmonyos
EterNity_TiMe_18 小时前
[鸿蒙PC命令行移植适配]移植rust三方库broot到鸿蒙PC的完整实践
华为·rust·harmonyos
Pocker_Spades_A19 小时前
[鸿蒙PC命令行移植适配]移植rust三方库ox到鸿蒙PC的完整实践
华为·rust·harmonyos
EterNity_TiMe_19 小时前
[鸿蒙PC命令行移植适配]移植rust三方库choose到鸿蒙PC的完整实践
华为·rust·harmonyos
Pocker_Spades_A19 小时前
[鸿蒙PC命令行移植适配]移植rust三方库tojson到鸿蒙PC的完整实践
华为·rust·harmonyos
Pocker_Spades_A19 小时前
[鸿蒙PC命令行移植适配]移植rust三方库hexyl到鸿蒙PC的完整实践
华为·rust·harmonyos
Pocker_Spades_A1 天前
[鸿蒙PC命令行移植适配]移植rust三方库peep到鸿蒙PC的完整实践
华为·rust·harmonyos