refactor: allow reset of panel and dock as well as full resets

This commit is contained in:
Ashley Wulber 2024-03-08 16:21:42 -05:00 committed by Ashley Wulber
parent 062904259e
commit 65289d9ec5
4 changed files with 139 additions and 30 deletions

38
Cargo.lock generated
View file

@ -1086,7 +1086,7 @@ dependencies = [
[[package]]
name = "cosmic-config"
version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic#3e6f314c6b003505ed06905eaf3e63f81f57f5a5"
source = "git+https://github.com/pop-os/libcosmic#52491e08ee83254e64d1c3730fed221a40696a85"
dependencies = [
"atomicwrites",
"calloop",
@ -1104,7 +1104,7 @@ dependencies = [
[[package]]
name = "cosmic-config-derive"
version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic#3e6f314c6b003505ed06905eaf3e63f81f57f5a5"
source = "git+https://github.com/pop-os/libcosmic#52491e08ee83254e64d1c3730fed221a40696a85"
dependencies = [
"quote",
"syn 1.0.109",
@ -1113,7 +1113,7 @@ dependencies = [
[[package]]
name = "cosmic-panel-config"
version = "0.1.0"
source = "git+https://github.com/pop-os/cosmic-panel#5023ac5e3008d946c22bee1fd7d91cbccbc09732"
source = "git+https://github.com/pop-os/cosmic-panel#e51be500fc0e62afe5a9002fd11f230f9fcbae41"
dependencies = [
"anyhow",
"cosmic-config",
@ -1273,7 +1273,7 @@ dependencies = [
[[package]]
name = "cosmic-theme"
version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic#3e6f314c6b003505ed06905eaf3e63f81f57f5a5"
source = "git+https://github.com/pop-os/libcosmic#52491e08ee83254e64d1c3730fed221a40696a85"
dependencies = [
"almost",
"cosmic-config",
@ -1897,9 +1897,9 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "font-types"
version = "0.4.2"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bd7f3ea17572640b606b35df42cfb6ecdf003704b062580e59918692190b73d"
checksum = "5b7f6040d337bd44434ab21fc6509154edf2cece88b23758d9d64654c4e7730b"
[[package]]
name = "fontconfig-parser"
@ -2559,7 +2559,7 @@ dependencies = [
[[package]]
name = "iced"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#3e6f314c6b003505ed06905eaf3e63f81f57f5a5"
source = "git+https://github.com/pop-os/libcosmic#52491e08ee83254e64d1c3730fed221a40696a85"
dependencies = [
"iced_accessibility",
"iced_core",
@ -2574,7 +2574,7 @@ dependencies = [
[[package]]
name = "iced_accessibility"
version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic#3e6f314c6b003505ed06905eaf3e63f81f57f5a5"
source = "git+https://github.com/pop-os/libcosmic#52491e08ee83254e64d1c3730fed221a40696a85"
dependencies = [
"accesskit",
"accesskit_unix",
@ -2583,7 +2583,7 @@ dependencies = [
[[package]]
name = "iced_core"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#3e6f314c6b003505ed06905eaf3e63f81f57f5a5"
source = "git+https://github.com/pop-os/libcosmic#52491e08ee83254e64d1c3730fed221a40696a85"
dependencies = [
"bitflags 1.3.2",
"iced_accessibility",
@ -2602,7 +2602,7 @@ dependencies = [
[[package]]
name = "iced_futures"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#3e6f314c6b003505ed06905eaf3e63f81f57f5a5"
source = "git+https://github.com/pop-os/libcosmic#52491e08ee83254e64d1c3730fed221a40696a85"
dependencies = [
"futures",
"iced_core",
@ -2615,7 +2615,7 @@ dependencies = [
[[package]]
name = "iced_graphics"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#3e6f314c6b003505ed06905eaf3e63f81f57f5a5"
source = "git+https://github.com/pop-os/libcosmic#52491e08ee83254e64d1c3730fed221a40696a85"
dependencies = [
"bitflags 1.3.2",
"bytemuck",
@ -2639,7 +2639,7 @@ dependencies = [
[[package]]
name = "iced_renderer"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#3e6f314c6b003505ed06905eaf3e63f81f57f5a5"
source = "git+https://github.com/pop-os/libcosmic#52491e08ee83254e64d1c3730fed221a40696a85"
dependencies = [
"iced_graphics",
"iced_tiny_skia",
@ -2651,7 +2651,7 @@ dependencies = [
[[package]]
name = "iced_runtime"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#3e6f314c6b003505ed06905eaf3e63f81f57f5a5"
source = "git+https://github.com/pop-os/libcosmic#52491e08ee83254e64d1c3730fed221a40696a85"
dependencies = [
"iced_accessibility",
"iced_core",
@ -2663,7 +2663,7 @@ dependencies = [
[[package]]
name = "iced_sctk"
version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic#3e6f314c6b003505ed06905eaf3e63f81f57f5a5"
source = "git+https://github.com/pop-os/libcosmic#52491e08ee83254e64d1c3730fed221a40696a85"
dependencies = [
"enum-repr",
"float-cmp",
@ -2689,7 +2689,7 @@ dependencies = [
[[package]]
name = "iced_style"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#3e6f314c6b003505ed06905eaf3e63f81f57f5a5"
source = "git+https://github.com/pop-os/libcosmic#52491e08ee83254e64d1c3730fed221a40696a85"
dependencies = [
"iced_core",
"once_cell",
@ -2699,7 +2699,7 @@ dependencies = [
[[package]]
name = "iced_tiny_skia"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#3e6f314c6b003505ed06905eaf3e63f81f57f5a5"
source = "git+https://github.com/pop-os/libcosmic#52491e08ee83254e64d1c3730fed221a40696a85"
dependencies = [
"bytemuck",
"cosmic-text",
@ -2716,7 +2716,7 @@ dependencies = [
[[package]]
name = "iced_wgpu"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#3e6f314c6b003505ed06905eaf3e63f81f57f5a5"
source = "git+https://github.com/pop-os/libcosmic#52491e08ee83254e64d1c3730fed221a40696a85"
dependencies = [
"bitflags 1.3.2",
"bytemuck",
@ -2735,7 +2735,7 @@ dependencies = [
[[package]]
name = "iced_widget"
version = "0.12.0"
source = "git+https://github.com/pop-os/libcosmic#3e6f314c6b003505ed06905eaf3e63f81f57f5a5"
source = "git+https://github.com/pop-os/libcosmic#52491e08ee83254e64d1c3730fed221a40696a85"
dependencies = [
"iced_renderer",
"iced_runtime",
@ -3132,7 +3132,7 @@ checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "libcosmic"
version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic#3e6f314c6b003505ed06905eaf3e63f81f57f5a5"
source = "git+https://github.com/pop-os/libcosmic#52491e08ee83254e64d1c3730fed221a40696a85"
dependencies = [
"apply",
"ashpd 0.7.0",

View file

@ -11,7 +11,9 @@ use cosmic_settings_page::{self as page, section, Section};
use log::error;
use slotmap::SlotMap;
use crate::pages::desktop::panel::inner::{add_panel, behavior_and_position, configuration, style};
use crate::pages::desktop::panel::inner::{
add_panel, behavior_and_position, configuration, reset_button, style,
};
use super::panel::inner::{self, PageInner, PanelPage};
@ -111,6 +113,20 @@ impl Default for Page {
// If the config is not present, it will be created with the default values and the name will not match
(panel_config.name == "Dock").then_some(panel_config)
});
let system_default = cosmic::cosmic_config::Config::system(
&format!("{}.Dock", cosmic_panel_config::NAME),
CosmicPanelConfig::VERSION,
)
.map(|c| match CosmicPanelConfig::get_entry(&c) {
Ok(c) => c,
Err((errs, c)) => {
for err in errs {
tracing::error!(?err, "Failed to load Dock system config.");
}
c
}
})
.ok();
let container_config = CosmicPanelContainerConfig::load().ok();
Self {
inner: PageInner {
@ -118,6 +134,7 @@ impl Default for Page {
panel_config,
container_config,
outputs_map: HashMap::new(),
system_default,
..Default::default()
},
}
@ -165,6 +182,9 @@ impl page::Page<crate::pages::Message> for Page {
crate::pages::Message::Dock(Message::Inner(m))
})),
sections.insert(configuration::<Page>(self)),
sections.insert(reset_button::<Page, _>(|m| {
crate::pages::Message::Dock(Message::Inner(m))
})),
]
} else {
vec![sections.insert(add_panel::<Page, _>(|m| {

View file

@ -4,8 +4,7 @@ use cosmic::{
iced::Length,
theme,
widget::{
button, container, dropdown, horizontal_space, icon, list, row, settings, slider, text,
toggler,
button, container, dropdown, horizontal_space, icon, row, settings, slider, text, toggler,
},
Element,
};
@ -27,6 +26,8 @@ pub struct PageInner {
pub(crate) container_config: Option<CosmicPanelContainerConfig>,
// TODO move these into panel config
pub(crate) outputs_map: HashMap<ObjectId, (String, WlOutput)>,
pub(crate) system_default: Option<CosmicPanelConfig>,
pub(crate) system_container: Option<CosmicPanelContainerConfig>,
}
impl Default for PageInner {
@ -48,6 +49,23 @@ impl Default for PageInner {
],
container_config: Option::default(),
outputs_map: HashMap::default(),
system_default: None,
system_container: cosmic::cosmic_config::Config::system(
cosmic_panel_config::NAME,
CosmicPanelConfig::VERSION,
)
.map(
|c| match CosmicPanelContainerConfig::load_from_config(&c, true) {
Ok(c) => c,
Err((errs, c)) => {
for err in errs {
tracing::error!(?err, "Error when loading Panel container config.");
}
c
}
},
)
.ok(),
}
}
}
@ -265,18 +283,39 @@ pub(crate) fn add_panel<
) -> Section<crate::pages::Message> {
Section::default()
.title(fl!("panel-missing"))
.descriptions(vec![
fl!("panel-missing", "desc").into(),
fl!("panel-missing", "fix").into(),
])
.descriptions(vec![fl!("reset-to-default").into()])
.view::<P>(move |_binder, _page, section| {
// _descriptions = &section.descriptions;
settings::view_section(&section.title)
let descriptions = &section.descriptions;
cosmic::iced::widget::row![button(text(&*descriptions[0])).on_press(Message::FullReset)]
.apply(Element::from)
.map(msg_map)
})
}
#[allow(clippy::too_many_lines)]
pub fn reset_button<
P: page::Page<crate::pages::Message> + PanelPage,
T: Fn(Message) -> crate::pages::Message + Copy + 'static,
>(
msg_map: T,
) -> Section<crate::pages::Message> {
Section::default()
.descriptions(vec![fl!("reset-to-default").into()])
.view::<P>(move |_binder, page, section| {
let descriptions = &section.descriptions;
let inner = page.inner();
if inner.system_default == inner.panel_config {
horizontal_space(1).apply(Element::from)
} else {
cosmic::iced::widget::row![
button(text(&*descriptions[0])).on_press(Message::ResetPanel)
]
.apply(Element::from)
}
.map(msg_map)
})
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Anchor(PanelAnchor);
@ -344,12 +383,41 @@ pub enum Message {
OutputAdded(String, WlOutput),
OutputRemoved(WlOutput),
PanelConfig(CosmicPanelConfig),
ResetPanel,
FullReset,
}
impl PageInner {
#[allow(clippy::too_many_lines)]
pub fn update(&mut self, message: Message) {
let helper = self.config_helper.as_ref().unwrap();
let Some(helper) = self.config_helper.as_ref() else {
return;
};
match &message {
Message::ResetPanel => {
if let Some((default, config)) = self
.system_default
.as_ref()
.zip(self.config_helper.as_ref())
{
self.panel_config = self.system_default.clone();
if let Err(err) = default.write_entry(config) {
tracing::error!(?err, "Error resetting panel config.");
}
} else {
tracing::error!("Panel config default is missing.");
}
}
Message::FullReset => {
if let Some(container) = self.system_container.as_ref() {
if let Err(err) = container.write_entries() {
tracing::error!(?err, "Error fully resetting the panel config.");
}
}
}
_ => {}
};
let Some(panel_config) = self.panel_config.as_mut() else {
return;
};
@ -430,6 +498,7 @@ impl PageInner {
self.panel_config = Some(c);
return;
}
Message::ResetPanel | Message::FullReset => {}
}
if panel_config.anchor_gap || !panel_config.expand_to_edges {

View file

@ -5,7 +5,9 @@ use cosmic_panel_config::{CosmicPanelConfig, CosmicPanelContainerConfig};
use cosmic_settings_page::{self as page, section, Section};
use slotmap::SlotMap;
use crate::pages::desktop::panel::inner::{add_panel, behavior_and_position, configuration, style};
use crate::pages::desktop::panel::inner::{
add_panel, behavior_and_position, configuration, reset_button, style,
};
use self::inner::{PageInner, PanelPage};
@ -69,6 +71,20 @@ impl Default for Page {
// If the config is not present, it will be created with the default values and the name will not match
(panel_config.name == "Panel").then_some(panel_config)
});
let system_default = cosmic::cosmic_config::Config::system(
&format!("{}.Panel", cosmic_panel_config::NAME),
CosmicPanelConfig::VERSION,
)
.map(|c| match CosmicPanelConfig::get_entry(&c) {
Ok(c) => c,
Err((errs, c)) => {
for err in errs {
tracing::error!(?err, "Failed to load Panel system config.");
}
c
}
})
.ok();
let container_config = CosmicPanelContainerConfig::load().ok();
Self {
inner: PageInner {
@ -76,6 +92,7 @@ impl Default for Page {
panel_config,
container_config,
outputs_map: HashMap::new(),
system_default,
..Default::default()
},
}
@ -98,6 +115,9 @@ impl page::Page<crate::pages::Message> for Page {
crate::pages::Message::Panel(Message(m))
})),
sections.insert(configuration::<Page>(self)),
sections.insert(reset_button::<Page, _>(|m| {
crate::pages::Message::Panel(Message(m))
})),
]
} else {
vec![sections.insert(add_panel::<Page, _>(|m| {