feat(startup): automatic acceptors
This commit is contained in:
@@ -19,6 +19,7 @@ use serde::Deserialize;
|
||||
use stardust_xr::schemas::flex::{deserialize, serialize};
|
||||
use stardust_xr::values::Transform;
|
||||
|
||||
use std::hash::Hash;
|
||||
use std::ops::Deref;
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
@@ -35,7 +36,7 @@ lazy_static! {
|
||||
static ref ITEM_ALIAS_REMOTE_SIGNALS: Vec<&'static str> = vec![];
|
||||
}
|
||||
|
||||
fn capture(item: &Arc<Item>, acceptor: &Arc<ItemAcceptor>) {
|
||||
pub fn capture(item: &Arc<Item>, acceptor: &Arc<ItemAcceptor>) {
|
||||
if item.captured_acceptor.lock().upgrade().is_some() {
|
||||
release(item);
|
||||
}
|
||||
@@ -65,6 +66,17 @@ pub struct TypeInfo {
|
||||
pub items: Registry<Item>,
|
||||
pub acceptors: Registry<ItemAcceptor>,
|
||||
}
|
||||
impl Hash for TypeInfo {
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
self.type_name.hash(state);
|
||||
}
|
||||
}
|
||||
impl PartialEq for TypeInfo {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.type_name == other.type_name
|
||||
}
|
||||
}
|
||||
impl Eq for TypeInfo {}
|
||||
|
||||
pub struct Item {
|
||||
node: Weak<Node>,
|
||||
@@ -269,7 +281,7 @@ impl Drop for ItemUI {
|
||||
pub struct ItemAcceptor {
|
||||
uid: String,
|
||||
node: Weak<Node>,
|
||||
type_info: &'static TypeInfo,
|
||||
pub type_info: &'static TypeInfo,
|
||||
field: Arc<Field>,
|
||||
accepted: LifeLinkedNodeMap<String>,
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::core::client::Client;
|
||||
|
||||
use super::{
|
||||
items::{ItemAcceptor, TypeInfo},
|
||||
spatial::find_spatial,
|
||||
Node,
|
||||
};
|
||||
@@ -8,15 +9,20 @@ use color_eyre::eyre::Result;
|
||||
use glam::Mat4;
|
||||
use parking_lot::Mutex;
|
||||
use rustc_hash::FxHashMap;
|
||||
use std::sync::Arc;
|
||||
use stardust_xr::schemas::flex::{deserialize, serialize};
|
||||
use std::{
|
||||
fmt::Debug,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
pub static ref DESKTOP_STARTUP_IDS: Mutex<FxHashMap<String, StartupSettings>> = Default::default();
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
#[derive(Default, Clone)]
|
||||
pub struct StartupSettings {
|
||||
pub transform: Mat4,
|
||||
pub acceptors: FxHashMap<&'static TypeInfo, Weak<ItemAcceptor>>,
|
||||
}
|
||||
impl StartupSettings {
|
||||
pub fn add_to(node: &Arc<Node>) {
|
||||
@@ -32,6 +38,22 @@ impl StartupSettings {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn add_automatic_acceptor_flex(
|
||||
node: &Node,
|
||||
calling_client: Arc<Client>,
|
||||
data: &[u8],
|
||||
) -> Result<()> {
|
||||
let acceptor_node = calling_client.get_node("Item acceptor", deserialize(data)?)?;
|
||||
let acceptor =
|
||||
acceptor_node.get_aspect("Item acceptor", "item acceptor", |n| &n.item_acceptor)?;
|
||||
let mut startup_settings = node.startup_settings.get().unwrap().lock();
|
||||
startup_settings
|
||||
.acceptors
|
||||
.insert(acceptor.type_info, Arc::downgrade(acceptor));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn generate_desktop_startup_id_flex(
|
||||
node: &Node,
|
||||
_calling_client: Arc<Client>,
|
||||
@@ -45,6 +67,21 @@ impl StartupSettings {
|
||||
Ok(data)
|
||||
}
|
||||
}
|
||||
impl Debug for StartupSettings {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("StartupSettings")
|
||||
.field("transform", &self.transform)
|
||||
.field(
|
||||
"acceptors",
|
||||
&self
|
||||
.acceptors
|
||||
.iter()
|
||||
.map(|(k, _)| k.type_name)
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_interface(client: &Arc<Client>) {
|
||||
let node = Node::create(client, "", "startup", false);
|
||||
@@ -62,6 +99,10 @@ pub fn create_startup_settings_flex(
|
||||
StartupSettings::add_to(&node);
|
||||
|
||||
node.add_local_signal("set_root", StartupSettings::set_root_flex);
|
||||
node.add_local_signal(
|
||||
"add_automatic_acceptor",
|
||||
StartupSettings::add_automatic_acceptor_flex,
|
||||
);
|
||||
node.add_local_method(
|
||||
"generate_desktop_startup_id",
|
||||
StartupSettings::generate_desktop_startup_id_flex,
|
||||
|
||||
@@ -8,7 +8,7 @@ use crate::{
|
||||
registry::Registry,
|
||||
},
|
||||
nodes::{
|
||||
items::{Item, ItemSpecialization, ItemType, TypeInfo},
|
||||
items::{self, Item, ItemSpecialization, ItemType, TypeInfo},
|
||||
spatial::Spatial,
|
||||
Node,
|
||||
},
|
||||
@@ -103,13 +103,22 @@ impl PanelItem {
|
||||
node.add_local_signal("keyboard_deactivate", PanelItem::keyboard_deactivate_flex);
|
||||
node.add_local_signal("keyboard_key_state", PanelItem::keyboard_key_state_flex);
|
||||
node.add_local_signal("resize", PanelItem::resize_flex);
|
||||
|
||||
if let Some(startup_settings) = core_surface
|
||||
.pid()
|
||||
.and_then(|pid| get_env(pid).ok())
|
||||
.and_then(|env| startup_settings(&env))
|
||||
{
|
||||
spatial.set_local_transform(startup_settings.transform);
|
||||
if let Some(acceptor) = startup_settings
|
||||
.acceptors
|
||||
.get(&*ITEM_TYPE_INFO_PANEL)
|
||||
.and_then(|acc| acc.upgrade())
|
||||
{
|
||||
items::capture(&item, &acceptor);
|
||||
}
|
||||
}
|
||||
|
||||
node
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user