Render a context menu when right-clicking in project panel

It doesn't currently do anything, but I managed to get it rendering in an absolutely positioned way.
This commit is contained in:
Nathan Sobo 2022-05-24 18:30:04 -06:00 committed by Antonio Scandurra
parent f4d13ef596
commit b110fd5fb7
23 changed files with 260 additions and 91 deletions

View file

@ -1,16 +1,28 @@
use serde_json::json;
use crate::{
geometry::{rect::RectF, vector::Vector2F},
json::ToJson,
DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, PaintContext,
SizeConstraint,
};
pub struct Overlay {
child: ElementBox,
abs_position: Option<Vector2F>,
}
impl Overlay {
pub fn new(child: ElementBox) -> Self {
Self { child }
Self {
child,
abs_position: None,
}
}
pub fn with_abs_position(mut self, position: Vector2F) -> Self {
self.abs_position = Some(position);
self
}
}
@ -23,6 +35,11 @@ impl Element for Overlay {
constraint: SizeConstraint,
cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) {
let constraint = if self.abs_position.is_some() {
SizeConstraint::new(Vector2F::zero(), cx.window_size)
} else {
constraint
};
let size = self.child.layout(constraint, cx);
(Vector2F::zero(), size)
}
@ -34,9 +51,10 @@ impl Element for Overlay {
size: &mut Self::LayoutState,
cx: &mut PaintContext,
) {
let bounds = RectF::new(bounds.origin(), *size);
let origin = self.abs_position.unwrap_or(bounds.origin());
let visible_bounds = RectF::new(origin, *size);
cx.scene.push_stacking_context(None);
self.child.paint(bounds.origin(), bounds, cx);
self.child.paint(origin, visible_bounds, cx);
cx.scene.pop_stacking_context();
}
@ -59,6 +77,10 @@ impl Element for Overlay {
_: &Self::PaintState,
cx: &DebugContext,
) -> serde_json::Value {
self.child.debug(cx)
json!({
"type": "Overlay",
"abs_position": self.abs_position.to_json(),
"child": self.child.debug(cx),
})
}
}