mirror of
https://github.com/emilk/egui.git
synced 2026-06-28 07:23:13 -04:00
Now in screen_rect min is the viewport position and max is the viewport size
This commit is contained in:
@@ -16,7 +16,7 @@ pub use accesskit_winit;
|
||||
pub use egui;
|
||||
#[cfg(feature = "accesskit")]
|
||||
use egui::accesskit;
|
||||
use egui::{mutex::RwLock, ViewportBuilder, ViewportCommand, ViewportId};
|
||||
use egui::{mutex::RwLock, Pos2, ViewportBuilder, ViewportCommand, ViewportId};
|
||||
pub use winit;
|
||||
|
||||
pub mod clipboard;
|
||||
@@ -178,9 +178,12 @@ impl State {
|
||||
let screen_size_in_points = screen_size_in_pixels / pixels_per_point;
|
||||
self.egui_input.screen_rect =
|
||||
if screen_size_in_points.x > 0.0 && screen_size_in_points.y > 0.0 {
|
||||
Some(egui::Rect::from_min_size(
|
||||
egui::Pos2::ZERO,
|
||||
screen_size_in_points,
|
||||
Some(egui::Rect::from_min_max(
|
||||
window
|
||||
.outer_position()
|
||||
.map(|pos| Pos2::new(pos.x as f32, pos.y as f32))
|
||||
.unwrap_or(Pos2::ZERO),
|
||||
screen_size_in_points.to_pos2(),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
|
||||
@@ -374,7 +374,7 @@ impl Prepared {
|
||||
}
|
||||
|
||||
pub(crate) fn content_ui(&self, ctx: &Context) -> Ui {
|
||||
let screen_rect = ctx.screen_rect();
|
||||
let screen_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max);
|
||||
|
||||
let bounds = if let Some(bounds) = self.drag_bounds {
|
||||
bounds.intersect(screen_rect) // protect against infinite bounds
|
||||
|
||||
@@ -335,7 +335,7 @@ impl SidePanel {
|
||||
let layer_id = LayerId::background();
|
||||
let side = self.side;
|
||||
let available_rect = ctx.available_rect();
|
||||
let clip_rect = ctx.screen_rect();
|
||||
let clip_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max);
|
||||
let mut panel_ui = Ui::new(ctx.clone(), layer_id, self.id, available_rect, clip_rect);
|
||||
|
||||
let inner_response = self.show_inside_dyn(&mut panel_ui, add_contents);
|
||||
@@ -787,7 +787,7 @@ impl TopBottomPanel {
|
||||
let available_rect = ctx.available_rect();
|
||||
let side = self.side;
|
||||
|
||||
let clip_rect = ctx.screen_rect();
|
||||
let clip_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max);
|
||||
let mut panel_ui = Ui::new(ctx.clone(), layer_id, self.id, available_rect, clip_rect);
|
||||
|
||||
let inner_response = self.show_inside_dyn(&mut panel_ui, add_contents);
|
||||
@@ -1044,7 +1044,7 @@ impl CentralPanel {
|
||||
let layer_id = LayerId::background();
|
||||
let id = Id::new("central_panel");
|
||||
|
||||
let clip_rect = ctx.screen_rect();
|
||||
let clip_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max);
|
||||
let mut panel_ui = Ui::new(ctx.clone(), layer_id, id, available_rect, clip_rect);
|
||||
|
||||
let inner_response = self.show_inside_dyn(&mut panel_ui, add_contents);
|
||||
|
||||
@@ -206,7 +206,7 @@ fn show_tooltip_at_avoid_dyn<'c, R>(
|
||||
}
|
||||
}
|
||||
|
||||
let position = position.at_least(ctx.screen_rect().min);
|
||||
let position = position.at_least(Pos2::ZERO);
|
||||
|
||||
let area_id = frame_state.common_id.with(frame_state.count);
|
||||
|
||||
@@ -262,7 +262,7 @@ fn show_tooltip_area_dyn<'c, R>(
|
||||
.fixed_pos(window_pos)
|
||||
.constrain(true)
|
||||
.interactable(false)
|
||||
.drag_bounds(ctx.screen_rect())
|
||||
.drag_bounds(Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max))
|
||||
.show(ctx, |ui| {
|
||||
Frame::popup(&ctx.style())
|
||||
.show(ui, |ui| {
|
||||
|
||||
@@ -180,7 +180,7 @@ impl Resize {
|
||||
.at_least(self.min_size)
|
||||
.at_most(self.max_size)
|
||||
.at_most(
|
||||
ui.ctx().screen_rect().size() - ui.spacing().window_margin.sum(), // hack for windows
|
||||
ui.ctx().screen_rect().max.to_vec2() - ui.spacing().window_margin.sum(), // hack for windows
|
||||
);
|
||||
|
||||
State {
|
||||
|
||||
@@ -421,7 +421,7 @@ impl<'open> Window<'open> {
|
||||
let mut resize = resize.id(resize_id);
|
||||
|
||||
let mut area = area.begin(ctx);
|
||||
let win_size = ctx.input(|i| i.screen_rect.size());
|
||||
let win_size = ctx.screen_rect().max.to_vec2();
|
||||
area.state_mut().set_left_top_pos(Pos2::ZERO);
|
||||
area.state_mut().size = win_size;
|
||||
let title_content_spacing = 2.0 * ctx.style().spacing.item_spacing.y;
|
||||
@@ -878,7 +878,7 @@ impl<'open> Window<'open> {
|
||||
let mut resize = resize.id(resize_id);
|
||||
|
||||
let mut area = area.begin(ctx);
|
||||
let win_size = ctx.input(|i| i.screen_rect.size());
|
||||
let win_size = ctx.screen_rect().max.to_vec2();
|
||||
area.state_mut().set_left_top_pos(Pos2::ZERO);
|
||||
area.state_mut().size = win_size;
|
||||
let title_content_spacing = 2.0 * ctx.style().spacing.item_spacing.y;
|
||||
|
||||
@@ -271,7 +271,7 @@ impl ContextImpl {
|
||||
let input = self.input.entry(viewport_id).or_default();
|
||||
// This is a bit hacky, but is required to avoid jitter:
|
||||
let ratio = input.pixels_per_point / new_pixels_per_point;
|
||||
let mut rect = input.screen_rect;
|
||||
let mut rect = Rect::from_min_max(Pos2::default(), input.screen_rect.max);
|
||||
rect.min = (ratio * rect.min.to_vec2()).to_pos2();
|
||||
rect.max = (ratio * rect.max.to_vec2()).to_pos2();
|
||||
new_raw_input.screen_rect = Some(rect);
|
||||
@@ -304,7 +304,7 @@ impl ContextImpl {
|
||||
|
||||
// Ensure we register the background area so panels and background ui can catch clicks:
|
||||
let input = self.input.get(&viewport_id).unwrap();
|
||||
let screen_rect = input.screen_rect();
|
||||
let screen_rect = Rect::from_min_max(Pos2::ZERO, input.screen_rect().max);
|
||||
self.memory.areas.set_state(
|
||||
LayerId::background(),
|
||||
containers::area::State {
|
||||
@@ -1036,7 +1036,7 @@ impl Context {
|
||||
|
||||
/// Get a full-screen painter for a new or existing layer
|
||||
pub fn layer_painter(&self, layer_id: LayerId) -> Painter {
|
||||
let screen_rect = self.screen_rect();
|
||||
let screen_rect = Rect::from_min_max(Pos2::ZERO, self.screen_rect().max);
|
||||
Painter::new(self.clone(), layer_id, screen_rect)
|
||||
}
|
||||
|
||||
@@ -1361,12 +1361,12 @@ impl Context {
|
||||
if window.width() > area.width() {
|
||||
// Allow overlapping side bars.
|
||||
// This is important for small screens, e.g. mobiles running the web demo.
|
||||
let screen_rect = self.screen_rect();
|
||||
let screen_rect = Rect::from_min_max(Pos2::ZERO, self.screen_rect().max);
|
||||
(area.min.x, area.max.x) = (screen_rect.min.x, screen_rect.max.x);
|
||||
}
|
||||
if window.height() > area.height() {
|
||||
// Allow overlapping top/bottom bars:
|
||||
let screen_rect = self.screen_rect();
|
||||
let screen_rect = Rect::from_min_max(Pos2::ZERO, self.screen_rect().max);
|
||||
(area.min.y, area.max.y) = (screen_rect.min.y, screen_rect.max.y);
|
||||
}
|
||||
|
||||
@@ -1577,6 +1577,7 @@ impl Context {
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/// Position and size of the egui area.
|
||||
/// min is the position, max is the size
|
||||
pub fn screen_rect(&self) -> Rect {
|
||||
self.input(|i| i.screen_rect())
|
||||
}
|
||||
@@ -1941,7 +1942,7 @@ impl Context {
|
||||
size *= (max_preview_size.y / size.y).min(1.0);
|
||||
ui.image(texture_id, size).on_hover_ui(|ui| {
|
||||
// show larger on hover
|
||||
let max_size = 0.5 * ui.ctx().screen_rect().size();
|
||||
let max_size = 0.5 * ui.ctx().screen_rect().max.to_vec2();
|
||||
let mut size = vec2(w as f32, h as f32);
|
||||
size *= max_size.x / size.x.max(max_size.x);
|
||||
size *= max_size.y / size.y.max(max_size.y);
|
||||
|
||||
@@ -93,8 +93,8 @@ impl FrameState {
|
||||
} = self;
|
||||
|
||||
used_ids.clear();
|
||||
*available_rect = input.screen_rect();
|
||||
*unused_rect = input.screen_rect();
|
||||
*available_rect = Rect::from_min_max(Pos2::ZERO, input.screen_rect().max);
|
||||
*unused_rect = Rect::from_min_max(Pos2::ZERO, input.screen_rect().max);
|
||||
*used_by_panels = Rect::NOTHING;
|
||||
*tooltip_state = None;
|
||||
*scroll_delta = input.scroll_delta;
|
||||
|
||||
@@ -229,6 +229,8 @@ impl InputState {
|
||||
}
|
||||
}
|
||||
|
||||
/// Position and size of the current viewport.
|
||||
/// min is the position, max is the size
|
||||
#[inline(always)]
|
||||
pub fn screen_rect(&self) -> Rect {
|
||||
self.screen_rect
|
||||
|
||||
@@ -146,7 +146,7 @@ pub(crate) fn menu_ui<'c, R>(
|
||||
.constrain(true)
|
||||
.fixed_pos(pos)
|
||||
.interactable(true)
|
||||
.drag_bounds(ctx.screen_rect());
|
||||
.drag_bounds(Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max));
|
||||
|
||||
area.show(ctx, |ui| {
|
||||
set_menu_style(ui.style_mut());
|
||||
@@ -324,7 +324,8 @@ impl MenuRoot {
|
||||
let mut pos = response.rect.left_bottom();
|
||||
if let Some(root) = root.inner.as_mut() {
|
||||
let menu_rect = root.menu_state.read().rect;
|
||||
let screen_rect = response.ctx.input(|i| i.screen_rect);
|
||||
let screen_rect =
|
||||
Rect::from_min_max(Pos2::ZERO, response.ctx.input(|i| i.screen_rect.max));
|
||||
|
||||
if pos.y + menu_rect.height() > screen_rect.max.y {
|
||||
pos.y = screen_rect.max.y - menu_rect.height() - response.rect.height();
|
||||
|
||||
@@ -449,7 +449,7 @@ impl WrapApp {
|
||||
let painter =
|
||||
ctx.layer_painter(LayerId::new(Order::Foreground, Id::new("file_drop_target")));
|
||||
|
||||
let screen_rect = ctx.screen_rect();
|
||||
let screen_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max);
|
||||
painter.rect_filled(screen_rect, 0.0, Color32::from_black_alpha(192));
|
||||
painter.text(
|
||||
screen_rect.center(),
|
||||
|
||||
@@ -179,7 +179,7 @@ impl DemoWindows {
|
||||
|
||||
fn mobile_ui(&mut self, ctx: &Context) {
|
||||
if self.about_is_open {
|
||||
let screen_size = ctx.input(|i| i.screen_rect.size());
|
||||
let screen_size = ctx.screen_rect().max.to_vec2();
|
||||
let default_width = (screen_size.x - 20.0).min(400.0);
|
||||
|
||||
let mut close = false;
|
||||
|
||||
@@ -116,6 +116,6 @@ fn test_egui_zero_window_size() {
|
||||
/// Detect narrow screens. This is used to show a simpler UI on mobile devices,
|
||||
/// especially for the web demo at <https://egui.rs>.
|
||||
pub fn is_mobile(ctx: &egui::Context) -> bool {
|
||||
let screen_size = ctx.screen_rect().size();
|
||||
let screen_size = ctx.screen_rect().max;
|
||||
screen_size.x < 550.0
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ fn preview_files_being_dropped(ctx: &egui::Context) {
|
||||
let painter =
|
||||
ctx.layer_painter(LayerId::new(Order::Foreground, Id::new("file_drop_target")));
|
||||
|
||||
let screen_rect = ctx.screen_rect();
|
||||
let screen_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max);
|
||||
painter.rect_filled(screen_rect, 0.0, Color32::from_black_alpha(192));
|
||||
painter.text(
|
||||
screen_rect.center(),
|
||||
|
||||
@@ -44,6 +44,8 @@ impl eframe::App for App {
|
||||
"Current Parent Viewport Id: {}",
|
||||
ctx.get_viewport_id()
|
||||
));
|
||||
ui.label(format!("Pos: {:?}", ctx.screen_rect().min));
|
||||
ui.label(format!("Size: {:?}", ctx.screen_rect().max));
|
||||
ui.label("Look at the \"Frame: \" will tell you, what viewport is rendering!");
|
||||
{
|
||||
let mut desktop = ctx.is_desktop();
|
||||
@@ -70,6 +72,8 @@ impl eframe::App for App {
|
||||
"Current Parent Viewport Id: {}",
|
||||
ctx.get_viewport_id()
|
||||
));
|
||||
ui.label(format!("Pos: {:?}", ctx.screen_rect().min));
|
||||
ui.label(format!("Size: {:?}", ctx.screen_rect().max));
|
||||
ui.label(format!("Count: {state}"));
|
||||
if ui.button("Add").clicked() {
|
||||
*state += 1;
|
||||
@@ -91,6 +95,8 @@ impl eframe::App for App {
|
||||
"Current Parent Viewport Id: {}",
|
||||
ctx.get_viewport_id()
|
||||
));
|
||||
ui.label(format!("Pos: {:?}", ctx.screen_rect().min));
|
||||
ui.label(format!("Size: {:?}", ctx.screen_rect().max));
|
||||
ui.label(format!("Count: {}", self.sync_viewport_state));
|
||||
if ui.button("Add").clicked() {
|
||||
self.sync_viewport_state += 1;
|
||||
@@ -114,6 +120,8 @@ impl eframe::App for App {
|
||||
"Current Parent Viewport Id: {}",
|
||||
ctx.get_viewport_id()
|
||||
));
|
||||
ui.label(format!("Pos: {:?}", ctx.screen_rect().min));
|
||||
ui.label(format!("Size: {:?}", ctx.screen_rect().max));
|
||||
ui.label(format!("Count: {state}"));
|
||||
if ui.button("Add").clicked() {
|
||||
*state += 1;
|
||||
@@ -127,8 +135,13 @@ impl eframe::App for App {
|
||||
.default_embedded(false)
|
||||
.show(ctx, |ui| {
|
||||
ui.label(format!("Frame: {}", ui.ctx().frame_nr()));
|
||||
ui.label(format!("Frame: {}", ctx.frame_nr()));
|
||||
ui.label(format!("Current Viewport Id: {}", ctx.get_viewport_id()));
|
||||
ui.label(format!(
|
||||
"Current Parent Viewport Id: {}",
|
||||
ctx.get_viewport_id()
|
||||
));
|
||||
ui.label(format!("Pos: {:?}", ctx.screen_rect().min));
|
||||
ui.label(format!("Size: {:?}", ctx.screen_rect().max));
|
||||
ui.label(format!("Count: {}", self.sync_window_state));
|
||||
if ui.button("Add").clicked() {
|
||||
self.sync_window_state += 1;
|
||||
|
||||
Reference in New Issue
Block a user