[refactor] Rename Layer to LayerId for clarity

This commit is contained in:
Emil Ernerfeldt 2020-10-21 14:39:08 +02:00
parent 9b01c75e16
commit 39431afd03
9 changed files with 112 additions and 98 deletions

View file

@ -57,8 +57,8 @@ impl Area {
}
}
pub fn layer(&self) -> Layer {
Layer {
pub fn layer(&self) -> LayerId {
LayerId {
order: self.order,
id: self.id,
}
@ -105,7 +105,7 @@ impl Area {
}
pub(crate) struct Prepared {
layer: Layer,
layer_id: LayerId,
state: State,
movable: bool,
}
@ -121,7 +121,7 @@ impl Area {
fixed_pos,
} = self;
let layer = Layer { order, id };
let layer_id = LayerId { order, id };
let state = ctx.memory().areas.get(id).cloned();
let mut state = state.unwrap_or_else(|| State {
@ -134,7 +134,7 @@ impl Area {
state.pos = ctx.round_pos_to_pixels(state.pos);
Prepared {
layer,
layer_id,
state,
movable,
}
@ -160,15 +160,15 @@ impl Prepared {
pub(crate) fn content_ui(&self, ctx: &Arc<Context>) -> Ui {
Ui::new(
ctx.clone(),
self.layer,
self.layer.id,
self.layer_id,
self.layer_id.id,
Rect::from_min_size(self.state.pos, Vec2::infinity()),
)
}
pub(crate) fn end(self, ctx: &Arc<Context>, content_ui: Ui) -> Response {
let Prepared {
layer,
layer_id,
mut state,
movable,
} = self;
@ -179,12 +179,17 @@ impl Prepared {
let clip_rect = Rect::everything(); // TODO: get from context
let interact_id = if movable {
Some(layer.id.with("move"))
Some(layer_id.id.with("move"))
} else {
None
};
let move_response =
ctx.interact(layer, clip_rect, rect, interact_id, Sense::click_and_drag());
let move_response = ctx.interact(
layer_id,
clip_rect,
rect,
interact_id,
Sense::click_and_drag(),
);
let input = ctx.input();
if move_response.active {
@ -215,21 +220,21 @@ impl Prepared {
state.pos = ctx.round_pos_to_pixels(state.pos);
if move_response.active
|| mouse_pressed_on_area(ctx, layer)
|| !ctx.memory().areas.visible_last_frame(&layer)
|| mouse_pressed_on_area(ctx, layer_id)
|| !ctx.memory().areas.visible_last_frame(&layer_id)
{
ctx.memory().areas.move_to_top(layer);
ctx.memory().areas.move_to_top(layer_id);
ctx.request_repaint();
}
ctx.memory().areas.set_state(layer, state);
ctx.memory().areas.set_state(layer_id, state);
move_response
}
}
fn mouse_pressed_on_area(ctx: &Context, layer: Layer) -> bool {
fn mouse_pressed_on_area(ctx: &Context, layer_id: LayerId) -> bool {
if let Some(mouse_pos) = ctx.input().mouse.pos {
ctx.input().mouse.pressed && ctx.layer_at(mouse_pos) == Some(layer)
ctx.input().mouse.pressed && ctx.layer_id_at(mouse_pos) == Some(layer_id)
} else {
false
}

View file

@ -197,7 +197,7 @@ impl<'open> Window<'open> {
}
let window_id = Id::new(title_label.text());
let area_layer = area.layer();
let area_layer_id = area.layer();
let resize_id = window_id.with("resize");
let collapsing_id = window_id.with("collapsing");
@ -226,7 +226,7 @@ impl<'open> Window<'open> {
window_interaction(
ctx,
possible,
area_layer,
area_layer_id,
window_id.with("frame_resize"),
last_frame_outer_rect,
)
@ -235,7 +235,7 @@ impl<'open> Window<'open> {
window_interaction,
ctx,
margins,
area_layer,
area_layer_id,
area.state_mut(),
resize_id,
)
@ -243,7 +243,7 @@ impl<'open> Window<'open> {
} else {
None
};
let hover_interaction = resize_hover(ctx, possible, area_layer, last_frame_outer_rect);
let hover_interaction = resize_hover(ctx, possible, area_layer_id, last_frame_outer_rect);
let mut area_content_ui = area.content_ui(ctx);
@ -346,7 +346,7 @@ struct PossibleInteractions {
#[derive(Clone, Copy, Debug)]
pub(crate) struct WindowInteraction {
pub(crate) area_layer: Layer,
pub(crate) area_layer_id: LayerId,
pub(crate) start_rect: Rect,
pub(crate) left: bool,
pub(crate) right: bool,
@ -380,7 +380,7 @@ fn interact(
window_interaction: WindowInteraction,
ctx: &Context,
margins: Vec2,
area_layer: Layer,
area_layer_id: LayerId,
area_state: &mut area::State,
resize_id: Id,
) -> Option<WindowInteraction> {
@ -397,7 +397,7 @@ fn interact(
ctx.memory().resize.insert(resize_id, resize_state);
}
ctx.memory().areas.move_to_top(area_layer);
ctx.memory().areas.move_to_top(area_layer_id);
Some(window_interaction)
}
@ -429,7 +429,7 @@ fn resize_window(ctx: &Context, window_interaction: &WindowInteraction) -> Optio
fn window_interaction(
ctx: &Context,
possible: PossibleInteractions,
area_layer: Layer,
area_layer_id: LayerId,
id: Id,
rect: Rect,
) -> Option<WindowInteraction> {
@ -444,7 +444,7 @@ fn window_interaction(
let mut window_interaction = { ctx.memory().window_interaction };
if window_interaction.is_none() {
if let Some(hover_window_interaction) = resize_hover(ctx, possible, area_layer, rect) {
if let Some(hover_window_interaction) = resize_hover(ctx, possible, area_layer_id, rect) {
hover_window_interaction.set_cursor(ctx);
if ctx.input().mouse.pressed {
ctx.memory().interaction.drag_id = Some(id);
@ -458,7 +458,7 @@ fn window_interaction(
if let Some(window_interaction) = window_interaction {
let is_active = ctx.memory().interaction.drag_id == Some(id);
if is_active && window_interaction.area_layer == area_layer {
if is_active && window_interaction.area_layer_id == area_layer_id {
return Some(window_interaction);
}
}
@ -469,12 +469,12 @@ fn window_interaction(
fn resize_hover(
ctx: &Context,
possible: PossibleInteractions,
area_layer: Layer,
area_layer_id: LayerId,
rect: Rect,
) -> Option<WindowInteraction> {
if let Some(mouse_pos) = ctx.input().mouse.pos {
if let Some(top_layer) = ctx.layer_at(mouse_pos) {
if top_layer != area_layer && top_layer.order != Order::Background {
if let Some(top_layer_id) = ctx.layer_id_at(mouse_pos) {
if top_layer_id != area_layer_id && top_layer_id.order != Order::Background {
return None; // Another window is on top here
}
}
@ -523,7 +523,7 @@ fn resize_hover(
if any_resize || possible.movable {
Some(WindowInteraction {
area_layer,
area_layer_id,
start_rect: rect,
left,
right,

View file

@ -234,13 +234,13 @@ impl Context {
fn fullscreen_ui(self: &Arc<Self>) -> Ui {
let rect = Rect::from_min_size(Default::default(), self.input().screen_size);
let id = Id::background();
let layer = Layer {
let layer_id = LayerId {
order: Order::Background,
id,
};
// Ensure we register the background area so it is painted:
self.memory().areas.set_state(
layer,
layer_id,
containers::area::State {
pos: rect.min,
size: rect.size(),
@ -248,7 +248,7 @@ impl Context {
vel: Default::default(),
},
);
Ui::new(self.clone(), layer, id, rect)
Ui::new(self.clone(), layer_id, id, rect)
}
// ---------------------------------------------------------------------
@ -304,7 +304,7 @@ impl Context {
/// Is the mouse over any Egui area?
pub fn is_mouse_over_area(&self) -> bool {
if let Some(mouse_pos) = self.input.mouse.pos {
if let Some(layer) = self.layer_at(mouse_pos) {
if let Some(layer) = self.layer_id_at(mouse_pos) {
// TODO: this currently returns false for hovering the menu bar.
// We should probably move the menu bar to its own area to fix this.
layer.order != Order::Background
@ -339,15 +339,15 @@ impl Context {
// ---------------------------------------------------------------------
pub fn layer_at(&self, pos: Pos2) -> Option<Layer> {
pub fn layer_id_at(&self, pos: Pos2) -> Option<LayerId> {
let resize_grab_radius_side = self.style().interaction.resize_grab_radius_side;
self.memory().layer_at(pos, resize_grab_radius_side)
self.memory().layer_id_at(pos, resize_grab_radius_side)
}
pub fn contains_mouse(&self, layer: Layer, clip_rect: Rect, rect: Rect) -> bool {
pub fn contains_mouse(&self, layer_id: LayerId, clip_rect: Rect, rect: Rect) -> bool {
let rect = rect.intersect(clip_rect);
if let Some(mouse_pos) = self.input.mouse.pos {
rect.contains(mouse_pos) && self.layer_at(mouse_pos) == Some(layer)
rect.contains(mouse_pos) && self.layer_id_at(mouse_pos) == Some(layer_id)
} else {
false
}
@ -356,14 +356,14 @@ impl Context {
/// Use `ui.interact` instead
pub(crate) fn interact(
self: &Arc<Self>,
layer: Layer,
layer_id: LayerId,
clip_rect: Rect,
rect: Rect,
interaction_id: Option<Id>,
sense: Sense,
) -> Response {
let interact_rect = rect.expand2(0.5 * self.style().spacing.item_spacing); // make it easier to click. TODO: nice way to do this
let hovered = self.contains_mouse(layer, clip_rect, interact_rect);
let hovered = self.contains_mouse(layer_id, clip_rect, interact_rect);
let has_kb_focus = interaction_id
.map(|id| self.memory().has_kb_focus(id))
.unwrap_or(false);
@ -498,7 +498,7 @@ impl Context {
/// ## Painting
impl Context {
pub fn debug_painter(self: &Arc<Self>) -> Painter {
Painter::new(self.clone(), Layer::debug(), self.rect())
Painter::new(self.clone(), LayerId::debug(), self.rect())
}
}

View file

@ -305,7 +305,7 @@ impl Painting {
let response = ui.interact(rect, ui.id(), Sense::drag());
let rect = response.rect;
let clip_rect = ui.clip_rect().intersect(rect); // Make sure we don't paint out of bounds
let painter = Painter::new(ui.ctx().clone(), ui.layer(), clip_rect);
let painter = Painter::new(ui.ctx().clone(), ui.layer_id(), clip_rect);
if self.lines.is_empty() {
self.lines.push(vec![]);

View file

@ -52,7 +52,7 @@ impl FractalClock {
ui.ctx().request_repaint();
}
let painter = Painter::new(ui.ctx().clone(), ui.layer(), ui.available_finite());
let painter = Painter::new(ui.ctx().clone(), ui.layer_id(), ui.available_finite());
self.fractal_ui(&painter);
Frame::popup(ui.style())

View file

@ -22,12 +22,12 @@ pub enum Order {
/// Also acts as an identifier for `Area`:s.
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Layer {
pub struct LayerId {
pub order: Order,
pub id: Id,
}
impl Layer {
impl LayerId {
pub fn debug() -> Self {
Self {
order: Order::Debug,
@ -71,26 +71,26 @@ impl PaintList {
// TODO: improve GraphicLayers
#[derive(Clone, Default)]
pub struct GraphicLayers(AHashMap<Layer, PaintList>);
pub struct GraphicLayers(AHashMap<LayerId, PaintList>);
impl GraphicLayers {
pub fn list(&mut self, layer: Layer) -> &mut PaintList {
self.0.entry(layer).or_default()
pub fn list(&mut self, layer_id: LayerId) -> &mut PaintList {
self.0.entry(layer_id).or_default()
}
pub fn drain(
&mut self,
area_order: &[Layer],
area_order: &[LayerId],
) -> impl ExactSizeIterator<Item = (Rect, PaintCmd)> {
let mut all_commands: Vec<_> = Default::default();
for layer in area_order {
if let Some(commands) = self.0.get_mut(layer) {
for layer_id in area_order {
if let Some(commands) = self.0.get_mut(layer_id) {
all_commands.extend(commands.0.drain(..));
}
}
if let Some(commands) = self.0.get_mut(&Layer::debug()) {
if let Some(commands) = self.0.get_mut(&LayerId::debug()) {
all_commands.extend(commands.0.drain(..));
}

View file

@ -7,7 +7,7 @@ use crate::{
paint::color::{Hsva, Srgba},
resize, scroll_area,
widgets::text_edit,
window, Id, Layer, Pos2, Rect,
window, Id, LayerId, Pos2, Rect,
};
/// The data that Egui persists between frames.
@ -119,16 +119,16 @@ impl Interaction {
pub struct Areas {
areas: HashMap<Id, area::State>,
/// Top is last
order: Vec<Layer>,
visible_last_frame: HashSet<Layer>,
visible_current_frame: HashSet<Layer>,
order: Vec<LayerId>,
visible_last_frame: HashSet<LayerId>,
visible_current_frame: HashSet<LayerId>,
/// When an area want to be on top, it is put in here.
/// At the end of the frame, this is used to reorder the layers.
/// This means if several layers want to be on top, they will keep their relative order.
/// So if you close three windows and then reopen them all in one frame,
/// they will all be sent to the top, but keep their previous internal order.
wants_to_be_on_top: HashSet<Layer>,
wants_to_be_on_top: HashSet<LayerId>,
}
impl Memory {
@ -142,11 +142,11 @@ impl Memory {
if let Some(window_interaction) = window_interaction {
if window_interaction.is_pure_move() {
// Throw windows because it is fun:
let area_layer = window_interaction.area_layer;
let area_state = self.areas.get(area_layer.id).cloned();
let area_layer_id = window_interaction.area_layer_id;
let area_state = self.areas.get(area_layer_id.id).cloned();
if let Some(mut area_state) = area_state {
area_state.vel = prev_input.mouse.velocity;
self.areas.set_state(area_layer, area_state);
self.areas.set_state(area_layer_id, area_state);
}
}
}
@ -157,8 +157,8 @@ impl Memory {
self.areas.end_frame();
}
pub fn layer_at(&self, pos: Pos2, resize_interact_radius_side: f32) -> Option<Layer> {
self.areas.layer_at(pos, resize_interact_radius_side)
pub fn layer_id_at(&self, pos: Pos2, resize_interact_radius_side: f32) -> Option<LayerId> {
self.areas.layer_id_at(pos, resize_interact_radius_side)
}
pub fn has_kb_focus(&self, id: Id) -> bool {
@ -216,19 +216,19 @@ impl Areas {
self.areas.get(&id)
}
pub(crate) fn order(&self) -> &[Layer] {
pub(crate) fn order(&self) -> &[LayerId] {
&self.order
}
pub(crate) fn set_state(&mut self, layer: Layer, state: area::State) {
self.visible_current_frame.insert(layer);
let did_insert = self.areas.insert(layer.id, state).is_none();
pub(crate) fn set_state(&mut self, layer_id: LayerId, state: area::State) {
self.visible_current_frame.insert(layer_id);
let did_insert = self.areas.insert(layer_id.id, state).is_none();
if did_insert {
self.order.push(layer);
self.order.push(layer_id);
}
}
pub fn layer_at(&self, pos: Pos2, resize_interact_radius_side: f32) -> Option<Layer> {
pub fn layer_id_at(&self, pos: Pos2, resize_interact_radius_side: f32) -> Option<LayerId> {
for layer in self.order.iter().rev() {
if self.is_visible(layer) {
if let Some(state) = self.areas.get(&layer.id) {
@ -246,15 +246,15 @@ impl Areas {
None
}
pub fn visible_last_frame(&self, layer: &Layer) -> bool {
self.visible_last_frame.contains(layer)
pub fn visible_last_frame(&self, layer_id: &LayerId) -> bool {
self.visible_last_frame.contains(layer_id)
}
pub fn is_visible(&self, layer: &Layer) -> bool {
self.visible_last_frame.contains(layer) || self.visible_current_frame.contains(layer)
pub fn is_visible(&self, layer_id: &LayerId) -> bool {
self.visible_last_frame.contains(layer_id) || self.visible_current_frame.contains(layer_id)
}
pub fn visible_layers(&self) -> HashSet<Layer> {
pub fn visible_layer_ids(&self) -> HashSet<LayerId> {
self.visible_last_frame
.iter()
.cloned()
@ -263,19 +263,19 @@ impl Areas {
}
pub(crate) fn visible_windows(&self) -> Vec<&area::State> {
self.visible_layers()
self.visible_layer_ids()
.iter()
.filter(|layer| layer.order == crate::layers::Order::Middle)
.filter_map(|layer| self.get(layer.id))
.collect()
}
pub fn move_to_top(&mut self, layer: Layer) {
self.visible_current_frame.insert(layer);
self.wants_to_be_on_top.insert(layer);
pub fn move_to_top(&mut self, layer_id: LayerId) {
self.visible_current_frame.insert(layer_id);
self.wants_to_be_on_top.insert(layer_id);
if self.order.iter().find(|x| **x == layer).is_none() {
self.order.push(layer);
if self.order.iter().find(|x| **x == layer_id).is_none() {
self.order.push(layer_id);
}
}

View file

@ -6,7 +6,7 @@ use crate::{
layers::PaintCmdIdx,
math::{Pos2, Rect, Vec2},
paint::{font, Fonts, PaintCmd, Stroke, TextStyle},
Context, Layer, Srgba,
Context, LayerId, Srgba,
};
/// Helper to paint shapes and text to a specific region on a specific layer.
@ -16,7 +16,7 @@ pub struct Painter {
ctx: Arc<Context>,
/// Where we paint
layer: Layer,
layer_id: LayerId,
/// Everything painted in this `Painter` will be clipped against this.
/// This means nothing outside of this rectangle will be visible on screen.
@ -24,10 +24,10 @@ pub struct Painter {
}
impl Painter {
pub fn new(ctx: Arc<Context>, layer: Layer, clip_rect: Rect) -> Self {
pub fn new(ctx: Arc<Context>, layer_id: LayerId, clip_rect: Rect) -> Self {
Self {
ctx,
layer,
layer_id,
clip_rect,
}
}
@ -37,7 +37,11 @@ impl Painter {
/// The clip-rect of the returned `Painter` will be the intersection
/// of the given rectangle and the `clip_rect()` of this `Painter`.
pub fn sub_region(&self, rect: Rect) -> Self {
Self::new(self.ctx.clone(), self.layer, rect.intersect(self.clip_rect))
Self::new(
self.ctx.clone(),
self.layer_id,
rect.intersect(self.clip_rect),
)
}
}
@ -53,8 +57,8 @@ impl Painter {
}
/// Where we paint
pub fn layer(&self) -> Layer {
self.layer
pub fn layer_id(&self) -> LayerId {
self.layer_id
}
/// Everything painted in this `Painter` will be clipped against this.
@ -93,14 +97,14 @@ impl Painter {
pub fn add(&self, paint_cmd: PaintCmd) -> PaintCmdIdx {
self.ctx
.graphics()
.list(self.layer)
.list(self.layer_id)
.add(self.clip_rect, paint_cmd)
}
pub fn extend(&self, cmds: Vec<PaintCmd>) {
self.ctx
.graphics()
.list(self.layer)
.list(self.layer_id)
.extend(self.clip_rect, cmds);
}
@ -108,7 +112,7 @@ impl Painter {
pub fn set(&self, idx: PaintCmdIdx, cmd: PaintCmd) {
self.ctx
.graphics()
.list(self.layer)
.list(self.layer_id)
.set(idx, self.clip_rect, cmd)
}
}

View file

@ -60,7 +60,7 @@ impl Ui {
// ------------------------------------------------------------------------
// Creation:
pub fn new(ctx: Arc<Context>, layer: Layer, id: Id, max_rect: Rect) -> Self {
pub fn new(ctx: Arc<Context>, layer_id: LayerId, id: Id, max_rect: Rect) -> Self {
let style = ctx.style();
let clip_rect = max_rect.expand(style.visuals.clip_rect_margin);
let layout = Layout::default();
@ -69,7 +69,7 @@ impl Ui {
let min_rect = layout.rect_from_cursor_size(cursor, min_size);
Ui {
id,
painter: Painter::new(ctx, layer, clip_rect),
painter: Painter::new(ctx, layer_id, clip_rect),
min_rect,
max_rect,
style,
@ -142,8 +142,8 @@ impl Ui {
}
/// Use this to paint stuff within this `Ui`.
pub fn layer(&self) -> Layer {
self.painter().layer()
pub fn layer_id(&self) -> LayerId {
self.painter().layer_id()
}
/// The `Input` of the `Context` associated with the `Ui`.
@ -398,12 +398,17 @@ impl Ui {
impl Ui {
pub fn interact(&self, rect: Rect, id: Id, sense: Sense) -> Response {
self.ctx()
.interact(self.layer(), self.clip_rect(), rect, Some(id), sense)
.interact(self.layer_id(), self.clip_rect(), rect, Some(id), sense)
}
pub fn interact_hover(&self, rect: Rect) -> Response {
self.ctx()
.interact(self.layer(), self.clip_rect(), rect, None, Sense::nothing())
self.ctx().interact(
self.layer_id(),
self.clip_rect(),
rect,
None,
Sense::nothing(),
)
}
pub fn hovered(&self, rect: Rect) -> bool {
@ -412,7 +417,7 @@ impl Ui {
pub fn contains_mouse(&self, rect: Rect) -> bool {
self.ctx()
.contains_mouse(self.layer(), self.clip_rect(), rect)
.contains_mouse(self.layer_id(), self.clip_rect(), rect)
}
// ------------------------------------------------------------------------