From 9746ccdd46a56eca5dfca720f7140e54aa3622d2 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Wed, 30 Apr 2025 12:39:35 -0400 Subject: [PATCH] refactor(accessibility): use settings subscription --- Cargo.lock | 5 +- cosmic-applet-a11y/Cargo.toml | 6 +- cosmic-applet-a11y/src/app.rs | 4 +- cosmic-applet-a11y/src/backend/dbus.rs | 139 ------------------------- cosmic-applet-a11y/src/backend/mod.rs | 1 - 5 files changed, 9 insertions(+), 146 deletions(-) delete mode 100644 cosmic-applet-a11y/src/backend/dbus.rs diff --git a/Cargo.lock b/Cargo.lock index 1620e5b8..6f8b9971 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1086,8 +1086,8 @@ version = "0.1.0" dependencies = [ "anyhow", "cosmic-client-toolkit", - "cosmic-dbus-a11y", "cosmic-protocols", + "cosmic-settings-subscriptions", "cosmic-time", "i18n-embed", "i18n-embed-fl", @@ -1527,9 +1527,10 @@ dependencies = [ [[package]] name = "cosmic-settings-subscriptions" version = "0.1.0" -source = "git+https://github.com/pop-os/cosmic-settings-subscriptions#613e56f858db9e10d913b6d57e761e58d146ebc7" +source = "git+https://github.com/pop-os/cosmic-settings-subscriptions#650f0bc1dbfdce2e541c104674257d1621b2de4c" dependencies = [ "bluez-zbus", + "cosmic-dbus-a11y", "cosmic-dbus-networkmanager", "futures", "iced_futures", diff --git a/cosmic-applet-a11y/Cargo.toml b/cosmic-applet-a11y/Cargo.toml index b7e831a7..2f14ab65 100644 --- a/cosmic-applet-a11y/Cargo.toml +++ b/cosmic-applet-a11y/Cargo.toml @@ -4,8 +4,10 @@ version = "0.1.0" edition = "2021" [dependencies] -cosmic-dbus-a11y = { git = "https://github.com/pop-os/dbus-settings-bindings" } - +# cosmic-dbus-a11y = { git = "https://github.com/pop-os/dbus-settings-bindings" } +cosmic-settings-subscriptions.workspace = true +cosmic-settings-subscriptions.default_features = false +cosmic-settings-subscriptions.accessibility = true anyhow.workspace = true cctk.workspace = true cosmic-protocols.workspace = true diff --git a/cosmic-applet-a11y/src/app.rs b/cosmic-applet-a11y/src/app.rs index 826faa5e..250f90b5 100644 --- a/cosmic-applet-a11y/src/app.rs +++ b/cosmic-applet-a11y/src/app.rs @@ -4,7 +4,6 @@ use crate::{ backend::{ self, - dbus::{DBusRequest, DBusUpdate}, wayland::{AccessibilityEvent, AccessibilityRequest, WaylandUpdate}, }, fl, @@ -26,6 +25,7 @@ use cosmic::{ Element, Task, }; use cosmic_protocols::a11y::v1::client::cosmic_a11y_manager_v1::Filter; +use cosmic_settings_subscriptions::accessibility::{self, DBusRequest, DBusUpdate}; use cosmic_time::{anim, chain, id, once_cell::sync::Lazy, Instant, Timeline}; use tokio::sync::mpsc::UnboundedSender; @@ -290,7 +290,7 @@ impl cosmic::Application for CosmicA11yApplet { fn subscription(&self) -> Subscription { Subscription::batch(vec![ - backend::dbus::subscription().map(Message::DBusUpdate), + accessibility::subscription().map(Message::DBusUpdate), backend::wayland::a11y_subscription().map(Message::WaylandUpdate), self.timeline .as_subscription() diff --git a/cosmic-applet-a11y/src/backend/dbus.rs b/cosmic-applet-a11y/src/backend/dbus.rs deleted file mode 100644 index 1a1b0d1e..00000000 --- a/cosmic-applet-a11y/src/backend/dbus.rs +++ /dev/null @@ -1,139 +0,0 @@ -// Copyright 2023 System76 -// SPDX-License-Identifier: GPL-3.0-only - -use cosmic::iced::futures::FutureExt; -use cosmic::{ - iced::{ - self, - futures::{self, select, SinkExt, StreamExt}, - Subscription, - }, - iced_futures::stream, -}; -use cosmic_dbus_a11y::*; -use std::fmt::Debug; -use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; -use zbus::Connection; - -#[derive(Debug, Clone)] -pub enum DBusUpdate { - Error(String), - Status(bool), - Init(bool, UnboundedSender), -} - -pub enum DBusRequest { - Status(bool), -} - -#[derive(Debug)] -pub enum State { - Ready, - Waiting(Connection, u8, bool, UnboundedReceiver), - Finished, -} - -pub fn subscription() -> iced::Subscription { - struct MyId; - - Subscription::run_with_id( - std::any::TypeId::of::(), - stream::channel(50, move |mut output| async move { - let mut state = State::Ready; - - loop { - state = start_listening(state, &mut output).await; - } - }), - ) -} - -async fn start_listening( - state: State, - output: &mut futures::channel::mpsc::Sender, -) -> State { - match state { - State::Ready => { - let conn = match Connection::session().await.map_err(|e| e.to_string()) { - Ok(conn) => conn, - Err(e) => { - _ = output.send(DBusUpdate::Error(e)).await; - return State::Finished; - } - }; - let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); - let mut enabled = false; - if let Ok(proxy) = StatusProxy::new(&conn).await { - if let Ok(status) = proxy.screen_reader_enabled().await { - enabled = status; - } - } - _ = output.send(DBusUpdate::Init(enabled, tx)).await; - State::Waiting(conn, 20, enabled, rx) - } - State::Waiting(conn, mut retry, mut enabled, mut rx) => { - let Ok(proxy) = StatusProxy::new(&conn).await else { - if retry == 0 { - tracing::error!("Accessibility Status is unavailable."); - return State::Finished; - } else { - _ = tokio::time::sleep(tokio::time::Duration::from_secs( - 2_u64.pow(retry as u32), - )) - .await; - retry -= 1; - return State::Waiting(conn, retry, enabled, rx); - } - }; - retry = 20; - - let mut watch_changes = proxy.receive_screen_reader_enabled_changed().await; - - if let Ok(status) = proxy.screen_reader_enabled().await { - if enabled != status { - _ = output.send(DBusUpdate::Status(enabled)); - } - enabled = status; - } - - loop { - if let Ok(status) = proxy.screen_reader_enabled().await { - if enabled != status { - _ = output.send(DBusUpdate::Status(enabled)); - } - enabled = status; - } - - let mut next_change = Box::pin(watch_changes.next()).fuse(); - let mut next_request = Box::pin(rx.recv()).fuse(); - - select! { - v = next_request => { - match v { - Some(DBusRequest::Status(is_enabled)) => { - // Set status - enabled = is_enabled; - _ = proxy.set_is_enabled(is_enabled).await; - _ = proxy.set_screen_reader_enabled(is_enabled).await; - } - None => return State::Finished, - } - } - v = next_change => { - match v { - Some(f) => { - if let Ok(enabled) = f.get().await { - _ = output.send(DBusUpdate::Status(enabled)); - } - } - None => break, - }; - } - } - } - - State::Waiting(conn, retry, enabled, rx) - } - State::Finished => iced::futures::future::pending().await, - } -} diff --git a/cosmic-applet-a11y/src/backend/mod.rs b/cosmic-applet-a11y/src/backend/mod.rs index fd96a1f5..d41cd3d6 100644 --- a/cosmic-applet-a11y/src/backend/mod.rs +++ b/cosmic-applet-a11y/src/backend/mod.rs @@ -1,2 +1 @@ -pub mod dbus; pub mod wayland;