Skip to content

Commit 0ba4182

Browse files
committed
Add back cosmic-panel-button
1 parent 17c268d commit 0ba4182

File tree

7 files changed

+148
-6
lines changed

7 files changed

+148
-6
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ members = [
1111
"cosmic-applet-power",
1212
"cosmic-applet-time",
1313
"cosmic-applet-workspaces",
14+
"cosmic-panel-button",
1415
]
1516

1617
[profile.release]

cosmic-panel-app-button/data/com.system76.CosmicPanelAppButton.desktop

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Name=Cosmic Panel App Library Button
33
Comment=Write a GTK + Rust application
44
Type=Application
5-
Exec=cosmic-panel-button --id com.system76.CosmicAppLibrary
5+
Exec=cosmic-panel-button com.system76.CosmicAppLibrary
66
Terminal=false
77
Categories=GNOME;GTK;
88
Keywords=Gnome;GTK;

cosmic-panel-button/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "cosmic-panel-button"
3+
version = "0.1.0"
4+
edition = "2021"
5+
license = "GPL-3.0-or-later"
6+
7+
[dependencies]
8+
freedesktop-desktop-entry = "0.5.0"
9+
libcosmic = { git = "https://github.com/pop-os/libcosmic/", branch = "master", default-features = false, features = ["tokio", "wayland", "applet"] }

cosmic-panel-button/src/main.rs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
use cosmic::{
2+
applet::CosmicAppletHelper,
3+
iced::{
4+
self,
5+
wayland::{InitialSurface, SurfaceIdWrapper},
6+
Application,
7+
},
8+
iced_sctk::layout::Limits,
9+
iced_style::application,
10+
};
11+
use freedesktop_desktop_entry::DesktopEntry;
12+
use std::{env, fs, process::Command};
13+
14+
#[derive(Clone, Default)]
15+
struct Desktop {
16+
name: String,
17+
icon: Option<String>,
18+
exec: String,
19+
}
20+
21+
struct Button {
22+
desktop: Desktop,
23+
}
24+
25+
#[derive(Debug, Clone)]
26+
enum Msg {
27+
Press,
28+
}
29+
30+
impl iced::Application for Button {
31+
type Message = Msg;
32+
type Theme = cosmic::Theme;
33+
type Executor = cosmic::SingleThreadExecutor;
34+
type Flags = Desktop;
35+
36+
fn new(desktop: Desktop) -> (Self, iced::Command<Msg>) {
37+
(Button { desktop }, iced::Command::none())
38+
}
39+
40+
fn title(&self) -> String {
41+
String::from("Button")
42+
}
43+
44+
fn close_requested(&self, _id: SurfaceIdWrapper) -> Msg {
45+
unimplemented!()
46+
}
47+
48+
fn style(&self) -> <Self::Theme as application::StyleSheet>::Style {
49+
<Self::Theme as application::StyleSheet>::Style::Custom(|theme| application::Appearance {
50+
background_color: iced::Color::from_rgba(0.0, 0.0, 0.0, 0.0),
51+
text_color: theme.cosmic().on_bg_color().into(),
52+
})
53+
}
54+
55+
fn subscription(&self) -> iced::Subscription<Msg> {
56+
iced::Subscription::none()
57+
}
58+
59+
fn update(&mut self, message: Msg) -> iced::Command<Msg> {
60+
match message {
61+
Msg::Press => {
62+
let _ = Command::new("sh").arg("-c").arg(&self.desktop.exec).spawn();
63+
iced::Command::none()
64+
}
65+
}
66+
}
67+
68+
fn view(&self, _id: SurfaceIdWrapper) -> cosmic::Element<Msg> {
69+
// TODO icon?
70+
cosmic::widget::button(cosmic::theme::Button::Text)
71+
.text(&self.desktop.name)
72+
.on_press(Msg::Press)
73+
.into()
74+
}
75+
}
76+
77+
pub fn main() -> iced::Result {
78+
let id = env::args()
79+
.skip(1)
80+
.next()
81+
.expect("Requires desktop file id as argument.");
82+
83+
let filename = format!("{id}.desktop");
84+
let mut desktop = None;
85+
for mut path in freedesktop_desktop_entry::default_paths() {
86+
path.push(&filename);
87+
if let Ok(bytes) = fs::read_to_string(&path) {
88+
if let Ok(entry) = DesktopEntry::decode(&path, &bytes) {
89+
desktop = Some(Desktop {
90+
name: entry
91+
.name(None)
92+
.map(|x| x.to_string())
93+
.expect(&format!("Desktop file '{filename}' doesn't have `Name`")),
94+
icon: entry.icon().map(|x| x.to_string()),
95+
exec: entry
96+
.exec()
97+
.map(|x| x.to_string())
98+
.expect(&format!("Desktop file '{filename}' doesn't have `Exec`")),
99+
});
100+
break;
101+
}
102+
}
103+
}
104+
let desktop = desktop.expect(&format!(
105+
"Failed to find valid desktop file '{filename}' in search paths"
106+
));
107+
let helper = CosmicAppletHelper::default();
108+
let mut settings = iced::Settings {
109+
flags: desktop,
110+
..helper.window_settings()
111+
};
112+
match &mut settings.initial_surface {
113+
InitialSurface::XdgWindow(s) => {
114+
s.iced_settings.min_size = Some((1, 1));
115+
s.iced_settings.max_size = None;
116+
s.autosize = true;
117+
s.size_limits = Limits::NONE.min_height(1).min_width(1);
118+
}
119+
_ => unreachable!(),
120+
};
121+
Button::run(settings)
122+
}

cosmic-panel-workspaces-button/data/com.system76.CosmicPanelWorkspacesButton.desktop

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Name=Cosmic Panel Workspaces Button
33
Comment=Write a GTK + Rust application
44
Type=Application
5-
Exec=cosmic-panel-button -id com.system76.CosmicWorkspaces
5+
Exec=cosmic-panel-button com.system76.CosmicWorkspaces
66
Terminal=false
77
Categories=GNOME;GTK;
88
Keywords=Gnome;GTK;

justfile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ _install_power: (_install 'com.system76.CosmicAppletPower' 'cosmic-applet-power'
3939
_install_workspace: (_install 'com.system76.CosmicAppletWorkspaces' 'cosmic-applet-workspaces')
4040
_install_time: (_install 'com.system76.CosmicAppletTime' 'cosmic-applet-time')
4141

42-
# TODO: `cosmic-panel-button` no longer exists and hasn't been ported to Iced
43-
# _install_app_button: (_install 'com.system76.CosmicPanelAppButton' 'cosmic-panel-app-button')
44-
# _install_workspaces_button: (_install 'com.system76.CosmicPanelWorkspacesButton' 'cosmic-panel-workspaces-button')
42+
# TODO: Turn this into one configurable applet?
43+
_install_panel_button: (_install_bin 'cosmic-panel-button')
44+
_install_button id name: (_install_icon name + '/data/icons/' + id + '.svg') (_install_desktop name + '/data/' + id + '.desktop')
45+
_install_app_button: (_install_button 'com.system76.CosmicPanelAppButton' 'cosmic-panel-app-button')
46+
_install_workspaces_button: (_install_button 'com.system76.CosmicPanelWorkspacesButton' 'cosmic-panel-workspaces-button')
4547

4648
# Installs files into the system
47-
install: _install_app_list _install_audio _install_battery _install_bluetooth _install_graphics _install_network _install_notifications _install_power _install_workspace _install_time
49+
install: _install_app_list _install_audio _install_battery _install_bluetooth _install_graphics _install_network _install_notifications _install_power _install_workspace _install_time _install_panel_button _install_app_button _install_workspaces_button
4850

4951
# Extracts vendored dependencies if vendor=1
5052
_extract_vendor:

0 commit comments

Comments
 (0)