cosmic-applets/cosmic-applet-bluetooth/src/app.rs

583 lines
22 KiB
Rust
Raw Normal View History

2024-05-06 15:39:04 +02:00
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only
use crate::bluetooth::{BluerDeviceStatus, BluerRequest, BluerState, DeviceProperty};
2024-07-09 15:17:44 +02:00
use cosmic::{
2025-03-14 13:14:51 -04:00
app,
2024-07-09 15:17:44 +02:00
applet::token::subscription::{activation_token_subscription, TokenRequest, TokenUpdate},
cctk::sctk::reexports::calloop,
2025-03-14 13:14:51 -04:00
surface,
};
2023-02-08 18:38:09 -05:00
use cosmic::{
2024-07-09 15:17:44 +02:00
applet::{menu_button, padded_control},
cosmic_theme::Spacing,
2023-02-08 18:38:09 -05:00
iced::{
self,
2024-10-30 22:51:08 -04:00
platform_specific::shell::wayland::commands::popup::{destroy_popup, get_popup},
widget::{column, container, row, scrollable, Column},
Alignment, Length, Subscription,
2023-02-08 18:38:09 -05:00
},
iced_runtime::core::{layout::Limits, window},
theme,
widget::{button, divider, icon, text},
2024-10-30 22:51:08 -04:00
Element, Task,
2023-02-08 18:38:09 -05:00
};
2023-12-05 14:19:44 -05:00
use cosmic_time::{anim, chain, id, once_cell::sync::Lazy, Instant, Timeline};
2024-07-09 15:17:44 +02:00
use std::{collections::HashMap, time::Duration};
2023-02-08 18:38:09 -05:00
use tokio::sync::mpsc::Sender;
2024-07-09 15:17:44 +02:00
use crate::{
bluetooth::{bluetooth_subscription, BluerDevice, BluerEvent},
config, fl,
};
2023-02-08 18:38:09 -05:00
2023-12-05 14:19:44 -05:00
static BLUETOOTH_ENABLED: Lazy<id::Toggler> = Lazy::new(id::Toggler::unique);
2023-02-08 18:38:09 -05:00
pub fn run() -> cosmic::iced::Result {
2024-10-30 22:51:08 -04:00
cosmic::applet::run::<CosmicBluetoothApplet>(())
2023-02-08 18:38:09 -05:00
}
#[derive(Default)]
struct CosmicBluetoothApplet {
core: cosmic::app::Core,
2023-02-08 18:38:09 -05:00
icon_name: String,
popup: Option<window::Id>,
bluer_state: BluerState,
bluer_sender: Option<Sender<BluerRequest>>,
// UI state
show_visible_devices: bool,
2023-02-09 20:09:31 -05:00
request_confirmation: Option<(BluerDevice, String, Sender<bool>)>,
token_tx: Option<calloop::channel::Sender<TokenRequest>>,
2023-12-05 14:19:44 -05:00
timeline: Timeline,
2023-02-08 18:38:09 -05:00
}
impl CosmicBluetoothApplet {
fn update_icon(&mut self) {
self.icon_name = if self.bluer_state.bluetooth_enabled {
"cosmic-applet-bluetooth-active-symbolic"
} else {
"cosmic-applet-bluetooth-disabled-symbolic"
}
.to_string();
}
}
2023-02-08 18:38:09 -05:00
#[derive(Debug, Clone)]
enum Message {
TogglePopup,
CloseRequested(window::Id),
2023-02-08 18:38:09 -05:00
ToggleVisibleDevices(bool),
Ignore,
BluetoothEvent(BluerEvent),
Request(BluerRequest),
2023-02-09 20:09:31 -05:00
Cancel,
Confirm,
Token(TokenUpdate),
OpenSettings,
2023-12-05 14:19:44 -05:00
Frame(Instant),
ToggleBluetooth(chain::Toggler, bool),
2025-03-14 13:14:51 -04:00
Surface(surface::Action),
2023-02-08 18:38:09 -05:00
}
impl cosmic::Application for CosmicBluetoothApplet {
2023-02-08 18:38:09 -05:00
type Message = Message;
type Executor = cosmic::SingleThreadExecutor;
type Flags = ();
const APP_ID: &'static str = config::APP_ID;
2023-02-08 18:38:09 -05:00
2025-03-14 13:14:51 -04:00
fn init(core: cosmic::app::Core, _flags: Self::Flags) -> (Self, app::Task<Self::Message>) {
2023-02-08 18:38:09 -05:00
(
2023-11-16 18:32:31 +00:00
Self {
core,
2023-02-08 18:38:09 -05:00
icon_name: "bluetooth-symbolic".to_string(),
token_tx: None,
2023-02-08 18:38:09 -05:00
..Default::default()
},
2024-10-30 22:51:08 -04:00
Task::none(),
2023-02-08 18:38:09 -05:00
)
}
fn core(&self) -> &cosmic::app::Core {
&self.core
}
fn core_mut(&mut self) -> &mut cosmic::app::Core {
&mut self.core
2023-02-08 18:38:09 -05:00
}
2025-03-14 13:14:51 -04:00
fn update(&mut self, message: Self::Message) -> app::Task<Self::Message> {
2023-02-08 18:38:09 -05:00
match message {
Message::TogglePopup => {
if let Some(p) = self.popup.take() {
return destroy_popup(p);
} else {
// TODO request update of state maybe
2023-12-11 14:45:36 -05:00
let new_id = window::Id::unique();
2023-02-08 18:38:09 -05:00
self.popup.replace(new_id);
self.timeline = Timeline::new();
2023-02-08 18:38:09 -05:00
2023-09-18 08:31:27 -07:00
let mut popup_settings = self.core.applet.get_popup_settings(
2024-10-30 22:51:08 -04:00
self.core.main_window_id().unwrap(),
2023-02-08 18:38:09 -05:00
new_id,
None,
2023-02-08 18:38:09 -05:00
None,
None,
);
2023-02-09 20:09:31 -05:00
let tx = self.bluer_sender.as_ref().cloned();
2024-10-30 22:51:08 -04:00
return Task::batch(vec![
iced::Task::perform(
2023-02-09 20:09:31 -05:00
async {
if let Some(tx) = tx {
let _ = tx.send(BluerRequest::StateUpdate).await;
}
},
2025-03-14 13:14:51 -04:00
|_| cosmic::action::app(Message::Ignore),
2023-02-09 20:09:31 -05:00
),
get_popup(popup_settings),
]);
2023-02-08 18:38:09 -05:00
}
}
Message::Ignore => {}
Message::ToggleVisibleDevices(enabled) => {
self.show_visible_devices = enabled;
}
Message::BluetoothEvent(e) => match e {
BluerEvent::RequestResponse {
2023-02-09 20:09:31 -05:00
req,
2023-02-08 18:38:09 -05:00
state,
err_msg,
} => {
if let Some(err_msg) = err_msg {
eprintln!("bluetooth request error: {}", err_msg);
}
2023-12-05 14:19:44 -05:00
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();
}
2023-02-08 18:38:09 -05:00
self.bluer_state = state;
// TODO special handling for some requests
2023-02-09 20:09:31 -05:00
match req {
BluerRequest::StateUpdate
if self.popup.is_some() && self.bluer_sender.is_some() =>
{
let tx = self.bluer_sender.as_ref().cloned().unwrap();
2023-12-05 14:19:44 -05:00
tokio::spawn(async move {
// sleep for a bit before requesting state update again
tokio::time::sleep(Duration::from_millis(3000)).await;
let _ = tx.send(BluerRequest::StateUpdate).await;
});
2023-02-09 20:09:31 -05:00
}
_ => {}
};
2023-02-08 18:38:09 -05:00
}
BluerEvent::Init { sender, state } => {
self.bluer_sender.replace(sender);
self.bluer_state = state;
}
BluerEvent::DevicesChanged { state } => {
self.bluer_state = state;
}
BluerEvent::Finished => {
// TODO should this exit with an error causing a restart?
2023-02-09 20:09:31 -05:00
eprintln!("bluetooth subscription finished. exiting...");
std::process::exit(0);
2023-02-08 18:38:09 -05:00
}
2023-02-09 20:09:31 -05:00
// TODO handle agent events
BluerEvent::AgentEvent(event) => match event {
crate::bluetooth::BluerAgentEvent::DisplayPinCode(_d, _code) => {}
crate::bluetooth::BluerAgentEvent::DisplayPasskey(_d, _code) => {}
crate::bluetooth::BluerAgentEvent::RequestPinCode(_d) => {
2023-02-09 20:09:31 -05:00
// TODO anything to be done here?
}
crate::bluetooth::BluerAgentEvent::RequestPasskey(_d) => {
2023-02-09 20:09:31 -05:00
// TODO anything to be done here?
}
crate::bluetooth::BluerAgentEvent::RequestConfirmation(d, code, tx) => {
self.request_confirmation.replace((d, code, tx));
}
crate::bluetooth::BluerAgentEvent::RequestDeviceAuthorization(_d, _tx) => {
2023-02-09 20:09:31 -05:00
// TODO anything to be done here?
}
crate::bluetooth::BluerAgentEvent::RequestServiceAuthorization(
_d,
_service,
2023-02-09 20:09:31 -05:00
_tx,
) => {
// my headphones seem to always request this
// doesn't seem to be defined in the UX mockups
// dbg!(
// "request service authorization",
// d.name,
// bluer::id::Service::try_from(service)
// .map(|s| s.to_string())
// .unwrap_or_else(|_| "unknown".to_string())
// );
2023-02-09 20:09:31 -05:00
}
},
2023-02-08 18:38:09 -05:00
},
Message::Request(r) => {
match &r {
BluerRequest::SetBluetoothEnabled(enabled) => {
self.bluer_state.bluetooth_enabled = *enabled;
}
BluerRequest::ConnectDevice(add) => {
if let Some(d) = self
.bluer_state
2023-02-08 18:38:09 -05:00
.devices
.iter_mut()
.find(|d| d.address == *add)
{
d.status = BluerDeviceStatus::Connecting;
}
2023-02-08 18:38:09 -05:00
}
BluerRequest::DisconnectDevice(add) => {
if let Some(d) = self
.bluer_state
2023-02-08 18:38:09 -05:00
.devices
.iter_mut()
.find(|d| d.address == *add)
{
d.status = BluerDeviceStatus::Disconnecting;
}
2023-02-08 18:38:09 -05:00
}
BluerRequest::PairDevice(add) => {
if let Some(d) = self
.bluer_state
2023-02-08 18:38:09 -05:00
.devices
.iter_mut()
.find(|d| d.address == *add)
{
d.status = BluerDeviceStatus::Pairing;
}
2023-02-08 18:38:09 -05:00
}
_ => {} // TODO
}
if let Some(tx) = self.bluer_sender.as_mut().cloned() {
2023-12-05 14:19:44 -05:00
tokio::spawn(async move {
let _ = tx.send(r).await;
});
2023-02-08 18:38:09 -05:00
}
}
2023-02-09 20:09:31 -05:00
Message::Cancel => {
if let Some((_, _, tx)) = self.request_confirmation.take() {
2023-12-05 14:19:44 -05:00
tokio::spawn(async move {
let _ = tx.send(false).await;
});
2023-02-09 20:09:31 -05:00
}
}
Message::Confirm => {
if let Some((_, _, tx)) = self.request_confirmation.take() {
2023-12-05 14:19:44 -05:00
tokio::spawn(async move {
let _ = tx.send(true).await;
});
2023-02-09 20:09:31 -05:00
}
}
Message::CloseRequested(id) => {
if Some(id) == self.popup {
self.popup = None;
}
}
Message::OpenSettings => {
let exec = "cosmic-settings bluetooth".to_string();
if let Some(tx) = self.token_tx.as_ref() {
let _ = tx.send(TokenRequest {
app_id: Self::APP_ID.to_string(),
exec,
});
};
}
Message::Token(u) => match u {
TokenUpdate::Init(tx) => {
self.token_tx = Some(tx);
}
TokenUpdate::Finished => {
self.token_tx = None;
}
TokenUpdate::ActivationToken { token, .. } => {
let mut cmd = std::process::Command::new("cosmic-settings");
cmd.arg("bluetooth");
if let Some(token) = token {
cmd.env("XDG_ACTIVATION_TOKEN", &token);
cmd.env("DESKTOP_STARTUP_ID", &token);
}
tokio::spawn(cosmic::process::spawn(cmd));
}
},
2023-12-05 14:19:44 -05:00
Message::Frame(instant) => self.timeline.now(instant),
Message::ToggleBluetooth(chain, enabled) => {
if self.bluer_state.bluetooth_enabled == enabled {
2024-10-30 22:51:08 -04:00
return Task::none();
2023-12-05 14:19:44 -05:00
}
self.timeline.set_chain(chain).start();
self.bluer_state.bluetooth_enabled = enabled;
if let Some(tx) = self.bluer_sender.clone() {
tokio::spawn(async move {
let _ = tx.send(BluerRequest::SetBluetoothEnabled(enabled)).await;
});
}
}
2025-03-17 22:18:25 -04:00
Message::Surface(a) => {
return cosmic::task::message(cosmic::Action::Cosmic(
cosmic::app::Action::Surface(a),
));
}
2023-02-08 18:38:09 -05:00
}
self.update_icon();
2024-10-30 22:51:08 -04:00
Task::none()
2023-02-08 18:38:09 -05:00
}
fn view(&self) -> Element<Message> {
self.core
2023-09-18 08:31:27 -07:00
.applet
.icon_button(&self.icon_name)
2024-08-13 19:03:34 +02:00
.on_press_down(Message::TogglePopup)
.into()
}
fn view_window(&self, _id: window::Id) -> Element<Message> {
let Spacing {
space_xxs, space_s, ..
} = theme::active().cosmic().spacing;
let mut known_bluetooth = vec![];
for dev in self.bluer_state.devices.iter().filter(|d| {
!self
.request_confirmation
.as_ref()
.map_or(false, |(dev, _, _)| d.address == dev.address)
}) {
let mut row = row![
2023-09-18 00:24:21 -07:00
icon::from_name(dev.icon.as_str()).size(16).symbolic(true),
text::body(dev.name.clone())
.align_x(Alignment::Start)
.align_y(Alignment::Center)
.width(Length::Fill)
]
2024-10-30 22:51:08 -04:00
.align_y(Alignment::Center)
.spacing(12);
if let Some(DeviceProperty::BatteryPercentage(battery)) = dev
.properties
.iter()
.find(|p| matches!(p, DeviceProperty::BatteryPercentage(_)))
{
let icon = match *battery {
b if b >= 20 && b < 40 => "battery-low",
b if b < 20 => "battery-caution",
_ => "battery",
};
let status = row!(
icon::from_name(icon).symbolic(true).size(14),
text::body(format!("{}%", battery))
)
2024-10-30 22:51:08 -04:00
.align_y(Alignment::Center)
.spacing(2)
.width(Length::Shrink);
let content = container(status)
.align_x(Alignment::End)
.align_y(Alignment::Center);
row = row.push(content);
}
match &dev.status {
BluerDeviceStatus::Connected => {
row = row.push(
text::body(fl!("connected"))
.align_x(Alignment::End)
.align_y(Alignment::Center),
);
}
BluerDeviceStatus::Paired => {}
BluerDeviceStatus::Connecting | BluerDeviceStatus::Disconnecting => {
2023-09-18 00:24:21 -07:00
row = row.push(
icon::from_name("process-working-symbolic")
.size(24)
.symbolic(true),
);
}
BluerDeviceStatus::Disconnected | BluerDeviceStatus::Pairing => continue,
};
known_bluetooth.push(
menu_button(row)
.on_press(match dev.status {
BluerDeviceStatus::Connected => {
Message::Request(BluerRequest::DisconnectDevice(dev.address))
}
BluerDeviceStatus::Disconnected => {
Message::Request(BluerRequest::PairDevice(dev.address))
}
BluerDeviceStatus::Paired => {
Message::Request(BluerRequest::ConnectDevice(dev.address))
}
BluerDeviceStatus::Connecting => {
Message::Request(BluerRequest::CancelConnect(dev.address))
}
BluerDeviceStatus::Disconnecting => Message::Ignore, // Start connecting?
BluerDeviceStatus::Pairing => Message::Ignore, // Cancel pairing?
})
.into(),
);
}
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)
),],]
2024-10-30 22:51:08 -04:00
.align_x(Alignment::Center)
.padding([8, 0]);
if !known_bluetooth.is_empty() {
content = content
.push(padded_control(divider::horizontal::default()).padding([space_xxs, space_s]));
content = content.push(Column::with_children(known_bluetooth));
}
let dropdown_icon = if self.show_visible_devices {
"go-up-symbolic"
2023-04-05 20:40:22 -04:00
} else {
"go-down-symbolic"
};
2023-10-19 14:47:13 -04:00
let available_connections_btn = menu_button(row![
text::body(fl!("other-devices"))
2023-09-18 00:24:21 -07:00
.width(Length::Fill)
.height(Length::Fixed(24.0))
.align_y(Alignment::Center),
container(icon::from_name(dropdown_icon).size(16).symbolic(true))
.center(Length::Fixed(24.0))
2023-09-18 00:24:21 -07:00
])
.on_press(Message::ToggleVisibleDevices(!self.show_visible_devices));
if self.bluer_state.bluetooth_enabled {
content = content
.push(padded_control(divider::horizontal::default()).padding([space_xxs, space_s]));
content = content.push(available_connections_btn);
}
let mut list_column: Vec<Element<'_, Message>> =
Vec::with_capacity(self.bluer_state.devices.len());
2023-04-05 20:40:22 -04:00
if let Some((device, pin, _)) = self.request_confirmation.as_ref() {
let row = column![
2023-10-19 17:05:13 -04:00
padded_control(row![
icon::from_name(device.icon.as_str())
.size(16)
.symbolic(true),
text::body(&device.name)
.align_x(Alignment::Start)
.align_y(Alignment::Center)
2023-10-19 17:05:13 -04:00
.width(Length::Fill)
]),
padded_control(
text::body(fl!(
2023-10-19 17:05:13 -04:00
"confirm-pin",
HashMap::from_iter(vec![("deviceName", device.name.clone())])
))
.align_x(Alignment::Start)
.align_y(Alignment::Center)
2023-04-05 20:40:22 -04:00
.width(Length::Fill)
2023-10-19 17:05:13 -04:00
),
padded_control(text::title3(pin).center().width(Length::Fixed(280.0)))
.align_x(Alignment::Center),
2023-10-19 17:05:13 -04:00
padded_control(
row![
button::custom(text::body(fl!("cancel")).center())
.padding([4, 0])
.height(Length::Fixed(28.0))
.width(Length::Fixed(105.0))
.on_press(Message::Cancel),
button::custom(text::body(fl!("confirm")).center())
.padding([4, 0])
.height(Length::Fixed(28.0))
.width(Length::Fixed(105.0))
.on_press(Message::Confirm),
2023-10-19 17:05:13 -04:00
]
.spacing(self.core.system_theme().cosmic().space_xxs())
.width(Length::Shrink)
2024-10-30 22:51:08 -04:00
.align_y(Alignment::Center)
2023-10-19 17:05:13 -04:00
)
.align_x(Alignment::Center)
2023-10-19 17:05:13 -04:00
];
list_column.push(row.into());
}
let mut visible_devices_count = 0;
2023-09-18 00:24:21 -07:00
if self.show_visible_devices && self.bluer_state.bluetooth_enabled {
let mut visible_devices = column![];
for dev in self.bluer_state.devices.iter().filter(|d| {
matches!(
d.status,
BluerDeviceStatus::Disconnected | BluerDeviceStatus::Pairing
) && !self
.request_confirmation
.as_ref()
.map_or(false, |(dev, _, _)| d.address == dev.address)
&& (d.has_name() || d.is_known_device_type())
2023-09-18 00:24:21 -07:00
}) {
let row = row![
icon::from_name(dev.icon.as_str()).size(16).symbolic(true),
text::body(dev.name.clone()).align_x(Alignment::Start),
2023-09-18 00:24:21 -07:00
]
2024-10-30 22:51:08 -04:00
.align_y(Alignment::Center)
2023-09-18 00:24:21 -07:00
.spacing(12);
2023-11-16 17:29:32 +00:00
visible_devices = visible_devices.push(
menu_button(row.width(Length::Fill))
.on_press(Message::Request(BluerRequest::PairDevice(dev.address))),
);
2023-09-18 00:24:21 -07:00
visible_devices_count += 1;
2023-04-05 20:40:22 -04:00
}
2023-09-18 00:24:21 -07:00
list_column.push(visible_devices.into());
}
let item_counter = visible_devices_count
// request confirmation is pretty big
+ if self.request_confirmation.is_some() {
5
} else {
0
};
if item_counter > 10 {
content = content
.push(scrollable(Column::with_children(list_column)).height(Length::Fixed(300.0)));
} else {
content = content.push(Column::with_children(list_column));
2023-02-08 18:38:09 -05:00
}
content = content
.push(padded_control(divider::horizontal::default()).padding([space_xxs, space_s]))
.push(menu_button(text::body(fl!("settings"))).on_press(Message::OpenSettings));
2024-05-02 17:40:00 -04:00
2023-09-18 08:31:27 -07:00
self.core.applet.popup_container(content).into()
2023-02-08 18:38:09 -05:00
}
fn subscription(&self) -> Subscription<Message> {
Subscription::batch(vec![
activation_token_subscription(0).map(Message::Token),
bluetooth_subscription(0).map(Message::BluetoothEvent),
2023-12-05 14:19:44 -05:00
self.timeline
.as_subscription()
.map(|(_, now)| Message::Frame(now)),
])
2023-02-08 18:38:09 -05:00
}
2024-10-30 22:51:08 -04:00
fn style(&self) -> Option<cosmic::iced_runtime::Appearance> {
2023-09-18 08:31:27 -07:00
Some(cosmic::applet::style())
2023-02-08 18:38:09 -05:00
}
fn on_close_requested(&self, id: window::Id) -> Option<Message> {
Some(Message::CloseRequested(id))
}
2023-02-08 18:38:09 -05:00
}