1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 05:40:03 -04:00

Add Context::set_pixels_per_point to control the scale of the UI

This commit is contained in:
Emil Ernerfeldt
2021-02-21 11:23:33 +01:00
parent c601db5956
commit 5f6a468812
9 changed files with 55 additions and 34 deletions

View File

@@ -388,7 +388,6 @@ impl Context {
}
/// Will become active at the start of the next frame.
/// `pixels_per_point` will be ignored (overwritten at start of each frame with the contents of input)
pub fn set_fonts(&self, font_definitions: FontDefinitions) {
self.memory().options.font_definitions = font_definitions;
}
@@ -429,6 +428,15 @@ impl Context {
self.input.pixels_per_point()
}
/// Set the number of physical pixels for each logical point.
/// Will become active at the start of the next frame.
///
/// Note that this may be overwritten by input from the integration via [`RawInput::pixels_per_point`].
/// For instance, when using `egui_web` the browsers native zoom level will always be used.
pub fn set_pixels_per_point(&self, pixels_per_point: f32) {
self.memory().new_pixels_per_point = Some(pixels_per_point);
}
/// Useful for pixel-perfect rendering
pub(crate) fn round_to_pixel(&self, point: f32) -> f32 {
let pixels_per_point = self.pixels_per_point();
@@ -493,7 +501,12 @@ impl Context {
fn begin_frame_mut(&mut self, new_raw_input: RawInput) {
self.memory().begin_frame(&self.input, &new_raw_input);
self.input = std::mem::take(&mut self.input).begin_frame(new_raw_input);
let mut input = std::mem::take(&mut self.input);
if let Some(new_pixels_per_point) = self.memory().new_pixels_per_point.take() {
input.pixels_per_point = new_pixels_per_point;
}
self.input = input.begin_frame(new_raw_input);
self.frame_state.lock().begin_frame(&self.input);
let font_definitions = self.memory().options.font_definitions.clone();

View File

@@ -27,6 +27,7 @@ pub struct RawInput {
/// Also known as device pixel ratio, > 1 for HDPI screens.
/// If text looks blurry on high resolution screens, you probably forgot to set this.
/// Set this the first frame, whenever it changes, or just on every frame.
pub pixels_per_point: Option<f32>,
/// Monotonically increasing time, in seconds. Relative to whatever. Used for animations.
@@ -68,9 +69,9 @@ impl RawInput {
RawInput {
scroll_delta: std::mem::take(&mut self.scroll_delta),
screen_size: self.screen_size,
screen_rect: self.screen_rect,
pixels_per_point: self.pixels_per_point,
time: self.time,
screen_rect: self.screen_rect.take(),
pixels_per_point: self.pixels_per_point.take(),
time: self.time.take(),
predicted_dt: self.predicted_dt,
modifiers: self.modifiers,
events: std::mem::take(&mut self.events),

View File

@@ -20,6 +20,9 @@ use epaint::color::{Color32, Hsva};
pub struct Memory {
pub(crate) options: Options,
/// new scale that will be applied at the start of the next frame
pub(crate) new_pixels_per_point: Option<f32>,
#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) interaction: Interaction,