From 8973374a8097d5e6ef995b645d9ec80a0cb5e9e7 Mon Sep 17 00:00:00 2001 From: Luca Biendl <78647562+BrunoWallner@users.noreply.github.com> Date: Thu, 15 Aug 2024 10:17:37 +0200 Subject: [PATCH] feat(bluetooth): add battery indicator to devices with battery status --- cosmic-applet-bluetooth/src/app.rs | 28 +++++++++++++++++++++++- cosmic-applet-bluetooth/src/bluetooth.rs | 4 +++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/cosmic-applet-bluetooth/src/app.rs b/cosmic-applet-bluetooth/src/app.rs index fe5a6daa..39d40171 100644 --- a/cosmic-applet-bluetooth/src/app.rs +++ b/cosmic-applet-bluetooth/src/app.rs @@ -1,10 +1,11 @@ // Copyright 2023 System76 // SPDX-License-Identifier: GPL-3.0-only -use crate::bluetooth::{BluerDeviceStatus, BluerRequest, BluerState}; +use crate::bluetooth::{BluerDeviceStatus, BluerRequest, BluerState, DeviceProperty}; use cosmic::{ applet::token::subscription::{activation_token_subscription, TokenRequest, TokenUpdate}, cctk::sctk::reexports::calloop, + widget::text::body, }; use cosmic::{ @@ -367,6 +368,31 @@ impl cosmic::Application for CosmicBluetoothApplet { .align_items(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), + body(format!("{}%", battery)) + ) + .align_items(Alignment::Center) + .spacing(2) + .width(Length::Shrink); + + let content = container(status) + .align_x(Horizontal::Right) + .align_y(Vertical::Center); + + row = row.push(content); + } + match &dev.status { BluerDeviceStatus::Connected => { row = row.push( diff --git a/cosmic-applet-bluetooth/src/bluetooth.rs b/cosmic-applet-bluetooth/src/bluetooth.rs index 0a6e0a31..6e08424d 100644 --- a/cosmic-applet-bluetooth/src/bluetooth.rs +++ b/cosmic-applet-bluetooth/src/bluetooth.rs @@ -3,10 +3,12 @@ use std::{collections::HashMap, fmt::Debug, hash::Hash, mem, sync::Arc, time::Duration}; +pub use bluer::DeviceProperty; use bluer::{ agent::{Agent, AgentHandle}, - Adapter, Address, DeviceProperty, Session, Uuid, + Adapter, Address, Session, Uuid, }; + use cosmic::iced::{ self, futures::{SinkExt, StreamExt},