【问题标题】:Rust egui window size and dark modeRust egui 窗口大小和暗模式
【发布时间】:2022-06-13 21:13:32
【问题描述】:

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

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,除非我手动调整其大小
第二:我不知道如何激活暗模式

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

【问题讨论】:

  • 使用set_visuals 切换深色主题。 cxt.set_visuals(egui::style::Visuals::dark())
  • 你能具体说明你想要什么,关于你的第一个问题吗?您希望能够直接从应用程序内部设置大小吗?您是否希望无法手动调整其大小?您希望它具有不同的默认尺寸吗?

标签: user-interface rust egui


【解决方案1】:

用这个替换主方法中的“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());

【讨论】:

    猜你喜欢
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 2011-09-13
    • 2018-10-23
    • 2016-10-23
    相关资源
    最近更新 更多