Power: Replace gtk applet with iced applet (#32)

* Power: Replace gtk applet with iced applet

No functionality yet, but the layout is nearly done. Only small
tweaks needed to font and svg backgrounds.

* Power: Add suspend+restart+shutdown functionality

* Power: Add lock + log out + settings

* Fix build.

* Fix window placement
This commit is contained in:
Brock 2022-12-12 11:41:16 -07:00 committed by GitHub
parent fd17241bcf
commit 31bea66801
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 3975 additions and 390 deletions

3644
applets/cosmic-applet-power/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -5,14 +5,39 @@ edition = "2021"
license = "GPL-3.0-or-later"
[dependencies]
futures-util = "0.3.21"
gtk4 = { git = "https://github.com/gtk-rs/gtk4-rs" }
adw = { git = "https://gitlab.gnome.org/World/Rust/libadwaita-rs", package = "libadwaita"}
libcosmic = { git = "https://github.com/pop-os/libcosmic", default-features = false }
libcosmic-applet = { path = "../../libcosmic-applet" }
logind-zbus = "3.0.1"
icon-loader = { version = "0.3.6", features = ["gtk"] }
libpulse-binding = "2.26.0"
libpulse-glib-binding = "2.25.0"
tokio = { version = "1.20.1", features=["full"] }
libcosmic = { git = "https://github.com/pop-os/libcosmic/", branch = "master", default-features = false, features = ["wayland", "applet"] }
iced_sctk = { git = "https://github.com/pop-os/iced-sctk" }
sctk = { package = "smithay-client-toolkit", git = "https://github.com/Smithay/client-toolkit", version = "0.16" }
nix = "0.24.1"
once_cell = "1.9.0"
relm4-macros = { git = "https://github.com/Relm4/Relm4.git", branch = "next" }
tokio = { version = "1.15.0", features = ["full"] }
zbus = "2.0.1"
[workspace]
resolved = "2"
[dependencies.iced]
git = "https://github.com/pop-os/iced.git"
branch = "sctk-cosmic"
# path = "../iced"
default-features = false
features = ["image", "svg", "tokio", "wayland"]
[dependencies.iced_native]
git = "https://github.com/pop-os/iced.git"
branch = "sctk-cosmic"
[dependencies.iced_futures]
git = "https://github.com/pop-os/iced.git"
branch = "sctk-cosmic"
# Until the 3.6.3 release, need the implementation of clone on zbus::Error
[dependencies.zbus]
git = "https://gitlab.freedesktop.org/dbus/zbus"
branch = "main"
# Until zbus 3.6.3 is released
[dependencies.logind-zbus]
git = "https://github.com/pop-os/logind-zbus"
branch = "main"

View file

@ -1,62 +1,295 @@
// SPDX-License-Identifier: GPL-3.0-or-later
use std::process;
#[macro_use]
extern crate relm4_macros;
use iced::widget::Space;
use cosmic::applet::CosmicAppletHelper;
use cosmic::widget::{horizontal_rule, icon};
use cosmic::Renderer;
use cosmic::iced::{
executor,
widget::{button, column, row},
window, Alignment, Application, Command, Length, Subscription,
};
use cosmic::iced_style::application::{self, Appearance};
use cosmic::iced_style::svg;
use cosmic::theme::{self, Svg};
use cosmic::{Element, Theme};
use iced_sctk::application::SurfaceIdWrapper;
use iced_sctk::commands::popup::{destroy_popup, get_popup};
use iced_sctk::widget::Row;
use iced_sctk::Color;
use logind_zbus::manager::ManagerProxy;
use logind_zbus::session::{SessionProxy, SessionType};
use logind_zbus::user::UserProxy;
use nix::unistd::getuid;
use zbus::Connection;
pub mod cosmic_session;
pub mod session_manager;
pub mod ui;
use gtk4::{gio::ApplicationFlags, prelude::*, Align, Button, Label, Orientation, Separator};
use once_cell::sync::Lazy;
use std::process::Command;
use tokio::runtime::Runtime;
use crate::cosmic_session::CosmicSessionProxy;
use crate::session_manager::SessionManagerProxy;
static RT: Lazy<Runtime> = Lazy::new(|| Runtime::new().expect("failed to build tokio runtime"));
fn main() {
let _monitors = libcosmic::init();
let application = gtk4::Application::new(None, ApplicationFlags::default());
application.connect_activate(build_ui);
application.run();
pub fn main() -> cosmic::iced::Result {
let helper = CosmicAppletHelper::default();
Audio::run(helper.window_settings())
}
fn build_ui(application: &gtk4::Application) {
view! {
window = libcosmic_applet::AppletWindow {
set_title: Some("COSMIC Power Applet"),
set_application: Some(application),
// TODO adjust battery icon based on charge
#[wrap(Some)]
set_child = &libcosmic_applet::AppletButton {
set_button_icon_name: "system-shutdown-symbolic",
#[wrap(Some)]
set_popover_child: main_box = &gtk4::Box {
set_orientation: Orientation::Vertical,
set_spacing: 10,
set_margin_top: 20,
set_margin_bottom: 20,
set_margin_start: 24,
set_margin_end: 24,
append: settings_button = &Button {
#[wrap(Some)]
set_child = &Label {
set_label: "Settings...",
set_halign: Align::Start,
set_hexpand: true
},
connect_clicked => move |_| {
let _ = Command::new("cosmic-settings").spawn();
}
},
append = &Separator {},
append: &ui::session::build(),
append: second_separator = &Separator {},
append: &ui::system::build(),
#[derive(Default)]
struct Audio {
applet_helper: CosmicAppletHelper,
icon_name: String,
theme: Theme,
popup: Option<window::Id>,
id_ctr: u32,
}
#[derive(Debug, Clone)]
enum Message {
Lock,
LogOut,
Suspend,
Restart,
Shutdown,
TogglePopup,
Settings,
Ignore,
Zbus(Result<(), zbus::Error>),
}
impl Application for Audio {
type Message = Message;
type Theme = Theme;
type Executor = executor::Default;
type Flags = ();
fn new(_flags: ()) -> (Audio, Command<Message>) {
(
Audio {
icon_name: "system-shutdown-symbolic".to_string(),
..Default::default()
},
Command::none(),
)
}
fn title(&self) -> String {
String::from("Power")
}
fn theme(&self) -> Theme {
self.theme
}
fn close_requested(&self, _id: iced_sctk::application::SurfaceIdWrapper) -> Self::Message {
Message::Ignore
}
fn style(&self) -> <Self::Theme as application::StyleSheet>::Style {
<Self::Theme as application::StyleSheet>::Style::Custom(|theme| Appearance {
background_color: Color::from_rgba(0.0, 0.0, 0.0, 0.0),
text_color: theme.cosmic().on_bg_color().into(),
})
}
fn subscription(&self) -> Subscription<Message> {
Subscription::none()
}
fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::TogglePopup => {
if let Some(p) = self.popup.take() {
destroy_popup(p)
} else {
self.id_ctr += 1;
let new_id = window::Id::new(self.id_ctr);
self.popup.replace(new_id);
let popup_settings = self.applet_helper.get_popup_settings(
window::Id::new(0),
new_id,
(400, 300),
None,
None,
);
get_popup(popup_settings)
}
}
Message::Settings => {
let _ = process::Command::new("cosmic-settings").spawn();
Command::none()
}
Message::Lock => Command::perform(lock(), Message::Zbus),
Message::LogOut => Command::perform(log_out(), Message::Zbus),
Message::Suspend => Command::perform(suspend(), Message::Zbus),
Message::Restart => Command::perform(restart(), Message::Zbus),
Message::Shutdown => Command::perform(shutdown(), Message::Zbus),
Message::Zbus(result) => {
if let Err(e) = result {
eprintln!("cosmic-applet-power ERROR: '{}'", e);
}
Command::none()
}
Message::Ignore => Command::none(),
}
}
fn view(&self, id: SurfaceIdWrapper) -> Element<Message> {
match id {
SurfaceIdWrapper::LayerSurface(_) => unimplemented!(),
SurfaceIdWrapper::Window(_) => self
.applet_helper
.icon_button(&self.icon_name)
.on_press(Message::TogglePopup)
.into(),
SurfaceIdWrapper::Popup(_) => {
let settings = row_button(vec!["Settings...".into()])
.on_press(Message::Settings);
let session = column![
row_button(vec![
text_icon("system-lock-screen-symbolic", 24).into(),
"Lock Screen".into(),
Space::with_width(Length::Fill).into(),
"Super + Escape".into(),
])
.on_press(Message::Lock),
row_button(vec![
text_icon("system-log-out-symbolic", 24).into(),
"Log Out".into(),
Space::with_width(Length::Fill).into(),
"Ctrl + Alt + Delete".into(),
])
.on_press(Message::LogOut),
];
let power = row![
power_buttons("system-lock-screen-symbolic", "Suspend")
.on_press(Message::Suspend),
power_buttons("system-restart-symbolic", "Restart").on_press(Message::Restart),
power_buttons("system-shutdown-symbolic", "Shutdown")
.on_press(Message::Shutdown),
]
.spacing(24)
.padding([0, 24]);
let content = column![]
.align_items(Alignment::Start)
.spacing(12)
.padding([24, 0])
.push(settings)
.push(horizontal_rule(1))
.push(session)
.push(horizontal_rule(1))
.push(power);
self.applet_helper.popup_container(content).into()
}
}
}
window.show();
}
// ### UI Helplers
// todo put into libcosmic doing so will fix the row_button's boarder radius
fn row_button(mut content: Vec<Element<Message>>) -> iced_sctk::widget::Button<Message, Renderer> {
content.insert(0, Space::with_width(Length::Units(24)).into());
content.push(Space::with_width(Length::Units(24)).into());
button(
Row::with_children(content)
.spacing(5)
.align_items(Alignment::Center),
)
.width(Length::Fill)
.height(Length::Units(35))
.style(theme::Button::Text)
}
fn power_buttons<'a>(
name: &'a str,
text: &'a str,
) -> iced_sctk::widget::Button<'a, Message, Renderer> {
button(
column![text_icon(name, 40), text]
.spacing(5)
.align_items(Alignment::Center),
)
.width(Length::Fill)
.height(Length::Units(75))
.style(theme::Button::Text)
}
fn text_icon(name: &str, size: u16) -> cosmic::widget::Icon {
icon(name, size).style(Svg::Custom(|theme| svg::Appearance {
fill: Some(theme.palette().text),
}))
}
// ### System helpers
async fn restart() -> zbus::Result<()> {
let connection = Connection::system().await?;
let manager_proxy = ManagerProxy::new(&connection).await?;
manager_proxy.reboot(true).await
}
async fn shutdown() -> zbus::Result<()> {
let connection = Connection::system().await?;
let manager_proxy = ManagerProxy::new(&connection).await?;
manager_proxy.power_off(true).await
}
async fn suspend() -> zbus::Result<()> {
let connection = Connection::system().await?;
let manager_proxy = ManagerProxy::new(&connection).await?;
manager_proxy.suspend(true).await
}
async fn lock() -> zbus::Result<()> {
let connection = Connection::system().await?;
let manager_proxy = ManagerProxy::new(&connection).await?;
// Get the session this current process is running in
let our_uid = getuid().as_raw() as u32;
let user_path = manager_proxy.get_user(our_uid).await?;
let user = UserProxy::builder(&connection)
.path(user_path)?
.build()
.await?;
// Lock all non-TTY sessions of this user
let sessions = user.sessions().await?;
for (_, session_path) in sessions {
let session = SessionProxy::builder(&connection)
.path(session_path)?
.build()
.await?;
if session.type_().await? != SessionType::TTY {
session.lock().await?;
}
}
Ok(())
}
async fn log_out() -> zbus::Result<()> {
let session_type = std::env::var("XDG_CURRENT_DESKTOP").ok();
let connection = Connection::session().await?;
match session_type.as_ref().map(|s| s.trim()) {
Some("pop:COSMIC") => {
let cosmic_session = CosmicSessionProxy::new(&connection).await?;
cosmic_session.exit().await?;
}
Some("pop:GNOME") => {
let manager_proxy = SessionManagerProxy::new(&connection).await?;
manager_proxy.logout(0).await?;
}
Some(desktop) => {
eprintln!("unknown XDG_CURRENT_DESKTOP: {desktop}")
}
None => {}
}
Ok(())
}

View file

@ -22,7 +22,7 @@
use zbus::dbus_proxy;
#[dbus_proxy(interface = "org.gnome.SessionManager")]
#[dbus_proxy(interface = "org.gnome.SessionManager", assume_defaults = true)]
trait SessionManager {
/// CanRebootToFirmwareSetup method
fn can_reboot_to_firmware_setup(&self) -> zbus::Result<bool>;

View file

@ -1,35 +0,0 @@
image.panel_icon {
padding-left: 0px;
padding-right: 0px;
padding-top: 0px;
padding-bottom: 0px;
}
button.panel_icon {
border-radius: 12px;
transition: 100ms;
padding: 4px;
border-color: transparent;
background: transparent;
outline-color: transparent;
}
button.panel_icon:hover {
border-radius: 12px;
transition: 100ms;
padding: 4px;
border-color: rgba(255, 255, 255, 0.1);
outline-color: rgba(255, 255, 255, 0.1);
background: rgba(255, 255, 255, 0.1);
}
window.root_window {
background: transparent;
}
box.icon_box {
padding: 0px;
border-color: transparent;
background: transparent;
outline-color: transparent;
}

View file

@ -1,4 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pub mod session;
pub mod system;

View file

@ -1,113 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-or-later
use crate::{cosmic_session::CosmicSessionProxy, session_manager::SessionManagerProxy, RT};
use gtk4::{prelude::*, Align, Button, Image, Label, Orientation};
use logind_zbus::{
manager::ManagerProxy,
session::{SessionProxy, SessionType},
user::UserProxy,
};
use nix::unistd::getuid;
use zbus::Connection;
async fn lock_screen() -> zbus::Result<()> {
let connection = Connection::system().await?;
let manager_proxy = ManagerProxy::new(&connection).await?;
// Get the session this current process is running in
let our_uid = getuid().as_raw() as u32;
let user_path = manager_proxy.get_user(our_uid).await?;
let user = UserProxy::builder(&connection)
.path(user_path)?
.build()
.await?;
// Lock all non-TTY sessions of this user
let sessions = user.sessions().await?;
for (_, session_path) in sessions {
let session = SessionProxy::builder(&connection)
.path(session_path)?
.build()
.await?;
if session.type_().await? != SessionType::TTY {
session.lock().await?;
}
}
Ok(())
}
async fn log_out() -> zbus::Result<()> {
let session_type = std::env::var("XDG_CURRENT_DESKTOP").ok();
let connection = Connection::session().await?;
match session_type.as_ref().map(|s| s.trim()) {
Some("pop:COSMIC") => {
let cosmic_session = CosmicSessionProxy::new(&connection).await?;
cosmic_session.exit().await?;
}
Some("pop:GNOME") => {
let manager_proxy = SessionManagerProxy::new(&connection).await?;
manager_proxy.logout(0).await?;
}
Some(desktop) => {
eprintln!("unknown XDG_CURRENT_DESKTOP: {desktop}")
}
None => {}
}
Ok(())
}
pub fn build() -> gtk4::Box {
view! {
inner_box = gtk4::Box {
set_orientation: Orientation::Vertical,
set_spacing: 5,
append: lock_screen_button = &Button {
#[wrap(Some)]
set_child: lock_screen_box = &gtk4::Box {
set_orientation: Orientation::Horizontal,
set_spacing: 10,
append: lock_screen_icon = &Image {
set_icon_name: Some("system-lock-screen-symbolic"),
},
append: lock_screen_label = &Label {
set_label: "Lock Screen",
set_halign: Align::Start,
set_hexpand: true
},
append: lock_screen_hotkey_label = &Label {
set_label: "Super + Escape",
set_halign: Align::End
}
}
},
append: log_out_button = &Button {
#[wrap(Some)]
set_child: log_out_box = &gtk4::Box {
set_orientation: Orientation::Horizontal,
set_spacing: 10,
append: log_out_icon = &Image {
set_icon_name: Some("system-log-out-symbolic"),
},
append: log_out_label = &Label {
set_label: "Log Out",
set_halign: Align::Start,
set_hexpand: true
},
append: log_out_hotkey_label = &Label {
set_label: "Ctrl + Alt + Delete",
set_halign: Align::End
}
}
}
}
}
lock_screen_button.connect_clicked(|_| {
RT.spawn(async move {
lock_screen().await.expect("failed to lock screen");
});
});
log_out_button.connect_clicked(|_| {
RT.spawn(async move {
log_out().await.expect("failed to log out");
});
});
inner_box
}

View file

@ -1,79 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-or-later
use crate::RT;
use gtk4::{prelude::*, Button, IconSize, Image, Label, Orientation};
use logind_zbus::manager::ManagerProxy;
use zbus::Connection;
async fn restart() -> zbus::Result<()> {
let connection = Connection::system().await?;
let manager_proxy = ManagerProxy::new(&connection).await?;
manager_proxy.reboot(true).await
}
async fn shut_down() -> zbus::Result<()> {
let connection = Connection::system().await?;
let manager_proxy = ManagerProxy::new(&connection).await?;
manager_proxy.power_off(true).await
}
async fn suspend() -> zbus::Result<()> {
let connection = Connection::system().await?;
let manager_proxy = ManagerProxy::new(&connection).await?;
manager_proxy.suspend(true).await
}
pub fn build() -> gtk4::Box {
let suspend_button = create_button("Suspend", "system-suspend-symbolic");
let restart_button = create_button("Restart", "system-reboot-symbolic");
let shut_down_button = create_button("Shut Down", "system-shutdown-symbolic");
suspend_button.connect_clicked(|_| {
RT.spawn(async move {
suspend().await.expect("failed to suspend system");
});
});
restart_button.connect_clicked(|_| {
RT.spawn(async move {
restart().await.expect("failed to reboot system");
});
});
shut_down_button.connect_clicked(|_| {
RT.spawn(async move {
shut_down().await.expect("failed to shut down system");
});
});
view! {
inner_box = gtk4::Box {
set_orientation: Orientation::Horizontal,
set_spacing: 24,
append: &suspend_button,
append: &restart_button,
append: &shut_down_button,
}
}
inner_box
}
pub fn create_button(name: &str, icon_name: &str) -> Button {
view! {
button = Button {
#[wrap(Some)]
set_child: inner_box = &gtk4::Box {
set_orientation: Orientation::Vertical,
set_spacing: 8,
set_margin_start: 8,
set_margin_end: 8,
set_margin_top: 8,
set_margin_bottom: 8,
append: icon = &Image {
set_icon_name: Some(icon_name),
set_icon_size: IconSize::Large
},
append: label = &Label {
set_label: name
}
}
}
}
button
}