mirror of
https://github.com/danbulant/mangui
synced 2026-07-06 19:30:34 +00:00
progress on slot demo
This commit is contained in:
parent
592b6f04cd
commit
ea298b9dc0
4 changed files with 71 additions and 30 deletions
|
|
@ -72,10 +72,11 @@ make_component!(
|
||||||
$|event| {
|
$|event| {
|
||||||
match event.event {
|
match event.event {
|
||||||
mangui::events::InnerEvent::MouseDown(_) => {
|
mangui::events::InnerEvent::MouseDown(_) => {
|
||||||
$test_ = true;
|
let test_ = &mut $test_;
|
||||||
|
*test_ = !*test_;
|
||||||
},
|
},
|
||||||
mangui::events::InnerEvent::MouseUp(_) => {
|
mangui::events::InnerEvent::MouseUp(_) => {
|
||||||
$test_ = false;
|
// $test_ = false;
|
||||||
},
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,14 @@
|
||||||
use std::sync::{Arc, Mutex, RwLock};
|
use std::sync::{Arc, Mutex, RwLock};
|
||||||
use mangui::nodes::layout::Layout;
|
use mangui::nodes::layout::Layout;
|
||||||
use mangui::nodes::primitives::Rectangle;
|
use mangui::nodes::primitives::Rectangle;
|
||||||
|
use mangui::WeakSharedNode;
|
||||||
use rusalka::component::Slot;
|
use rusalka::component::Slot;
|
||||||
use rusalka::store::{DerefGuardExt, ReadableStore, Signal, StoreUnsubscribe, Writable, WritableStore};
|
use rusalka::store::{DerefGuardExt, ReadableStore, Signal, StoreUnsubscribe, Writable, WritableStore};
|
||||||
|
|
||||||
pub struct SlotAcceptDemo {
|
pub struct SlotAcceptDemo {
|
||||||
comp0: rusalka::SharedNodeComponent<Layout>,
|
comp0: rusalka::SharedNodeComponent<Layout>,
|
||||||
comp1: Mutex<Option<Slot>>,
|
comp1: Arc<Mutex<Option<Slot>>>,
|
||||||
|
parent: Mutex<Option<WeakSharedNode>>,
|
||||||
selfref: rusalka::WeakSharedComponent<Self>,
|
selfref: rusalka::WeakSharedComponent<Self>,
|
||||||
attrs: ReactiveSlotAcceptDemoAttributes,
|
attrs: ReactiveSlotAcceptDemoAttributes,
|
||||||
}
|
}
|
||||||
|
|
@ -52,14 +54,30 @@ impl rusalka::component::Component for SlotAcceptDemo {
|
||||||
comp0: std::sync::Arc::new(
|
comp0: std::sync::Arc::new(
|
||||||
std::sync::RwLock::new(Layout { ..Default::default() }),
|
std::sync::RwLock::new(Layout { ..Default::default() }),
|
||||||
),
|
),
|
||||||
comp1: Mutex::new(None),
|
comp1: Arc::new(Mutex::new(None)),
|
||||||
attrs: attrs.into(),
|
attrs: attrs.into(),
|
||||||
|
parent: Mutex::new(None),
|
||||||
selfref
|
selfref
|
||||||
};
|
};
|
||||||
this
|
this
|
||||||
}
|
}
|
||||||
fn set(&mut self, attrs: Self::PartialComponentAttrs) {
|
fn set(&mut self, attrs: Self::PartialComponentAttrs) {
|
||||||
|
if let Some(__default_slot) = attrs.__default_slot {
|
||||||
|
if let Some(slot) = &self.attrs.__default_slot {
|
||||||
|
if self.parent.lock().unwrap().is_some() {
|
||||||
|
(*self.comp1.lock().unwrap().as_mut().unwrap().unmount)();
|
||||||
|
}
|
||||||
|
*self.comp1.lock().unwrap() = None;
|
||||||
|
}
|
||||||
|
if let Some(slot) = __default_slot {
|
||||||
|
*self.comp1.lock().unwrap() = Some(slot.lock().unwrap()(()));
|
||||||
|
if let Some(parent) = &self.parent.lock().unwrap().as_ref() {
|
||||||
|
if let Some(parent) = parent.upgrade() {
|
||||||
|
(*self.comp1.lock().unwrap().as_mut().unwrap().mount)(&parent, None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fn get(&self) -> &Self::ReactiveComponentAttrs {
|
fn get(&self) -> &Self::ReactiveComponentAttrs {
|
||||||
&self.attrs
|
&self.attrs
|
||||||
|
|
@ -69,6 +87,7 @@ impl rusalka::component::Component for SlotAcceptDemo {
|
||||||
parent: &mangui::SharedNode,
|
parent: &mangui::SharedNode,
|
||||||
before: Option<&mangui::SharedNode>,
|
before: Option<&mangui::SharedNode>,
|
||||||
) {
|
) {
|
||||||
|
self.parent.lock().unwrap().replace(Arc::downgrade(parent));
|
||||||
rusalka::nodes::insert(parent, &{ self.comp0.clone() }, before);
|
rusalka::nodes::insert(parent, &{ self.comp0.clone() }, before);
|
||||||
match &self.attrs.__default_slot {
|
match &self.attrs.__default_slot {
|
||||||
Some(slot) => {
|
Some(slot) => {
|
||||||
|
|
@ -79,6 +98,7 @@ impl rusalka::component::Component for SlotAcceptDemo {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn unmount(&self) {
|
fn unmount(&self) {
|
||||||
|
self.parent.lock().unwrap().take();
|
||||||
rusalka::nodes::detach(&{ self.comp0.clone() });
|
rusalka::nodes::detach(&{ self.comp0.clone() });
|
||||||
match &self.attrs.__default_slot {
|
match &self.attrs.__default_slot {
|
||||||
Some(slot) => {
|
Some(slot) => {
|
||||||
|
|
@ -98,7 +118,7 @@ struct SlotDemoSlot1 {
|
||||||
pub struct SlotDemo {
|
pub struct SlotDemo {
|
||||||
comp0: rusalka::SharedNodeComponent<Layout>,
|
comp0: rusalka::SharedNodeComponent<Layout>,
|
||||||
comp1: rusalka::SharedComponent<SlotAcceptDemo>,
|
comp1: rusalka::SharedComponent<SlotAcceptDemo>,
|
||||||
test_: std::sync::Arc<std::sync::Mutex<rusalka::store::Writable<bool>>>,
|
test_: std::sync::Arc<rusalka::store::Writable<bool>>,
|
||||||
sub0: Box<dyn StoreUnsubscribe>,
|
sub0: Box<dyn StoreUnsubscribe>,
|
||||||
selfref: rusalka::WeakSharedComponent<Self>,
|
selfref: rusalka::WeakSharedComponent<Self>,
|
||||||
attrs: ReactiveSlotDemoAttributes,
|
attrs: ReactiveSlotDemoAttributes,
|
||||||
|
|
@ -109,7 +129,7 @@ pub struct SlotDemoAttributes {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ReactiveSlotDemoAttributes {
|
pub struct ReactiveSlotDemoAttributes {
|
||||||
pub test: Arc<Mutex<Writable<f32>>>
|
pub test: Arc<Writable<f32>>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|
@ -124,17 +144,17 @@ impl From<SlotDemoAttributes> for PartialSlotDemoAttributes {
|
||||||
|
|
||||||
impl From<ReactiveSlotDemoAttributes> for SlotDemoAttributes {
|
impl From<ReactiveSlotDemoAttributes> for SlotDemoAttributes {
|
||||||
fn from(attrs: ReactiveSlotDemoAttributes) -> Self {
|
fn from(attrs: ReactiveSlotDemoAttributes) -> Self {
|
||||||
Self { test: *attrs.test.lock().unwrap().get() }
|
Self { test: *attrs.test.get() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl From<&ReactiveSlotDemoAttributes> for SlotDemoAttributes {
|
impl From<&ReactiveSlotDemoAttributes> for SlotDemoAttributes {
|
||||||
fn from(attrs: &ReactiveSlotDemoAttributes) -> Self {
|
fn from(attrs: &ReactiveSlotDemoAttributes) -> Self {
|
||||||
Self { test: *attrs.test.lock().unwrap().get() }
|
Self { test: *attrs.test.get() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl From<SlotDemoAttributes> for ReactiveSlotDemoAttributes {
|
impl From<SlotDemoAttributes> for ReactiveSlotDemoAttributes {
|
||||||
fn from(attrs: SlotDemoAttributes) -> Self {
|
fn from(attrs: SlotDemoAttributes) -> Self {
|
||||||
Self { test: Arc::new(Mutex::new(Writable::new(attrs.test))) }
|
Self { test: Arc::new(Writable::new(attrs.test)) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -149,9 +169,9 @@ impl rusalka::component::Component for SlotDemo {
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let attrs: Self::ReactiveComponentAttrs = attrs.into();
|
let attrs: Self::ReactiveComponentAttrs = attrs.into();
|
||||||
let test_: std::sync::Arc<
|
let test_: std::sync::Arc<
|
||||||
std::sync::Mutex<rusalka::store::Writable<bool>>,
|
rusalka::store::Writable<bool>,
|
||||||
> = std::sync::Arc::new(
|
> = std::sync::Arc::new(
|
||||||
std::sync::Mutex::new(rusalka::store::Writable::new(false)),
|
rusalka::store::Writable::new(false),
|
||||||
);
|
);
|
||||||
let test = attrs.test.clone();
|
let test = attrs.test.clone();
|
||||||
let this = Self {
|
let this = Self {
|
||||||
|
|
@ -172,17 +192,17 @@ impl rusalka::component::Component for SlotDemo {
|
||||||
let slot = Some(
|
let slot = Some(
|
||||||
SlotDemoSlot1 {
|
SlotDemoSlot1 {
|
||||||
comp0: std::sync::Arc::new(
|
comp0: std::sync::Arc::new(
|
||||||
std::sync::RwLock::new(Rectangle { radius: *test.clone().lock().unwrap().get(), ..Default::default() }),
|
std::sync::RwLock::new(Rectangle { radius: *test.clone().get(), ..Default::default() }),
|
||||||
),
|
),
|
||||||
sub0: {
|
sub0: {
|
||||||
let comp0 = comp0.clone();
|
let comp0 = comp0.clone();
|
||||||
let test = test.clone();
|
let test = test.clone();
|
||||||
[test.clone().lock().unwrap()].subscribe(Box::new(move || {
|
[test.clone()].subscribe(Box::new(move || {
|
||||||
let comp1 = comp0.clone();
|
let comp1 = comp0.clone();
|
||||||
let test1 = test.clone();
|
let test1 = test.clone();
|
||||||
let mut comp1l = comp1.lock().unwrap();
|
let mut comp1l = comp1.lock().unwrap();
|
||||||
if let Some(comp1) = comp1l.as_mut() {
|
if let Some(comp1) = comp1l.as_mut() {
|
||||||
comp1.comp0.write().unwrap().radius = *test1.lock().unwrap().get();
|
comp1.comp0.write().unwrap().radius = *test1.get();
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
},
|
},
|
||||||
|
|
@ -211,9 +231,9 @@ impl rusalka::component::Component for SlotDemo {
|
||||||
)),
|
)),
|
||||||
sub0: {
|
sub0: {
|
||||||
let test = test_.clone();
|
let test = test_.clone();
|
||||||
[test.clone().lock().unwrap()].subscribe(Box::new(move || {
|
[test.clone()].subscribe(Box::new(move || {
|
||||||
let test = test.clone();
|
let test = test.clone();
|
||||||
dbg!(test.lock().unwrap().get());
|
dbg!(test.get());
|
||||||
}))
|
}))
|
||||||
},
|
},
|
||||||
attrs,
|
attrs,
|
||||||
|
|
@ -233,10 +253,10 @@ impl rusalka::component::Component for SlotDemo {
|
||||||
let test_ = &this.test_;
|
let test_ = &this.test_;
|
||||||
match event.event {
|
match event.event {
|
||||||
mangui::events::InnerEvent::MouseDown(_) => {
|
mangui::events::InnerEvent::MouseDown(_) => {
|
||||||
**test_.lock().unwrap().guard() = true;
|
**test_.guard() = true;
|
||||||
}
|
}
|
||||||
mangui::events::InnerEvent::MouseUp(_) => {
|
mangui::events::InnerEvent::MouseUp(_) => {
|
||||||
**test_.lock().unwrap().guard() = false;
|
**test_.guard() = false;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
@ -246,8 +266,7 @@ impl rusalka::component::Component for SlotDemo {
|
||||||
}
|
}
|
||||||
fn set(&mut self, attrs: Self::PartialComponentAttrs) {
|
fn set(&mut self, attrs: Self::PartialComponentAttrs) {
|
||||||
if let Some(test) = attrs.test {
|
if let Some(test) = attrs.test {
|
||||||
**self.attrs.test.lock().unwrap().guard() = test;
|
**self.attrs.test.guard() = test;
|
||||||
// self.attrs.test.lock().unwrap().set(test);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn get(&self) -> &Self::ReactiveComponentAttrs {
|
fn get(&self) -> &Self::ReactiveComponentAttrs {
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ struct EventListener {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum ComponentType {
|
enum ComponentType {
|
||||||
|
SlotDefinition,
|
||||||
RealComponent,
|
RealComponent,
|
||||||
Node
|
Node
|
||||||
}
|
}
|
||||||
|
|
@ -293,15 +294,24 @@ pub fn make_component(item: proc_macro::TokenStream) -> proc_macro::TokenStream
|
||||||
},
|
},
|
||||||
ComponentType::Node => {
|
ComponentType::Node => {
|
||||||
midstream.extend(quote!(SharedNodeComponent<));
|
midstream.extend(quote!(SharedNodeComponent<));
|
||||||
|
},
|
||||||
|
ComponentType::SlotDefinition => {
|
||||||
|
midstream.extend(quote!(SharedSlot));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
match component.component_type {
|
||||||
|
ComponentType::RealComponent | ComponentType::Node => {
|
||||||
|
let component_name = component.name.clone();
|
||||||
|
|
||||||
|
component_struct_stream.extend(Some(TokenTree::Ident(ident)));
|
||||||
|
component_struct_stream.extend(midstream);
|
||||||
|
component_struct_stream.extend(Some(TokenTree::Ident(component_name)));
|
||||||
|
component_struct_stream.extend(quote!(>,));
|
||||||
|
},
|
||||||
|
ComponentType::SlotDefinition => {}
|
||||||
|
}
|
||||||
|
|
||||||
let component_name = component.name.clone();
|
|
||||||
|
|
||||||
component_struct_stream.extend(Some(TokenTree::Ident(ident)));
|
|
||||||
component_struct_stream.extend(midstream);
|
|
||||||
component_struct_stream.extend(Some(TokenTree::Ident(component_name)));
|
|
||||||
component_struct_stream.extend(quote!(>,));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
component_struct_stream.extend(quote!(
|
component_struct_stream.extend(quote!(
|
||||||
|
|
@ -515,10 +525,10 @@ pub fn make_component(item: proc_macro::TokenStream) -> proc_macro::TokenStream
|
||||||
|
|
||||||
let mut component_stream = TokenStream::new();
|
let mut component_stream = TokenStream::new();
|
||||||
|
|
||||||
component_stream.extend(Some(TokenTree::Ident(component_name.clone())));
|
|
||||||
|
|
||||||
match component.component_type {
|
match component.component_type {
|
||||||
ComponentType::RealComponent => {
|
ComponentType::RealComponent => {
|
||||||
|
component_stream.extend(Some(TokenTree::Ident(component_name.clone())));
|
||||||
|
|
||||||
component_stream.extend(quote!(::new));
|
component_stream.extend(quote!(::new));
|
||||||
|
|
||||||
let mut component_new_stream = TokenStream::new();
|
let mut component_new_stream = TokenStream::new();
|
||||||
|
|
@ -544,10 +554,15 @@ pub fn make_component(item: proc_macro::TokenStream) -> proc_macro::TokenStream
|
||||||
new_returnvalue_stream.extend(wrap_in_arcmutex_cyclic(component_stream));
|
new_returnvalue_stream.extend(wrap_in_arcmutex_cyclic(component_stream));
|
||||||
},
|
},
|
||||||
ComponentType::Node => {
|
ComponentType::Node => {
|
||||||
|
component_stream.extend(Some(TokenTree::Ident(component_name.clone())));
|
||||||
|
|
||||||
let (_reactive_variables, subcomponent_stream) = replace_variables(component.contents.clone());
|
let (_reactive_variables, subcomponent_stream) = replace_variables(component.contents.clone());
|
||||||
let node_group = Group::new(Delimiter::Brace, subcomponent_stream);
|
let node_group = Group::new(Delimiter::Brace, subcomponent_stream);
|
||||||
component_stream.extend(Some(TokenTree::Group(node_group)));
|
component_stream.extend(Some(TokenTree::Group(node_group)));
|
||||||
new_returnvalue_stream.extend(wrap_in_arcrwlock(component_stream));
|
new_returnvalue_stream.extend(wrap_in_arcrwlock(component_stream));
|
||||||
|
},
|
||||||
|
ComponentType::SlotDefinition => {
|
||||||
|
component_stream.extend(quote!(Arc::new(Mutex::new(None))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
new_returnvalue_stream.extend(quote!(,));
|
new_returnvalue_stream.extend(quote!(,));
|
||||||
|
|
@ -594,6 +609,8 @@ pub fn make_component(item: proc_macro::TokenStream) -> proc_macro::TokenStream
|
||||||
for component in &components_used {
|
for component in &components_used {
|
||||||
match component.component_type {
|
match component.component_type {
|
||||||
ComponentType::RealComponent => continue,
|
ComponentType::RealComponent => continue,
|
||||||
|
// currently just ignore those, in the future it should probably be an error.
|
||||||
|
ComponentType::SlotDefinition => continue,
|
||||||
ComponentType::Node => {}
|
ComponentType::Node => {}
|
||||||
};
|
};
|
||||||
for event_listener in &component.event_listeners {
|
for event_listener in &component.event_listeners {
|
||||||
|
|
@ -706,6 +723,7 @@ pub fn make_component(item: proc_macro::TokenStream) -> proc_macro::TokenStream
|
||||||
let ident = Ident::new(&format!("comp{}", i), component.name.span());
|
let ident = Ident::new(&format!("comp{}", i), component.name.span());
|
||||||
|
|
||||||
match component.component_type {
|
match component.component_type {
|
||||||
|
ComponentType::SlotDefinition => continue,
|
||||||
ComponentType::RealComponent => {
|
ComponentType::RealComponent => {
|
||||||
// mount
|
// mount
|
||||||
mount_stream.extend(quote!(self.));
|
mount_stream.extend(quote!(self.));
|
||||||
|
|
@ -805,6 +823,7 @@ pub fn make_component(item: proc_macro::TokenStream) -> proc_macro::TokenStream
|
||||||
let ident = Ident::new(&format!("comp{}", i), component.name.span());
|
let ident = Ident::new(&format!("comp{}", i), component.name.span());
|
||||||
|
|
||||||
match component.component_type {
|
match component.component_type {
|
||||||
|
ComponentType::SlotDefinition => continue,
|
||||||
ComponentType::RealComponent => {
|
ComponentType::RealComponent => {
|
||||||
let mut component_stream = TokenStream::new();
|
let mut component_stream = TokenStream::new();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ use std::sync::{Arc, Mutex, RwLock};
|
||||||
|
|
||||||
use component::Component;
|
use component::Component;
|
||||||
use mangui::nodes::Node;
|
use mangui::nodes::Node;
|
||||||
|
use crate::component::Slot;
|
||||||
|
|
||||||
pub mod component;
|
pub mod component;
|
||||||
pub mod nodes;
|
pub mod nodes;
|
||||||
|
|
@ -9,4 +10,5 @@ pub mod store;
|
||||||
|
|
||||||
pub type SharedComponent<T: Component> = Arc<Mutex<T>>;
|
pub type SharedComponent<T: Component> = Arc<Mutex<T>>;
|
||||||
pub type WeakSharedComponent<T: Component> = std::sync::Weak<Mutex<T>>;
|
pub type WeakSharedComponent<T: Component> = std::sync::Weak<Mutex<T>>;
|
||||||
pub type SharedNodeComponent<T: Node> = Arc<RwLock<T>>;
|
pub type SharedNodeComponent<T: Node> = Arc<RwLock<T>>;
|
||||||
|
pub type SharedSlot = Arc<Mutex<Option<Slot>>>;
|
||||||
Loading…
Reference in a new issue