Skip to content
This repository was archived by the owner on Sep 2, 2021. It is now read-only.

Commit 314b9a2

Browse files
committed
Window state remembering - initial commit
1 parent 37d0870 commit 314b9a2

File tree

1 file changed

+93
-9
lines changed

1 file changed

+93
-9
lines changed

appshell/browser/root_window_gtk.cc

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,89 @@ void MaximizeWindow(GtkWindow* window) {
4949
gtk_window_maximize(window);
5050
}
5151

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+
52135
} // namespace
53136

54137
RootWindowGtk::RootWindowGtk()
@@ -197,6 +280,7 @@ void RootWindowGtk::Close(bool force) {
197280
REQUIRE_MAIN_THREAD();
198281

199282
if (window_) {
283+
SaveWindowState(GTK_WINDOW(window_));
200284
force_close_ = force;
201285
gtk_widget_destroy(window_);
202286
}
@@ -242,18 +326,18 @@ void RootWindowGtk::CreateRootWindow(const CefBrowserSettings& settings) {
242326
// in the upper-left corner. Maybe there's a better default place to put it?
243327
int x = start_rect_.x;
244328
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+
246334
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_));
250336
} 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);
253339
}
254340

255-
window_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
256-
gtk_window_set_default_size(GTK_WINDOW(window_), width, height);
257341
g_signal_connect(G_OBJECT(window_), "focus-in-event",
258342
G_CALLBACK(&RootWindowGtk::WindowFocusIn), this);
259343
g_signal_connect(G_OBJECT(window_), "window-state-event",
@@ -341,7 +425,7 @@ void RootWindowGtk::CreateRootWindow(const CefBrowserSettings& settings) {
341425
// Most window managers ignore requests for initial window positions (instead
342426
// using a user-defined placement algorithm) and honor requests after the
343427
// window has already been shown.
344-
gtk_window_move(GTK_WINDOW(window_), x, y);
428+
//gtk_window_move(GTK_WINDOW(window_), x, y);
345429

346430
// Windowed browsers are parented to the X11 Window underlying the GtkWindow*
347431
// and must be sized manually. The OSR GTK widget, on the other hand, can be

0 commit comments

Comments
 (0)