~ubuntu-branches/debian/sid/docky/sid

« back to all changes in this revision

Viewing changes to Docky.Services/Docky.Services/Windows/WindowControl.cs

  • Committer: Package Import Robot
  • Author(s): Rico Tzschichholz
  • Date: 2012-01-19 19:03:38 UTC
  • mfrom: (1.1.14) (10.1.9 experimental)
  • Revision ID: package-import@ubuntu.com-20120119190338-n44q7tmqsrkudvk7
Tags: 2.1.3-2
* Upload to unstable
* debian/watch:
  + Look for xz tarballs from now on

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// WindowControl.cs
 
2
// 
 
3
// Copyright (C) 2008 GNOME Do
 
4
//
 
5
// This program is free software: you can redistribute it and/or modify
 
6
// it under the terms of the GNU General Public License as published by
 
7
// the Free Software Foundation, either version 3 of the License, or
 
8
// (at your option) any later version.
 
9
//
 
10
// This program is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
// GNU General Public License for more details.
 
14
//
 
15
// You should have received a copy of the GNU General Public License
 
16
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
//
 
18
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
using System.Linq;
 
22
 
 
23
using Wnck;
 
24
 
 
25
namespace Docky.Services.Windows
 
26
{
 
27
        
 
28
        public static class WindowControl
 
29
        {
 
30
                
 
31
                const int SleepTime = 10;
 
32
                const int FocusDelay = 200;
 
33
                
 
34
                /// <summary>
 
35
                /// Handles intelligent minimize/restoring of windows.  If one or more windows is minimized, it restores
 
36
                /// all windows.  If more all are visible, it minimizes.  This operation only takes into account windows
 
37
                /// on the current workspace (by design).
 
38
                /// </summary>
 
39
                /// <param name="windows">
 
40
                /// A <see cref="IEnumerable"/>
 
41
                /// </param>
 
42
                public static void MinimizeRestoreWindows (IEnumerable<Window> windows)
 
43
                {
 
44
                        bool restore = false;
 
45
                        foreach (Window w in windows) {
 
46
                                if (w.IsMinimized) {
 
47
                                        restore = true;
 
48
                                        break;
 
49
                                }
 
50
                        }
 
51
                        if (restore)
 
52
                                RestoreWindows (windows);
 
53
                        else
 
54
                                MinimizeWindows (windows);
 
55
                }
 
56
                
 
57
                /// <summary>
 
58
                /// Minimizes every window in the list if it is not minimized
 
59
                /// </summary>
 
60
                /// <param name="windows">
 
61
                /// A <see cref="IEnumerable"/>
 
62
                /// </param>
 
63
                public static void MinimizeWindows (IEnumerable<Window> windows)
 
64
                {
 
65
                        foreach (Window window in windows) {
 
66
                                if (window.IsInViewport (window.Screen.ActiveWorkspace) && !window.IsMinimized) {
 
67
                                        window.Minimize ();
 
68
                                        System.Threading.Thread.Sleep (SleepTime);
 
69
                                }
 
70
                        }
 
71
                }
 
72
                
 
73
                /// <summary>
 
74
                /// Restores every window in the list that is minimized
 
75
                /// </summary>
 
76
                /// <param name="windows">
 
77
                /// A <see cref="IEnumerable"/>
 
78
                /// </param>
 
79
                public static void RestoreWindows (IEnumerable<Window> windows)
 
80
                {
 
81
                        foreach (Window window in windows.Reverse ()) {
 
82
                                if (window.IsInViewport (window.Screen.ActiveWorkspace) && window.IsMinimized) {
 
83
                                        window.Unminimize (Gtk.Global.CurrentEventTime);
 
84
                                        System.Threading.Thread.Sleep (SleepTime);
 
85
                                }
 
86
                        }
 
87
                }
 
88
                
 
89
                public static void FocusWindows (IEnumerable<Window> windows)
 
90
                {
 
91
                        if (!windows.Any ())
 
92
                                return;
 
93
                        
 
94
                        if (windows.Any (w => w.IsInViewport (w.Screen.ActiveWorkspace))) {
 
95
                                foreach (Window window in windows.Reverse ()) {
 
96
                                        if (window.IsInViewport (window.Screen.ActiveWorkspace) && !window.IsMinimized) {
 
97
                                                window.CenterAndFocusWindow ();
 
98
                                                System.Threading.Thread.Sleep (SleepTime);
 
99
                                        }
 
100
                                }
 
101
                        } else {
 
102
                                windows.First ().CenterAndFocusWindow ();
 
103
                        }
 
104
                        
 
105
                        if (windows.Count () <= 1)
 
106
                                return;
 
107
                        
 
108
                        // we do this to make sure our active window is also at the front... Its a tricky thing to do.
 
109
                        // sometimes compiz plays badly.  This hacks around it
 
110
                        uint time = Gtk.Global.CurrentEventTime + FocusDelay;
 
111
                        GLib.Timeout.Add (FocusDelay, delegate {
 
112
                                try { //unimportant if this fails, its just "nice"
 
113
                                        windows.Where (w => w.IsInViewport (w.Screen.ActiveWorkspace) && !w.IsMinimized).First ().Activate (time);
 
114
                                } catch { }
 
115
                                return false;
 
116
                        });
 
117
                }
 
118
                
 
119
                public static void FocusWindows (Window window)
 
120
                {
 
121
                        FocusWindows (new [] {window});
 
122
                }
 
123
                
 
124
                public static void IntelligentFocusOffViewportWindow (Window targetWindow, IEnumerable<Window> additionalWindows)
 
125
                {
 
126
                        foreach (Window window in additionalWindows.Reverse ()) {
 
127
                                if (!window.IsMinimized && WindowsShareViewport (targetWindow, window)) {
 
128
                                        window.CenterAndFocusWindow ();
 
129
                                        System.Threading.Thread.Sleep (SleepTime);
 
130
                                }
 
131
                        }
 
132
                        
 
133
                        targetWindow.CenterAndFocusWindow ();
 
134
                        
 
135
                        if (additionalWindows.Count () <= 1)
 
136
                                return;
 
137
                        
 
138
                        // we do this to make sure our active window is also at the front... Its a tricky thing to do.
 
139
                        // sometimes compiz plays badly.  This hacks around it
 
140
                        uint time = Gtk.Global.CurrentEventTime + FocusDelay;
 
141
                        GLib.Timeout.Add (FocusDelay, delegate {
 
142
                                targetWindow.Activate (time);
 
143
                                return false;
 
144
                        });
 
145
                }
 
146
                
 
147
                static bool WindowsShareViewport (Wnck.Window first, Wnck.Window second)
 
148
                {
 
149
                        if (first == null || second == null) return false;
 
150
                        
 
151
                        Wnck.Workspace wksp = first.Workspace ?? second.Workspace;
 
152
                        if (wksp == null) return false;
 
153
                        
 
154
                        Gdk.Rectangle firstGeo, secondGeo;
 
155
                        
 
156
                        first.GetGeometry (out firstGeo.X, out firstGeo.Y, out firstGeo.Width, out firstGeo.Height);
 
157
                        second.GetGeometry (out secondGeo.X, out secondGeo.Y, out secondGeo.Width, out secondGeo.Height);
 
158
                        
 
159
                        firstGeo.X += wksp.ViewportX;
 
160
                        firstGeo.Y += wksp.ViewportY;
 
161
                        
 
162
                        secondGeo.X += wksp.ViewportX;
 
163
                        secondGeo.Y += wksp.ViewportY;
 
164
                        
 
165
                        int viewportWidth, viewportHeight;
 
166
                        viewportWidth = first.Screen.Width;
 
167
                        viewportHeight = first.Screen.Height;
 
168
                        
 
169
                        int firstViewportX = ((firstGeo.X + firstGeo.Width / 2) / viewportWidth) * viewportWidth;
 
170
                        int firstViewportY = ((firstGeo.Y + firstGeo.Height / 2) / viewportHeight) * viewportHeight;
 
171
                        
 
172
                        Gdk.Rectangle viewpRect = new Gdk.Rectangle (firstViewportX, firstViewportY, 
 
173
                                                                     viewportWidth, viewportHeight);
 
174
                        
 
175
                        return viewpRect.IntersectsWith (secondGeo);
 
176
                }
 
177
                
 
178
                public static void CloseWindows (IEnumerable<Window> windows)
 
179
                {
 
180
                        foreach (Window window in windows.Where (w => !w.IsSkipTasklist))
 
181
                                window.Close (Gtk.Global.CurrentEventTime);
 
182
                }
 
183
                
 
184
                public static void CloseWindows (Window window)
 
185
                {
 
186
                        CloseWindows (new [] {window});
 
187
                }
 
188
                
 
189
                public static void MinimizeRestoreWindows (Window window)
 
190
                {
 
191
                        MinimizeRestoreWindows (new [] {window});
 
192
                }
 
193
                
 
194
                public static void MaximizeWindows (IEnumerable<Window> windows)
 
195
                {
 
196
                        foreach (Window window in windows)
 
197
                                window.Maximize ();
 
198
                }
 
199
                
 
200
                public static void MaximizeWindows (Window window)
 
201
                {
 
202
                        window.Maximize ();
 
203
                }
 
204
                
 
205
                public static void UnmaximizeWindows (IEnumerable<Window> windows)
 
206
                {
 
207
                        foreach (Wnck.Window window in windows)
 
208
                                UnmaximizeWindows (window);
 
209
                }
 
210
                
 
211
                public static void UnmaximizeWindows (Wnck.Window window)
 
212
                {
 
213
                        if (window.IsMaximized)
 
214
                                window.Unmaximize ();
 
215
                }
 
216
                
 
217
                public static void MoveToWorkspace (Window window, Workspace workspace)
 
218
                {
 
219
                        MoveToWorkspace (new [] {window}, workspace);
 
220
                }
 
221
                
 
222
                public static void MoveToWorkspace (IEnumerable<Window> windows, Workspace workspace)
 
223
                {
 
224
                        foreach (Window window in windows.Where (w => w.Workspace != workspace))
 
225
                                window.MoveToWorkspace (workspace);
 
226
                }
 
227
                
 
228
                /// <summary>
 
229
                /// Moves the current viewport to the selected window and then raises it
 
230
                /// </summary>
 
231
                /// <param name="w">
 
232
                /// A <see cref="Window"/>
 
233
                /// </param>
 
234
                public static void CenterAndFocusWindow (this Window w) 
 
235
                {
 
236
                        if (w == null)
 
237
                                return;
 
238
 
 
239
                        uint time = Gtk.Global.CurrentEventTime;
 
240
                        if (w.Workspace != null && w.Workspace != w.Screen.ActiveWorkspace) 
 
241
                                w.Workspace.Activate (time);
 
242
                        
 
243
                        
 
244
                        if (w.IsMinimized) 
 
245
                                w.Unminimize (time);
 
246
                        
 
247
                        w.ActivateTransient (time);
 
248
                }
 
249
        }
 
250
}