WIP: Return WindowHandle<V: View> from AppContext::add_window
This commit is contained in:
@@ -23,8 +23,10 @@ struct ElementStateRefCount {
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct RefCounts {
|
||||
window_counts: HashMap<usize, usize>,
|
||||
entity_counts: HashMap<usize, usize>,
|
||||
element_state_counts: HashMap<ElementStateId, ElementStateRefCount>,
|
||||
dropped_windows: HashSet<usize>,
|
||||
dropped_models: HashSet<usize>,
|
||||
dropped_views: HashSet<(usize, usize)>,
|
||||
dropped_element_states: HashSet<ElementStateId>,
|
||||
@@ -43,6 +45,18 @@ impl RefCounts {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inc_window(&mut self, window_id: usize) {
|
||||
match self.window_counts.entry(window_id) {
|
||||
Entry::Occupied(mut entry) => {
|
||||
*entry.get_mut() += 1;
|
||||
}
|
||||
Entry::Vacant(entry) => {
|
||||
entry.insert(1);
|
||||
self.dropped_windows.remove(&window_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inc_model(&mut self, model_id: usize) {
|
||||
match self.entity_counts.entry(model_id) {
|
||||
Entry::Occupied(mut entry) => {
|
||||
@@ -85,6 +99,15 @@ impl RefCounts {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dec_window(&mut self, window_id: usize) {
|
||||
let count = self.window_counts.get_mut(&window_id).unwrap();
|
||||
*count -= 1;
|
||||
if *count == 0 {
|
||||
self.entity_counts.remove(&window_id);
|
||||
self.dropped_windows.insert(window_id);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dec_model(&mut self, model_id: usize) {
|
||||
let count = self.entity_counts.get_mut(&model_id).unwrap();
|
||||
*count -= 1;
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::{
|
||||
platform::{Event, InputHandler, KeyDownEvent, Platform},
|
||||
Action, AppContext, BorrowAppContext, BorrowWindowContext, Entity, FontCache, Handle,
|
||||
ModelContext, ModelHandle, Subscription, Task, View, ViewContext, ViewHandle, WeakHandle,
|
||||
WindowContext,
|
||||
WindowContext, WindowHandle,
|
||||
};
|
||||
use collections::BTreeMap;
|
||||
use futures::Future;
|
||||
@@ -148,17 +148,18 @@ impl TestAppContext {
|
||||
self.cx.borrow_mut().add_model(build_model)
|
||||
}
|
||||
|
||||
pub fn add_window<T, F>(&mut self, build_root_view: F) -> (usize, ViewHandle<T>)
|
||||
pub fn add_window<T, F>(&mut self, build_root_view: F) -> WindowHandle<T>
|
||||
where
|
||||
T: View,
|
||||
F: FnOnce(&mut ViewContext<T>) -> T,
|
||||
{
|
||||
let (window_id, view) = self
|
||||
let window = self
|
||||
.cx
|
||||
.borrow_mut()
|
||||
.add_window(Default::default(), build_root_view);
|
||||
self.simulate_window_activation(Some(window_id));
|
||||
(window_id, view)
|
||||
self.simulate_window_activation(Some(window.id()));
|
||||
|
||||
WindowHandle::new(window.id(), self.cx.borrow_mut().ref_counts.clone())
|
||||
}
|
||||
|
||||
pub fn add_view<T, F>(&mut self, window_id: usize, build_view: F) -> ViewHandle<T>
|
||||
|
||||
Reference in New Issue
Block a user