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

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| {