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

launch settings with activation token #157

Merged
merged 6 commits into from
Nov 22, 2023
Merged
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
112 changes: 60 additions & 52 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ cosmic-time = { git = "https://github.com/pop-os/cosmic-time", default-features
] }
libcosmic = { git = "https://github.com/pop-os/libcosmic", default-features = false, features = [
"applet",
"applet-token",
"tokio",
"wayland",
"process",
] }
zbus = { version = "3.14", default-features = false, features = ["tokio"] }

tracing = "0.1"

[profile.release]
lto = "thin"
Expand Down
61 changes: 47 additions & 14 deletions cosmic-applet-audio/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
mod localize;

use crate::localize::localize;
use crate::pulse::DeviceInfo;
use config::AudioAppletConfig;
use cosmic::app::Command;
use cosmic::applet::cosmic_panel_config::PanelAnchor;
use cosmic::applet::menu_button;
use cosmic::applet::menu_control_padding;
use cosmic::applet::padded_control;
use cosmic::applet::token::subscription::{
activation_token_subscription, TokenRequest, TokenUpdate,
};
use cosmic::cctk::sctk::reexports::calloop;
use cosmic::cosmic_config::CosmicConfigEntry;
use cosmic::iced::widget;
use cosmic::iced::Limits;
use cosmic::iced::{
self,
widget::{column, row, slider, text},
window, Alignment, Length, Subscription,
};
use cosmic::iced_runtime::core::alignment::Horizontal;

use cosmic::iced_style::application;
use cosmic::widget::button;
use cosmic::widget::horizontal_space;
use cosmic::widget::Column;
use cosmic::widget::Row;
use cosmic::widget::{divider, icon};
use cosmic::Renderer;

use cosmic::iced::{
self,
widget::{column, row, slider, text},
window, Alignment, Length, Subscription,
};
use cosmic::iced_style::application;
use cosmic::{Element, Theme};
use cosmic_time::{anim, chain, id, once_cell::sync::Lazy, Instant, Timeline};

use iced::wayland::popup::{destroy_popup, get_popup};
use iced::widget::container;
use libpulse_binding::volume::VolumeLinear;
use mpris2_zbus::player::PlaybackStatus;
use mpris_subscription::MprisRequest;
use mpris_subscription::MprisUpdate;

mod config;
mod mpris_subscription;
mod pulse;
use crate::localize::localize;
use crate::pulse::DeviceInfo;
use libpulse_binding::volume::VolumeLinear;

pub fn main() -> cosmic::iced::Result {
pretty_env_logger::init();

Expand Down Expand Up @@ -67,6 +67,7 @@
timeline: Timeline,
config: AudioAppletConfig,
player_status: Option<mpris_subscription::PlayerStatus>,
token_tx: Option<calloop::channel::Sender<TokenRequest>>,
}

impl Audio {
Expand Down Expand Up @@ -144,6 +145,8 @@
ConfigChanged(AudioAppletConfig),
Mpris(mpris_subscription::MprisUpdate),
MprisRequest(MprisRequest),
Token(TokenUpdate),
OpenSettings,
}

impl Audio {
Expand Down Expand Up @@ -272,6 +275,7 @@
inputs: vec![],
icon_name: "audio-volume-high-symbolic".to_string(),
input_icon_name: "audio-input-microphone-symbolic".to_string(),
token_tx: None,
..Default::default()
},
Command::none(),
Expand Down Expand Up @@ -444,10 +448,10 @@
pulse::Event::Disconnected => {
self.pulse_state.disconnected();
if let Some(mut conn) = self.pulse_state.connection().cloned() {
_ = tokio::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
conn.send(pulse::Message::UpdateConnection);
});

Check warning on line 454 in cosmic-applet-audio/src/main.rs

View workflow job for this annotation

GitHub Actions / linting

non-binding `let` on a future

warning: non-binding `let` on a future --> cosmic-applet-audio/src/main.rs:451:25 | 451 | / _ = tokio::spawn(async move { 452 | | tokio::time::sleep(tokio::time::Duration::from_secs(30)).await; 453 | | conn.send(pulse::Message::UpdateConnection); 454 | | }); | |__________________________^ | = help: consider awaiting the future or dropping explicitly with `std::mem::drop` = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_underscore_future = note: `#[warn(clippy::let_underscore_future)]` on by default

Check warning on line 454 in cosmic-applet-audio/src/main.rs

View workflow job for this annotation

GitHub Actions / linting

non-binding `let` on a future

warning: non-binding `let` on a future --> cosmic-applet-audio/src/main.rs:451:25 | 451 | / _ = tokio::spawn(async move { 452 | | tokio::time::sleep(tokio::time::Duration::from_secs(30)).await; 453 | | conn.send(pulse::Message::UpdateConnection); 454 | | }); | |__________________________^ | = help: consider awaiting the future or dropping explicitly with `std::mem::drop` = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_underscore_future = note: `#[warn(clippy::let_underscore_future)]` on by default
}
}
},
Expand Down Expand Up @@ -513,6 +517,34 @@
}),
};
}
Message::OpenSettings => {
let exec = "cosmic-settings sound".to_string();
if let Some(tx) = self.token_tx.as_ref() {
let _ = tx.send(TokenRequest {
app_id: Self::APP_ID.to_string(),
exec,
});
} else {
tracing::error!("Wayland tx is None");
};
}
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("sound");
if let Some(token) = token {
cmd.env("XDG_ACTIVATION_TOKEN", &token);
cmd.env("DESKTOP_STARTUP_ID", &token);
}
cosmic::process::spawn(cmd);
}
},
};

Command::none()
Expand All @@ -539,6 +571,7 @@
}
}),
mpris_subscription::mpris_subscription(0).map(Message::Mpris),
activation_token_subscription(0).map(Message::Token),
])
}

Expand Down Expand Up @@ -728,7 +761,7 @@
)
.padding([0, 24]),
padded_control(divider::horizontal::default()),
menu_button(text(fl!("sound-settings")).size(14))
menu_button(text(fl!("sound-settings")).size(14)).on_press(Message::OpenSettings)
]
.align_items(Alignment::Start)
.padding([8, 0]);
Expand Down
1 change: 1 addition & 0 deletions cosmic-applet-battery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
once_cell = "1.16.0"
libcosmic.workspace = true
cosmic-time.workspace = true
tracing.workspace = true
futures = "0.3"
zbus.workspace = true
log = "0.4"
Expand Down
44 changes: 39 additions & 5 deletions cosmic-applet-battery/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ use crate::upower_device::{device_subscription, DeviceDbusEvent};
use crate::upower_kbdbacklight::{
kbd_backlight_subscription, KeyboardBacklightRequest, KeyboardBacklightUpdate,
};
use cosmic::applet::token::subscription::{
activation_token_subscription, TokenRequest, TokenUpdate,
};
use cosmic::applet::{menu_button, padded_control};
use cosmic::cctk::sctk::reexports::calloop;
use cosmic::iced::alignment::Horizontal;
use cosmic::iced::wayland::popup::{destroy_popup, get_popup};
use cosmic::iced::{
Expand Down Expand Up @@ -68,6 +72,7 @@ struct CosmicBatteryApplet {
power_profile: Power,
power_profile_sender: Option<UnboundedSender<PowerProfileRequest>>,
timeline: Timeline,
token_tx: Option<calloop::channel::Sender<TokenRequest>>,
}

impl CosmicBatteryApplet {
Expand Down Expand Up @@ -138,14 +143,15 @@ enum Message {
SetChargingLimit(chain::Toggler, bool),
UpdateKbdBrightness(f64),
UpdateScreenBrightness(f64),
OpenBatterySettings,
InitKbdBacklight(UnboundedSender<KeyboardBacklightRequest>, f64),
InitScreenBacklight(UnboundedSender<ScreenBacklightRequest>, f64),
Errored(String),
InitProfile(UnboundedSender<PowerProfileRequest>, Power),
Profile(Power),
SelectProfile(Power),
Frame(Instant),
Token(TokenUpdate),
OpenSettings,
}

impl cosmic::Application for CosmicBatteryApplet {
Expand All @@ -166,6 +172,8 @@ impl cosmic::Application for CosmicBatteryApplet {
core,
icon_name: "battery-symbolic".to_string(),
display_icon_name: "display-brightness-symbolic".to_string(),
token_tx: None,

..Default::default()
},
Command::none(),
Expand Down Expand Up @@ -202,9 +210,6 @@ impl cosmic::Application for CosmicBatteryApplet {
self.timeline.set_chain(chain).start();
self.set_charging_limit(enable);
}
Message::OpenBatterySettings => {
// TODO Ashley
}
Message::Errored(e) => {
error!("{}", e);
}
Expand Down Expand Up @@ -288,6 +293,34 @@ impl cosmic::Application for CosmicBatteryApplet {
self.popup = None;
}
}
Message::OpenSettings => {
let exec = "cosmic-settings power".to_string();
if let Some(tx) = self.token_tx.as_ref() {
let _ = tx.send(TokenRequest {
app_id: Self::APP_ID.to_string(),
exec,
});
} else {
tracing::error!("Wayland tx is None");
};
}
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("power");
if let Some(token) = token {
cmd.env("XDG_ACTIVATION_TOKEN", &token);
cmd.env("DESKTOP_STARTUP_ID", &token);
}
cosmic::process::spawn(cmd);
}
},
}
Command::none()
}
Expand Down Expand Up @@ -436,7 +469,7 @@ impl cosmic::Application for CosmicBatteryApplet {
),
padded_control(divider::horizontal::default()),
menu_button(text(fl!("power-settings")).size(14).width(Length::Fill))
.on_press(Message::OpenBatterySettings)
.on_press(Message::OpenSettings)
]
.padding([8, 0]),
)
Expand Down Expand Up @@ -472,6 +505,7 @@ impl cosmic::Application for CosmicBatteryApplet {
self.timeline
.as_subscription()
.map(|(_, now)| Message::Frame(now)),
activation_token_subscription(0).map(Message::Token),
])
}

Expand Down
42 changes: 40 additions & 2 deletions cosmic-applet-bluetooth/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::bluetooth::{BluerDeviceStatus, BluerRequest, BluerState};
use cosmic::applet::token::subscription::{
activation_token_subscription, TokenRequest, TokenUpdate,
};
use cosmic::cctk::sctk::reexports::calloop;

use cosmic::applet::{menu_button, padded_control};
use cosmic::Command;
use cosmic::{
Expand Down Expand Up @@ -39,6 +44,7 @@ struct CosmicBluetoothApplet {
// UI state
show_visible_devices: bool,
request_confirmation: Option<(BluerDevice, String, Sender<bool>)>,
token_tx: Option<calloop::channel::Sender<TokenRequest>>,
}

impl CosmicBluetoothApplet {
Expand All @@ -62,6 +68,8 @@ enum Message {
Request(BluerRequest),
Cancel,
Confirm,
Token(TokenUpdate),
OpenSettings,
}

impl cosmic::Application for CosmicBluetoothApplet {
Expand All @@ -78,6 +86,7 @@ impl cosmic::Application for CosmicBluetoothApplet {
Self {
core,
icon_name: "bluetooth-symbolic".to_string(),
token_tx: None,
..Default::default()
},
Command::none(),
Expand Down Expand Up @@ -284,6 +293,32 @@ impl cosmic::Application for CosmicBluetoothApplet {
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);
}
cosmic::process::spawn(cmd);
}
},
}
self.update_icon();
Command::none()
Expand Down Expand Up @@ -492,13 +527,16 @@ impl cosmic::Application for CosmicBluetoothApplet {
content = content.push(padded_control(divider::horizontal::default()));
content = content.push(
menu_button(text(fl!("settings")).size(14).width(Length::Fill))
.on_press(Message::Ignore),
.on_press(Message::OpenSettings),
);
self.core.applet.popup_container(content).into()
}

fn subscription(&self) -> Subscription<Message> {
bluetooth_subscription(0).map(Message::BluetoothEvent)
Subscription::batch(vec![
activation_token_subscription(0).map(Message::Token),
bluetooth_subscription(0).map(Message::BluetoothEvent),
])
}

fn style(&self) -> Option<<Theme as application::StyleSheet>::Style> {
Expand Down
Loading
Loading