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

Commit 4de3f69

Browse files
authored
Merge pull request #1 from adobe/nethip/Linux-Window-State
Code cleanup
2 parents d283b0b + b6b1371 commit 4de3f69

File tree

1 file changed

+158
-77
lines changed

1 file changed

+158
-77
lines changed

appshell/browser/root_window_gtk.cc

Lines changed: 158 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -21,117 +21,196 @@
2121
#include "appshell/browser/window_test.h"
2222
#include "appshell/common/client_switches.h"
2323

24-
// Brackets specific change.
24+
// Brackets specific changes.
2525
#include "appshell/native_menu_model.h"
2626
#include "appshell/command_callbacks.h"
27+
#include "appshell/appshell_helpers.h"
28+
29+
#define DEFAULT_WINDOW_WIDTH 800
30+
#define DEFAULT_WINDOW_HEIGHT 600
31+
// End of Brackets specific changes.
2732

2833
namespace client {
2934

3035
namespace {
3136

3237
const char kMenuIdKey[] = "menu_id";
3338

34-
bool IsWindowMaximized(GtkWindow* window) {
35-
GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
36-
gint state = gdk_window_get_state(gdk_window);
37-
return (state & GDK_WINDOW_STATE_MAXIMIZED) ? true : false;
39+
// Brackets specific changes.
40+
gboolean IsWindowMaximized(GtkWindow* window) {
41+
42+
if (window) {
43+
GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
44+
if (gdk_window) {
45+
gint state = gdk_window_get_state(gdk_window);
46+
return (state & GDK_WINDOW_STATE_MAXIMIZED) ? TRUE : FALSE;
47+
}
48+
} else {
49+
return FALSE;
50+
}
51+
3852
}
3953

4054
void MinimizeWindow(GtkWindow* window) {
41-
// Unmaximize the window before minimizing so restore behaves correctly.
42-
if (IsWindowMaximized(window))
43-
gtk_window_unmaximize(window);
4455

45-
gtk_window_iconify(window);
56+
if (window) {
57+
// Unmaximize the window before minimizing so restore behaves correctly.
58+
if (IsWindowMaximized(window))
59+
gtk_window_unmaximize(window);
60+
61+
gtk_window_iconify(window);
62+
}
4663
}
4764

4865
void MaximizeWindow(GtkWindow* window) {
49-
gtk_window_maximize(window);
66+
if (window) {
67+
gtk_window_maximize(window);
68+
}
5069
}
5170

5271
void SaveWindowState(GtkWindow* window) {
53-
gint left, top;
54-
gint width, height;
72+
73+
if (!window)
74+
return;
75+
76+
gint left = 1;
77+
gint top = 1;
78+
gint width = DEFAULT_WINDOW_WIDTH;
79+
gint height = DEFAULT_WINDOW_HEIGHT;
5580

5681
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, restore defaults
75-
if (left == 0 || top == 0 || width == 0 || height == 0) {
76-
left = 1;
77-
top = 1;
78-
width = 800;
79-
height = 600;
82+
GError* err = NULL;
83+
gchar* filePath = NULL;
84+
85+
if (key_file) {
86+
filePath = g_strdup_printf("%s/%s", appshell::AppGetSupportDirectory().ToString().c_str(), "window.ini");
87+
gboolean maximized = IsWindowMaximized(window);
88+
89+
// If window is not maximized, save current size and position
90+
91+
if (!maximized) {
92+
gtk_window_get_position(window, &left, &top);
93+
gtk_window_get_size(window, &width, &height);
94+
} else if (g_key_file_load_from_file(key_file, filePath, G_KEY_FILE_NONE, &err)) {
95+
96+
// If maximized, load size and position from file
97+
// to preserve last saved values
98+
left = g_key_file_get_integer(key_file, "position", "left", &err);
99+
if (!err)
100+
top = g_key_file_get_integer(key_file, "position", "top", &err);
101+
102+
if (!err)
103+
width = g_key_file_get_integer(key_file, "size", "width", &err);
104+
105+
if (!err)
106+
height = g_key_file_get_integer(key_file, "size", "height", &err);
107+
108+
// If any value can not be read, restore defaults
109+
if (err) {
110+
left = 1;
111+
top = 1;
112+
width = DEFAULT_WINDOW_WIDTH;
113+
height = DEFAULT_WINDOW_HEIGHT;
114+
g_error_free(err);
80115
}
81-
// If we can not load the file, set defaults
82-
} else {
83-
left = 1;
84-
top = 1;
85-
width = 800;
86-
height = 600;
87116
}
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");
117+
118+
// The values would always be written to file.
119+
g_key_file_set_integer(key_file, "position", "left", left);
120+
g_key_file_set_integer(key_file, "position", "top", top);
121+
g_key_file_set_integer(key_file, "size", "width", width - 1); // DelayedResize() 1 pixel compensation
122+
g_key_file_set_integer(key_file, "size", "height", height - 1); // DelayedResize() 1 pixel compensation
123+
g_key_file_set_boolean(key_file, "state", "maximized", maximized);
124+
125+
err = NULL;
126+
g_key_file_save_to_file(key_file, filePath, &err);
127+
128+
if (err) {
129+
fprintf(stderr, "Err -> SaveWindowState(): could not write to `window.ini`. Error Description: %s\n", err->message);
130+
}
131+
} else {
132+
fprintf(stderr, "Err -> SaveWindowState(): could not write to `window.ini`\n");
99133
}
100134
}
101135

102136
void LoadWindowState(GtkWindow* window) {
103-
// Default values if It is not possible to load the key file
104-
gint left = 1;
105-
gint top = 1;
106-
gint width = 800;
107-
gint height = 600;
108-
bool maximized = FALSE;
137+
138+
if (!window) {
139+
return;
140+
}
141+
142+
// Default values for the window state.
143+
gint left = 1;
144+
gint top = 1;
145+
gint width = DEFAULT_WINDOW_WIDTH;
146+
gint height = DEFAULT_WINDOW_HEIGHT;
147+
148+
// Try to center the window.
149+
GdkScreen* screen = gdk_screen_get_default();
150+
if (screen) {
151+
left = (gdk_screen_get_width(screen) - DEFAULT_WINDOW_WIDTH) / 2 ;
152+
top = (gdk_screen_get_height(screen) - DEFAULT_WINDOW_HEIGHT) / 2 ;
153+
}
154+
155+
gboolean maximized = false;
109156

110157
GKeyFile* key_file = g_key_file_new();
111-
GError* error = NULL;
112-
gchar* filePath = g_strdup_printf("%s/%s/%s", g_get_user_config_dir(), APP_NAME, "window.ini");
113-
114-
if(g_key_file_load_from_file(key_file, filePath, G_KEY_FILE_NONE, NULL))
115-
{
116-
left = g_key_file_get_integer(key_file, "position", "left", &error);
117-
top = g_key_file_get_integer(key_file, "position", "top", &error);
118-
width = g_key_file_get_integer(key_file, "size", "width", &error);
119-
height = g_key_file_get_integer(key_file, "size", "height", &error);
120-
maximized = g_key_file_get_boolean(key_file, "state", "maximized", &error);
158+
bool any_error = false;
159+
GError* err = NULL;
160+
gchar* filePath = g_strdup_printf("%s/%s", appshell::AppGetSupportDirectory().ToString().c_str(), "window.ini");
161+
162+
if (key_file && g_key_file_load_from_file(key_file, filePath, G_KEY_FILE_NONE, &err)) {
163+
164+
left = g_key_file_get_integer(key_file, "position", "left", &err);
165+
if (!err)
166+
top = g_key_file_get_integer(key_file, "position", "top", &err);
167+
168+
if (!err)
169+
width = g_key_file_get_integer(key_file, "size", "width", &err);
170+
171+
if (!err)
172+
height = g_key_file_get_integer(key_file, "size", "height", &err);
173+
174+
if (!err)
175+
maximized = g_key_file_get_boolean(key_file, "state", "maximized", &err);
176+
121177
// If any value can not be readed, set defaults again
122-
if (left == 0 || top == 0 || width == 0 || height == 0) {
123-
left = 1;
124-
top = 1;
125-
width = 800;
126-
height = 600;
127-
maximized = FALSE;
178+
if (err) {
179+
left = 1;
180+
top = 1;
181+
width = DEFAULT_WINDOW_WIDTH;
182+
height = DEFAULT_WINDOW_HEIGHT;
183+
maximized = TRUE;
128184
}
185+
186+
gtk_window_move(GTK_WINDOW(window), left, top);
187+
gtk_window_set_default_size(GTK_WINDOW(window), width, height);
188+
189+
if (maximized)
190+
MaximizeWindow(window);
191+
192+
} else {
193+
any_error = true;
129194
}
130-
gtk_window_move(GTK_WINDOW(window), left, top);
131-
gtk_window_set_default_size(GTK_WINDOW(window), width, height);
132-
if (maximized)
195+
196+
if (err || any_error) {
197+
198+
// The failure could be because the file may not have been present,
199+
// or the file read itself failed.
200+
// In either of the cases default to maximizing the window.
133201
MaximizeWindow(window);
202+
203+
if (err) {
204+
if (err->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND){
205+
fprintf(stderr, "LoadWindowState(): Could not read %s. Error Description:%s.\n", filePath, err->message);
206+
}
207+
g_error_free(err);
208+
} else {
209+
fprintf(stderr, "LoadWindowState(): Could not read %s.\n", filePath);
210+
}
211+
}
134212
}
213+
// End of Brackets specific changes.
135214

136215
} // namespace
137216

@@ -332,12 +411,14 @@ void RootWindowGtk::CreateRootWindow(const CefBrowserSettings& settings) {
332411

333412
window_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
334413

414+
// Brackets specific change.
335415
if (start_rect_.IsEmpty()) {
336416
LoadWindowState(GTK_WINDOW(window_));
337417
} else {
338418
gtk_window_move(GTK_WINDOW(window_), x, y);
339419
gtk_window_set_default_size(GTK_WINDOW(window_), width, height);
340420
}
421+
// End of Brackets specific change.
341422

342423
g_signal_connect(G_OBJECT(window_), "focus-in-event",
343424
G_CALLBACK(&RootWindowGtk::WindowFocusIn), this);

0 commit comments

Comments
 (0)