chore: updates after iced-rebase
This commit is contained in:
parent
bd0d180482
commit
71d9d6d5bb
41 changed files with 1786 additions and 2396 deletions
|
|
@ -7,6 +7,7 @@ use cosmic::{
|
|||
applet::token::subscription::{TokenRequest, TokenUpdate, activation_token_subscription},
|
||||
cctk::sctk::reexports::calloop,
|
||||
surface,
|
||||
widget::toggler,
|
||||
};
|
||||
|
||||
use cosmic::{
|
||||
|
|
@ -22,7 +23,6 @@ use cosmic::{
|
|||
theme,
|
||||
widget::{button, divider, icon, scrollable, text},
|
||||
};
|
||||
use cosmic_time::{Instant, Timeline, anim, chain, id};
|
||||
use futures::FutureExt;
|
||||
use std::{collections::HashMap, sync::LazyLock, time::Duration};
|
||||
use tokio::sync::mpsc::Sender;
|
||||
|
|
@ -32,8 +32,6 @@ use crate::{
|
|||
config, fl,
|
||||
};
|
||||
|
||||
static BLUETOOTH_ENABLED: LazyLock<id::Toggler> = LazyLock::new(id::Toggler::unique);
|
||||
|
||||
#[inline]
|
||||
pub fn run() -> cosmic::iced::Result {
|
||||
cosmic::applet::run::<CosmicBluetoothApplet>(())
|
||||
|
|
@ -50,7 +48,6 @@ struct CosmicBluetoothApplet {
|
|||
show_visible_devices: bool,
|
||||
request_confirmation: Option<(BluerDevice, String, Sender<bool>)>,
|
||||
token_tx: Option<calloop::channel::Sender<TokenRequest>>,
|
||||
timeline: Timeline,
|
||||
}
|
||||
|
||||
impl CosmicBluetoothApplet {
|
||||
|
|
@ -77,8 +74,7 @@ enum Message {
|
|||
Confirm,
|
||||
Token(TokenUpdate),
|
||||
OpenSettings,
|
||||
Frame(Instant),
|
||||
ToggleBluetooth(chain::Toggler, bool),
|
||||
ToggleBluetooth(bool),
|
||||
Surface(surface::Action),
|
||||
}
|
||||
|
||||
|
|
@ -127,7 +123,6 @@ impl cosmic::Application for CosmicBluetoothApplet {
|
|||
// TODO request update of state maybe
|
||||
let new_id = window::Id::unique();
|
||||
self.popup.replace(new_id);
|
||||
self.timeline = Timeline::new();
|
||||
|
||||
let popup_settings = self.core.applet.get_popup_settings(
|
||||
self.core.main_window_id().unwrap(),
|
||||
|
|
@ -157,15 +152,6 @@ impl cosmic::Application for CosmicBluetoothApplet {
|
|||
if let Some(err_msg) = err_msg {
|
||||
eprintln!("bluetooth request error: {err_msg}");
|
||||
}
|
||||
if self.bluer_state.bluetooth_enabled != state.bluetooth_enabled {
|
||||
self.timeline
|
||||
.set_chain(if state.bluetooth_enabled {
|
||||
chain::Toggler::on(BLUETOOTH_ENABLED.clone(), 1.0)
|
||||
} else {
|
||||
chain::Toggler::off(BLUETOOTH_ENABLED.clone(), 1.0)
|
||||
})
|
||||
.start();
|
||||
}
|
||||
|
||||
self.bluer_state = state;
|
||||
}
|
||||
|
|
@ -300,12 +286,10 @@ impl cosmic::Application for CosmicBluetoothApplet {
|
|||
tokio::spawn(cosmic::process::spawn(cmd));
|
||||
}
|
||||
},
|
||||
Message::Frame(instant) => self.timeline.now(instant),
|
||||
Message::ToggleBluetooth(chain, enabled) => {
|
||||
Message::ToggleBluetooth(enabled) => {
|
||||
if self.bluer_state.bluetooth_enabled == enabled {
|
||||
return Task::none();
|
||||
}
|
||||
self.timeline.set_chain(chain).start();
|
||||
self.bluer_state.bluetooth_enabled = enabled;
|
||||
if let Some(tx) = self.bluer_sender.clone() {
|
||||
tokio::spawn(async move {
|
||||
|
|
@ -416,16 +400,11 @@ impl cosmic::Application for CosmicBluetoothApplet {
|
|||
}
|
||||
|
||||
let mut content = column![column![padded_control(
|
||||
anim!(
|
||||
//toggler
|
||||
BLUETOOTH_ENABLED,
|
||||
&self.timeline,
|
||||
fl!("bluetooth"),
|
||||
self.bluer_state.bluetooth_enabled,
|
||||
Message::ToggleBluetooth,
|
||||
)
|
||||
.text_size(14)
|
||||
.width(Length::Fill)
|
||||
toggler(self.bluer_state.bluetooth_enabled)
|
||||
.label(fl!("bluetooth"))
|
||||
.on_toggle(Message::ToggleBluetooth)
|
||||
.width(Length::Fill)
|
||||
.text_size(14)
|
||||
),],]
|
||||
.align_x(Alignment::Center)
|
||||
.padding([8, 0]);
|
||||
|
|
@ -549,13 +528,10 @@ impl cosmic::Application for CosmicBluetoothApplet {
|
|||
Subscription::batch([
|
||||
activation_token_subscription(0).map(Message::Token),
|
||||
bluetooth_subscription(0).map(Message::BluetoothEvent),
|
||||
self.timeline
|
||||
.as_subscription()
|
||||
.map(|(_, now)| Message::Frame(now)),
|
||||
])
|
||||
}
|
||||
|
||||
fn style(&self) -> Option<cosmic::iced_runtime::Appearance> {
|
||||
fn style(&self) -> Option<iced::theme::Style> {
|
||||
Some(cosmic::applet::style())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ use rustc_hash::FxHashMap;
|
|||
use std::{
|
||||
fmt::Debug,
|
||||
hash::Hash,
|
||||
mem,
|
||||
sync::{
|
||||
Arc, LazyLock,
|
||||
atomic::{AtomicBool, Ordering},
|
||||
|
|
@ -90,100 +89,104 @@ fn rfkill_path_var() -> std::ffi::OsString {
|
|||
pub fn bluetooth_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
|
||||
id: I,
|
||||
) -> iced::Subscription<BluerEvent> {
|
||||
Subscription::run_with_id(
|
||||
id,
|
||||
stream::channel(50, move |mut output| async move {
|
||||
let mut retry_count = 0u32;
|
||||
Subscription::run_with(id, |_| {
|
||||
stream::channel(
|
||||
50,
|
||||
move |mut output: futures::channel::mpsc::Sender<BluerEvent>| async move {
|
||||
let mut retry_count = 0u32;
|
||||
|
||||
// Initialize connection.
|
||||
let mut session_state = loop {
|
||||
if let Ok(session) = Session::new().await {
|
||||
if let Ok(state) = BluerSessionState::new(session).await {
|
||||
break state;
|
||||
}
|
||||
}
|
||||
|
||||
retry_count = retry_count.saturating_add(1);
|
||||
() = tokio::time::sleep(Duration::from_millis(
|
||||
2_u64.saturating_pow(retry_count).min(68719476734),
|
||||
))
|
||||
.await;
|
||||
};
|
||||
|
||||
let state = bluer_state(&session_state.adapter).await;
|
||||
|
||||
// reconnect to paired and trusted devices
|
||||
if state.bluetooth_enabled {
|
||||
for d in &state.devices {
|
||||
if d.paired_and_trusted() && !matches!(d.status, BluerDeviceStatus::Connected) {
|
||||
_ = session_state
|
||||
.req_tx
|
||||
.send(BluerRequest::ConnectDevice(d.address))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = output
|
||||
.send(BluerEvent::Init {
|
||||
sender: session_state.req_tx.clone(),
|
||||
state: state.clone(),
|
||||
})
|
||||
.await;
|
||||
|
||||
let mut event_handler = async |event| {
|
||||
let message = match event {
|
||||
BluerSessionEvent::ChangesProcessed(state) => {
|
||||
BluerEvent::DevicesChanged { state }
|
||||
}
|
||||
|
||||
BluerSessionEvent::RequestResponse {
|
||||
req,
|
||||
state,
|
||||
err_msg,
|
||||
} => BluerEvent::RequestResponse {
|
||||
req,
|
||||
state,
|
||||
err_msg,
|
||||
},
|
||||
|
||||
BluerSessionEvent::AgentEvent(e) => BluerEvent::AgentEvent(e),
|
||||
|
||||
_ => return,
|
||||
};
|
||||
|
||||
_ = output.send(message).await;
|
||||
};
|
||||
|
||||
let mut interval = tokio::time::interval(Duration::from_secs(10));
|
||||
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
loop {
|
||||
let Some(mut session_rx) = session_state.rx.take() else {
|
||||
break;
|
||||
};
|
||||
|
||||
if let Some(event) = session_rx.recv().await {
|
||||
event_handler(event).await;
|
||||
// Consume any additional available events.
|
||||
let mut count = 0;
|
||||
while let Ok(event) = session_rx.try_recv() {
|
||||
event_handler(event).await;
|
||||
count += 1;
|
||||
if count == 100 {
|
||||
break;
|
||||
// Initialize connection.
|
||||
let mut session_state = loop {
|
||||
if let Ok(session) = Session::new().await {
|
||||
if let Ok(state) = BluerSessionState::new(session).await {
|
||||
break state;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
|
||||
retry_count = retry_count.saturating_add(1);
|
||||
() = tokio::time::sleep(Duration::from_millis(
|
||||
2_u64.saturating_pow(retry_count).min(68719476734),
|
||||
))
|
||||
.await;
|
||||
};
|
||||
|
||||
let state = bluer_state(&session_state.adapter).await;
|
||||
|
||||
// reconnect to paired and trusted devices
|
||||
if state.bluetooth_enabled {
|
||||
for d in &state.devices {
|
||||
if d.paired_and_trusted()
|
||||
&& !matches!(d.status, BluerDeviceStatus::Connected)
|
||||
{
|
||||
_ = session_state
|
||||
.req_tx
|
||||
.send(BluerRequest::ConnectDevice(d.address))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = output
|
||||
.send(BluerEvent::Init {
|
||||
sender: session_state.req_tx.clone(),
|
||||
state: state.clone(),
|
||||
})
|
||||
.await;
|
||||
|
||||
let mut event_handler = async |event| {
|
||||
let message = match event {
|
||||
BluerSessionEvent::ChangesProcessed(state) => {
|
||||
BluerEvent::DevicesChanged { state }
|
||||
}
|
||||
|
||||
BluerSessionEvent::RequestResponse {
|
||||
req,
|
||||
state,
|
||||
err_msg,
|
||||
} => BluerEvent::RequestResponse {
|
||||
req,
|
||||
state,
|
||||
err_msg,
|
||||
},
|
||||
|
||||
BluerSessionEvent::AgentEvent(e) => BluerEvent::AgentEvent(e),
|
||||
|
||||
_ => return,
|
||||
};
|
||||
|
||||
_ = output.send(message).await;
|
||||
};
|
||||
|
||||
let mut interval = tokio::time::interval(Duration::from_secs(10));
|
||||
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
loop {
|
||||
let Some(mut session_rx) = session_state.rx.take() else {
|
||||
break;
|
||||
};
|
||||
|
||||
if let Some(event) = session_rx.recv().await {
|
||||
event_handler(event).await;
|
||||
// Consume any additional available events.
|
||||
let mut count = 0;
|
||||
while let Ok(event) = session_rx.try_recv() {
|
||||
event_handler(event).await;
|
||||
count += 1;
|
||||
if count == 100 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
session_state.rx = Some(session_rx);
|
||||
interval.tick().await;
|
||||
}
|
||||
|
||||
session_state.rx = Some(session_rx);
|
||||
interval.tick().await;
|
||||
}
|
||||
|
||||
_ = output.send(BluerEvent::Finished).await;
|
||||
futures::future::pending().await
|
||||
}),
|
||||
)
|
||||
_ = output.send(BluerEvent::Finished).await;
|
||||
futures::future::pending().await
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue