cosmic-applets/cosmic-panel-button/src/main.rs

114 lines
3.5 KiB
Rust
Raw Normal View History

use cosmic::applet::cosmic_panel_config::PanelAnchor;
use cosmic::iced::Length;
use cosmic::iced_widget::{row, text};
use cosmic::widget::vertical_space;
use cosmic::{app, iced, iced_style::application, theme::Theme};
2023-02-17 20:05:42 -08:00
use freedesktop_desktop_entry::DesktopEntry;
use std::{env, fs, process::Command};
#[derive(Clone, Default)]
struct Desktop {
name: String,
icon: Option<String>,
exec: String,
}
struct Button {
core: cosmic::app::Core,
2023-02-17 20:05:42 -08:00
desktop: Desktop,
}
#[derive(Debug, Clone)]
enum Msg {
Press,
}
impl cosmic::Application for Button {
2023-02-17 20:05:42 -08:00
type Message = Msg;
type Executor = cosmic::SingleThreadExecutor;
type Flags = Desktop;
const APP_ID: &'static str = "com.system76.CosmicPanelButton";
2023-02-17 20:05:42 -08:00
fn init(core: cosmic::app::Core, desktop: Desktop) -> (Self, app::Command<Msg>) {
2023-11-16 18:32:31 +00:00
(Self { core, desktop }, app::Command::none())
2023-02-17 20:05:42 -08:00
}
fn core(&self) -> &cosmic::app::Core {
&self.core
2023-02-17 20:05:42 -08:00
}
fn core_mut(&mut self) -> &mut cosmic::app::Core {
&mut self.core
2023-02-17 20:05:42 -08:00
}
fn style(&self) -> Option<<Theme as application::StyleSheet>::Style> {
2023-09-18 08:31:27 -07:00
Some(cosmic::applet::style())
2023-02-17 20:05:42 -08:00
}
fn update(&mut self, message: Msg) -> app::Command<Msg> {
2023-02-17 20:05:42 -08:00
match message {
Msg::Press => {
let _ = Command::new("sh").arg("-c").arg(&self.desktop.exec).spawn();
2023-06-09 16:10:20 -04:00
}
2023-02-17 20:05:42 -08:00
}
app::Command::none()
2023-02-17 20:05:42 -08:00
}
fn view(&self) -> cosmic::Element<Msg> {
if matches!(
self.core.applet.anchor,
PanelAnchor::Left | PanelAnchor::Right
) && self.desktop.icon.is_some()
{
self.core
.applet
.icon_button(self.desktop.icon.as_ref().unwrap())
} else {
let content = row!(
text(&self.desktop.name).size(14.0),
vertical_space(Length::Fixed(
(self.core.applet.suggested_size().1 + 2 * self.core.applet.suggested_padding())
as f32
))
)
.align_items(iced::Alignment::Center);
cosmic::widget::button(content)
.padding([0, self.core.applet.suggested_padding()])
.style(cosmic::theme::Button::AppletIcon)
}
.on_press(Msg::Press)
.into()
2023-02-17 20:05:42 -08:00
}
}
pub fn main() -> iced::Result {
let id = env::args()
2023-09-18 00:24:21 -07:00
.nth(1)
2023-02-17 20:05:42 -08:00
.expect("Requires desktop file id as argument.");
let filename = format!("{id}.desktop");
let mut desktop = None;
for mut path in freedesktop_desktop_entry::default_paths() {
path.push(&filename);
if let Ok(bytes) = fs::read_to_string(&path) {
if let Ok(entry) = DesktopEntry::decode(&path, &bytes) {
2023-11-17 07:04:57 +00:00
desktop =
Some(Desktop {
name: entry.name(None).map(|x| x.to_string()).unwrap_or_else(|| {
panic!("Desktop file '{filename}' doesn't have `Name`")
}),
icon: entry.icon().map(|x| x.to_string()),
exec: entry.exec().map(|x| x.to_string()).unwrap_or_else(|| {
panic!("Desktop file '{filename}' doesn't have `Exec`")
}),
});
2023-02-17 20:05:42 -08:00
break;
}
}
}
2023-11-17 07:04:57 +00:00
let desktop = desktop.unwrap_or_else(|| {
panic!("Failed to find valid desktop file '{filename}' in search paths")
});
2023-09-18 08:31:27 -07:00
cosmic::applet::run::<Button>(true, desktop)
2023-02-17 20:05:42 -08:00
}