Add window.id(…) and area.id(…) for overriding the default Id

This commit is contained in:
Emil Ernerfeldt 2021-01-03 00:12:15 +01:00
parent 31930e25e3
commit 25b8a8ebfd
3 changed files with 18 additions and 5 deletions

View file

@ -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 🔧

View file

@ -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)
}

View file

@ -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<String>) -> 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| {