目录

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

目录

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

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

暗色主题亮色主题
dark-theme.pnglight-theme.png

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

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

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

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讨论: