From e6d2a5fd0d4528453471dff1d049423c488d02bb Mon Sep 17 00:00:00 2001 From: David Rodriguez Date: Fri, 8 Oct 2021 23:05:30 -0400 Subject: [PATCH 1/3] add foundation of basic gsettings schema --- data/gschema.xml | 28 ++++++++++++++++++++++++++++ data/meson.build | 5 +++++ meson.build | 5 ++++- meson/post_install.py | 11 +++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 data/gschema.xml create mode 100644 data/meson.build create mode 100644 meson/post_install.py diff --git a/data/gschema.xml b/data/gschema.xml new file mode 100644 index 0000000..6e2f17e --- /dev/null +++ b/data/gschema.xml @@ -0,0 +1,28 @@ + + + + + 320 + The X position of the window + The saved Horizontal position of the window + + + + 320 + The Y position of the window + The saved Vertical position of the window + + + + 640 + The width of the window + The saved width of the window + + + + 480 + The height of the window + The saved height of the window + + + \ No newline at end of file diff --git a/data/meson.build b/data/meson.build new file mode 100644 index 0000000..af1a12c --- /dev/null +++ b/data/meson.build @@ -0,0 +1,5 @@ +install_data( + 'gschema.xml', + install_dir: join_paths (get_option ('datadir'), 'glib-2.0', 'schemas'), + rename: meson.project_name() + '.gschema.xml', +) \ No newline at end of file diff --git a/meson.build b/meson.build index 63076ca..678f6ed 100644 --- a/meson.build +++ b/meson.build @@ -1,3 +1,6 @@ project('com.bsdadm.linux.vala-and-gtk-starter', 'vala', 'c') -subdir('src') \ No newline at end of file +subdir('src') +subdir('data') + +meson.add_install_script('meson/post_install.py') \ No newline at end of file diff --git a/meson/post_install.py b/meson/post_install.py new file mode 100644 index 0000000..a7d411c --- /dev/null +++ b/meson/post_install.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 + +import os +import subprocess + +install_prefix = os.environ['MESON_INSTALL_PREFIX'] +schemadir = os.path.join(install_prefix, 'share/glib-2.0/schemas') + +if not os.environ.get('DESTDIR'): + print('Compiling gsettings schemas...') + subprocess.call(['glib-compile-schemas', schemadir]) From 89b744ab412197d129c91e33b4a77ae9eaa67fc1 Mon Sep 17 00:00:00 2001 From: David Rodriguez Date: Fri, 8 Oct 2021 23:38:18 -0400 Subject: [PATCH 2/3] Set application window position using gsettings --- src/Widgets/Window.vala | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Widgets/Window.vala b/src/Widgets/Window.vala index 531ec0b..5acbd99 100644 --- a/src/Widgets/Window.vala +++ b/src/Widgets/Window.vala @@ -9,8 +9,20 @@ public class Vags.Window : Gtk.ApplicationWindow { set_title ("Vala and Gtk Starter"); set_default_size (640, 480); set_border_width (10); - set_position (Gtk.WindowPosition.CENTER); + /* + We're going to use set_position during the first ever launch + after that, we will use gsettings to remember the last position + of the window and the size of the window. + */ + //set_position (Gtk.WindowPosition.CENTER); set_resizable (true); + + // Lets pull some settings from our gschema file + GLib.Settings settings = new GLib.Settings ("com.bsdadm.linux.vala-and-gtk-starter"); + + // Let's move the window to the last position saved in gsettings + move(settings.get_int("pos-x"), settings.get_int("pos-y")); + show_all (); } } \ No newline at end of file From 03f2f0b07d3a1f5497f0de4a4c352e73921df86b Mon Sep 17 00:00:00 2001 From: David Rodriguez Date: Sat, 9 Oct 2021 00:21:11 -0400 Subject: [PATCH 3/3] add 'first-run' to schema and enable gschema settings --- data/gschema.xml | 10 ++++++++-- src/Widgets/Window.vala | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/data/gschema.xml b/data/gschema.xml index 6e2f17e..2ef3f8b 100644 --- a/data/gschema.xml +++ b/data/gschema.xml @@ -3,13 +3,13 @@ 320 - The X position of the window + The horizontal position of the window The saved Horizontal position of the window 320 - The Y position of the window + The vertical position of the window The saved Vertical position of the window @@ -24,5 +24,11 @@ The height of the window The saved height of the window + + + true + Whether this is the first time the application has been run + Whether this is the first time the application has been run + \ No newline at end of file diff --git a/src/Widgets/Window.vala b/src/Widgets/Window.vala index 5acbd99..bca4c65 100644 --- a/src/Widgets/Window.vala +++ b/src/Widgets/Window.vala @@ -1,4 +1,6 @@ public class Vags.Window : Gtk.ApplicationWindow { + public GLib.Settings settings; + public Window (Gtk.Application app) { Object ( application: app @@ -7,22 +9,45 @@ public class Vags.Window : Gtk.ApplicationWindow { construct { set_title ("Vala and Gtk Starter"); - set_default_size (640, 480); set_border_width (10); /* We're going to use set_position during the first ever launch after that, we will use gsettings to remember the last position of the window and the size of the window. */ - //set_position (Gtk.WindowPosition.CENTER); set_resizable (true); - + // Lets pull some settings from our gschema file - GLib.Settings settings = new GLib.Settings ("com.bsdadm.linux.vala-and-gtk-starter"); + settings = new GLib.Settings ("com.bsdadm.linux.vala-and-gtk-starter"); + + if (settings.get_boolean ("first-run")) { + settings.set_boolean ("first-run", false); + set_default_size (640, 480); + set_position (Gtk.WindowPosition.CENTER); + } else { + // Let's move the window to the last position saved in gsettings + move(settings.get_int("pos-x"), settings.get_int("pos-y")); + // Let's resize the window to the last size saved in gsettings + resize(settings.get_int("window-width"), settings.get_int("window-height")); + } - // Let's move the window to the last position saved in gsettings - move(settings.get_int("pos-x"), settings.get_int("pos-y")); + delete_event.connect (e => { + return before_destroy (); + }); show_all (); } + + public bool before_destroy () { + int width, height, x, y; + get_size (out width, out height); + get_position (out x, out y); + + settings.set_int ("window-width", width); + settings.set_int ("window-height", height); + settings.set_int ("pos-x", x); + settings.set_int ("pos-y", y); + + return false; + } } \ No newline at end of file