Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get area of each window? #493

Open
VRichardJP opened this issue Jun 17, 2021 · 2 comments
Open

How to get area of each window? #493

VRichardJP opened this issue Jun 17, 2021 · 2 comments
Labels
feature New feature or request

Comments

@VRichardJP
Copy link

Hi

I would like to know the position of all egui windows within my application like it is done in memory_ui.

To retrieve the list of window area, memory_ui has access to the areas field in Memory struct which is not public:

egui/egui/src/context.rs

Lines 866 to 888 in 60fd709

ui.indent("areas", |ui| {
let layers_ids: Vec<LayerId> = self.memory().areas.order().to_vec();
for layer_id in layers_ids {
let area = self.memory().areas.get(layer_id.id).cloned();
if let Some(area) = area {
let is_visible = self.memory().areas.is_visible(&layer_id);
if ui
.label(format!(
"{:?} {:?} {}",
layer_id.order,
area.rect(),
if is_visible { "" } else { "(INVISIBLE)" }
))
.hovered
&& is_visible
{
ui.ctx()
.debug_painter()
.debug_rect(area.rect(), Color32::RED, "");
}
}
}
});

used_rect() function is public and almost does the job, but it returns the union of all the areas and not a list of areas:

egui/egui/src/context.rs

Lines 644 to 650 in 60fd709

pub fn used_rect(&self) -> Rect {
let mut used = self.frame_state().used_by_panels;
for window in self.memory().areas.visible_windows() {
used = used.union(window.rect());
}
used
}

Is there any other way to get this information?

@VRichardJP VRichardJP changed the title How to get area of all windows? How to get area of each window? Jun 17, 2021
@emilk
Copy link
Owner

emilk commented Apr 16, 2022

Widnwo::show returns where it currently is.

I guess you want an API for getting the position of any Area or Window given its Id.

I think we could add that, e.g. with Memory::get_area_rect() or similar. It is very easy to add if you follow how ctx.memory().areas.get(id) works.

@emilk emilk added the feature New feature or request label Apr 16, 2022
@AadamZ5
Copy link

AadamZ5 commented Dec 17, 2023

Stumbling across this after finding the warning in the comment of Window::current_pos(...) reads ... If the window is movable it is up to you to keep track of where it moved to!. Additionally, if we use Window::current_pos(...) the response does not include the updated Rect for the in-progress drag. So we can't grab it from the response.

Initial reaction to seeing that was supply current pos, run immediate render/interaction, then update state position with window's new position. It was not straight forward to retrieve this. My first hope was to use the window response InnerResponse.response.drag_delta() but that does not expose window drags, maybe when using current_pos(...).

My goal was to link the window position to another item's position (in my app's "edit mode", I create the window. In "view mode" I use an Area. I want the window to be created where the area was, and vice-versa.)

I arrived at this implementation for now:

let window_response = Window::new("Some window").id(window_id).current_pos(current_window_pos);

let new_window_pos = ctx.memory(|mem| {
    mem.area_rect(window_id)
        .map(|rect| rect.center())
        .unwrap_or(current_window_pos)
});

// Assign new window pos back to your state somehow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants