From 25b8a8ebfdfcbf0edbdcaaf805a4fb728cd93c78 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 3 Jan 2021 00:12:15 +0100 Subject: [PATCH] =?UTF-8?q?Add=20window.id(=E2=80=A6)=20and=20area.id(?= =?UTF-8?q?=E2=80=A6)=20for=20overriding=20the=20default=20Id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + egui/src/containers/area.rs | 7 ++++++- egui/src/containers/window.rs | 15 +++++++++++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 941fdfe7..cf17892b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added ⭐ * Add `ui.scroll_to_cursor` and `response.scroll_to_me` ([#81](https://github.com/emilk/egui/pull/81) by [lucaspoffo](https://github.com/lucaspoffo)). +* Add `window.id(…)` and `area.id(…)` for overriding the default `Id`. ### Changed 🔧 diff --git a/egui/src/containers/area.rs b/egui/src/containers/area.rs index 3dffabc4..6af447f6 100644 --- a/egui/src/containers/area.rs +++ b/egui/src/containers/area.rs @@ -42,7 +42,7 @@ impl State { /// }); #[derive(Clone, Copy, Debug)] pub struct Area { - id: Id, + pub(crate) id: Id, movable: bool, interactable: bool, order: Order, @@ -62,6 +62,11 @@ impl Area { } } + pub fn id(mut self, id: Id) -> Self { + self.id = id; + self + } + pub fn layer(&self) -> LayerId { LayerId::new(self.order, self.id) } diff --git a/egui/src/containers/window.rs b/egui/src/containers/window.rs index 41ea999a..32e30c79 100644 --- a/egui/src/containers/window.rs +++ b/egui/src/containers/window.rs @@ -34,6 +34,7 @@ pub struct Window<'open> { impl<'open> Window<'open> { /// The window title is used as a unique [`Id`] and must be unique, and should not change. /// This is true even if you disable the title bar with `.title_bar(false)`. + /// If you need a changing title, you must call `window.id(…)` with a fixed id. pub fn new(title: impl Into) -> Self { let title = title.into(); let area = Area::new(&title); @@ -55,6 +56,12 @@ impl<'open> Window<'open> { } } + /// Assign a unique id to the Window. Required if the title changes, or is shared with another window. + pub fn id(mut self, id: Id) -> Self { + self.area = self.area.id(id); + self + } + /// Call this to add a close-button to the window title bar. /// /// * If `*open == false`, the window will not be visible. @@ -213,10 +220,10 @@ impl<'open> Window<'open> { return None; } - let window_id = Id::new(title_label.text()); + let area_id = area.id; let area_layer_id = area.layer(); - let resize_id = window_id.with("resize"); - let collapsing_id = window_id.with("collapsing"); + let resize_id = area_id.with("resize"); + let collapsing_id = area_id.with("collapsing"); let is_maximized = !with_title_bar || collapsing_header::State::is_open(ctx, collapsing_id).unwrap_or_default(); @@ -242,7 +249,7 @@ impl<'open> Window<'open> { ctx, possible, area_layer_id, - window_id.with("frame_resize"), + area_id.with("frame_resize"), last_frame_outer_rect, ) .and_then(|window_interaction| {