@@ -49,6 +49,89 @@ void MaximizeWindow(GtkWindow* window) {
49
49
gtk_window_maximize (window);
50
50
}
51
51
52
+ void SaveWindowState (GtkWindow* window) {
53
+ gint left, top;
54
+ gint width, height;
55
+
56
+ GKeyFile* key_file = g_key_file_new ();
57
+ GError* error = NULL ;
58
+ gchar* filePath = g_strdup_printf (" %s/%s/%s" , g_get_user_config_dir (), APP_NAME, " window.ini" );
59
+ bool maximized = IsWindowMaximized (window);
60
+
61
+ // If window is not maximized, save current size and position
62
+ if (!maximized) {
63
+ gtk_window_get_position (window, &left, &top);
64
+ gtk_window_get_size (window, &width, &height);
65
+ // If maximized, load size and position from file
66
+ // to preserve last saved values
67
+ } else {
68
+ if (g_key_file_load_from_file (key_file, filePath, G_KEY_FILE_NONE, NULL ))
69
+ {
70
+ left = g_key_file_get_integer (key_file, " position" , " left" , &error);
71
+ top = g_key_file_get_integer (key_file, " position" , " top" , &error);
72
+ width = g_key_file_get_integer (key_file, " size" , " width" , &error);
73
+ height = g_key_file_get_integer (key_file, " size" , " height" , &error);
74
+ // If any value can not be readed, save defaults
75
+ if (left == 0 || top == 0 || width == 0 || height == 0 ) {
76
+ left = 1 ;
77
+ top = 1 ;
78
+ width = 800 ;
79
+ height = 600 ;
80
+ }
81
+ // If we can not load the file, save defaults
82
+ } else {
83
+ left = 1 ;
84
+ top = 1 ;
85
+ width = 800 ;
86
+ height = 600 ;
87
+ }
88
+ }
89
+ // The file will be written always.
90
+ g_key_file_set_integer (key_file, " position" , " left" , left);
91
+ g_key_file_set_integer (key_file, " position" , " top" , top);
92
+ g_key_file_set_integer (key_file, " size" , " width" , width - 1 ); // DelayedResize() 1 pixel compensation
93
+ g_key_file_set_integer (key_file, " size" , " height" , height - 1 ); // DelayedResize() 1 pixel compensation
94
+ g_key_file_set_boolean (key_file, " state" , " maximized" , maximized);
95
+
96
+ if (!g_key_file_save_to_file (key_file, filePath, NULL ))
97
+ {
98
+ printf (" %s" , " Error -> SaveWindowState(): can not write `window.ini`\n " );
99
+ }
100
+ }
101
+
102
+ void LoadWindowState (GtkWindow* window) {
103
+ gint left = 1 ;
104
+ gint top = 1 ;
105
+ gint width = 800 ;
106
+ gint height = 600 ;
107
+ bool maximized = FALSE ;
108
+
109
+ GKeyFile* key_file = g_key_file_new ();
110
+ GError* error = NULL ;
111
+ gchar* filePath = g_strdup_printf (" %s/%s/%s" , g_get_user_config_dir (), APP_NAME, " window.ini" );
112
+
113
+ if (g_key_file_load_from_file (key_file, filePath, G_KEY_FILE_NONE, NULL ))
114
+ {
115
+ left = g_key_file_get_integer (key_file, " position" , " left" , &error);
116
+ top = g_key_file_get_integer (key_file, " position" , " top" , &error);
117
+ width = g_key_file_get_integer (key_file, " size" , " width" , &error);
118
+ height = g_key_file_get_integer (key_file, " size" , " height" , &error);
119
+ maximized = g_key_file_get_boolean (key_file, " state" , " maximized" , &error);
120
+ // If any value can not be readed, load defaults
121
+ if (left == 0 || top == 0 || width == 0 || height == 0 ) {
122
+ left = 1 ;
123
+ top = 1 ;
124
+ width = 800 ;
125
+ height = 600 ;
126
+ maximized = FALSE ;
127
+ }
128
+ }
129
+ gtk_window_move (GTK_WINDOW (window), left, top);
130
+ gtk_window_set_default_size (GTK_WINDOW (window), width, height);
131
+ if (maximized)
132
+ MaximizeWindow (window);
133
+ }
134
+
52
135
} // namespace
53
136
54
137
RootWindowGtk::RootWindowGtk ()
@@ -197,6 +280,7 @@ void RootWindowGtk::Close(bool force) {
197
280
REQUIRE_MAIN_THREAD ();
198
281
199
282
if (window_) {
283
+ SaveWindowState (GTK_WINDOW (window_));
200
284
force_close_ = force;
201
285
gtk_widget_destroy (window_);
202
286
}
@@ -242,18 +326,18 @@ void RootWindowGtk::CreateRootWindow(const CefBrowserSettings& settings) {
242
326
// in the upper-left corner. Maybe there's a better default place to put it?
243
327
int x = start_rect_.x ;
244
328
int y = start_rect_.y ;
245
- int width, height;
329
+ int width = start_rect_.width ;
330
+ int height = start_rect_.height ;
331
+
332
+ window_ = gtk_window_new (GTK_WINDOW_TOPLEVEL);
333
+
246
334
if (start_rect_.IsEmpty ()) {
247
- // TODO(port): Also, maybe there's a better way to choose the default size.
248
- width = 800 ;
249
- height = 600 ;
335
+ LoadWindowState (GTK_WINDOW (window_));
250
336
} else {
251
- width = start_rect_. width ;
252
- height = start_rect_. height ;
337
+ gtk_window_move ( GTK_WINDOW (window_), x, y) ;
338
+ gtk_window_set_default_size ( GTK_WINDOW (window_), width, height) ;
253
339
}
254
340
255
- window_ = gtk_window_new (GTK_WINDOW_TOPLEVEL);
256
- gtk_window_set_default_size (GTK_WINDOW (window_), width, height);
257
341
g_signal_connect (G_OBJECT (window_), " focus-in-event" ,
258
342
G_CALLBACK (&RootWindowGtk::WindowFocusIn), this );
259
343
g_signal_connect (G_OBJECT (window_), " window-state-event" ,
@@ -341,7 +425,7 @@ void RootWindowGtk::CreateRootWindow(const CefBrowserSettings& settings) {
341
425
// Most window managers ignore requests for initial window positions (instead
342
426
// using a user-defined placement algorithm) and honor requests after the
343
427
// window has already been shown.
344
- gtk_window_move (GTK_WINDOW (window_), x, y);
428
+ // gtk_window_move(GTK_WINDOW(window_), x, y);
345
429
346
430
// Windowed browsers are parented to the X11 Window underlying the GtkWindow*
347
431
// and must be sized manually. The OSR GTK widget, on the other hand, can be
0 commit comments