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

Refactor viewport ids in eframe (#3607)

Simplifies some things
This commit is contained in:
Emil Ernerfeldt
2023-11-22 13:50:43 +01:00
committed by GitHub
parent e037489ac2
commit ea53246c60
9 changed files with 49 additions and 55 deletions

View File

@@ -227,8 +227,13 @@ struct ContextImpl {
impl ContextImpl {
fn begin_frame_mut(&mut self, mut new_raw_input: RawInput) {
let ids = new_raw_input.viewport_ids;
let viewport_id = ids.this;
let viewport_id = new_raw_input.viewport_id;
let parent_id = new_raw_input
.viewports
.get(&viewport_id)
.and_then(|v| v.parent)
.unwrap_or_default();
let ids = ViewportIdPair::from_self_and_parent(viewport_id, parent_id);
self.viewport_stack.push(ids);
let viewport = self.viewports.entry(viewport_id).or_default();

View File

@@ -2,7 +2,7 @@
use epaint::ColorImage;
use crate::{emath::*, ViewportIdMap, ViewportIdPair};
use crate::{emath::*, ViewportId, ViewportIdMap};
/// What the integrations provides to egui at the start of each frame.
///
@@ -15,8 +15,8 @@ use crate::{emath::*, ViewportIdMap, ViewportIdPair};
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct RawInput {
/// The id of the active viewport, and out parent.
pub viewport_ids: ViewportIdPair,
/// The id of the active viewport.
pub viewport_id: ViewportId,
/// Information about all egui viewports.
pub viewports: ViewportIdMap<ViewportInfo>,
@@ -89,7 +89,7 @@ pub struct RawInput {
impl Default for RawInput {
fn default() -> Self {
Self {
viewport_ids: Default::default(),
viewport_id: Default::default(),
viewports: Default::default(),
screen_rect: None,
pixels_per_point: None,
@@ -107,13 +107,19 @@ impl Default for RawInput {
}
impl RawInput {
/// Info about the active viewport
#[inline]
pub fn viewport(&self) -> &ViewportInfo {
self.viewports.get(&self.viewport_id).expect("Failed to find current viewport in egui RawInput. This is the fault of the egui backend")
}
/// Helper: move volatile (deltas and events), clone the rest.
///
/// * [`Self::hovered_files`] is cloned.
/// * [`Self::dropped_files`] is moved.
pub fn take(&mut self) -> RawInput {
RawInput {
viewport_ids: self.viewport_ids,
viewport_id: self.viewport_id,
viewports: self.viewports.clone(),
screen_rect: self.screen_rect.take(),
pixels_per_point: self.pixels_per_point.take(), // take the diff
@@ -132,7 +138,7 @@ impl RawInput {
/// Add on new input.
pub fn append(&mut self, newer: Self) {
let Self {
viewport_ids,
viewport_id: viewport_ids,
viewports,
screen_rect,
pixels_per_point,
@@ -147,7 +153,7 @@ impl RawInput {
focused,
} = newer;
self.viewport_ids = viewport_ids;
self.viewport_id = viewport_ids;
self.viewports = viewports;
self.screen_rect = screen_rect.or(self.screen_rect);
self.pixels_per_point = pixels_per_point.or(self.pixels_per_point);
@@ -1106,7 +1112,7 @@ fn format_kb_shortcut() {
impl RawInput {
pub fn ui(&self, ui: &mut crate::Ui) {
let Self {
viewport_ids,
viewport_id,
viewports,
screen_rect,
pixels_per_point,
@@ -1121,10 +1127,7 @@ impl RawInput {
focused,
} = self;
ui.label(format!(
"Active viwport: {:?}, parent: {:?}",
viewport_ids.this, viewport_ids.parent,
));
ui.label(format!("Active viwport: {viewport_id:?}"));
for (id, viewport) in viewports {
ui.group(|ui| {
ui.label(format!("Viewport {id:?}"));

View File

@@ -232,8 +232,9 @@ impl InputState {
}
/// Info about the active viewport
#[inline]
pub fn viewport(&self) -> &ViewportInfo {
self.raw.viewports.get(&self.raw.viewport_ids.this).expect("Failed to find current viewport in egui RawInput. This is the fault of the egui backend")
self.raw.viewport()
}
#[inline(always)]

View File

@@ -559,7 +559,7 @@ impl Memory {
self.window_interactions
.retain(|id, _| viewports.contains(id));
self.viewport_id = new_input.viewport_ids.this;
self.viewport_id = new_input.viewport_id;
self.interactions
.entry(self.viewport_id)
.or_default()