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::schemas::flex::{deserialize, serialize};
|
||||||
use stardust_xr::values::Transform;
|
use stardust_xr::values::Transform;
|
||||||
|
|
||||||
|
use std::hash::Hash;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::sync::{Arc, Weak};
|
use std::sync::{Arc, Weak};
|
||||||
|
|
||||||
@@ -35,7 +36,7 @@ lazy_static! {
|
|||||||
static ref ITEM_ALIAS_REMOTE_SIGNALS: Vec<&'static str> = vec![];
|
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() {
|
if item.captured_acceptor.lock().upgrade().is_some() {
|
||||||
release(item);
|
release(item);
|
||||||
}
|
}
|
||||||
@@ -65,6 +66,17 @@ pub struct TypeInfo {
|
|||||||
pub items: Registry<Item>,
|
pub items: Registry<Item>,
|
||||||
pub acceptors: Registry<ItemAcceptor>,
|
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 {
|
pub struct Item {
|
||||||
node: Weak<Node>,
|
node: Weak<Node>,
|
||||||
@@ -269,7 +281,7 @@ impl Drop for ItemUI {
|
|||||||
pub struct ItemAcceptor {
|
pub struct ItemAcceptor {
|
||||||
uid: String,
|
uid: String,
|
||||||
node: Weak<Node>,
|
node: Weak<Node>,
|
||||||
type_info: &'static TypeInfo,
|
pub type_info: &'static TypeInfo,
|
||||||
field: Arc<Field>,
|
field: Arc<Field>,
|
||||||
accepted: LifeLinkedNodeMap<String>,
|
accepted: LifeLinkedNodeMap<String>,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use crate::core::client::Client;
|
use crate::core::client::Client;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
|
items::{ItemAcceptor, TypeInfo},
|
||||||
spatial::find_spatial,
|
spatial::find_spatial,
|
||||||
Node,
|
Node,
|
||||||
};
|
};
|
||||||
@@ -8,15 +9,20 @@ use color_eyre::eyre::Result;
|
|||||||
use glam::Mat4;
|
use glam::Mat4;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use rustc_hash::FxHashMap;
|
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! {
|
lazy_static::lazy_static! {
|
||||||
pub static ref DESKTOP_STARTUP_IDS: Mutex<FxHashMap<String, StartupSettings>> = Default::default();
|
pub static ref DESKTOP_STARTUP_IDS: Mutex<FxHashMap<String, StartupSettings>> = Default::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Default, Clone)]
|
||||||
pub struct StartupSettings {
|
pub struct StartupSettings {
|
||||||
pub transform: Mat4,
|
pub transform: Mat4,
|
||||||
|
pub acceptors: FxHashMap<&'static TypeInfo, Weak<ItemAcceptor>>,
|
||||||
}
|
}
|
||||||
impl StartupSettings {
|
impl StartupSettings {
|
||||||
pub fn add_to(node: &Arc<Node>) {
|
pub fn add_to(node: &Arc<Node>) {
|
||||||
@@ -32,6 +38,22 @@ impl StartupSettings {
|
|||||||
Ok(())
|
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(
|
fn generate_desktop_startup_id_flex(
|
||||||
node: &Node,
|
node: &Node,
|
||||||
_calling_client: Arc<Client>,
|
_calling_client: Arc<Client>,
|
||||||
@@ -45,6 +67,21 @@ impl StartupSettings {
|
|||||||
Ok(data)
|
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>) {
|
pub fn create_interface(client: &Arc<Client>) {
|
||||||
let node = Node::create(client, "", "startup", false);
|
let node = Node::create(client, "", "startup", false);
|
||||||
@@ -62,6 +99,10 @@ pub fn create_startup_settings_flex(
|
|||||||
StartupSettings::add_to(&node);
|
StartupSettings::add_to(&node);
|
||||||
|
|
||||||
node.add_local_signal("set_root", StartupSettings::set_root_flex);
|
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(
|
node.add_local_method(
|
||||||
"generate_desktop_startup_id",
|
"generate_desktop_startup_id",
|
||||||
StartupSettings::generate_desktop_startup_id_flex,
|
StartupSettings::generate_desktop_startup_id_flex,
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use crate::{
|
|||||||
registry::Registry,
|
registry::Registry,
|
||||||
},
|
},
|
||||||
nodes::{
|
nodes::{
|
||||||
items::{Item, ItemSpecialization, ItemType, TypeInfo},
|
items::{self, Item, ItemSpecialization, ItemType, TypeInfo},
|
||||||
spatial::Spatial,
|
spatial::Spatial,
|
||||||
Node,
|
Node,
|
||||||
},
|
},
|
||||||
@@ -103,13 +103,22 @@ impl PanelItem {
|
|||||||
node.add_local_signal("keyboard_deactivate", PanelItem::keyboard_deactivate_flex);
|
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("keyboard_key_state", PanelItem::keyboard_key_state_flex);
|
||||||
node.add_local_signal("resize", PanelItem::resize_flex);
|
node.add_local_signal("resize", PanelItem::resize_flex);
|
||||||
|
|
||||||
if let Some(startup_settings) = core_surface
|
if let Some(startup_settings) = core_surface
|
||||||
.pid()
|
.pid()
|
||||||
.and_then(|pid| get_env(pid).ok())
|
.and_then(|pid| get_env(pid).ok())
|
||||||
.and_then(|env| startup_settings(&env))
|
.and_then(|env| startup_settings(&env))
|
||||||
{
|
{
|
||||||
spatial.set_local_transform(startup_settings.transform);
|
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
|
node
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user