feat: add cosmic-panel-button to multicall binary

This commit is contained in:
wiiznokes 2024-09-09 19:08:21 +02:00 committed by GitHub
parent 7e61e8b829
commit b449d0668b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 185 additions and 174 deletions

1
Cargo.lock generated
View file

@ -1214,6 +1214,7 @@ dependencies = [
"cosmic-applet-tiling",
"cosmic-applet-time",
"cosmic-applet-workspaces",
"cosmic-panel-button",
"libcosmic",
"tracing",
"tracing-log",

View file

@ -18,6 +18,7 @@ cosmic-applet-tiling = { path = "../cosmic-applet-tiling" }
cosmic-applet-time = { path = "../cosmic-applet-time" }
cosmic-applet-workspaces = { path = "../cosmic-applet-workspaces" }
cosmic-applet-input-sources = { path = "../cosmic-applet-input-sources"}
cosmic-panel-button = { path = "../cosmic-panel-button"}
libcosmic.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true

View file

@ -30,6 +30,7 @@ fn main() -> cosmic::iced::Result {
"cosmic-applet-time" => cosmic_applet_time::run(),
"cosmic-applet-workspaces" => cosmic_applet_workspaces::run(),
"cosmic-applet-input-sources" => cosmic_applet_input_sources::run(),
"cosmic-panel-button" => cosmic_panel_button::run(),
_ => return Ok(()),
}
}

View file

@ -7,8 +7,8 @@ license = "GPL-3.0"
[dependencies]
freedesktop-desktop-entry.workspace = true
libcosmic.workspace = true
tracing-log.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
tracing-log.workspace = true
cosmic-config.workspace = true
serde.workspace = true

View file

@ -0,0 +1,175 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only
use config::{CosmicPanelButtonConfig, IndividualConfig, Override};
use cosmic::{
app,
applet::{
cosmic_panel_config::{PanelAnchor, PanelSize},
Size,
},
iced,
iced::Length,
iced_style::application,
iced_widget::row,
theme::Theme,
widget::vertical_space,
};
use cosmic_config::{Config, CosmicConfigEntry};
use freedesktop_desktop_entry::{get_languages_from_env, DesktopEntry};
use std::{env, fs, process::Command};
mod config;
#[derive(Debug, Clone, Default)]
struct Desktop {
name: String,
icon: Option<String>,
exec: String,
}
struct Button {
core: cosmic::app::Core,
desktop: Desktop,
config: IndividualConfig,
}
#[derive(Debug, Clone)]
enum Msg {
Press,
ConfigUpdated(CosmicPanelButtonConfig),
}
impl cosmic::Application for Button {
type Message = Msg;
type Executor = cosmic::SingleThreadExecutor;
type Flags = Desktop;
const APP_ID: &'static str = "com.system76.CosmicPanelButton";
fn init(core: cosmic::app::Core, desktop: Desktop) -> (Self, app::Command<Msg>) {
let config = Config::new(Self::APP_ID, CosmicPanelButtonConfig::VERSION)
.ok()
.and_then(|c| CosmicPanelButtonConfig::get_entry(&c).ok())
.unwrap_or_default()
.configs
.get(&core.applet.panel_type.to_string())
.cloned()
.unwrap_or_default();
(
Self {
core,
desktop,
config,
},
app::Command::none(),
)
}
fn core(&self) -> &cosmic::app::Core {
&self.core
}
fn core_mut(&mut self) -> &mut cosmic::app::Core {
&mut self.core
}
fn style(&self) -> Option<<Theme as application::StyleSheet>::Style> {
Some(cosmic::applet::style())
}
fn update(&mut self, message: Msg) -> app::Command<Msg> {
match message {
Msg::Press => {
let _ = Command::new("sh").arg("-c").arg(&self.desktop.exec).spawn();
}
Msg::ConfigUpdated(conf) => {
self.config = conf
.configs
.get(&self.core.applet.panel_type.to_string())
.cloned()
.unwrap_or_default();
}
}
app::Command::none()
}
fn view(&self) -> cosmic::Element<Msg> {
// currently, panel being anchored to the left or right is a hard
// override for icon, later if text is updated to wrap, we may
// use Override::Text to override this behavior
if self.desktop.icon.is_some()
&& matches!(
self.core.applet.anchor,
PanelAnchor::Left | PanelAnchor::Right
)
|| matches!(self.config.force_presentation, Some(Override::Icon))
|| matches!(
(&self.core.applet.size, &self.config.force_presentation),
(
Size::PanelSize(PanelSize::M | PanelSize::L | PanelSize::XL),
None
)
)
{
self.core.applet.icon_button_from_handle(
cosmic::widget::icon::from_name(self.desktop.icon.clone().unwrap()).handle(),
)
} else {
let content = row!(
self.core.applet.text(&self.desktop.name),
vertical_space(Length::Fixed(
(self.core.applet.suggested_size(true).1
+ 2 * self.core.applet.suggested_padding(true)) as f32
))
)
.align_items(iced::Alignment::Center);
cosmic::widget::button(content)
.padding([0, self.core.applet.suggested_padding(true)])
.style(cosmic::theme::Button::AppletIcon)
}
.on_press_down(Msg::Press)
.into()
}
fn subscription(&self) -> iced::Subscription<Self::Message> {
self.core.watch_config(Self::APP_ID).map(|u| {
for why in u.errors {
tracing::error!(why = why.to_string(), "Error watching config");
}
Msg::ConfigUpdated(u.config)
})
}
}
pub fn run() -> iced::Result {
let id = env::args()
.nth(1)
.expect("Requires desktop file id as argument.");
let filename = format!("{id}.desktop");
let mut desktop = None;
let locales = get_languages_from_env();
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::from_str(&path, &bytes, Some(&locales)) {
desktop = Some(Desktop {
name: entry
.name(&locales)
.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`")),
});
break;
}
}
}
let desktop = desktop.unwrap_or_else(|| {
panic!("Failed to find valid desktop file '{filename}' in search paths")
});
cosmic::applet::run::<Button>(true, desktop)
}

View file

@ -1,181 +1,13 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only
use config::{CosmicPanelButtonConfig, IndividualConfig, Override};
use cosmic::{
app,
applet::{
cosmic_panel_config::{PanelAnchor, PanelSize},
Size,
},
iced,
iced::Length,
iced_style::application,
iced_widget::row,
theme::Theme,
widget::vertical_space,
};
use cosmic_config::{Config, CosmicConfigEntry};
use freedesktop_desktop_entry::{get_languages_from_env, DesktopEntry};
use std::{env, fs, process::Command};
mod config;
const VERSION: &str = env!("CARGO_PKG_VERSION");
#[derive(Debug, Clone, Default)]
struct Desktop {
name: String,
icon: Option<String>,
exec: String,
}
struct Button {
core: cosmic::app::Core,
desktop: Desktop,
config: IndividualConfig,
}
#[derive(Debug, Clone)]
enum Msg {
Press,
ConfigUpdated(CosmicPanelButtonConfig),
}
impl cosmic::Application for Button {
type Message = Msg;
type Executor = cosmic::SingleThreadExecutor;
type Flags = Desktop;
const APP_ID: &'static str = "com.system76.CosmicPanelButton";
fn init(core: cosmic::app::Core, desktop: Desktop) -> (Self, app::Command<Msg>) {
let config = Config::new(Self::APP_ID, CosmicPanelButtonConfig::VERSION)
.ok()
.and_then(|c| CosmicPanelButtonConfig::get_entry(&c).ok())
.unwrap_or_default()
.configs
.get(&core.applet.panel_type.to_string())
.cloned()
.unwrap_or_default();
(
Self {
core,
desktop,
config,
},
app::Command::none(),
)
}
fn core(&self) -> &cosmic::app::Core {
&self.core
}
fn core_mut(&mut self) -> &mut cosmic::app::Core {
&mut self.core
}
fn style(&self) -> Option<<Theme as application::StyleSheet>::Style> {
Some(cosmic::applet::style())
}
fn update(&mut self, message: Msg) -> app::Command<Msg> {
match message {
Msg::Press => {
let _ = Command::new("sh").arg("-c").arg(&self.desktop.exec).spawn();
}
Msg::ConfigUpdated(conf) => {
self.config = conf
.configs
.get(&self.core.applet.panel_type.to_string())
.cloned()
.unwrap_or_default();
}
}
app::Command::none()
}
fn view(&self) -> cosmic::Element<Msg> {
// currently, panel being anchored to the left or right is a hard
// override for icon, later if text is updated to wrap, we may
// use Override::Text to override this behavior
if self.desktop.icon.is_some()
&& matches!(
self.core.applet.anchor,
PanelAnchor::Left | PanelAnchor::Right
)
|| matches!(self.config.force_presentation, Some(Override::Icon))
|| matches!(
(&self.core.applet.size, &self.config.force_presentation),
(
Size::PanelSize(PanelSize::M | PanelSize::L | PanelSize::XL),
None
)
)
{
self.core.applet.icon_button_from_handle(
cosmic::widget::icon::from_name(self.desktop.icon.clone().unwrap()).handle(),
)
} else {
let content = row!(
self.core.applet.text(&self.desktop.name),
vertical_space(Length::Fixed(
(self.core.applet.suggested_size(true).1
+ 2 * self.core.applet.suggested_padding(true)) as f32
))
)
.align_items(iced::Alignment::Center);
cosmic::widget::button(content)
.padding([0, self.core.applet.suggested_padding(true)])
.style(cosmic::theme::Button::AppletIcon)
}
.on_press_down(Msg::Press)
.into()
}
fn subscription(&self) -> iced::Subscription<Self::Message> {
self.core.watch_config(Self::APP_ID).map(|u| {
for why in u.errors {
tracing::error!(why = why.to_string(), "Error watching config");
}
Msg::ConfigUpdated(u.config)
})
}
}
pub fn main() -> iced::Result {
fn main() -> cosmic::iced::Result {
tracing_subscriber::fmt::init();
let _ = tracing_log::LogTracer::init();
tracing::info!("Starting panel button applet with version {VERSION}");
let id = env::args()
.nth(1)
.expect("Requires desktop file id as argument.");
let filename = format!("{id}.desktop");
let mut desktop = None;
let locales = get_languages_from_env();
tracing::info!("Starting workspaces applet with version {VERSION}");
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::from_str(&path, &bytes, Some(&locales)) {
desktop = Some(Desktop {
name: entry
.name(&locales)
.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`")),
});
break;
}
}
}
let desktop = desktop.unwrap_or_else(|| {
panic!("Failed to find valid desktop file '{filename}' in search paths")
});
cosmic::applet::run::<Button>(true, desktop)
cosmic_panel_button::run()
}

1
debian/links vendored
View file

@ -11,3 +11,4 @@
/usr/bin/cosmic-applets /usr/bin/cosmic-applet-time
/usr/bin/cosmic-applets /usr/bin/cosmic-applet-workspaces
/usr/bin/cosmic-applets /usr/bin/cosmic-applet-input-sources
/usr/bin/cosmic-applets /usr/bin/cosmic-panel-button

View file

@ -51,7 +51,7 @@ _install_applet id name: (_install_icons name) \
_install_button id name: (_install_icons name) (_install_desktop name + '/data/' + id + '.desktop')
# Installs files into the system
install: (_install_bin 'cosmic-applets') (_install_applet 'com.system76.CosmicAppList' 'cosmic-app-list') (_install_default_schema 'cosmic-app-list') (_install_applet 'com.system76.CosmicAppletAudio' 'cosmic-applet-audio') (_install_applet 'com.system76.CosmicAppletInputSources' 'cosmic-applet-input-sources') (_install_applet 'com.system76.CosmicAppletBattery' 'cosmic-applet-battery') (_install_applet 'com.system76.CosmicAppletBluetooth' 'cosmic-applet-bluetooth') (_install_applet 'com.system76.CosmicAppletMinimize' 'cosmic-applet-minimize') (_install_applet 'com.system76.CosmicAppletNetwork' 'cosmic-applet-network') (_install_applet 'com.system76.CosmicAppletNotifications' 'cosmic-applet-notifications') (_install_applet 'com.system76.CosmicAppletPower' 'cosmic-applet-power') (_install_applet 'com.system76.CosmicAppletStatusArea' 'cosmic-applet-status-area') (_install_applet 'com.system76.CosmicAppletTiling' 'cosmic-applet-tiling') (_install_applet 'com.system76.CosmicAppletTime' 'cosmic-applet-time') (_install_applet 'com.system76.CosmicAppletWorkspaces' 'cosmic-applet-workspaces') (_install_bin 'cosmic-panel-button') (_install_button 'com.system76.CosmicPanelAppButton' 'cosmic-panel-app-button') (_install_button 'com.system76.CosmicPanelLauncherButton' 'cosmic-panel-launcher-button') (_install_button 'com.system76.CosmicPanelWorkspacesButton' 'cosmic-panel-workspaces-button')
install: (_install_bin 'cosmic-applets') (_link_applet 'cosmic-panel-button') (_install_applet 'com.system76.CosmicAppList' 'cosmic-app-list') (_install_default_schema 'cosmic-app-list') (_install_applet 'com.system76.CosmicAppletAudio' 'cosmic-applet-audio') (_install_applet 'com.system76.CosmicAppletInputSources' 'cosmic-applet-input-sources') (_install_applet 'com.system76.CosmicAppletBattery' 'cosmic-applet-battery') (_install_applet 'com.system76.CosmicAppletBluetooth' 'cosmic-applet-bluetooth') (_install_applet 'com.system76.CosmicAppletMinimize' 'cosmic-applet-minimize') (_install_applet 'com.system76.CosmicAppletNetwork' 'cosmic-applet-network') (_install_applet 'com.system76.CosmicAppletNotifications' 'cosmic-applet-notifications') (_install_applet 'com.system76.CosmicAppletPower' 'cosmic-applet-power') (_install_applet 'com.system76.CosmicAppletStatusArea' 'cosmic-applet-status-area') (_install_applet 'com.system76.CosmicAppletTiling' 'cosmic-applet-tiling') (_install_applet 'com.system76.CosmicAppletTime' 'cosmic-applet-time') (_install_applet 'com.system76.CosmicAppletWorkspaces' 'cosmic-applet-workspaces') (_install_button 'com.system76.CosmicPanelAppButton' 'cosmic-panel-app-button') (_install_button 'com.system76.CosmicPanelLauncherButton' 'cosmic-panel-launcher-button') (_install_button 'com.system76.CosmicPanelWorkspacesButton' 'cosmic-panel-workspaces-button')
# Vendor Cargo dependencies locally
vendor: