FAQ - Help and questions

How do I read the display scale in egui, and let the user make the interface bigger?

#egui#high-dpi#zoom#eframepublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

In egui one point is one logical unit, and pixels_per_point says how many physical pixels one point covers on the current monitor. Read it with ctx.pixels_per_point(). To let users enlarge the whole interface, change ctx.set_zoom_factor, which multiplies the monitor's scale. The code passed cargo check.

Checked with rustc 1.93.0, Windows 11, eframe 0.35.0; `cargo check` only, the window was not opened.

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

egui lays out in points. The number of physical pixels per point comes from the monitor's scale factor, and egui exposes it as pixels_per_point. egui also has a separate zoom factor that multiplies it, which is the right tool for an "enlarge the interface" setting.

src/main.rsrust
use eframe::egui;

struct App;

impl eframe::App for App {
    fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
        let ppp = ui.ctx().pixels_per_point(); // physical pixels per egui point on this monitor
        egui::CentralPanel::default().show(ui, |ui| {
            ui.label(format!("pixels_per_point = {ppp}"));
            ui.label(format!("zoom factor = {}", ui.ctx().zoom_factor()));
            if ui.button("Zoom in").clicked() {
                let z = ui.ctx().zoom_factor();
                ui.ctx().set_zoom_factor(z * 1.1); // the user's own scaling, on top of the monitor's
            }
        });
    }
}

fn main() -> eframe::Result {
    eframe::run_native("Scale", eframe::NativeOptions::default(), Box::new(|_cc| Ok(Box::new(App))))
}

This compiled with cargo check using eframe 0.35.0. It was not run, so no values are shown; they depend on your screen.

Practical notes:

  • Do not set pixels_per_point yourself to follow the monitor: egui and eframe take it from the window and update it when the window moves to another screen. Use the zoom factor for the user's preference.
  • Save the zoom factor with the other settings (see the settings threads) and apply it at startup.
  • Drawing your own textures or images: compute their pixel size as size_in_points * pixels_per_point so they stay sharp on scaled screens.
  • Sizes in your code are points. ui.add_space(8.0) is eight points on every screen.

Common mistakes#

  • Multiplying every size by the scale yourself. egui already does it; you would scale twice.
  • Setting a zoom on every frame instead of when the user asks.

Is this the same as the Windows scale setting?#

The monitor's scale comes from the operating system and is what pixels_per_point starts from. See the threads on logical and physical pixels and on blurry windows.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet