Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Remember size, maximized, and position
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart committed Jan 24, 2019
1 parent 4b8c392 commit 367c1f8
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 10 deletions.
17 changes: 15 additions & 2 deletions data/com.github.bartzaalberg.php-tester.gschema.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
<schemalist>
<schema id="com.github.bartzaalberg.php-tester" path="/com/github/bartzaalberg/php-tester/" gettext-domain="my-app">

<key name="window-maximized" type="b">
<default>false</default>
<summary>Maximized</summary>
<description>Whether the window is maximized</description>
</key>
<key name="window-position" type="(ii)">
<default>(1024, 750)</default>
<summary>Window position</summary>
<description>Most recent window position (x, y)</description>
</key>
<key name="window-size" type="(ii)">
<default>(-1, -1)</default>
<summary>Window size</summary>
<description>Most recent window size (width, height)</description>
</key>
<key name="style-scheme" type="s">
<default>'classic'</default>
<summary>Preferred Style Scheme</summary>
Expand All @@ -26,7 +40,6 @@
<summary>The PHP path</summary>
<description>This path is used to search for PHP. If this path is wrong the application wont be able to operate</description>
</key>

</schema>
</schemalist>

34 changes: 34 additions & 0 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ namespace PhpTester {
public class App:Granite.Application {

public static MainWindow window = null;
public static GLib.Settings settings;

construct {
program_name = Constants.APPLICATION_NAME;
application_id = Constants.APPLICATION_NAME;
settings = new GLib.Settings (Constants.APPLICATION_NAME);

}

protected override void activate () {
Expand All @@ -21,14 +24,45 @@ public class App:Granite.Application {
}

window = new MainWindow (this);
go_to_last_saved_position (window);
go_to_last_saved_size (window);

window.show_all ();

change_view_if_no_php_was_found ();
}

public static int main (string[] args) {
var app = new PhpTester.App ();
return app.run (args);
}

private void go_to_last_saved_position (MainWindow main_window) {
int window_x, window_y;
settings.get ("window-position", "(ii)", out window_x, out window_y);
if (window_x != -1 || window_y != -1) {
window.move (window_x, window_y);
}
}

private void go_to_last_saved_size (MainWindow main_window) {
var rect = Gtk.Allocation ();

settings.get ("window-size", "(ii)", out rect.width, out rect.height);
window.set_allocation (rect);

if (settings.get_boolean ("window-maximized")) {
window.maximize ();
}
}

private void change_view_if_no_php_was_found () {
var stack_manager = StackManager.get_instance ();
var php_version_manager = PhpVersionManager.get_instance ();
if (php_version_manager.no_versions_found ()) {
stack_manager.get_stack ().visible_child_name = "no-php-found-view";
}
}
}
}

39 changes: 32 additions & 7 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public class MainWindow : Gtk.Window {
private Gtk.Clipboard clipboard = Gtk.Clipboard.get (Gdk.SELECTION_CLIPBOARD);
private StackManager stack_manager = StackManager.get_instance ();
private HeaderBar header_bar = HeaderBar.get_instance ();
private PhpVersionManager php_version_manager = PhpVersionManager.get_instance ();

private uint configure_id;

public MainWindow (Gtk.Application application) {
Object (application: application,
Expand All @@ -20,13 +21,7 @@ public class MainWindow : Gtk.Window {

construct {
stack_manager.load_views (this);

set_titlebar (header_bar);

if (php_version_manager.no_versions_found ()) {
stack_manager.get_stack ().visible_child_name = "no-php-found-view";
}

add_shortcuts ();
}

Expand Down Expand Up @@ -63,5 +58,35 @@ public class MainWindow : Gtk.Window {
return false;
});
}

public override bool configure_event (Gdk.EventConfigure event) {
var settings = new GLib.Settings (Constants.APPLICATION_NAME);

if (configure_id != 0) {
GLib.Source.remove (configure_id);
}

configure_id = Timeout.add (100, () => {
configure_id = 0;

if (is_maximized) {
settings.set_boolean ("window-maximized", true);
} else {
settings.set_boolean ("window-maximized", false);

Gdk.Rectangle rect;
get_allocation (out rect);
settings.set ("window-size", "(ii)", rect.width, rect.height);

int root_x, root_y;
get_position (out root_x, out root_y);
settings.set ("window-position", "(ii)", root_x, root_y);
}

return false;
});

return base.configure_event (event);
}
}
}
1 change: 0 additions & 1 deletion src/StackManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class StackManager : Object {
});

window.add (stack);
window.show_all ();
}
}
}

0 comments on commit 367c1f8

Please sign in to comment.