File: sp-window-settings.c

package info (click to toggle)
sysprof 3.30.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,616 kB
  • sloc: ansic: 20,056; xml: 96; cpp: 23; sh: 18; makefile: 9
file content (175 lines) | stat: -rw-r--r-- 5,355 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* sp-window-settings.c
 *
 * Copyright (C) 2016 Christian Hergert <chergert@redhat.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "sp-window.h"
#include "sp-window-settings.h"

#define WINDOW_MIN_WIDTH  1200
#define WINDOW_MIN_HEIGHT  700
#define SAVE_TIMEOUT_SECS    1

static GSettings *settings;

static gboolean
sp_window_settings__window_save_settings_cb (gpointer data)
{
  GtkWindow *window = data;
  GdkRectangle geom;
  gboolean maximized;

  g_assert (GTK_IS_WINDOW (window));
  g_assert (G_IS_SETTINGS (settings));

  g_object_set_data (G_OBJECT (window), "SETTINGS_HANDLER_ID", NULL);

  gtk_window_get_size (window, &geom.width, &geom.height);
  gtk_window_get_position (window, &geom.x, &geom.y);
  maximized = gtk_window_is_maximized (window);

  g_settings_set (settings, "window-size", "(ii)", geom.width, geom.height);
  g_settings_set (settings, "window-position", "(ii)", geom.x, geom.y);
  g_settings_set_boolean (settings, "window-maximized", maximized);

  return G_SOURCE_REMOVE;
}

static gboolean
sp_window_settings__window_configure_event (GtkWindow         *window,
                                            GdkEventConfigure *event)
{
  guint handler;

  g_assert (GTK_IS_WINDOW (window));
  g_assert (event != NULL);
  g_assert (G_IS_SETTINGS (settings));

  handler = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (window), "SETTINGS_HANDLER_ID"));

  if (handler == 0)
    {
      handler = g_timeout_add_seconds (SAVE_TIMEOUT_SECS,
                                       sp_window_settings__window_save_settings_cb,
                                       window);
      g_object_set_data (G_OBJECT (window), "SETTINGS_HANDLER_ID", GINT_TO_POINTER (handler));
    }

  return GDK_EVENT_PROPAGATE;
}

static void
sp_window_settings__window_realize (GtkWindow *window)
{
  GtkApplication *app;
  GdkRectangle geom = { 0 };
  gboolean maximized = FALSE;
  GList *list;
  guint count = 0;

  g_assert (GTK_IS_WINDOW (window));
  g_assert (G_IS_SETTINGS (settings));

  g_settings_get (settings, "window-position", "(ii)", &geom.x, &geom.y);
  g_settings_get (settings, "window-size", "(ii)", &geom.width, &geom.height);
  g_settings_get (settings, "window-maximized", "b", &maximized);

  geom.width = MAX (geom.width, WINDOW_MIN_WIDTH);
  geom.height = MAX (geom.height, WINDOW_MIN_HEIGHT);
  gtk_window_set_default_size (window, geom.width, geom.height);

  /*
   * If there are other windows currently visible other than this one,
   * then ignore positioning and let the window manager decide.
   */
  count = 0;
  app = GTK_APPLICATION (g_application_get_default ());
  list = gtk_application_get_windows (app);
  for (; list != NULL; list = list->next)
    {
      GtkWindow *ele = list->data;

      if (SP_IS_WINDOW (ele) && (ele != window) &&
          gtk_widget_get_visible (GTK_WIDGET (window)))
        count++;
    }

  if (count == 0)
    gtk_window_move (window, geom.x, geom.y);

  if (maximized)
    gtk_window_maximize (window);
}

static void
sp_window_settings__window_destroy (GtkWindow *window)
{
  guint handler;

  g_assert (GTK_IS_WINDOW (window));
  g_assert (G_IS_SETTINGS (settings));

  handler = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (window), "SETTINGS_HANDLER_ID"));

  if (handler != 0)
    {
      g_source_remove (handler);
      g_object_set_data (G_OBJECT (window), "SETTINGS_HANDLER_ID", NULL);
    }

  g_signal_handlers_disconnect_by_func (window,
                                        G_CALLBACK (sp_window_settings__window_configure_event),
                                        NULL);

  g_signal_handlers_disconnect_by_func (window,
                                        G_CALLBACK (sp_window_settings__window_destroy),
                                        NULL);

  g_signal_handlers_disconnect_by_func (window,
                                        G_CALLBACK (sp_window_settings__window_realize),
                                        NULL);

  g_object_unref (settings);
}

void
sp_window_settings_register (GtkWindow *window)
{
  if (settings == NULL)
    {
      settings = g_settings_new ("org.gnome.sysprof2");
      g_object_add_weak_pointer (G_OBJECT (settings), (gpointer *)&settings);
    }
  else
    {
      g_object_ref (settings);
    }

  g_signal_connect (window,
                    "configure-event",
                    G_CALLBACK (sp_window_settings__window_configure_event),
                    NULL);

  g_signal_connect (window,
                    "destroy",
                    G_CALLBACK (sp_window_settings__window_destroy),
                    NULL);

  g_signal_connect (window,
                    "realize",
                    G_CALLBACK (sp_window_settings__window_realize),
                    NULL);
}