Skip to content

Commit

Permalink
Properly drop the top-level adapter struct
Browse files Browse the repository at this point in the history
  • Loading branch information
DataTriny committed Dec 27, 2023
1 parent 40efae9 commit ecddbd7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
20 changes: 16 additions & 4 deletions platforms/unix/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@ impl TreeChangeHandler for AdapterChangeHandler<'_> {
}
}

static NEXT_ADAPTER_ID: AtomicUsize = AtomicUsize::new(0);

pub(crate) struct AdapterImpl {
id: usize,
messages: Sender<Message>,
Expand All @@ -208,13 +206,13 @@ pub(crate) struct AdapterImpl {

impl AdapterImpl {
fn new(
id: usize,
initial_state: TreeUpdate,
is_window_focused: bool,
root_window_bounds: WindowBounds,
action_handler: Box<dyn ActionHandler + Send>,
) -> Self {
let tree = Tree::new(initial_state, is_window_focused);
let id = NEXT_ADAPTER_ID.fetch_add(1, Ordering::SeqCst);
let (messages, context) = {
let mut app_context = AppContext::write();
let messages = app_context.messages.clone().unwrap();
Expand Down Expand Up @@ -430,7 +428,10 @@ impl Drop for AdapterImpl {

pub(crate) type LazyAdapter = Pin<Arc<Lazy<AdapterImpl, Boxed<AdapterImpl>>>>;

static NEXT_ADAPTER_ID: AtomicUsize = AtomicUsize::new(0);

pub struct Adapter {
id: usize,
r#impl: LazyAdapter,
is_window_focused: Arc<AtomicBool>,
root_window_bounds: Arc<Mutex<WindowBounds>>,
Expand All @@ -442,6 +443,7 @@ impl Adapter {
source: impl 'static + FnOnce() -> TreeUpdate + Send,
action_handler: Box<dyn ActionHandler + Send>,
) -> Self {
let id = NEXT_ADAPTER_ID.fetch_add(1, Ordering::SeqCst);
let is_window_focused = Arc::new(AtomicBool::new(false));
let is_window_focused_copy = is_window_focused.clone();
let root_window_bounds = Arc::new(Mutex::new(Default::default()));
Expand All @@ -451,6 +453,7 @@ impl Adapter {
let is_window_focused = is_window_focused_copy.load(Ordering::Relaxed);
let root_window_bounds = *root_window_bounds_copy.lock().unwrap();
AdapterImpl::new(
id,
source(),
is_window_focused,
root_window_bounds,
Expand All @@ -460,11 +463,12 @@ impl Adapter {
.boxed(),
));
let adapter = Self {
id,
r#impl: r#impl.clone(),
is_window_focused,
root_window_bounds,
};
block_on(async move { ActivationContext::activate_eventually(r#impl).await });
block_on(async move { ActivationContext::activate_eventually(id, r#impl).await });
adapter
}

Expand Down Expand Up @@ -496,6 +500,14 @@ impl Adapter {
}
}

impl Drop for Adapter {
fn drop(&mut self) {
block_on(async {
ActivationContext::remove_adapter(self.id).await;
})
}
}

pub(crate) enum Message {
RegisterInterfaces {
adapter_id: usize,
Expand Down
22 changes: 17 additions & 5 deletions platforms/unix/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl AppContext {

pub(crate) struct ActivationContext {
_task: Task<()>,
adapters: Vec<LazyAdapter>,
adapters: Vec<(usize, LazyAdapter)>,
}

static ACTIVATION_CONTEXT: AsyncOnceCell<Arc<AsyncMutex<ActivationContext>>> = AsyncOnceCell::new();
Expand All @@ -143,15 +143,27 @@ impl ActivationContext {
.await
}

pub(crate) async fn activate_eventually(adapter: LazyAdapter) {
pub(crate) async fn activate_eventually(id: usize, adapter: LazyAdapter) {
let mut activation_context = ActivationContext::get_or_init().await;
activation_context.adapters.push(adapter);
activation_context.adapters.push((id, adapter));
let is_a11y_enabled = AppContext::get_or_init().messages.is_some();
if is_a11y_enabled {
let adapter = activation_context.adapters.last().unwrap();
let adapter = &activation_context.adapters.last().unwrap().1;
adapter.as_ref().await;
}
}

pub(crate) async fn remove_adapter(id: usize) {
if let Some(activation_context) = ACTIVATION_CONTEXT.get() {
let mut context = activation_context.lock().await;
if let Ok(index) = context
.adapters
.binary_search_by(|adapter| adapter.0.cmp(&id))
{
context.adapters.remove(index);
}
}
}
}

async fn listen(session_bus: Connection) -> zbus::Result<()> {
Expand Down Expand Up @@ -182,7 +194,7 @@ async fn listen(session_bus: Connection) -> zbus::Result<()> {
if atspi_bus.is_some() {
if let Some(activation_context) = ACTIVATION_CONTEXT.get() {
let activation_context = activation_context.lock().await;
for adapter in &activation_context.adapters {
for (_, adapter) in &activation_context.adapters {
adapter.as_ref().await.register_tree().await;
}
}
Expand Down

0 comments on commit ecddbd7

Please sign in to comment.