2021-03-10 04:00:51 +00:00
|
|
|
use crate::{
|
|
|
|
app::{AppContext, MutableAppContext, WindowInvalidation},
|
|
|
|
elements::Element,
|
2021-03-18 19:13:31 +00:00
|
|
|
fonts::FontCache,
|
2021-03-24 15:51:28 +00:00
|
|
|
platform::{self, Event},
|
2021-03-18 19:13:31 +00:00
|
|
|
text_layout::TextLayoutCache,
|
2021-03-22 02:54:23 +00:00
|
|
|
AssetCache, ElementBox, Scene,
|
2021-03-10 04:00:51 +00:00
|
|
|
};
|
|
|
|
use pathfinder_geometry::vector::{vec2f, Vector2F};
|
2021-03-19 03:33:16 +00:00
|
|
|
use std::{any::Any, collections::HashMap, sync::Arc};
|
2021-03-10 04:00:51 +00:00
|
|
|
|
|
|
|
pub struct Presenter {
|
|
|
|
window_id: usize,
|
2021-03-22 02:54:23 +00:00
|
|
|
rendered_views: HashMap<usize, ElementBox>,
|
2021-03-10 04:00:51 +00:00
|
|
|
parents: HashMap<usize, usize>,
|
2021-03-19 03:33:16 +00:00
|
|
|
font_cache: Arc<FontCache>,
|
2021-03-18 19:13:31 +00:00
|
|
|
text_layout_cache: TextLayoutCache,
|
2021-03-19 03:33:16 +00:00
|
|
|
asset_cache: Arc<AssetCache>,
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Presenter {
|
|
|
|
pub fn new(
|
|
|
|
window_id: usize,
|
2021-03-19 03:33:16 +00:00
|
|
|
font_cache: Arc<FontCache>,
|
2021-03-25 09:42:46 +00:00
|
|
|
text_layout_cache: TextLayoutCache,
|
2021-03-19 03:33:16 +00:00
|
|
|
asset_cache: Arc<AssetCache>,
|
2021-03-10 04:00:51 +00:00
|
|
|
app: &MutableAppContext,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
window_id,
|
|
|
|
rendered_views: app.render_views(window_id).unwrap(),
|
|
|
|
parents: HashMap::new(),
|
|
|
|
font_cache,
|
2021-03-25 09:42:46 +00:00
|
|
|
text_layout_cache,
|
2021-03-10 04:00:51 +00:00
|
|
|
asset_cache,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 17:38:23 +00:00
|
|
|
pub fn dispatch_path(&self, app: &AppContext) -> Vec<usize> {
|
|
|
|
let mut view_id = app.focused_view_id(self.window_id).unwrap();
|
|
|
|
let mut path = vec![view_id];
|
|
|
|
while let Some(parent_id) = self.parents.get(&view_id).copied() {
|
|
|
|
path.push(parent_id);
|
|
|
|
view_id = parent_id;
|
|
|
|
}
|
|
|
|
path.reverse();
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2021-03-19 03:33:16 +00:00
|
|
|
pub fn invalidate(&mut self, invalidation: WindowInvalidation, app: &AppContext) {
|
2021-03-10 04:00:51 +00:00
|
|
|
for view_id in invalidation.updated {
|
|
|
|
self.rendered_views
|
|
|
|
.insert(view_id, app.render_view(self.window_id, view_id).unwrap());
|
|
|
|
}
|
|
|
|
for view_id in invalidation.removed {
|
|
|
|
self.rendered_views.remove(&view_id);
|
|
|
|
self.parents.remove(&view_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build_scene(
|
|
|
|
&mut self,
|
|
|
|
window_size: Vector2F,
|
|
|
|
scale_factor: f32,
|
|
|
|
app: &mut MutableAppContext,
|
|
|
|
) -> Scene {
|
2021-03-19 19:31:25 +00:00
|
|
|
let mut scene = Scene::new(scale_factor);
|
|
|
|
|
|
|
|
if let Some(root_view_id) = app.root_view_id(self.window_id) {
|
|
|
|
self.layout(window_size, app.downgrade());
|
|
|
|
self.after_layout(app);
|
2021-03-22 02:54:23 +00:00
|
|
|
let mut ctx = PaintContext {
|
2021-03-19 19:31:25 +00:00
|
|
|
scene: &mut scene,
|
|
|
|
font_cache: &self.font_cache,
|
|
|
|
text_layout_cache: &self.text_layout_cache,
|
|
|
|
rendered_views: &mut self.rendered_views,
|
2021-03-22 02:54:23 +00:00
|
|
|
app: app.downgrade(),
|
2021-03-19 19:31:25 +00:00
|
|
|
};
|
2021-03-22 02:54:23 +00:00
|
|
|
ctx.paint(root_view_id, Vector2F::zero());
|
2021-03-19 19:31:25 +00:00
|
|
|
self.text_layout_cache.finish_frame();
|
|
|
|
} else {
|
|
|
|
log::error!("could not find root_view_id for window {}", self.window_id);
|
|
|
|
}
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
scene
|
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(&mut self, size: Vector2F, app: &AppContext) {
|
|
|
|
if let Some(root_view_id) = app.root_view_id(self.window_id) {
|
|
|
|
let mut layout_ctx = LayoutContext {
|
|
|
|
rendered_views: &mut self.rendered_views,
|
|
|
|
parents: &mut self.parents,
|
|
|
|
font_cache: &self.font_cache,
|
|
|
|
text_layout_cache: &self.text_layout_cache,
|
|
|
|
asset_cache: &self.asset_cache,
|
|
|
|
view_stack: Vec::new(),
|
2021-03-22 02:54:23 +00:00
|
|
|
app,
|
2021-03-10 04:00:51 +00:00
|
|
|
};
|
2021-03-22 02:54:23 +00:00
|
|
|
layout_ctx.layout(root_view_id, SizeConstraint::strict(size));
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn after_layout(&mut self, app: &mut MutableAppContext) {
|
|
|
|
if let Some(root_view_id) = app.root_view_id(self.window_id) {
|
|
|
|
let mut ctx = AfterLayoutContext {
|
|
|
|
rendered_views: &mut self.rendered_views,
|
|
|
|
font_cache: &self.font_cache,
|
|
|
|
text_layout_cache: &self.text_layout_cache,
|
2021-03-22 02:54:23 +00:00
|
|
|
app,
|
2021-03-10 04:00:51 +00:00
|
|
|
};
|
2021-03-22 02:54:23 +00:00
|
|
|
ctx.after_layout(root_view_id);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 02:54:23 +00:00
|
|
|
pub fn dispatch_event(&mut self, event: Event, app: &AppContext) -> Vec<ActionToDispatch> {
|
2021-03-10 04:00:51 +00:00
|
|
|
if let Some(root_view_id) = app.root_view_id(self.window_id) {
|
2021-03-22 02:54:23 +00:00
|
|
|
let mut ctx = EventContext {
|
|
|
|
rendered_views: &mut self.rendered_views,
|
|
|
|
actions: Vec::new(),
|
|
|
|
font_cache: &self.font_cache,
|
|
|
|
text_layout_cache: &self.text_layout_cache,
|
|
|
|
view_stack: Vec::new(),
|
|
|
|
app,
|
|
|
|
};
|
|
|
|
ctx.dispatch_event(root_view_id, &event);
|
|
|
|
ctx.actions
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 15:44:14 +00:00
|
|
|
pub struct ActionToDispatch {
|
|
|
|
pub path: Vec<usize>,
|
|
|
|
pub name: &'static str,
|
|
|
|
pub arg: Box<dyn Any>,
|
|
|
|
}
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
pub struct LayoutContext<'a> {
|
2021-03-22 02:54:23 +00:00
|
|
|
rendered_views: &'a mut HashMap<usize, ElementBox>,
|
2021-03-10 04:00:51 +00:00
|
|
|
parents: &'a mut HashMap<usize, usize>,
|
|
|
|
pub font_cache: &'a FontCache,
|
2021-03-18 19:13:31 +00:00
|
|
|
pub text_layout_cache: &'a TextLayoutCache,
|
2021-03-10 04:00:51 +00:00
|
|
|
pub asset_cache: &'a AssetCache,
|
2021-03-22 02:54:23 +00:00
|
|
|
pub app: &'a AppContext,
|
2021-03-10 04:00:51 +00:00
|
|
|
view_stack: Vec<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> LayoutContext<'a> {
|
2021-03-22 02:54:23 +00:00
|
|
|
fn layout(&mut self, view_id: usize, constraint: SizeConstraint) -> Vector2F {
|
2021-03-10 04:00:51 +00:00
|
|
|
if let Some(parent_id) = self.view_stack.last() {
|
|
|
|
self.parents.insert(view_id, *parent_id);
|
|
|
|
}
|
|
|
|
self.view_stack.push(view_id);
|
|
|
|
let mut rendered_view = self.rendered_views.remove(&view_id).unwrap();
|
2021-03-22 02:54:23 +00:00
|
|
|
let size = rendered_view.layout(constraint, self);
|
2021-03-10 04:00:51 +00:00
|
|
|
self.rendered_views.insert(view_id, rendered_view);
|
|
|
|
self.view_stack.pop();
|
|
|
|
size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AfterLayoutContext<'a> {
|
2021-03-22 02:54:23 +00:00
|
|
|
rendered_views: &'a mut HashMap<usize, ElementBox>,
|
2021-03-10 04:00:51 +00:00
|
|
|
pub font_cache: &'a FontCache,
|
2021-03-18 19:13:31 +00:00
|
|
|
pub text_layout_cache: &'a TextLayoutCache,
|
2021-03-22 02:54:23 +00:00
|
|
|
pub app: &'a mut MutableAppContext,
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> AfterLayoutContext<'a> {
|
2021-03-22 02:54:23 +00:00
|
|
|
fn after_layout(&mut self, view_id: usize) {
|
2021-03-10 04:00:51 +00:00
|
|
|
if let Some(mut view) = self.rendered_views.remove(&view_id) {
|
2021-03-22 02:54:23 +00:00
|
|
|
view.after_layout(self);
|
2021-03-10 04:00:51 +00:00
|
|
|
self.rendered_views.insert(view_id, view);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct PaintContext<'a> {
|
2021-03-22 02:54:23 +00:00
|
|
|
rendered_views: &'a mut HashMap<usize, ElementBox>,
|
2021-03-19 19:31:25 +00:00
|
|
|
pub scene: &'a mut Scene,
|
2021-03-10 04:00:51 +00:00
|
|
|
pub font_cache: &'a FontCache,
|
2021-03-18 19:13:31 +00:00
|
|
|
pub text_layout_cache: &'a TextLayoutCache,
|
2021-03-22 02:54:23 +00:00
|
|
|
pub app: &'a AppContext,
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PaintContext<'a> {
|
2021-03-22 02:54:23 +00:00
|
|
|
fn paint(&mut self, view_id: usize, origin: Vector2F) {
|
2021-03-10 04:00:51 +00:00
|
|
|
if let Some(mut tree) = self.rendered_views.remove(&view_id) {
|
2021-03-22 02:54:23 +00:00
|
|
|
tree.paint(origin, self);
|
2021-03-10 04:00:51 +00:00
|
|
|
self.rendered_views.insert(view_id, tree);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct EventContext<'a> {
|
2021-03-22 02:54:23 +00:00
|
|
|
rendered_views: &'a mut HashMap<usize, ElementBox>,
|
2021-03-21 15:44:14 +00:00
|
|
|
actions: Vec<ActionToDispatch>,
|
2021-03-10 04:00:51 +00:00
|
|
|
pub font_cache: &'a FontCache,
|
2021-03-18 19:13:31 +00:00
|
|
|
pub text_layout_cache: &'a TextLayoutCache,
|
2021-03-22 02:54:23 +00:00
|
|
|
pub app: &'a AppContext,
|
2021-03-10 04:00:51 +00:00
|
|
|
view_stack: Vec<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> EventContext<'a> {
|
2021-03-22 02:54:23 +00:00
|
|
|
fn dispatch_event(&mut self, view_id: usize, event: &Event) -> bool {
|
|
|
|
if let Some(mut element) = self.rendered_views.remove(&view_id) {
|
2021-03-10 04:00:51 +00:00
|
|
|
self.view_stack.push(view_id);
|
2021-03-22 02:54:23 +00:00
|
|
|
let result = element.dispatch_event(event, self);
|
2021-03-10 04:00:51 +00:00
|
|
|
self.view_stack.pop();
|
2021-03-22 02:54:23 +00:00
|
|
|
self.rendered_views.insert(view_id, element);
|
2021-03-10 04:00:51 +00:00
|
|
|
result
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn dispatch_action<A: 'static + Any>(&mut self, name: &'static str, arg: A) {
|
2021-03-21 15:44:14 +00:00
|
|
|
self.actions.push(ActionToDispatch {
|
|
|
|
path: self.view_stack.clone(),
|
|
|
|
name,
|
|
|
|
arg: Box::new(arg),
|
|
|
|
});
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
pub enum Axis {
|
|
|
|
Horizontal,
|
|
|
|
Vertical,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Axis {
|
|
|
|
pub fn invert(self) -> Self {
|
|
|
|
match self {
|
|
|
|
Self::Horizontal => Self::Vertical,
|
|
|
|
Self::Vertical => Self::Horizontal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Vector2FExt {
|
|
|
|
fn along(self, axis: Axis) -> f32;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Vector2FExt for Vector2F {
|
|
|
|
fn along(self, axis: Axis) -> f32 {
|
|
|
|
match axis {
|
|
|
|
Axis::Horizontal => self.x(),
|
|
|
|
Axis::Vertical => self.y(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct SizeConstraint {
|
|
|
|
pub min: Vector2F,
|
|
|
|
pub max: Vector2F,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SizeConstraint {
|
|
|
|
pub fn new(min: Vector2F, max: Vector2F) -> Self {
|
|
|
|
Self { min, max }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn strict(size: Vector2F) -> Self {
|
|
|
|
Self {
|
|
|
|
min: size,
|
|
|
|
max: size,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn strict_along(axis: Axis, max: f32) -> Self {
|
|
|
|
match axis {
|
|
|
|
Axis::Horizontal => Self {
|
|
|
|
min: vec2f(max, 0.0),
|
|
|
|
max: vec2f(max, f32::INFINITY),
|
|
|
|
},
|
|
|
|
Axis::Vertical => Self {
|
|
|
|
min: vec2f(0.0, max),
|
|
|
|
max: vec2f(f32::INFINITY, max),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn max_along(&self, axis: Axis) -> f32 {
|
|
|
|
match axis {
|
|
|
|
Axis::Horizontal => self.max.x(),
|
|
|
|
Axis::Vertical => self.max.y(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ChildView {
|
|
|
|
view_id: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChildView {
|
|
|
|
pub fn new(view_id: usize) -> Self {
|
2021-03-22 02:54:23 +00:00
|
|
|
Self { view_id }
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Element for ChildView {
|
2021-03-22 02:54:23 +00:00
|
|
|
type LayoutState = ();
|
|
|
|
type PaintState = ();
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
fn layout(
|
|
|
|
&mut self,
|
|
|
|
constraint: SizeConstraint,
|
|
|
|
ctx: &mut LayoutContext,
|
2021-03-22 02:54:23 +00:00
|
|
|
) -> (Vector2F, Self::LayoutState) {
|
|
|
|
let size = ctx.layout(self.view_id, constraint);
|
|
|
|
(size, ())
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:54:23 +00:00
|
|
|
fn after_layout(
|
|
|
|
&mut self,
|
|
|
|
_: Vector2F,
|
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
ctx: &mut AfterLayoutContext,
|
|
|
|
) {
|
|
|
|
ctx.after_layout(self.view_id);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:54:23 +00:00
|
|
|
fn paint(
|
|
|
|
&mut self,
|
|
|
|
bounds: pathfinder_geometry::rect::RectF,
|
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
ctx: &mut PaintContext,
|
|
|
|
) -> Self::PaintState {
|
|
|
|
ctx.paint(self.view_id, bounds.origin());
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:54:23 +00:00
|
|
|
fn dispatch_event(
|
|
|
|
&mut self,
|
|
|
|
event: &Event,
|
|
|
|
_: pathfinder_geometry::rect::RectF,
|
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
_: &mut Self::PaintState,
|
|
|
|
ctx: &mut EventContext,
|
|
|
|
) -> bool {
|
|
|
|
ctx.dispatch_event(self.view_id, event)
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
// #[test]
|
|
|
|
// fn test_responder_chain() {
|
|
|
|
// let settings = settings_rx(None);
|
|
|
|
// let mut app = App::new().unwrap();
|
|
|
|
// let workspace = app.add_model(|ctx| Workspace::new(Vec::new(), ctx));
|
|
|
|
// let (window_id, workspace_view) =
|
|
|
|
// app.add_window(|ctx| WorkspaceView::new(workspace.clone(), settings, ctx));
|
|
|
|
|
|
|
|
// let invalidations = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
// let invalidations_ = invalidations.clone();
|
|
|
|
// app.on_window_invalidated(window_id, move |invalidation, _| {
|
|
|
|
// invalidations_.borrow_mut().push(invalidation)
|
|
|
|
// });
|
|
|
|
|
|
|
|
// let active_pane_id = workspace_view.update(&mut app, |view, ctx| {
|
|
|
|
// ctx.focus(view.active_pane());
|
|
|
|
// view.active_pane().id()
|
|
|
|
// });
|
|
|
|
|
|
|
|
// app.update(|app| {
|
|
|
|
// let mut presenter = Presenter::new(
|
|
|
|
// window_id,
|
|
|
|
// Rc::new(FontCache::new()),
|
|
|
|
// Rc::new(AssetCache::new()),
|
|
|
|
// app,
|
|
|
|
// );
|
|
|
|
// for invalidation in invalidations.borrow().iter().cloned() {
|
|
|
|
// presenter.update(vec2f(1024.0, 768.0), 2.0, Some(invalidation), app);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// assert_eq!(
|
|
|
|
// presenter.responder_chain(app.ctx()).unwrap(),
|
|
|
|
// vec![workspace_view.id(), active_pane_id]
|
|
|
|
// );
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
}
|