Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: display osd on keyboard event #60

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions src/components/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -28,6 +28,7 @@ pub enum Msg {
OsdIndicator(osd_indicator::Msg),
AirplaneMode(bool),
KeyboardBacklight(KeyboardBacklightUpdate),
Keyboard(keyboard::Msg),
}

enum Surface {
Expand Down Expand Up @@ -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) => {
Expand All @@ -212,36 +207,20 @@ 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) => {
if self.source_mute.is_none() {
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) => {
if self.source_volume.is_none() {
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,
));
}
}
}
}
Expand Down Expand Up @@ -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()
}
}
}

Expand All @@ -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)
}

Expand Down
33 changes: 33 additions & 0 deletions src/subscriptions/keyboard.rs
Original file line number Diff line number Diff line change
@@ -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<Msg> {
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,
})
}
1 change: 1 addition & 0 deletions src/subscriptions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod dbus;
pub mod keyboard;
pub mod polkit_agent;
pub mod polkit_agent_helper;