#![cfg_attr(not(feature = "full"), allow(dead_code, unused_imports))]
use std::{cell::Cell, rc::Rc};
use gpui::{
anchored, deferred, div, point, prelude::*, px, size, AnyElement, App, AvailableSpace, Bounds,
Corner, Global, GlobalElementId, InspectorElementId, LayoutId, Pixels, Point, Window,
};
enum PortalAnchor {
Fixed(Point<Pixels>),
Marker(Rc<Cell<Point<Pixels>>>),
}
struct Portal {
anchor: PortalAnchor,
corner: Corner,
margin: Pixels,
content: AnyElement,
}
#[derive(Default)]
struct LayerContext {
depth: usize,
portals: Vec<Portal>,
}
impl Global for LayerContext {}
fn inside_layer(cx: &App) -> bool {
cx.try_global::<LayerContext>()
.is_some_and(|context| context.depth > 0)
}
fn within_layer<R>(cx: &mut App, body: impl FnOnce(&mut App) -> R) -> R {
cx.default_global::<LayerContext>().depth += 1;
let result = body(cx);
cx.default_global::<LayerContext>().depth -= 1;
result
}
fn take_portals(cx: &mut App) -> Vec<Portal> {
std::mem::take(&mut cx.default_global::<LayerContext>().portals)
}
#[doc(hidden)]
pub fn layer_at(
position: Point<Pixels>,
corner: Corner,
margin: Pixels,
content: impl IntoElement,
priority: usize,
_cx: &mut App,
) -> AnyElement {
LayerSlot {
anchor: SlotAnchor::Fixed(position),
corner,
margin,
priority,
content: Some(content.into_any_element()),
inner: None,
marker: None,
}
.into_any_element()
}
#[doc(hidden)]
pub fn layer_at_marker(corner: Corner, content: impl IntoElement, _cx: &mut App) -> AnyElement {
LayerSlot {
anchor: SlotAnchor::Here,
corner,
margin: px(8.),
priority: 1,
content: Some(content.into_any_element()),
inner: None,
marker: None,
}
.into_any_element()
}
enum SlotAnchor {
Fixed(Point<Pixels>),
Here,
}
struct LayerSlot {
anchor: SlotAnchor,
corner: Corner,
margin: Pixels,
priority: usize,
content: Option<AnyElement>,
inner: Option<AnyElement>,
marker: Option<Rc<Cell<Point<Pixels>>>>,
}
impl IntoElement for LayerSlot {
type Element = Self;
fn into_element(self) -> Self::Element {
self
}
}
impl Element for LayerSlot {
type RequestLayoutState = ();
type PrepaintState = ();
fn id(&self) -> Option<gpui::ElementId> {
None
}
fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
None
}
fn request_layout(
&mut self,
_id: Option<&GlobalElementId>,
_inspector_id: Option<&InspectorElementId>,
window: &mut Window,
cx: &mut App,
) -> (LayoutId, Self::RequestLayoutState) {
let content = self
.content
.take()
.unwrap_or_else(|| div().into_any_element());
let mut inner = if inside_layer(cx) {
let anchor = match self.anchor {
SlotAnchor::Fixed(position) => PortalAnchor::Fixed(position),
SlotAnchor::Here => {
let marker = Rc::new(Cell::new(Point::default()));
self.marker = Some(marker.clone());
PortalAnchor::Marker(marker)
}
};
cx.default_global::<LayerContext>().portals.push(Portal {
anchor,
corner: self.corner,
margin: self.margin,
content,
});
div().absolute().size_0().into_any_element()
} else {
let anchored = anchored()
.anchor(self.corner)
.snap_to_window_with_margin(self.margin);
let anchored = match self.anchor {
SlotAnchor::Fixed(position) => anchored.position(position),
SlotAnchor::Here => anchored,
};
deferred(LayerRoot::new(anchored.child(content)))
.with_priority(self.priority)
.into_any_element()
};
let layout_id = inner.request_layout(window, cx);
self.inner = Some(inner);
(layout_id, ())
}
fn prepaint(
&mut self,
_id: Option<&GlobalElementId>,
_inspector_id: Option<&InspectorElementId>,
bounds: Bounds<Pixels>,
_request_layout: &mut Self::RequestLayoutState,
window: &mut Window,
cx: &mut App,
) -> Self::PrepaintState {
if let Some(marker) = self.marker.as_ref() {
marker.set(bounds.origin);
}
if let Some(inner) = self.inner.as_mut() {
inner.prepaint(window, cx);
}
}
fn paint(
&mut self,
_id: Option<&GlobalElementId>,
_inspector_id: Option<&InspectorElementId>,
_bounds: Bounds<Pixels>,
_request_layout: &mut Self::RequestLayoutState,
_prepaint: &mut Self::PrepaintState,
window: &mut Window,
cx: &mut App,
) {
if let Some(inner) = self.inner.as_mut() {
inner.paint(window, cx);
}
}
}
#[doc(hidden)]
#[must_use]
pub fn place_in_viewport(
anchor: Point<Pixels>,
corner: Corner,
content_size: gpui::Size<Pixels>,
viewport: gpui::Size<Pixels>,
margin: Pixels,
) -> Point<Pixels> {
let mut origin = match corner {
Corner::TopLeft => anchor,
Corner::TopRight => point(anchor.x - content_size.width, anchor.y),
Corner::BottomLeft => point(anchor.x, anchor.y - content_size.height),
Corner::BottomRight => point(
anchor.x - content_size.width,
anchor.y - content_size.height,
),
};
if origin.x + content_size.width > viewport.width - margin {
origin.x = viewport.width - margin - content_size.width;
}
if origin.y + content_size.height > viewport.height - margin {
origin.y = viewport.height - margin - content_size.height;
}
origin.x = origin.x.max(margin);
origin.y = origin.y.max(margin);
origin
}
#[doc(hidden)]
pub struct LayerRoot {
content: AnyElement,
pending: Vec<Portal>,
portals: Vec<AnyElement>,
}
impl LayerRoot {
fn new(content: impl IntoElement) -> Self {
Self {
content: content.into_any_element(),
pending: Vec::new(),
portals: Vec::new(),
}
}
}
impl IntoElement for LayerRoot {
type Element = Self;
fn into_element(self) -> Self::Element {
self
}
}
impl Element for LayerRoot {
type RequestLayoutState = ();
type PrepaintState = ();
fn id(&self) -> Option<gpui::ElementId> {
None
}
fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
None
}
fn request_layout(
&mut self,
_id: Option<&GlobalElementId>,
_inspector_id: Option<&InspectorElementId>,
window: &mut Window,
cx: &mut App,
) -> (LayoutId, Self::RequestLayoutState) {
let outer = take_portals(cx);
let layout_id = within_layer(cx, |cx| self.content.request_layout(window, cx));
self.pending = take_portals(cx);
cx.default_global::<LayerContext>().portals = outer;
(layout_id, ())
}
fn prepaint(
&mut self,
_id: Option<&GlobalElementId>,
_inspector_id: Option<&InspectorElementId>,
_bounds: Bounds<Pixels>,
_request_layout: &mut Self::RequestLayoutState,
window: &mut Window,
cx: &mut App,
) -> Self::PrepaintState {
within_layer(cx, |cx| {
let outer = take_portals(cx);
let mut pending = std::mem::take(&mut self.pending);
self.content.prepaint(window, cx);
pending.extend(take_portals(cx));
let viewport = window.viewport_size();
while !pending.is_empty() {
for mut portal in std::mem::take(&mut pending) {
let anchor = match &portal.anchor {
PortalAnchor::Fixed(position) => *position,
PortalAnchor::Marker(recorded) => recorded.get(),
};
let content_size = portal.content.layout_as_root(
size(AvailableSpace::MinContent, AvailableSpace::MinContent),
window,
cx,
);
let origin = place_in_viewport(
anchor,
portal.corner,
content_size,
viewport,
portal.margin,
);
portal.content.prepaint_at(origin, window, cx);
self.portals.push(portal.content);
pending.extend(take_portals(cx));
}
}
cx.default_global::<LayerContext>().portals = outer;
});
}
fn paint(
&mut self,
_id: Option<&GlobalElementId>,
_inspector_id: Option<&InspectorElementId>,
_bounds: Bounds<Pixels>,
_request_layout: &mut Self::RequestLayoutState,
_prepaint: &mut Self::PrepaintState,
window: &mut Window,
cx: &mut App,
) {
self.content.paint(window, cx);
for portal in &mut self.portals {
portal.paint(window, cx);
}
}
}
#[cfg(test)]
mod tests {
use gpui::{point, px, size, Corner};
use super::place_in_viewport;
#[test]
fn layers_stay_inside_the_window() {
let viewport = size(px(800.), px(600.));
let menu = size(px(200.), px(300.));
assert_eq!(
place_in_viewport(
point(px(100.), px(100.)),
Corner::TopLeft,
menu,
viewport,
px(8.)
),
point(px(100.), px(100.))
);
assert_eq!(
place_in_viewport(
point(px(700.), px(100.)),
Corner::TopLeft,
menu,
viewport,
px(8.)
)
.x,
px(592.)
);
assert_eq!(
place_in_viewport(
point(px(400.), px(100.)),
Corner::TopRight,
menu,
viewport,
px(8.)
)
.x,
px(200.)
);
assert_eq!(
place_in_viewport(
point(px(100.), px(500.)),
Corner::TopLeft,
menu,
viewport,
px(8.)
)
.y,
px(292.)
);
}
}