【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 ?

相关推荐
布列瑟农的星空14 小时前
前端都能看懂的Rust入门教程(三)——控制流语句
前端·后端·rust
Andrew_Ryan20 小时前
用 Rust 构建高性能 LiteLLM 客户端:支持流式与非流式调用
rust
魔力军21 小时前
Rust学习Day3: 3个小demo实现
java·学习·rust
Smart-Space1 天前
htmlbuilder - rust灵活构建html
rust·html
魔力军1 天前
Rust学习Day2: 变量与可变性、数据类型和函数和控制流
开发语言·学习·rust
暴躁小师兄数据学院1 天前
【WEB3.0零基础转行笔记】Rust编程篇-第一讲:课程简介
rust·web3·区块链·智能合约
七夜zippoe2 天前
脉向AI|当豆包手机遭遇“全网封杀“:GUI Agent是通向AGI的必经之路吗?
人工智能·ai·智能手机·agent·gui
Hello.Reader2 天前
Rocket Fairings 实战把全局能力做成“结构化中间件”
中间件·rust·rocket
Andrew_Ryan2 天前
rust arena 内存分配
rust
Andrew_Ryan2 天前
深入理解 Rust 内存管理:基于 typed_arena 的指针操作实践
rust