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

zlayer (continued) #2633

Draft
wants to merge 25 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions crates/egui/src/containers/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ impl Area {
self
}

pub fn layer(&self) -> LayerId {
LayerId::new(self.order, self.id)
pub fn layer(&self) -> AreaLayerId {
AreaLayerId::new(self.order, self.id)
}

/// If false, no content responds to click
Expand Down Expand Up @@ -187,7 +187,7 @@ impl Area {
}

pub(crate) struct Prepared {
layer_id: LayerId,
layer_id: AreaLayerId,
state: State,
move_response: Response,
enabled: bool,
Expand Down Expand Up @@ -229,7 +229,7 @@ impl Area {
constrain,
} = self;

let layer_id = LayerId::new(order, id);
let layer_id = AreaLayerId::new(order, id);

let state = ctx.memory(|mem| mem.areas.get(id).copied());
let is_new = state.is_none();
Expand Down Expand Up @@ -268,7 +268,7 @@ impl Area {
let move_response = ctx.interact(
Rect::EVERYTHING,
ctx.style().spacing.item_spacing,
layer_id,
layers::ZLayer::from_area_layer(layer_id),
interact_id,
state.rect(),
sense,
Expand Down Expand Up @@ -328,7 +328,7 @@ impl Area {
return;
}

let layer_id = LayerId::new(self.order, self.id);
let layer_id = AreaLayerId::new(self.order, self.id);
let area_rect = ctx.memory(|mem| mem.areas.get(self.id).map(|area| area.rect()));
if let Some(area_rect) = area_rect {
let clip_rect = ctx.available_rect();
Expand Down Expand Up @@ -416,7 +416,7 @@ impl Prepared {
}
}

fn pointer_pressed_on_area(ctx: &Context, layer_id: LayerId) -> bool {
fn pointer_pressed_on_area(ctx: &Context, layer_id: AreaLayerId) -> bool {
if let Some(pointer_pos) = ctx.pointer_interact_pos() {
let any_pressed = ctx.input(|i| i.pointer.any_pressed());
any_pressed && ctx.layer_id_at(pointer_pos) == Some(layer_id)
Expand Down
32 changes: 17 additions & 15 deletions crates/egui/src/containers/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl SidePanel {
let we_are_on_top = ui
.ctx()
.layer_id_at(pointer)
.map_or(true, |top_layer_id| top_layer_id == ui.layer_id());
.map_or(true, |top_layer_id| top_layer_id == ui.area_layer_id());

let resize_x = side.opposite().side_x(panel_rect);
let mouse_over_resize_line = we_are_on_top
Expand Down Expand Up @@ -306,12 +306,13 @@ impl SidePanel {
} else {
Stroke::NONE
};
// TODO(emilk): draw line on top of all panels in this ui when https://github.com/emilk/egui/issues/1516 is done
// In the meantime: nudge the line so its inside the panel, so it won't be covered by neighboring panel
// (hence the shrink).
let resize_x = side.opposite().side_x(rect.shrink(1.0));
let resize_x = side.opposite().side_x(rect);
let resize_x = ui.painter().round_to_pixel(resize_x);
ui.painter().vline(resize_x, rect.y_range(), stroke);
ui.painter().clone().with_z(layers::ZOrder::FRONT).vline(
resize_x,
rect.y_range(),
stroke,
);
}

inner_response
Expand All @@ -332,7 +333,7 @@ impl SidePanel {
ctx: &Context,
add_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>,
) -> InnerResponse<R> {
let layer_id = LayerId::background();
let layer_id = AreaLayerId::background();
let side = self.side;
let available_rect = ctx.available_rect();
let clip_rect = ctx.screen_rect();
Expand Down Expand Up @@ -688,7 +689,7 @@ impl TopBottomPanel {
let we_are_on_top = ui
.ctx()
.layer_id_at(pointer)
.map_or(true, |top_layer_id| top_layer_id == ui.layer_id());
.map_or(true, |top_layer_id| top_layer_id == ui.area_layer_id());

let resize_y = side.opposite().side_y(panel_rect);
let mouse_over_resize_line = we_are_on_top
Expand Down Expand Up @@ -757,12 +758,13 @@ impl TopBottomPanel {
} else {
Stroke::NONE
};
// TODO(emilk): draw line on top of all panels in this ui when https://github.com/emilk/egui/issues/1516 is done
// In the meantime: nudge the line so its inside the panel, so it won't be covered by neighboring panel
// (hence the shrink).
let resize_y = side.opposite().side_y(rect.shrink(1.0));
let resize_y = side.opposite().side_y(rect);
let resize_y = ui.painter().round_to_pixel(resize_y);
ui.painter().hline(rect.x_range(), resize_y, stroke);
ui.painter().clone().with_z(layers::ZOrder::FRONT).hline(
rect.x_range(),
resize_y,
stroke,
);
}

inner_response
Expand All @@ -783,7 +785,7 @@ impl TopBottomPanel {
ctx: &Context,
add_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>,
) -> InnerResponse<R> {
let layer_id = LayerId::background();
let layer_id = AreaLayerId::background();
let available_rect = ctx.available_rect();
let side = self.side;

Expand Down Expand Up @@ -1041,7 +1043,7 @@ impl CentralPanel {
add_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>,
) -> InnerResponse<R> {
let available_rect = ctx.available_rect();
let layer_id = LayerId::background();
let layer_id = AreaLayerId::background();
let id = Id::new("central_panel");

let clip_rect = ctx.screen_rect();
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/containers/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ pub fn was_tooltip_open_last_frame(ctx: &Context, tooltip_id: Id) -> bool {
for (count, (individual_id, _size)) in &state.individual_ids_and_sizes {
if *individual_id == tooltip_id {
let area_id = common_id.with(count);
let layer_id = LayerId::new(Order::Tooltip, area_id);
let layer_id = AreaLayerId::new(Order::Tooltip, area_id);
if ctx.memory(|mem| mem.areas.visible_last_frame(&layer_id)) {
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions crates/egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ impl PossibleInteractions {
/// Either a move or resize
#[derive(Clone, Copy, Debug)]
pub(crate) struct WindowInteraction {
pub(crate) area_layer_id: LayerId,
pub(crate) area_layer_id: AreaLayerId,
pub(crate) start_rect: Rect,
pub(crate) left: bool,
pub(crate) right: bool,
Expand Down Expand Up @@ -540,7 +540,7 @@ fn interact(
window_interaction: WindowInteraction,
ctx: &Context,
margins: Vec2,
area_layer_id: LayerId,
area_layer_id: AreaLayerId,
area: &mut area::Prepared,
resize_id: Id,
) -> Option<WindowInteraction> {
Expand Down Expand Up @@ -607,7 +607,7 @@ fn move_and_resize_window(ctx: &Context, window_interaction: &WindowInteraction)
fn window_interaction(
ctx: &Context,
possible: PossibleInteractions,
area_layer_id: LayerId,
area_layer_id: AreaLayerId,
id: Id,
rect: Rect,
) -> Option<WindowInteraction> {
Expand Down Expand Up @@ -649,7 +649,7 @@ fn window_interaction(
fn resize_hover(
ctx: &Context,
possible: PossibleInteractions,
area_layer_id: LayerId,
area_layer_id: AreaLayerId,
rect: Rect,
) -> Option<WindowInteraction> {
let pointer = ctx.input(|i| i.pointer.interact_pos())?;
Expand Down
46 changes: 24 additions & 22 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ struct ContextImpl {
requested_repaint_last_frame: bool,

/// Written to during the frame.
layer_rects_this_frame: ahash::HashMap<LayerId, Vec<(Id, Rect)>>,
layer_rects_this_frame: ahash::HashMap<AreaLayerId, Vec<(Id, layers::ZOrder, Rect)>>,
/// Read
layer_rects_prev_frame: ahash::HashMap<LayerId, Vec<(Id, Rect)>>,
layer_rects_prev_frame: ahash::HashMap<AreaLayerId, Vec<(Id, layers::ZOrder, Rect)>>,

#[cfg(feature = "accesskit")]
is_accesskit_enabled: bool,
Expand Down Expand Up @@ -101,7 +101,7 @@ impl ContextImpl {
// Ensure we register the background area so panels and background ui can catch clicks:
let screen_rect = self.input.screen_rect();
self.memory.areas.set_state(
LayerId::background(),
AreaLayerId::background(),
containers::area::State {
pos: screen_rect.min,
size: screen_rect.size(),
Expand Down Expand Up @@ -534,7 +534,7 @@ impl Context {
&self,
clip_rect: Rect,
item_spacing: Vec2,
layer_id: LayerId,
layer: layers::ZLayer,
id: Id,
rect: Rect,
sense: Sense,
Expand All @@ -551,13 +551,13 @@ impl Context {

// Respect clip rectangle when interacting
let interact_rect = clip_rect.intersect(interact_rect);
let mut hovered = self.rect_contains_pointer(layer_id, interact_rect);
let mut hovered = self.rect_contains_pointer(layer.area_layer, interact_rect);

// This solves the problem of overlapping widgets.
// Whichever widget is added LAST (=on top) gets the input:
if interact_rect.is_positive() && sense.interactive() {
if self.style().debug.show_interactive_widgets {
Self::layer_painter(self, LayerId::debug()).rect(
Self::layer_painter(self, AreaLayerId::debug()).rect(
interact_rect,
0.0,
Color32::YELLOW.additive().linear_multiply(0.005),
Expand All @@ -567,29 +567,31 @@ impl Context {

self.write(|ctx| {
ctx.layer_rects_this_frame
.entry(layer_id)
.entry(layer.area_layer)
.or_default()
.push((id, interact_rect));
.push((id, layer.z, interact_rect));

if hovered {
let pointer_pos = ctx.input.pointer.interact_pos();
if let Some(pointer_pos) = pointer_pos {
if let Some(rects) = ctx.layer_rects_prev_frame.get(&layer_id) {
for &(prev_id, prev_rect) in rects.iter().rev() {
if prev_id == id {
if let Some(rects) = ctx.layer_rects_prev_frame.get_mut(&layer.area_layer) {
rects.sort_by_key(|(_id, z, ..)| *z);

for &(prev_id, prev_z, prev_rect) in rects.iter().rev() {
if prev_id == id && prev_z <= layer.z {
break; // there is no other interactive widget covering us at the pointer position.
}
if prev_rect.contains(pointer_pos) {
// Another interactive widget is covering us at the pointer position,
// so we aren't hovered.

if ctx.memory.options.style.debug.show_blocking_widget {
Self::layer_painter(self, LayerId::debug()).debug_rect(
Self::layer_painter(self, AreaLayerId::debug()).debug_rect(
interact_rect,
Color32::GREEN,
"Covered",
);
Self::layer_painter(self, LayerId::debug()).debug_rect(
Self::layer_painter(self, AreaLayerId::debug()).debug_rect(
prev_rect,
Color32::LIGHT_BLUE,
"On top",
Expand All @@ -606,13 +608,13 @@ impl Context {
});
}

self.interact_with_hovered(layer_id, id, rect, sense, enabled, hovered)
self.interact_with_hovered(layer.area_layer, id, rect, sense, enabled, hovered)
}

/// You specify if a thing is hovered, and the function gives a [`Response`].
pub(crate) fn interact_with_hovered(
&self,
layer_id: LayerId,
layer_id: AreaLayerId,
id: Id,
rect: Rect,
sense: Sense,
Expand Down Expand Up @@ -759,14 +761,14 @@ impl Context {
}

/// Get a full-screen painter for a new or existing layer
pub fn layer_painter(&self, layer_id: LayerId) -> Painter {
pub fn layer_painter(&self, layer_id: AreaLayerId) -> Painter {
let screen_rect = self.screen_rect();
Painter::new(self.clone(), layer_id, screen_rect)
}

/// Paint on top of everything else
pub fn debug_painter(&self) -> Painter {
Self::layer_painter(self, LayerId::debug())
Self::layer_painter(self, AreaLayerId::debug())
}

/// What operating system are we running on?
Expand Down Expand Up @@ -1322,14 +1324,14 @@ impl Context {
/// Move all the graphics at the given layer.
///
/// Can be used to implement drag-and-drop (see relevant demo).
pub fn translate_layer(&self, layer_id: LayerId, delta: Vec2) {
pub fn translate_layer(&self, layer_id: AreaLayerId, delta: Vec2) {
if delta != Vec2::ZERO {
self.graphics_mut(|g| g.list(layer_id).translate(delta));
}
}

/// Top-most layer at the given position.
pub fn layer_id_at(&self, pos: Pos2) -> Option<LayerId> {
pub fn layer_id_at(&self, pos: Pos2) -> Option<AreaLayerId> {
self.memory(|mem| {
mem.layer_id_at(pos, mem.options.style.interaction.resize_grab_radius_side)
})
Expand All @@ -1338,11 +1340,11 @@ impl Context {
/// Moves the given area to the top in its [`Order`].
///
/// [`Area`]:s and [`Window`]:s also do this automatically when being clicked on or interacted with.
pub fn move_to_top(&self, layer_id: LayerId) {
pub fn move_to_top(&self, layer_id: AreaLayerId) {
self.memory_mut(|mem| mem.areas.move_to_top(layer_id));
}

pub(crate) fn rect_contains_pointer(&self, layer_id: LayerId, rect: Rect) -> bool {
pub(crate) fn rect_contains_pointer(&self, layer_id: AreaLayerId, rect: Rect) -> bool {
rect.is_positive() && {
let pointer_pos = self.input(|i| i.pointer.interact_pos());
if let Some(pointer_pos) = pointer_pos {
Expand Down Expand Up @@ -1599,7 +1601,7 @@ impl Context {
ui.indent("areas", |ui| {
ui.label("Visible areas, ordered back to front.");
ui.label("Hover to highlight");
let layers_ids: Vec<LayerId> = self.memory(|mem| mem.areas.order().to_vec());
let layers_ids: Vec<AreaLayerId> = self.memory(|mem| mem.areas.order().to_vec());
for layer_id in layers_ids {
let area = self.memory(|mem| mem.areas.get(layer_id.id).copied());
if let Some(area) = area {
Expand Down
Loading