Make statehandle a weak reference
This commit is contained in:
parent
8599d7bb0e
commit
9e28804a38
1 changed files with 32 additions and 18 deletions
|
@ -5,7 +5,7 @@ use assetpath::AssetPath;
|
|||
use std::any::Any;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use std::sync::{Arc, Mutex, RwLock};
|
||||
use std::sync::{Arc, Mutex, RwLock, Weak};
|
||||
|
||||
pub trait FutureStateChange: Fn() -> Result<()> + Send + Sync {
|
||||
fn clone_box<'a>(&self) -> Box<dyn 'a + FutureStateChange>
|
||||
|
@ -45,74 +45,88 @@ struct State {
|
|||
/// only used for updating callbacks
|
||||
#[derive(Clone)]
|
||||
pub struct StateHandle {
|
||||
state: Arc<State>,
|
||||
state: Weak<State>,
|
||||
}
|
||||
|
||||
impl StateHandle {
|
||||
pub fn update<'a>(&self, update_type: StateUpdateType<'a>) -> Result<()> {
|
||||
self.state.update(update_type)
|
||||
self.state.upgrade().unwrap().update(update_type)
|
||||
}
|
||||
}
|
||||
|
||||
impl GetElement<Button> for StateHandle {
|
||||
fn element(&self, id: &str) -> Result<Arc<Button>> {
|
||||
match self.state.top_level_gui.elements() {
|
||||
let state = self.state.upgrade().unwrap();
|
||||
|
||||
match state.top_level_gui.elements() {
|
||||
Some(elements) => elements.element(id),
|
||||
None => panic!("state ({}) has no elements", self.state.name),
|
||||
None => panic!("state ({}) has no elements", state.name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GetElement<Grid> for StateHandle {
|
||||
fn element(&self, id: &str) -> Result<Arc<Grid>> {
|
||||
match self.state.top_level_gui.elements() {
|
||||
let state = self.state.upgrade().unwrap();
|
||||
|
||||
match state.top_level_gui.elements() {
|
||||
Some(elements) => elements.element(id),
|
||||
None => panic!("state ({}) has no elements", self.state.name),
|
||||
None => panic!("state ({}) has no elements", state.name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GetElement<Label> for StateHandle {
|
||||
fn element(&self, id: &str) -> Result<Arc<Label>> {
|
||||
match self.state.top_level_gui.elements() {
|
||||
let state = self.state.upgrade().unwrap();
|
||||
|
||||
match state.top_level_gui.elements() {
|
||||
Some(elements) => elements.element(id),
|
||||
None => panic!("state ({}) has no elements", self.state.name),
|
||||
None => panic!("state ({}) has no elements", state.name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GetElement<TextField> for StateHandle {
|
||||
fn element(&self, id: &str) -> Result<Arc<TextField>> {
|
||||
match self.state.top_level_gui.elements() {
|
||||
let state = self.state.upgrade().unwrap();
|
||||
|
||||
match state.top_level_gui.elements() {
|
||||
Some(elements) => elements.element(id),
|
||||
None => panic!("state ({}) has no elements", self.state.name),
|
||||
None => panic!("state ({}) has no elements", state.name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GetElement<MultiLineTextField> for StateHandle {
|
||||
fn element(&self, id: &str) -> Result<Arc<MultiLineTextField>> {
|
||||
match self.state.top_level_gui.elements() {
|
||||
let state = self.state.upgrade().unwrap();
|
||||
|
||||
match state.top_level_gui.elements() {
|
||||
Some(elements) => elements.element(id),
|
||||
None => panic!("state ({}) has no elements", self.state.name),
|
||||
None => panic!("state ({}) has no elements", state.name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GetElement<Icon> for StateHandle {
|
||||
fn element(&self, id: &str) -> Result<Arc<Icon>> {
|
||||
match self.state.top_level_gui.elements() {
|
||||
let state = self.state.upgrade().unwrap();
|
||||
|
||||
match state.top_level_gui.elements() {
|
||||
Some(elements) => elements.element(id),
|
||||
None => panic!("state ({}) has no elements", self.state.name),
|
||||
None => panic!("state ({}) has no elements", state.name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GetElement<ProgressBar> for StateHandle {
|
||||
fn element(&self, id: &str) -> Result<Arc<ProgressBar>> {
|
||||
match self.state.top_level_gui.elements() {
|
||||
let state = self.state.upgrade().unwrap();
|
||||
|
||||
match state.top_level_gui.elements() {
|
||||
Some(elements) => elements.element(id),
|
||||
None => panic!("state ({}) has no elements", self.state.name),
|
||||
None => panic!("state ({}) has no elements", state.name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -302,7 +316,7 @@ impl States {
|
|||
/// Retrieve a StateHandle
|
||||
pub fn state_handle(&self, id: &str) -> Result<StateHandle> {
|
||||
Ok(StateHandle {
|
||||
state: self.get_state(id)?,
|
||||
state: Arc::downgrade(&self.get_state(id)?),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue