3

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.

Chandan
  • 11,465
  • 1
  • 6
  • 25
Topster_
  • 45
  • 1
  • 4
  • 4
    Use `set_visuals` to toggle the dark theme. `cxt.set_visuals(egui::style::Visuals::dark())` – Locke Apr 14 '22 at 01:55
  • 1
    Could you specify exactly what you want, regarding your first problem? Would you like to be able to set the size directly from inside the application? Would you like it to be impossible to resize it by hand? Would you like it to have a different default size? – jthulhu Apr 14 '22 at 06:13

1 Answers1

6

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.

Chandan
  • 11,465
  • 1
  • 6
  • 25
EskerePvP
  • 76
  • 1
  • 4