Skip to content

Commit

Permalink
get time millis through engine
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelselleck committed Sep 30, 2024
1 parent bc15d61 commit 008e8e2
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 19 deletions.
1 change: 1 addition & 0 deletions pax-chassis-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ console_log = "1.0.0"
log = "0.4.20"
console_error_panic_hook = { version = "0.1.6", optional = true }
js-sys = "0.3.63"
web-time = "1.1.0"

[dependencies.web-sys]
version = "0.3.10"
Expand Down
14 changes: 10 additions & 4 deletions pax-chassis-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use pax_runtime::DefinitionToInstanceTraverser;
use pax_runtime_api::borrow_mut;
use pax_runtime_api::Event;
use pax_runtime_api::Focus;
use web_time::Duration;
use web_time::Instant;
use_RefCell!();

use std::rc::Rc;
Expand Down Expand Up @@ -75,7 +77,7 @@ impl PaxChassisWeb {
userland_definition_to_instance_traverser: Box<dyn DefinitionToInstanceTraverser>,
designer_definition_to_instance_traverser: Box<dyn DefinitionToInstanceTraverser>,
) -> Self {
let (width, height, os_info) = Self::init_common();
let (width, height, os_info, get_ellapsed_millis) = Self::init_common();
let query_string = window()
.unwrap()
.location()
Expand All @@ -97,6 +99,7 @@ impl PaxChassisWeb {
designtime_manager.clone(),
Platform::Web,
os_info,
get_ellapsed_millis,
);
let engine_container: Rc<RefCell<PaxEngine>> = Rc::new(RefCell::new(engine));
Self {
Expand All @@ -112,7 +115,7 @@ impl PaxChassisWeb {
pub async fn new(
definition_to_instance_traverser: Box<dyn DefinitionToInstanceTraverser>,
) -> Self {
let (width, height, os_info) = Self::init_common();
let (width, height, os_info, get_time) = Self::init_common();

let main_component_instance =
definition_to_instance_traverser.get_main_component(USERLAND_COMPONENT_ROOT);
Expand All @@ -121,6 +124,7 @@ impl PaxChassisWeb {
(width, height),
Platform::Web,
os_info,
get_time,
);

let engine_container: Rc<RefCell<PaxEngine>> = Rc::new(RefCell::new(engine));
Expand All @@ -131,7 +135,7 @@ impl PaxChassisWeb {
}
}

fn init_common() -> (f64, f64, OS) {
fn init_common() -> (f64, f64, OS, Box<dyn Fn() -> u128>) {
let window = window().unwrap();
let user_agent_str = window.navigator().user_agent().ok();
let os_info = user_agent_str
Expand All @@ -140,7 +144,9 @@ impl PaxChassisWeb {

let width = window.inner_width().unwrap().as_f64().unwrap();
let height = window.inner_height().unwrap().as_f64().unwrap();
(width, height, os_info)
let start = Instant::now();
let get_time = Box::new(move || start.elapsed().as_millis());
(width, height, os_info, get_time)
}
}

Expand Down
1 change: 0 additions & 1 deletion pax-designer/src/glass/mod.pax
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
@settings {
@mount: on_mount,
@tick: on_pre_render,
@double_click: handle_double_click
@context_menu: self.context_menu

#project {
Expand Down
20 changes: 15 additions & 5 deletions pax-designer/src/glass/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::rc::Rc;
use std::time::Instant;

use anyhow::anyhow;
use pax_engine::api::Fill;
use pax_engine::api::*;
Expand Down Expand Up @@ -45,6 +48,8 @@ pub struct Glass {
pub on_tool_change: Property<bool>,
// used to make scroller containers open if manifest version changed
pub scroller_manifest_version_listener: Property<bool>,
// TODO this needs to track real time, not frame count, eventually
pub time_last_click: Property<u64>,
}

impl Glass {
Expand Down Expand Up @@ -114,11 +119,9 @@ impl Glass {
args.prevent_default();
}

pub fn handle_double_click(&mut self, ctx: &NodeContext, event: Event<DoubleClick>) {
// if a ControlPoint was double clicked, don't handle glass double click
if event.cancelled() {
return;
}
// NOTE: This is NOT triggered by engine - we are handling our own double click behavior
// by checking time in self.handle_mouse_down
pub fn handle_double_click(&mut self, ctx: &NodeContext, event: &Event<MouseDown>) {
// TODO move to another file (need to figure out structure)
let info = model::read_app_state(|app_state| {
let selected_nodes = app_state.selected_template_node_ids.get();
Expand Down Expand Up @@ -193,6 +196,13 @@ impl Glass {
}

pub fn handle_mouse_down(&mut self, ctx: &NodeContext, args: Event<MouseDown>) {
let last_time = self.time_last_click.get();
let curr_time = ctx.ellapsed_time_millis() as u64;
if (curr_time - last_time) < 500 {
self.handle_double_click(ctx, &args);
}
self.time_last_click.set(curr_time);

let prevent_default = || args.prevent_default();
model::perform_action(
&crate::model::action::pointer::MouseEntryPointAction {
Expand Down
29 changes: 21 additions & 8 deletions pax-designer/src/model/action/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,30 @@ impl Action for MouseEntryPointAction<'_> {
match res {
std::ops::ControlFlow::Continue(_) => false,
std::ops::ControlFlow::Break(_) => {
// TODO this could most likely be done in a nicer way:
// make a tool "stack", and return to last tool here instead
if let Err(e) = tool.finish(ctx) {
log::warn!("finishing tool failed: {e}");
};
ctx.app_state
.selected_tool
.set(match ctx.app_state.unit_mode.get() {
SizeUnit::Pixels => Tool::PointerPixels,
SizeUnit::Percent => Tool::PointerPercent,
});
// TODO this could most likely be done in a nicer way:
// make a tool "stack", and return to last tool here
// instead. How to handle ToolBehavior vs Toolbar state
// if doing this? (needs to be synced). Associate each
// ToolBehavior with a Tool in toolbar?
// TODO might want to make a toolbar click immediately
// activate a tool, and then make the tool itself handle
// mousedown. (would allow for easier way for tool
// settings view to show on tool selection)
match ctx.app_state.selected_tool.get() {
Tool::Paintbrush => (),
_ => {
ctx.app_state.selected_tool.set(
match ctx.app_state.unit_mode.get() {
SizeUnit::Pixels => Tool::PointerPixels,
SizeUnit::Percent => Tool::PointerPercent,
},
);
}
}

true
}
}
Expand Down
3 changes: 3 additions & 0 deletions pax-runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::{HashMap, HashSet, VecDeque};
use std::fmt::Display;
use std::ops::{Add, Deref, Mul, Neg, Sub};
use std::time::Instant;

use crate::math::Space;
use kurbo::BezPath;
Expand Down Expand Up @@ -916,6 +917,8 @@ impl<I: Interpolatable> Interpolatable for Vec<I> {
}
}

impl Interpolatable for Instant {}

impl Interpolatable for char {}

impl Interpolatable for f64 {
Expand Down
11 changes: 10 additions & 1 deletion pax-runtime/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::rc::{Rc, Weak};
use std::{
rc::{Rc, Weak},
time::Instant,
};

use_RefCell!();
use crate::{
Expand Down Expand Up @@ -42,6 +45,7 @@ pub struct NodeContext {

#[cfg(feature = "designtime")]
pub designtime: Rc<RefCell<DesigntimeManager>>,
pub(crate) get_ellapsed_millis: Rc<dyn Fn() -> u128>,
}

impl NodeContext {
Expand All @@ -57,6 +61,11 @@ impl NodeContext {
self.node_transform_and_bounds.as_transform().inverse() * p
}

/// Get std::time::Instant::now()
pub fn ellapsed_time_millis(&self) -> u128 {
(self.get_ellapsed_millis)()
}

pub fn navigate_to(&self, url: &str, target: NavigationTarget) {
self.runtime_context
.enqueue_native_message(NativeMessage::Navigate(NavigationPatch {
Expand Down
2 changes: 2 additions & 0 deletions pax-runtime/src/engine/expanded_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ impl ExpandedNode {
_context: &Rc<RuntimeContext>,
) {
*borrow_mut!(self.instance_node) = Rc::clone(&template);
log::debug!("recreate");
(&template
.base()
.instance_prototypical_common_properties_factory)(
Expand Down Expand Up @@ -634,6 +635,7 @@ impl ExpandedNode {
runtime_context: ctx.clone(),
platform: globals.platform.clone(),
os: globals.os.clone(),
get_ellapsed_millis: globals.get_ellapsed_millis,
slot_children_count,
node_transform_and_bounds: self.transform_and_bounds.get(),
#[cfg(feature = "designtime")]
Expand Down
6 changes: 6 additions & 0 deletions pax-runtime/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use piet::InterpolationMode;

use crate::{ComponentInstance, RuntimeContext};
use pax_runtime_api::Platform;
use std::time::Instant;

pub mod node_interface;
pub mod occlusion;
Expand Down Expand Up @@ -44,6 +45,7 @@ pub struct Globals {
pub os: OS,
#[cfg(feature = "designtime")]
pub designtime: Rc<RefCell<DesigntimeManager>>,
pub get_ellapsed_millis: Rc<dyn Fn() -> u128>,
}

impl Globals {
Expand Down Expand Up @@ -257,6 +259,7 @@ impl PaxEngine {
viewport_size: (f64, f64),
platform: Platform,
os: OS,
get_ellapsed_millis: Box<dyn Fn() -> u128>,
) -> Self {
use crate::api::math::Transform2;
use pax_runtime_api::{properties, Functions};
Expand All @@ -272,6 +275,7 @@ impl PaxEngine {
}),
platform,
os,
get_ellapsed_millis: Rc::from(get_ellapsed_millis),
};
let runtime_context = Rc::new(RuntimeContext::new(globals));
let root_node =
Expand All @@ -292,6 +296,7 @@ impl PaxEngine {
designtime: Rc<RefCell<DesigntimeManager>>,
platform: Platform,
os: OS,
get_ellapsed_millis: Box<dyn Fn() -> u128>,
) -> Self {
use pax_runtime_api::{math::Transform2, properties, Functions};
Functions::register_all_functions();
Expand All @@ -307,6 +312,7 @@ impl PaxEngine {
platform,
os,
designtime: designtime.clone(),
get_ellapsed_millis: Rc::from(get_ellapsed_millis),
};

let mut runtime_context = Rc::new(RuntimeContext::new(
Expand Down

0 comments on commit 008e8e2

Please sign in to comment.