Rust egui 窗口大小和深色模式

问题描述 投票:0回答:2

我正在尝试使用 egui 制作一个本机 GUI 应用程序。 一段时间后,编译了 hello_world 示例
代码如下:

use eframe::{epi, egui};

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

impl Default for MyEguiApp {
    fn default() -> Self {
        Self {
            name: "Arthur".to_owned(),
            age: 42,
        }
    }
}

impl epi::App for MyEguiApp {
   fn name(&self) -> &str {
       "Test"
   }

    fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            ui.heading("My egui aplication");
            ui.horizontal(|ui|{
                ui.label("Your name: ");
                ui.text_edit_singleline(&mut self.name);
            });
            ui.add(egui::Slider::new(&mut self.age,0..=120));
            if ui.button("Click each year").clicked() {
                self.age += 1;
            }
            ui.label(format!("Hello '{}', age {}", self.name, self.age));
        });
        frame.set_window_size(ctx.used_size());
    }
}

fn main() {
    let app = MyEguiApp::default();
    let native_options = eframe::NativeOptions::default();
    eframe::run_native(Box::new(app), native_options);
}

但是我有两个问题:
第一:窗口始终是 800x600,除非我手动调整它的大小pic related
第二:我不知道如何激活黑暗模式

我刚刚开始学习 Rust,所以如果有人能提供帮助那就太好了。

user-interface rust egui
2个回答
7
投票

用这个替换主方法中的“native_options”。

let options = eframe::NativeOptions {
    always_on_top: false,
    maximized: false,
    decorated: true,
    drag_and_drop_support: true,
    icon_data: None,
    initial_window_pos: None,
    initial_window_size: Option::from(
      Vec2::new(PUT X SIZE HERE as f32, PUT Y SIZE HERE as f32)
    ),
    min_window_size: None,
    max_window_size: None,
    resizable: true,
    transparent: true,
    vsync: true,
    multisampling: 0,
    depth_buffer: 0,
    stencil_buffer: 0,
};

要设置为深色模式,您可以使用内置的深色模式按钮:

egui::widgets::global_dark_light_mode_buttons(ui);
,或者您可以在 eframe 运行本机函数中执行
 cc.egui_ctx.set_visuals(egui::Visuals::dark());


0
投票

深色模式是egui中的默认主题,但我们不应该忘记,是否实际使用深色模式取决于系统设置,您可以在设置 - >首选项 - >颜色中更改。

您必须在 NativeOptions 中手动设置它才能获得深色模式,或者只需 直接设置为深色主题:

fn enable_dark_theme(ctx: &Context) {
    ctx.set_visuals(egui::Visuals::dark());
}
© www.soinside.com 2019 - 2024. All rights reserved.