Skip to content

Commit

Permalink
feat: option to force panel button to display icons instead of text
Browse files Browse the repository at this point in the history
This commit adds the ability to tell COSMIC panel buttons (for example, the Launcher button, the App Library button, and the Workspaces button) to always present themselves as an icon on a Panel instead of being text on horizontal panels, and icons on vertical panels.

Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
  • Loading branch information
ryanabx authored and rbwork2024 committed Sep 23, 2024
1 parent c4532e0 commit 42e3072
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
1 change: 1 addition & 0 deletions i18n/en/cosmic_ext_tweaks.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ installed = Installed
# Panel only
show-panel = Show panel
force-icon-buttons-in-panel = Force icon buttons in panel
padding = Padding
padding-description = Padding is the space between the contents and the borders of the dock or panel.
Expand Down
45 changes: 45 additions & 0 deletions src/core/cosmic_panel_button_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::collections::HashMap;

use cosmic::cosmic_config;
use cosmic_config::{cosmic_config_derive::CosmicConfigEntry, CosmicConfigEntry};
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize, PartialEq, Clone, CosmicConfigEntry)]
#[version = 1]
#[serde(deny_unknown_fields)]
pub struct CosmicPanelButtonConfig {
/// configs indexed by panel name
pub configs: HashMap<String, IndividualConfig>,
}

impl Default for CosmicPanelButtonConfig {
fn default() -> Self {
Self {
configs: HashMap::from([
(
"Panel".to_string(),
IndividualConfig {
force_presentation: None,
},
),
(
"Dock".to_string(),
IndividualConfig {
force_presentation: Some(Override::Icon),
},
),
]),
}
}
}

#[derive(Debug, Deserialize, Serialize, PartialEq, Default, Clone)]
pub struct IndividualConfig {
pub force_presentation: Option<Override>,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum Override {
Icon,
Text,
}
1 change: 1 addition & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod config;
pub mod cosmic_panel_button_config;
pub mod icons;
pub mod localization;
pub mod nav;
73 changes: 72 additions & 1 deletion src/pages/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ use cosmic::{
use cosmic_panel_config::CosmicPanelConfig;
use serde::{Deserialize, Serialize};

use crate::{core::icons, fl};
use crate::{
core::{
cosmic_panel_button_config::{CosmicPanelButtonConfig, IndividualConfig, Override},
icons,
},
fl,
};

#[derive(Debug)]
pub struct Panel {
Expand All @@ -16,6 +22,9 @@ pub struct Panel {
pub show_panel: bool,
pub cosmic_panel_config: CosmicPanel,
pub cosmic_panel_config_helper: Option<Config>,
pub cosmic_panel_button_config: CosmicPanelButtonConfig,
pub cosmic_panel_button_config_helper: Option<Config>,
pub force_icons: bool,
}

#[derive(
Expand Down Expand Up @@ -57,6 +66,27 @@ impl Default for Panel {
}
};

let (cosmic_panel_button_config_helper, cosmic_panel_button_config) =
match cosmic_config::Config::new("com.system76.CosmicPanelButton", 1) {
Ok(config_handler) => {
let config = match CosmicPanelButtonConfig::get_entry(&config_handler) {
Ok(ok) => ok,
Err((errs, config)) => {
eprintln!("errors loading config for cosmic panel button: {:?}", errs);
config
}
};
(Some(config_handler), config)
}
Err(err) => {
eprintln!(
"failed to create config handler for cosmic panel button: {}",
err
);
(None, CosmicPanelButtonConfig::default())
}
};

let padding = panel_config
.clone()
.map(|config| config.padding)
Expand All @@ -66,6 +96,16 @@ impl Default for Panel {
.map(|config| config.spacing)
.unwrap_or(0);
let show_panel = cosmic_panel_config.entries.iter().any(|e| e == "Panel");
let force_icons = cosmic_panel_button_config
.configs
.iter()
.find(|(e, _)| *e == "Panel")
.map(|(_, conf)| {
conf.force_presentation
.as_ref()
.is_some_and(|presentation| *presentation == Override::Icon)
})
.unwrap_or(false);
Self {
panel_helper,
panel_config,
Expand All @@ -74,6 +114,9 @@ impl Default for Panel {
show_panel,
cosmic_panel_config,
cosmic_panel_config_helper,
cosmic_panel_button_config,
cosmic_panel_button_config_helper,
force_icons,
}
}
}
Expand All @@ -83,6 +126,7 @@ pub enum Message {
SetPadding(u32),
SetSpacing(u32),
ShowPanel(bool),
ForceIcons(bool),
}

impl Panel {
Expand All @@ -95,6 +139,10 @@ impl Panel {
widget::settings::item::builder(fl!("show-panel"))
.toggler(self.show_panel, Message::ShowPanel),
)
.add(
widget::settings::item::builder(fl!("force-icon-buttons-in-panel"))
.toggler(self.force_icons, Message::ForceIcons),
)
.add(
widget::settings::item::builder(fl!("padding"))
.description(fl!("padding-description"))
Expand Down Expand Up @@ -146,6 +194,29 @@ impl Panel {
eprintln!("Error updating panel spacing: {}", err);
}
}
Message::ForceIcons(force) => {
let mut configs = self.cosmic_panel_button_config.configs.clone();
if let Some(inner_config) = configs.get_mut("Panel") {
inner_config.force_presentation =
if force { Some(Override::Icon) } else { None };
} else {
configs.insert(
"Panel".to_owned(),
IndividualConfig {
force_presentation: if force { Some(Override::Icon) } else { None },
},
);
}

if let Some(helper) = &self.cosmic_panel_button_config_helper {
let update = self.cosmic_panel_button_config.set_configs(helper, configs);
if let Err(err) = update {
eprintln!("Error updating cosmic panel button configs: {}", err);
} else {
self.force_icons = force;
}
}
}
Message::ShowPanel(show) => {
if show {
if !self
Expand Down

0 comments on commit 42e3072

Please sign in to comment.