window: remember image property visibility state
Added GSettings support to Loupe. Inspiration was taken from Fractal: LpApplication will contain `gio::Settings` and all code needing them will reach them as a singleton by calling `g_application_get_default`. This design makes the settings dependency less explicit than dependency injection, but allows for shorter code when new settings will be added across different parts of the application. A `show-properties` setting is added for remembaring the image property sidebar state. It's read by LpWindow when the current page changes, and saved when toggling the sidebar. When no image is being shown, the sidebar is always hidden, while the setting remains unchanged. The sidebar state will be restored when a picture is opened. Closes #147
This commit is contained in:
parent
4c352ed86a
commit
5e32cfa1df
7 changed files with 85 additions and 4 deletions
|
|
@ -33,8 +33,18 @@ mod imp {
|
|||
|
||||
// The basic struct that holds our
|
||||
// state and widgets
|
||||
#[derive(Default, Debug)]
|
||||
pub struct LpApplication {}
|
||||
#[derive(Debug)]
|
||||
pub struct LpApplication {
|
||||
pub settings: gio::Settings,
|
||||
}
|
||||
|
||||
impl Default for LpApplication {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
settings: gio::Settings::new(config::APP_ID),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sets up the basics for the GObject
|
||||
// The `#[glib::object_subclass] macro implements
|
||||
|
|
@ -209,4 +219,17 @@ impl LpApplication {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn settings(&self) -> gio::Settings {
|
||||
self.imp().settings.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for LpApplication {
|
||||
fn default() -> Self {
|
||||
gio::Application::default()
|
||||
.unwrap()
|
||||
.downcast::<LpApplication>()
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ use gtk::CompositeTemplate;
|
|||
use std::cell::{Cell, OnceCell, RefCell};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::application::LpApplication;
|
||||
use crate::config;
|
||||
use crate::util::{Direction, Position};
|
||||
use crate::widgets::{LpDragOverlay, LpImage, LpImageView, LpPropertiesView};
|
||||
|
|
@ -275,6 +276,10 @@ mod imp {
|
|||
.connect_pressed(clone!(@weak obj => move |_,_,_,_| {
|
||||
obj.image_view().navigate(Direction::Back);
|
||||
}));
|
||||
self.properties_button
|
||||
.connect_toggled(clone!(@weak obj => move |_| {
|
||||
obj.on_properties_button_toggled();
|
||||
}));
|
||||
|
||||
if config::PROFILE == ".Devel" {
|
||||
obj.add_css_class("devel");
|
||||
|
|
@ -764,10 +769,14 @@ impl LpWindow {
|
|||
);
|
||||
|
||||
if has_image {
|
||||
let settings = LpApplication::default().settings();
|
||||
imp.properties_button
|
||||
.set_active(settings.boolean("show-properties"));
|
||||
imp.stack.set_visible_child(&*imp.image_view);
|
||||
imp.image_view.grab_focus();
|
||||
self.schedule_hide_controls();
|
||||
} else {
|
||||
imp.properties_button.set_active(false);
|
||||
imp.stack.set_visible_child(&*imp.status_page);
|
||||
imp.status_page.grab_focus();
|
||||
// Leave fullscreen since status page has no controls to leave it
|
||||
|
|
@ -775,6 +784,22 @@ impl LpWindow {
|
|||
}
|
||||
}
|
||||
|
||||
/// When the image-properties sidebar is displayed or hidden, we should update the
|
||||
/// "show-properties" setting.
|
||||
fn on_properties_button_toggled(&self) {
|
||||
let imp = self.imp();
|
||||
// When no image is shown, we skip this update as the sidebar should always be hidden.
|
||||
// This can happen when deleting a picture.
|
||||
if imp.image_view.current_page().is_none() {
|
||||
return;
|
||||
}
|
||||
let settings = LpApplication::default().settings();
|
||||
let result = settings.set_boolean("show-properties", imp.properties_button.is_active());
|
||||
if let Err(err) = result {
|
||||
log::warn!("Failed to save show-properties state, {}", err);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_title(&self) {
|
||||
let title = self
|
||||
.imp()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue