egui教程三——切换主题

前言

egui内置了亮色和暗色两种主题,这里仅仅实现主题的切换,不进行自定义主题。

代码

代码还在前面两篇文章的基础上进行。仅仅是为PicPicApp增加了主题切换的方法和在update中的调用,但是这里附上完整的代码,完整代码依然保留自定义字体的代码。

rust 复制代码
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
#![allow(rustdoc::missing_crate_level_docs)] // it's an example

use eframe::egui;

fn main() -> eframe::Result {
    let options = eframe::NativeOptions {
        viewport: egui::ViewportBuilder::default().with_inner_size([640.0, 480.0]),
        ..Default::default()
    };
    eframe::run_native(
        "picpic",
        options,
        Box::new(|cc| Ok(Box::new(PicPicApp::new(cc)))),
    )
}

struct PicPicApp {
    name: String,
    age: u32,
}

impl PicPicApp {
    fn new(cc: &eframe::CreationContext<'_>) -> Self {
        setup_custom_fonts(&cc.egui_ctx);
        Self {
            name: "Aspirin".to_owned(),
            age: 18,
        }
    }

    // 切换主题
    fn theme_switcher(&mut self, ui: &mut egui::Ui, ctx: &egui::Context) {
        ui.horizontal(|ui| {
            if ui.button("Dark").clicked() {
                ctx.set_visuals(egui::Visuals::dark());
            }
            if ui.button("Light").clicked() {
                ctx.set_visuals(egui::Visuals::light());
            }
        });
    }
}

impl eframe::App for PicPicApp {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            // 在更新方法中调用切换主题的方法
            self.theme_switcher(ui, ctx);

            ui.horizontal(|ui| {
                let name_label = ui.label("Your name: ");
                ui.text_edit_singleline(&mut self.name)
                    .labelled_by(name_label.id);
            });
            ui.add(egui::Slider::new(&mut self.age, 0..=120).text("age"));
            if ui.button("Increment").clicked() {
                self.age += 1;
            }
            ui.label(format!("Hello '{}', age {}", self.name, self.age));
        });
    }
}

fn setup_custom_fonts(ctx: &egui::Context) {
    // Start with the default fonts (we will be adding to them rather than replacing them).
    let mut fonts = egui::FontDefinitions::default();

    // Install my own font (maybe supporting non-latin characters).
    // .ttf and .otf files supported.
    fonts.font_data.insert(
        "my_font".to_owned(),
        egui::FontData::from_static(include_bytes!("../fonts/FangZhengFangSongJianTi.ttf")),
    );

    // Put my font first (highest priority) for proportional text:
    fonts
        .families
        .entry(egui::FontFamily::Proportional)
        .or_default()
        .insert(0, "my_font".to_owned());

    // Put my font as last fallback for monospace:
    fonts
        .families
        .entry(egui::FontFamily::Monospace)
        .or_default()
        .push("my_font".to_owned());

    // Tell egui to use these fonts:
    ctx.set_fonts(fonts);
}

总结

切换主题目前还存在一些问题:

  • 切换主题不能持久化,即重新打开程序以后,上次的主题会丢失。如果想要保存状态,可以在点击切换主题数,打包好的exe目录下建一个隐藏文件,比如.env然后在其中记录主题,下次启动程序时读取状态。不过这种方式仍然有瑕疵,比如可执行文件被移动,状态仍然会丢失。
  • 目前只能手动切换亮色主题或者暗色主题,无法跟随系统变化。后续如果有解决方案会同步更新该文章。也欢迎路过的朋友指导。
相关推荐
好家伙VCC7 小时前
**发散创新:基于Rust的轻量级权限管理库设计与开源许可证实践**在现代分布式系统中,**权限控制(RBAC
java·开发语言·python·rust·开源
@atweiwei7 小时前
用 Rust 构建agent的 LLM 应用的高性能框架
开发语言·后端·rust·langchain·eclipse·llm·agent
skilllite作者7 小时前
Spec + Task 作为「开发协议层」:Rust 大模型辅助的标准化、harness 化与可回滚
开发语言·人工智能·后端·安全·架构·rust·rust沙箱
zsqw12311 小时前
以 Rust 为例,聊聊线性类型,以及整个类型系统
rust·编译器
Rust研习社12 小时前
Rust Tracing 实战指南:从基础用法到生产级落地
rust
分布式存储与RustFS12 小时前
MinIO迎来“恶龙”?RustFS这款开源存储简直“不讲武德”
架构·rust·开源·对象存储·minio·企业存储·rustfs
数据知道1 天前
claw-code 源码分析:从 TypeScript 心智到 Python/Rust——跨栈移植时类型、边界与错误模型怎么对齐?
python·ai·rust·typescript·claude code·claw code
Rust研习社1 天前
深入浅出 Rust 迭代器:从基础用法到性能优化
rust
@atweiwei1 天前
langchainrust:Rust 版 LangChain 框架(LLM+Agent+RAG)
开发语言·rust·langchain·agent·向量数据库·rag