~tintou/gala/hidpi

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//
//  Copyright (C) 2014 Tom Beckmann
//
//  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/>.
//

using Clutter;
using Meta;

namespace Gala
{
	/**
	 * Utility class which adds a border and a shadow to a Background
	 */
	class FramedBackground : BackgroundManager
	{
		public FramedBackground (Screen screen)
		{
			Object (screen: screen, monitor_index: screen.get_primary_monitor (), control_position: false);
		}

		construct
		{
			var primary = screen.get_primary_monitor ();
			var monitor_geom = screen.get_monitor_geometry (primary);

			add_effect (new ShadowEffect (40, 5));
		}

		public override void paint ()
		{
			base.paint ();

			Cogl.set_source_color4ub (0, 0, 0, 100);
			Cogl.Path.rectangle (0, 0, width, height);
			Cogl.Path.stroke ();

			Cogl.set_source_color4ub (255, 255, 255, 25);
			Cogl.Path.rectangle (0.5f, 0.5f, width - 1, height - 1);
			Cogl.Path.stroke ();
		}
	}

	/**
	 * This is the container which manages a clone of the background which will
	 * be scaled and animated inwards, a WindowCloneContainer for the windows on
	 * this workspace and also holds the instance for this workspace's IconGroup.
	 * The latter is not added to the WorkspaceClone itself though but to a container
	 * of the MultitaskingView.
	 */
	public class WorkspaceClone : Actor
	{
		/**
		 * The offset of the scaled background to the bottom of the monitor bounds
		 */
		public const int BOTTOM_OFFSET = 100;

		/**
		 * The offset of the scaled background to the top of the monitor bounds
		 */
		const int TOP_OFFSET = 20;

		/**
		 * The amount of time a window has to be over the WorkspaceClone while in drag
		 * before we activate the workspace.
		 */
		const int HOVER_ACTIVATE_DELAY = 400;

		/**
		 * A window has been selected, the MultitaskingView should consider activating
		 * and closing the view.
		 */
		public signal void window_selected (Window window);

		/**
		 * The background has been selected. Switch to that workspace.
		 *
		 * @param close_view If the MultitaskingView should also consider closing itself
		 *                   after switching.
		 */
		public signal void selected (bool close_view);

		public Workspace workspace { get; construct; }
		public IconGroup icon_group { get; private set; }
		public WindowCloneContainer window_container { get; private set; }

		bool _active = false;
		/**
		 * If this WorkspaceClone is currently the active one. Also sets the active
		 * state on its IconGroup.
		 */
		public bool active {
			get {
				return _active;
			}
			set {
				_active = value;
				icon_group.active = value;
			}
		}

		BackgroundManager background;
		bool opened;

		uint hover_activate_timeout = 0;

		public WorkspaceClone (Workspace workspace)
		{
			Object (workspace: workspace);
		}

		construct
		{
			opened = false;

			unowned Screen screen = workspace.get_screen ();
			var monitor_geometry = screen.get_monitor_geometry (screen.get_primary_monitor ());

			background = new FramedBackground (workspace.get_screen ());
			background.reactive = true;
			background.button_press_event.connect (() => {
				selected (true);
				return false;
			});

			window_container = new WindowCloneContainer ();
			window_container.window_selected.connect ((w) => { window_selected (w); });
			window_container.set_size (monitor_geometry.width, monitor_geometry.height);
			screen.restacked.connect (window_container.restack_windows);

			icon_group = new IconGroup (workspace);
			icon_group.selected.connect (() => {
				if (workspace == screen.get_active_workspace ())
					Utils.bell (screen);
				else
					selected (false);
			});

			var icons_drop_action = new DragDropAction (DragDropActionType.DESTINATION, "multitaskingview-window");
			icon_group.add_action (icons_drop_action);

			var background_drop_action = new DragDropAction (DragDropActionType.DESTINATION, "multitaskingview-window");
			background.add_action (background_drop_action);
			background_drop_action.crossed.connect ((hovered) => {
				if (!hovered && hover_activate_timeout != 0) {
					Source.remove (hover_activate_timeout);
					hover_activate_timeout = 0;
					return;
				}

				if (hovered && hover_activate_timeout == 0) {
					hover_activate_timeout = Timeout.add (HOVER_ACTIVATE_DELAY, () => {
						selected (false);
						hover_activate_timeout = 0;
						return false;
					});
				}
			});

			screen.window_entered_monitor.connect (window_entered_monitor);
			screen.window_left_monitor.connect (window_left_monitor);
			workspace.window_added.connect (add_window);
			workspace.window_removed.connect (remove_window);

			add_child (background);
			add_child (window_container);

			// add existing windows
			var windows = workspace.list_windows ();
			foreach (var window in windows) {
				if (window.window_type == WindowType.NORMAL
					&& !window.on_all_workspaces
					&& window.get_monitor () == screen.get_primary_monitor ()) {
					window_container.add_window (window);
					icon_group.add_window (window, true);
				}
			}

			var listener = WindowListener.get_default ();
			listener.window_no_longer_on_all_workspaces.connect (add_window);
		}

		~WorkspaceClone ()
		{
			unowned Screen screen = workspace.get_screen ();

			screen.restacked.disconnect (window_container.restack_windows);

			screen.window_entered_monitor.disconnect (window_entered_monitor);
			screen.window_left_monitor.disconnect (window_left_monitor);
			workspace.window_added.disconnect (add_window);
			workspace.window_removed.disconnect (remove_window);

			var listener = WindowListener.get_default ();
			listener.window_no_longer_on_all_workspaces.disconnect (add_window);

			background.destroy ();
		}

		/**
		 * Add a window to the WindowCloneContainer and the IconGroup if it really
		 * belongs to this workspace and this monitor.
		 */
		void add_window (Window window)
		{
			if (window.window_type != WindowType.NORMAL
				|| window.get_workspace () != workspace
				|| window.on_all_workspaces
				|| window.get_monitor () != window.get_screen ().get_primary_monitor ())
				return;

			foreach (var child in window_container.get_children ())
				if (((WindowClone) child).window == window)
					return;

			window_container.add_window (window);
			icon_group.add_window (window);
		}

		/**
		 * Remove a window from the WindowCloneContainer and the IconGroup
		 */
		void remove_window (Window window)
		{
			window_container.remove_window (window);
			icon_group.remove_window (window, opened);
		}

		void window_entered_monitor (Screen screen, int monitor, Window window)
		{
			add_window (window);
		}

		void window_left_monitor (Screen screen, int monitor, Window window)
		{
			if (monitor == screen.get_primary_monitor ())
				remove_window (window);
		}

		void update_size (Meta.Rectangle monitor_geometry)
		{
			if (window_container.width != monitor_geometry.width || window_container.height != monitor_geometry.height) {
				window_container.set_size (monitor_geometry.width, monitor_geometry.height);
				background.set_size (window_container.width, window_container.height);
			}
		}

		/**
		 * Utility function to shrink a MetaRectangle on all sides for the given amount.
		 * Negative amounts will scale it instead.
		 *
		 * @param amount The amount in px to shrink.
		 */
		static inline void shrink_rectangle (ref Meta.Rectangle rect, int amount)
		{
			rect.x += amount;
			rect.y += amount;
			rect.width -= amount * 2;
			rect.height -= amount * 2;
		}

		/**
		 * Animates the background to its scale, causes a redraw on the IconGroup and
		 * makes sure the WindowCloneContainer animates its windows to their tiled layout.
		 * Also sets the current_window of the WindowCloneContainer to the active window
		 * if it belongs to this workspace.
		 */
		public void open ()
		{
			if (opened)
				return;

			opened = true;

			var screen = workspace.get_screen ();
			var display = screen.get_display ();

			var monitor = screen.get_monitor_geometry (screen.get_primary_monitor ());
			var scale = (float)(monitor.height - TOP_OFFSET - BOTTOM_OFFSET) / monitor.height;
			var pivotY = TOP_OFFSET / (monitor.height - monitor.height * scale);

			update_size (monitor);

			background.set_pivot_point (0.5f, pivotY);

			background.save_easing_state ();
			background.set_easing_duration (MultitaskingView.ANIMATION_DURATION);
			background.set_easing_mode (MultitaskingView.ANIMATION_MODE);
			background.set_scale (scale, scale);
			background.restore_easing_state ();

			Meta.Rectangle area = {
				(int)Math.floorf (monitor.x + monitor.width - monitor.width * scale) / 2,
				(int)Math.floorf (monitor.y + TOP_OFFSET),
				(int)Math.floorf (monitor.width * scale),
				(int)Math.floorf (monitor.height * scale)
			};
			shrink_rectangle (ref area, 32);

			window_container.padding_top = TOP_OFFSET;
			window_container.padding_left =
				window_container.padding_right = (int)(monitor.width - monitor.width * scale) / 2;
			window_container.padding_bottom = BOTTOM_OFFSET;

			icon_group.redraw ();

			window_container.open (screen.get_active_workspace () == workspace ? display.get_focus_window () : null);
		}

		/**
		 * Close the view again by animating the background back to its scale and
		 * the windows back to their old locations.
		 */
		public void close ()
		{
			if (!opened)
				return;
			
			opened = false;

			background.save_easing_state ();
			background.set_easing_duration (MultitaskingView.ANIMATION_DURATION);
			background.set_easing_mode (MultitaskingView.ANIMATION_MODE);
			background.set_scale (1, 1);
			background.restore_easing_state ();

			window_container.close ();
		}
	}
}