refactor(accessibility): use settings subscription
This commit is contained in:
parent
50711eac62
commit
9746ccdd46
5 changed files with 9 additions and 146 deletions
5
Cargo.lock
generated
5
Cargo.lock
generated
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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<Message> {
|
||||
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()
|
||||
|
|
|
|||
|
|
@ -1,139 +0,0 @@
|
|||
// Copyright 2023 System76 <info@system76.com>
|
||||
// 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<DBusRequest>),
|
||||
}
|
||||
|
||||
pub enum DBusRequest {
|
||||
Status(bool),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum State {
|
||||
Ready,
|
||||
Waiting(Connection, u8, bool, UnboundedReceiver<DBusRequest>),
|
||||
Finished,
|
||||
}
|
||||
|
||||
pub fn subscription() -> iced::Subscription<DBusUpdate> {
|
||||
struct MyId;
|
||||
|
||||
Subscription::run_with_id(
|
||||
std::any::TypeId::of::<MyId>(),
|
||||
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<DBusUpdate>,
|
||||
) -> 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,
|
||||
}
|
||||
}
|
||||
|
|
@ -1,2 +1 @@
|
|||
pub mod dbus;
|
||||
pub mod wayland;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue