Skip to content

Commit

Permalink
Merge pull request #3
Browse files Browse the repository at this point in the history
Gsetting Implimented
  • Loading branch information
David Rodriguez authored Oct 9, 2021
2 parents 0494f9f + 03f2f0b commit 087a82a
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
34 changes: 34 additions & 0 deletions data/gschema.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8" ?>
<schemalist>
<schema path="/com/bsdadm/linux/vala-and-gtk-starter/" id="com.bsdadm.linux.vala-and-gtk-starter">
<key name="pos-x" type="i">
<default>320</default>
<summary>The horizontal position of the window</summary>
<description>The saved Horizontal position of the window</description>
</key>

<key name="pos-y" type="i">
<default>320</default>
<summary>The vertical position of the window</summary>
<description>The saved Vertical position of the window</description>
</key>

<key name="window-width" type="i">
<default>640</default>
<summary>The width of the window</summary>
<description>The saved width of the window</description>
</key>

<key name="window-height" type="i">
<default>480</default>
<summary>The height of the window</summary>
<description>The saved height of the window</description>
</key>

<key name="first-run" type="b">
<default>true</default>
<summary>Whether this is the first time the application has been run</summary>
<description>Whether this is the first time the application has been run</description>
</key>
</schema>
</schemalist>
5 changes: 5 additions & 0 deletions data/meson.build
Original file line number Diff line number Diff line change
@@ -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',
)
5 changes: 4 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
project('com.bsdadm.linux.vala-and-gtk-starter', 'vala', 'c')

subdir('src')
subdir('src')
subdir('data')

meson.add_install_script('meson/post_install.py')
11 changes: 11 additions & 0 deletions meson/post_install.py
Original file line number Diff line number Diff line change
@@ -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])
41 changes: 39 additions & 2 deletions src/Widgets/Window.vala
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
public class Vags.Window : Gtk.ApplicationWindow {
public GLib.Settings settings;

public Window (Gtk.Application app) {
Object (
application: app
Expand All @@ -7,10 +9,45 @@ public class Vags.Window : Gtk.ApplicationWindow {

construct {
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_resizable (true);

// Lets pull some settings from our gschema file
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"));
}

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;
}
}

0 comments on commit 087a82a

Please sign in to comment.