Merge lp:~donadigo/granite/saved-state-in-appclass into lp:~elementary-pantheon/granite/granite

Proposed by Adam Bieńkowski
Status: Rejected
Rejected by: Danielle Foré
Proposed branch: lp:~donadigo/granite/saved-state-in-appclass
Merge into: lp:~elementary-pantheon/granite/granite
Diff against target: 175 lines (+128/-2)
1 file modified
lib/Application.vala (+128/-2)
To merge this branch: bzr merge lp:~donadigo/granite/saved-state-in-appclass
Reviewer Review Type Date Requested Status
elementary Pantheon team Pending
Review via email: mp+290085@code.launchpad.net

Commit message

Fixes bug #1475825: Window states are saved inconsistently.

Description of the change

Fixes bug #1475825: Window states are saved inconsistently.

Saving state is implemented directly in Application class. To make window saving it's state simply set the GSettings schema path "settings_schema_id" to where you store your app settings and call add_window () in the application.

Much of the code was taken from the saved-state branch:
https://code.launchpad.net/~elementary-apps/granite/saved-state.

Process of porting apps:
* Remove old, separate saved-state code from the application.
* Add "saved-state" key to schema .xml file.
* Set settings_schema_id to where are the app settings stored.
* Call add_window () in the activate () method.

Example:
public class App : Granite.Application {
   ...

   public App () {
       ...
       settings_schema_id = "org.app";
       ...
   }

   public override void activate () {
       ...
       var window = new Gtk.Window ();
       add_window (window);

       window.show_all ();
       ...
   }
}

To post a comment you must log in.
Revision history for this message
Rico Tzschichholz (ricotz) wrote :

Breaks ABI

Revision history for this message
Rico Tzschichholz (ricotz) wrote :

Public fields are a pain while it is hard enough to try keeping ABI-compatibility with vala.

Revision history for this message
Zisu Andrei (matzipan) wrote :

Hey Rico, can you advise on what one needs to do to avoid breaking ABI compatibility?

934. By Adam Bieńkowski

Implement saved state in Application class

Revision history for this message
Adam Bieńkowski (donadigo) wrote :

@ricotz: is there anyway I can prevent a breakage in the ABI compatibility, and get this branch merged?

Revision history for this message
Corentin Noël (tintou) wrote :

Is this necessary ?

Revision history for this message
Danielle Foré (danrabbit) wrote :

Since this hasn't seen any movement in a few months, I'm going to mark it as "rejected". You can open a new PR on GitHub :)

Unmerged revisions

934. By Adam Bieńkowski

Implement saved state in Application class

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/Application.vala'
--- lib/Application.vala 2016-06-09 01:28:31 +0000
+++ lib/Application.vala 2016-07-18 12:15:21 +0000
@@ -35,6 +35,14 @@
35 * to create a great deal of an app's functionality.35 * to create a great deal of an app's functionality.
36 */36 */
37 public abstract class Application : Gtk.Application {37 public abstract class Application : Gtk.Application {
38 private const string SETTINGS_KEY_NAME = "saved-state";
39
40 private enum WindowState {
41 NORMAL = 0,
42 MAXIMIZED = 1,
43 FULLSCREEN = 2,
44 MAXIMIZED_FULLSCREEN = 3
45 }
3846
39 public string build_data_dir;47 public string build_data_dir;
40 public string build_pkg_data_dir;48 public string build_pkg_data_dir;
@@ -139,6 +147,33 @@
139 public string about_license;147 public string about_license;
140 public License about_license_type;148 public License about_license_type;
141149
150
151 /**
152 * GSettings schema path to save added Gtk.Window state settings.
153 * This is where "saved-state" key will be created and stored
154 * for added Gtk.Window's: position and their state.
155 * You also need a proper saved-state key in your schema .xml file with the name "saved-state"
156 * with the type '(iiiiu)'
157 * where the types represent in the order of listing:
158 * x-coordinate of the window (the value -1 means centered window),
159 * y-coordinate of the window (the value -1 means centered window),
160 * width of the window,
161 * height of the window,
162 * state of the window (0 = Normal, 1 = Maximized, 2 = Fullscreen, 3 = Maximized and Fullscreen).
163 *
164 * This is an example:
165 * <key name="saved-state" type="(iiiiu)">
166 * <default>(-1,-1,700,500,0)</default>
167 * <summary>The saved state of the window.</summary>
168 * <description>The saved width of the window. The values represent in the respective order:
169 * x-coordinate of the window (the value -1 means centered window),y-coordinate of the window (the value -1 means centered window),
170 * width of the window, height of the window, state of the window (0 = Normal, 1 = Maximized, 2 = Fullscreen).</description>
171 * </key>
172 */
173 public string settings_schema_id;
174
175 private HashTable<Gtk.Window, ulong> window_handlers = new HashTable<Gtk.Window, ulong> (null, null);
176
142 /**177 /**
143 * This creates a new Application class178 * This creates a new Application class
144 */179 */
@@ -161,6 +196,9 @@
161196
162 add_actions ();197 add_actions ();
163198
199 window_added.connect (on_window_added);
200 window_removed.connect (on_window_removed);
201
164 // Deprecated202 // Deprecated
165 Granite.app = this;203 Granite.app = this;
166 }204 }
@@ -205,6 +243,56 @@
205 return base.run (args);243 return base.run (args);
206 }244 }
207245
246 private void on_window_added (Gtk.Window window) {
247 if (SettingsSchemaSource.get_default ().lookup (settings_schema_id, true) == null) {
248 return;
249 }
250
251 var settings = new GLib.Settings (settings_schema_id);
252 var tuple = settings.get_value (SETTINGS_KEY_NAME);
253
254 int x, y, width, height;
255 uint state;
256 tuple.@get ("(iiiiu)",
257 out x,
258 out y,
259 out width,
260 out height,
261 out state);
262
263 if (x != -1 && y != -1) {
264 window.move (x, y);
265 }
266
267 window.set_default_size (width, height);
268 switch ((WindowState)state) {
269 case WindowState.MAXIMIZED:
270 window.maximize ();
271 break;
272 case WindowState.FULLSCREEN:
273 window.fullscreen ();
274 break;
275 case WindowState.MAXIMIZED_FULLSCREEN:
276 window.maximize ();
277 window.fullscreen ();
278 break;
279 default:
280 window.unfullscreen ();
281 window.unmaximize ();
282 break;
283 }
284
285 window_handlers.insert (window, window.delete_event.connect (on_window_delete_event));
286 }
287
288 private void on_window_removed (Gtk.Window window) {
289 if (!window_handlers.contains (window)) {
290 return;
291 }
292
293 window.disconnect (window_handlers.@get (window));
294 }
295
208 protected static bool DEBUG = false;296 protected static bool DEBUG = false;
209 protected static bool ABOUT = false;297 protected static bool ABOUT = false;
210298
@@ -215,9 +303,9 @@
215 };303 };
216304
217 protected virtual void set_options () {305 protected virtual void set_options () {
218306 if (DEBUG) {
219 if (DEBUG)
220 Logger.DisplayLevel = LogLevel.DEBUG;307 Logger.DisplayLevel = LogLevel.DEBUG;
308 }
221 }309 }
222310
223 /**311 /**
@@ -333,5 +421,43 @@
333 Gtk.main ();421 Gtk.main ();
334 }422 }
335 }423 }
424
425 private WindowState get_window_state (Gtk.Widget widget) {
426 var window = widget.get_window ();
427 if ((window.get_state () & Gdk.WindowState.MAXIMIZED) != 0) {
428 if ((window.get_state () & Gdk.WindowState.FULLSCREEN) != 0) {
429 return WindowState.MAXIMIZED_FULLSCREEN;
430 } else {
431 return WindowState.MAXIMIZED;
432 }
433 } else if ((window.get_state () & Gdk.WindowState.FULLSCREEN) != 0) {
434 return WindowState.FULLSCREEN;
435 }
436
437 return WindowState.NORMAL;
438 }
439
440 private bool on_window_delete_event (Gtk.Widget widget, Gdk.EventAny event) {
441 if (SettingsSchemaSource.get_default ().lookup (settings_schema_id, true) == null
442 || !(widget is Gtk.Window)) {
443 return false;
444 }
445
446 int x, y, width, height;
447
448 ((Gtk.Window)widget).get_size (out width, out height);
449 ((Gtk.Window)widget).get_position (out x, out y);
450
451 var settings = new GLib.Settings (settings_schema_id);
452 var v_x = new GLib.Variant.int32 (x);
453 var v_y = new GLib.Variant.int32 (y);
454 var v_width = new GLib.Variant.int32 (width);
455 var v_height = new GLib.Variant.int32 (height);
456 var v_state = new GLib.Variant.uint32 ((uint32)get_window_state (widget));
457
458 var tuple = new GLib.Variant.tuple ({ v_x, v_y, v_width, v_height, v_state });
459 settings.set_value (SETTINGS_KEY_NAME, tuple);
460 return false;
461 }
336 }462 }
337}463}

Subscribers

People subscribed via source and target branches