From c9f8f0ea39ced35a60b304ce1ae93f6583ae4b47 Mon Sep 17 00:00:00 2001 From: wiiznokes <78230769+wiiznokes@users.noreply.github.com> Date: Tue, 3 Sep 2024 00:32:40 +0200 Subject: [PATCH] fix: display osd on keyboard event --- src/components/app.rs | 45 +++++++++++++++++------------------ src/subscriptions/keyboard.rs | 33 +++++++++++++++++++++++++ src/subscriptions/mod.rs | 1 + 3 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 src/subscriptions/keyboard.rs diff --git a/src/components/app.rs b/src/components/app.rs index 1adf46e..6511f75 100644 --- a/src/components/app.rs +++ b/src/components/app.rs @@ -11,7 +11,7 @@ use std::{ use crate::{ components::{osd_indicator, polkit_dialog}, - subscriptions::{dbus, polkit_agent}, + subscriptions::{dbus, keyboard, polkit_agent}, }; use cosmic_settings_subscriptions::{ airplane_mode, pulse, settings_daemon, @@ -28,6 +28,7 @@ pub enum Msg { OsdIndicator(osd_indicator::Msg), AirplaneMode(bool), KeyboardBacklight(KeyboardBacklightUpdate), + Keyboard(keyboard::Msg), } enum Surface { @@ -192,12 +193,6 @@ impl cosmic::Application for App { self.sink_mute = Some(mute); } else if self.sink_mute != Some(mute) { self.sink_mute = Some(mute); - if let Some(sink_volume) = self.sink_volume { - return self.create_indicator(osd_indicator::Params::SinkVolume( - sink_volume, - mute, - )); - } } } pulse::Event::SinkVolume(volume) => { @@ -212,11 +207,6 @@ impl cosmic::Application for App { self.sink_volume = Some(volume); } else if self.sink_volume != Some(volume) { self.sink_volume = Some(volume); - if let Some(mute) = self.sink_mute { - return self.create_indicator(osd_indicator::Params::SinkVolume( - volume, mute, - )); - } } } pulse::Event::SourceMute(mute) => { @@ -224,12 +214,6 @@ impl cosmic::Application for App { self.source_mute = Some(mute); } else if self.source_mute != Some(mute) { self.source_mute = Some(mute); - if let Some(source_volume) = self.source_volume { - return self.create_indicator(osd_indicator::Params::SourceVolume( - source_volume, - mute, - )); - } } } pulse::Event::SourceVolume(volume) => { @@ -237,11 +221,6 @@ impl cosmic::Application for App { self.source_volume = Some(volume); } else if self.source_volume != Some(volume) { self.source_volume = Some(volume); - if let Some(mute) = self.source_mute { - return self.create_indicator(osd_indicator::Params::SourceVolume( - volume, mute, - )); - } } } } @@ -280,6 +259,24 @@ impl cosmic::Application for App { } } }, + Msg::Keyboard(msg) => { + match msg { + keyboard::Msg::BrightnessDown | keyboard::Msg::BrightnessUp => { + // todo ? + } + keyboard::Msg::AudioVolumeDown + | keyboard::Msg::AudioVolumeUp + | keyboard::Msg::AudioVolumeMute => { + if let (Some(volume), Some(mute)) = (self.source_volume, self.source_mute) { + return self.create_indicator(osd_indicator::Params::SourceVolume( + volume, mute, + )); + } + } + } + + Command::none() + } } } @@ -306,6 +303,8 @@ impl cosmic::Application for App { Surface::PolkitDialog(state) => state.subscription().with(*id).map(Msg::PolkitDialog), })); + subscriptions.push(keyboard::subscription().map(Msg::Keyboard)); + iced::Subscription::batch(subscriptions) } diff --git a/src/subscriptions/keyboard.rs b/src/subscriptions/keyboard.rs new file mode 100644 index 0000000..2a5075a --- /dev/null +++ b/src/subscriptions/keyboard.rs @@ -0,0 +1,33 @@ +use cosmic::{ + iced::{self, event::listen_with, keyboard::Key}, + iced_core::keyboard, +}; + +#[derive(Clone, Debug)] +pub enum Msg { + BrightnessDown, + BrightnessUp, + AudioVolumeDown, + AudioVolumeUp, + AudioVolumeMute, +} + +pub fn subscription() -> iced::Subscription { + listen_with(|event, status| match status { + iced::event::Status::Ignored => match event { + iced::Event::Keyboard(keyboard::Event::KeyPressed { + key: Key::Named(named), + .. + }) => match named { + keyboard::key::Named::BrightnessDown => Some(Msg::BrightnessDown), + keyboard::key::Named::BrightnessUp => Some(Msg::BrightnessUp), + keyboard::key::Named::AudioVolumeDown => Some(Msg::AudioVolumeDown), + keyboard::key::Named::AudioVolumeUp => Some(Msg::AudioVolumeUp), + keyboard::key::Named::AudioVolumeMute => Some(Msg::AudioVolumeMute), + _ => None, + }, + _ => None, + }, + iced::event::Status::Captured => None, + }) +} diff --git a/src/subscriptions/mod.rs b/src/subscriptions/mod.rs index 8813d14..b41e6ae 100644 --- a/src/subscriptions/mod.rs +++ b/src/subscriptions/mod.rs @@ -1,3 +1,4 @@ pub mod dbus; +pub mod keyboard; pub mod polkit_agent; pub mod polkit_agent_helper;