Search code examples
user-interfacerustegui

Rust egui window size and dark mode


I'm tring to make a native gui app using egui. After some time got the hello_world example to compile.
Heres the code:

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);
}

But i have 2 problems:
First: the window is always 800x600 unless i manually resize it pic related
Second: i have no idea how to activate dark mode

I just started learning rust so if anyone could help that would be great.


Solution

  • Replace the "native_options" in the main method with this.

    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,
    };
    

    To set to dark mode, you can use the built in dark mode buttons: egui::widgets::global_dark_light_mode_buttons(ui); or you can do cc.egui_ctx.set_visuals(egui::Visuals::dark()); in your eframe run native function.