Skip to content

Commit

Permalink
Merge pull request #65 from swsnr/swsnr/issue62
Browse files Browse the repository at this point in the history
Save and restore main window state
  • Loading branch information
swsnr authored Dec 28, 2024
2 parents 19d61f3 + 0d4e4fe commit 020a0e6
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@

# Compiled gettext catalogs
/po/*.mo

# Compiled schemas
/schemas/gschemas.compiled
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ devel:
sed -i '/de.swsnr.turnon.Devel/! s/de\.swsnr\.turnon/de.swsnr.turnon.Devel/g' \
src/config.rs \
resources/de.swsnr.turnon.metainfo.xml.in de.swsnr.turnon.desktop.in \
dbus-1/de.swsnr.turnon.service de.swsnr.turnon.search-provider.ini
dbus-1/de.swsnr.turnon.service de.swsnr.turnon.search-provider.ini \
schemas/de.swsnr.turnon.gschema.xml
23 changes: 23 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,34 @@ pub fn compile_resources<P: AsRef<Path>>(source_dirs: &[P], gresource: &str, tar
}
}

fn compile_schemas() {
let schemas: Vec<PathBuf> = glob::glob("schemas/*.gschema.xml")
.unwrap()
.collect::<Result<_, _>>()
.unwrap();
for schema in schemas {
println!("cargo:rerun-if-changed={}", schema.display());
}

let output = Command::new("glib-compile-schemas")
.args(["--strict", "schemas"])
.output()
.unwrap();

assert!(
output.status.success(),
"glib-compile-schemas failed with exit status {} and stderr:\n{}",
output.status,
String::from_utf8_lossy(&output.stderr)
);
}

fn main() {
// Compile blueprints and msgfmt our metainfo template first, as these are
// inputs to resource compilation.
compile_blueprint();
msgfmt();
compile_schemas();
compile_resources(
&["resources"],
"resources/resources.gresource.xml",
Expand Down
3 changes: 3 additions & 0 deletions flatpak/de.swsnr.turnon.Devel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ modules:
- install -Dm0644 resources/de.swsnr.turnon.metainfo.xml /app/share/metainfo/de.swsnr.turnon.Devel.metainfo.xml
- install -Dm0644 dbus-1/de.swsnr.turnon.service /app/share/dbus-1/services/de.swsnr.turnon.Devel.service
- install -Dm0644 de.swsnr.turnon.search-provider.ini /app/share/gnome-shell/search-providers/de.swsnr.turnon.Devel.search-provider.ini
# Install settings and compile them
- install -Dm0644 schemas/de.swsnr.turnon.gschema.xml /app/share/glib-2.0/schemas/de.swsnr.turnon.Devel.gschema.xml
- glib-compile-schemas --strict /app/share/glib-2.0/schemas
4 changes: 4 additions & 0 deletions resources/de.swsnr.turnon.metainfo.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@
<releases>
<release version="next" date="9999-01-01">
<description>
<p>Save and restore main window state.</p>
<p>Update translations.</p>
</description>
<issues>
<issue url="https://github.com/swsnr/turnon/issues/62">GH-62</issue>
</issues>
<url>https://github.com/swsnr/turnon/releases/tag/next</url>
</release>
<release version="2.0.0" date="2024-12-23">
Expand Down
2 changes: 0 additions & 2 deletions resources/ui/turnon-application-window.blp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ using Gtk 4.0;
using Adw 1;

template $TurnOnApplicationWindow: Adw.ApplicationWindow {
default-width: 480;
default-height: 480;
title: _('Turn On');

content: Adw.ToolbarView {
Expand Down
2 changes: 0 additions & 2 deletions resources/ui/turnon-application-window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ corresponding .blp file and regenerate this file with blueprint-compiler.
<interface>
<requires lib="gtk" version="4.0"/>
<template class="TurnOnApplicationWindow" parent="AdwApplicationWindow">
<property name="default-width">480</property>
<property name="default-height">480</property>
<property name="title" translatable="yes">Turn On</property>
<property name="content">
<object class="AdwToolbarView">
Expand Down
16 changes: 16 additions & 0 deletions schemas/de.swsnr.turnon.gschema.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<schemalist>
<schema id="de.swsnr.turnon" path="/de/swsnr/turnon/">
<key name="main-window-width" type="i">
<default>480</default>
</key>
<key name="main-window-height" type="i">
<default>480</default>
</key>
<key name="main-window-maximized" type="b">
<default>false</default>
</key>
<key name="main-window-fullscreen" type="b">
<default>false</default>
</key>
</schema>
</schemalist>
39 changes: 36 additions & 3 deletions src/app/widgets/application_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,19 @@ mod imp {
use futures_util::{stream, StreamExt, TryStreamExt};
use glib::dpgettext2;
use gtk::glib::subclass::InitializingObject;
use gtk::{glib, CompositeTemplate};
use gtk::{gio, glib, CompositeTemplate};

use crate::app::model::{Device, Devices};
use crate::config::G_LOG_DOMAIN;
use crate::net;

use super::super::DeviceRow;

#[derive(CompositeTemplate, Default, glib::Properties)]
#[derive(CompositeTemplate, glib::Properties)]
#[properties(wrapper_type = super::TurnOnApplicationWindow)]
#[template(resource = "/de/swsnr/turnon/ui/turnon-application-window.ui")]
pub struct TurnOnApplicationWindow {
settings: gio::Settings,
#[property(get, set)]
startpage_icon_name: RefCell<String>,
#[template_child]
Expand Down Expand Up @@ -254,6 +255,21 @@ mod imp {

type ParentType = adw::ApplicationWindow;

fn new() -> Self {
Self {
settings: gio::Settings::new_full(
&crate::config::schema_source()
.lookup(crate::config::APP_ID, true)
.unwrap(),
gio::SettingsBackend::NONE,
None,
),
startpage_icon_name: Default::default(),
devices_list: Default::default(),
feedback: Default::default(),
}
}

fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
Expand All @@ -264,7 +280,24 @@ mod imp {
}

#[glib::derived_properties]
impl ObjectImpl for TurnOnApplicationWindow {}
impl ObjectImpl for TurnOnApplicationWindow {
fn constructed(&self) {
self.parent_constructed();

self.settings
.bind("main-window-width", &*self.obj(), "default-width")
.build();
self.settings
.bind("main-window-height", &*self.obj(), "default-height")
.build();
self.settings
.bind("main-window-maximized", &*self.obj(), "maximized")
.build();
self.settings
.bind("main-window-fullscreen", &*self.obj(), "fullscreened")
.build();
}
}

impl WidgetImpl for TurnOnApplicationWindow {}

Expand Down
21 changes: 21 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use std::path::PathBuf;

use glib::{gstr, GStr};
use gtk::gio;

/// The app ID to use.
pub static APP_ID: &GStr = gstr!("de.swsnr.turnon");
Expand All @@ -21,6 +22,26 @@ pub fn running_in_flatpak() -> bool {
std::fs::exists("/.flatpak-info").unwrap_or_default()
}

/// Get a schema source for this application.
///
/// In a debug build load compiled schemas from the manifest directory, to allow
/// running the application uninstalled.
///
/// In a release build only use the default schema source.
pub fn schema_source() -> gio::SettingsSchemaSource {
let default = gio::SettingsSchemaSource::default().unwrap();
if cfg!(debug_assertions) {
let directory = concat!(env!("CARGO_MANIFEST_DIR"), "/schemas");
if std::fs::exists(directory).unwrap_or_default() {
gio::SettingsSchemaSource::from_directory(directory, Some(&default), false).unwrap()
} else {
default
}
} else {
default
}
}

/// Get the locale directory.
///
/// Return the flatpak locale directory when in
Expand Down

0 comments on commit 020a0e6

Please sign in to comment.