Refactored VARS APP integration.
This commit is contained in:
parent
3a1536c7be
commit
d5e3a8d19b
|
@ -36,6 +36,7 @@ WindowManager//WindowId(1) update var of type zero_ui_units::factor::Factor (250
|
|||
```
|
||||
- tracing permanently disables callsite if there no interest on the first usage, so need to build with some
|
||||
kind of subscriber from the start.
|
||||
- Or could delay log calls until enabled, our own thing.
|
||||
|
||||
# Hit-test
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ use std::{
|
|||
|
||||
use zero_ui_app_context::{app_local, AppScope};
|
||||
use zero_ui_layout::units::Deadline;
|
||||
use zero_ui_var::{response_var, ResponderVar, ResponseVar, VARS};
|
||||
use zero_ui_var::{response_var, ResponderVar, ResponseVar, VARS, VARS_APP};
|
||||
|
||||
use crate::{
|
||||
event::{
|
||||
|
@ -59,9 +59,11 @@ impl<E: AppExtension> RunningApp<E> {
|
|||
|
||||
UPDATES.init(sender);
|
||||
|
||||
VARS.init_app_waker(|| {
|
||||
fn app_waker() {
|
||||
UPDATES.update(None);
|
||||
});
|
||||
}
|
||||
VARS_APP.init_app_waker(app_waker);
|
||||
VARS_APP.init_modify_trace(UpdatesTrace::log_var);
|
||||
|
||||
let mut info = AppExtensionsInfo::start();
|
||||
extensions.register(&mut info);
|
||||
|
|
|
@ -782,6 +782,7 @@ impl UpdatesTrace {
|
|||
|
||||
/// Log a var update request.
|
||||
pub fn log_var(type_name: &str) {
|
||||
dbg!(type_name);
|
||||
tracing::event!(
|
||||
target: UpdatesTrace::UPDATES_TARGET,
|
||||
tracing::Level::TRACE,
|
||||
|
|
|
@ -48,6 +48,7 @@ pub(crate) struct VarsService {
|
|||
updates_after: Mutex<Vec<(ModifyInfo, VarUpdateFn)>>,
|
||||
|
||||
app_waker: Option<Box<dyn Fn() + Send + Sync>>,
|
||||
modify_trace: Option<Box<dyn Fn(&'static str) + Send + Sync>>,
|
||||
}
|
||||
impl VarsService {
|
||||
pub(crate) fn new() -> Self {
|
||||
|
@ -58,6 +59,7 @@ impl VarsService {
|
|||
updating_thread: None,
|
||||
updates_after: Mutex::new(vec![]),
|
||||
app_waker: None,
|
||||
modify_trace: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -239,17 +241,10 @@ impl VARS {
|
|||
}
|
||||
|
||||
pub(super) fn schedule_update(&self, update: VarUpdateFn, type_name: &'static str) {
|
||||
{
|
||||
// same as UpdatesTrace::log_var(type_name), without the dependency.
|
||||
const UPDATES_TARGET: &str = "zero-ui-updates";
|
||||
tracing::event!(
|
||||
target: UPDATES_TARGET,
|
||||
tracing::Level::TRACE,
|
||||
{ kind = "update var", type_name = pretty_type_name::pretty_type_name_str(type_name) }
|
||||
);
|
||||
}
|
||||
|
||||
let vars = VARS_SV.read();
|
||||
if let Some(trace) = &vars.modify_trace {
|
||||
trace(type_name);
|
||||
}
|
||||
let curr_modify = match VARS_MODIFY_CTX.get_clone() {
|
||||
Some(current) => current, // override set by modify and animation closures.
|
||||
None => vars.ans.current_modify.clone(),
|
||||
|
@ -270,19 +265,6 @@ impl VARS {
|
|||
}
|
||||
}
|
||||
|
||||
/// Register a closure called when [`apply_updates`] should be called because there are changes pending.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if already called for the current app. This must be called by app framework implementers only.
|
||||
///
|
||||
/// [`apply_updates`]: Self::apply_updates
|
||||
pub fn init_app_waker(&self, waker: impl Fn() + Send + Sync + 'static) {
|
||||
let mut vars = VARS_SV.write();
|
||||
assert!(vars.app_waker.is_none());
|
||||
vars.app_waker = Some(Box::new(waker));
|
||||
}
|
||||
|
||||
pub(crate) fn wake_app(&self) {
|
||||
VARS_SV.read().wake_app();
|
||||
}
|
||||
|
@ -382,3 +364,33 @@ impl VARS {
|
|||
!VARS_SV.write().updates.get_mut().is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// VARS APP integration.
|
||||
#[allow(non_camel_case_types)]
|
||||
pub struct VARS_APP;
|
||||
impl VARS_APP {
|
||||
/// Register a closure called when [`apply_updates`] should be called because there are changes pending.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if already called for the current app. This must be called by app framework implementers only.
|
||||
///
|
||||
/// [`apply_updates`]: Self::apply_updates
|
||||
pub fn init_app_waker(&self, waker: impl Fn() + Send + Sync + 'static) {
|
||||
let mut vars = VARS_SV.write();
|
||||
assert!(vars.app_waker.is_none());
|
||||
vars.app_waker = Some(Box::new(waker));
|
||||
}
|
||||
|
||||
/// Register a closure called when a variable modify is about to be scheduled. The
|
||||
/// closure parameter is the type name of the variable type.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if already called for the current app. This must be called by app framework implementers only.
|
||||
pub fn init_modify_trace(&self, trace: impl Fn(&'static str) + Send + Sync + 'static) {
|
||||
let mut vars = VARS_SV.write();
|
||||
assert!(vars.modify_trace.is_none());
|
||||
vars.modify_trace = Some(Box::new(trace));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue