egui icon indicating copy to clipboard operation
egui copied to clipboard

How to get area of each window?

Open VRichardJP opened this issue 4 years ago • 2 comments
trafficstars

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: https://github.com/emilk/egui/blob/60fd70921df538c5cfab9918ddac1a2df5a06c66/egui/src/context.rs#L866-L888

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: https://github.com/emilk/egui/blob/60fd70921df538c5cfab9918ddac1a2df5a06c66/egui/src/context.rs#L644-L650

Is there any other way to get this information?

VRichardJP avatar Jun 17 '21 05:06 VRichardJP

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 avatar Apr 16 '22 08:04 emilk

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.

AadamZ5 avatar Dec 17 '23 23:12 AadamZ5