~alexlauni/do/notify-osd

« back to all changes in this revision

Viewing changes to Do.Interface.Wink/src/Do.Interface.Wink/WindowControl.cs

  • Committer: Jason Smith
  • Date: 2009-03-21 02:51:56 UTC
  • mfrom: (1104.1.4 real)
  • Revision ID: jassmith@gmail.com-20090321025156-y15c802y8hfmnka2
Merge wink branch

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 Do.Interface.Wink
 
26
{
 
27
        
 
28
        
 
29
        public static class WindowControl
 
30
        {
 
31
                
 
32
                const int SleepTime = 10;
 
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
                        foreach (Window window in windows.Reverse ()) {
 
92
                                if (window.IsInViewport (window.Screen.ActiveWorkspace) && !window.IsMinimized) {
 
93
                                        window.CenterAndFocusWindow ();
 
94
                                        System.Threading.Thread.Sleep (SleepTime);
 
95
                                }
 
96
                        }
 
97
                        
 
98
                        if (windows.Count () <= 1)
 
99
                                return;
 
100
                        
 
101
                        // we do this to make sure our active window is also at the front... Its a tricky thing to do.
 
102
                        // sometimes compiz plays badly.  This hacks around it
 
103
                        uint time = Gtk.Global.CurrentEventTime + 200;
 
104
                        GLib.Timeout.Add (200, delegate {
 
105
                                windows.Where (w => w.IsInViewport (w.Screen.ActiveWorkspace) && !w.IsMinimized).First ().Activate (time);
 
106
                                return false;
 
107
                        });
 
108
                }
 
109
                
 
110
                public static void FocusWindows (Window window)
 
111
                {
 
112
                        FocusWindows (new [] {window});
 
113
                }
 
114
                
 
115
                public static void IntelligentFocusOffViewportWindow (Window targetWindow, IEnumerable<Window> additionalWindows)
 
116
                {
 
117
                        foreach (Window window in additionalWindows.Reverse ()) {
 
118
                                if (!window.IsMinimized && WindowsShareViewport (targetWindow, window)) {
 
119
                                        window.CenterAndFocusWindow ();
 
120
                                        System.Threading.Thread.Sleep (SleepTime);
 
121
                                }
 
122
                        }
 
123
                        
 
124
                        targetWindow.CenterAndFocusWindow ();
 
125
                        
 
126
                        if (additionalWindows.Count () <= 1)
 
127
                                return;
 
128
                        
 
129
                        // we do this to make sure our active window is also at the front... Its a tricky thing to do.
 
130
                        // sometimes compiz plays badly.  This hacks around it
 
131
                        uint time = Gtk.Global.CurrentEventTime + 200;
 
132
                        GLib.Timeout.Add (200, delegate {
 
133
                                targetWindow.Activate (time);
 
134
                                return false;
 
135
                        });
 
136
                }
 
137
                
 
138
                static bool WindowsShareViewport (Wnck.Window first, Wnck.Window second)
 
139
                {
 
140
                        if (first == null || second == null) return false;
 
141
                        
 
142
                        Wnck.Workspace wksp = first.Workspace ?? second.Workspace;
 
143
                        if (wksp == null) return false;
 
144
                        
 
145
                        Gdk.Rectangle firstGeo, secondGeo;
 
146
                        
 
147
                        first.GetGeometry (out firstGeo.X, out firstGeo.Y, out firstGeo.Width, out firstGeo.Height);
 
148
                        second.GetGeometry (out secondGeo.X, out secondGeo.Y, out secondGeo.Width, out secondGeo.Height);
 
149
                        
 
150
                        firstGeo.X += wksp.ViewportX;
 
151
                        firstGeo.Y += wksp.ViewportY;
 
152
                        
 
153
                        secondGeo.X += wksp.ViewportX;
 
154
                        secondGeo.Y += wksp.ViewportY;
 
155
                        
 
156
                        int viewportWidth, viewportHeight;
 
157
                        viewportWidth = first.Screen.Width;
 
158
                        viewportHeight = first.Screen.Height;
 
159
                        
 
160
                        int firstViewportX = ((firstGeo.X + firstGeo.Width / 2) / viewportWidth) * viewportWidth;
 
161
                        int firstViewportY = ((firstGeo.Y + firstGeo.Height / 2) / viewportHeight) * viewportHeight;
 
162
                        
 
163
                        Gdk.Rectangle viewpRect = new Gdk.Rectangle (firstViewportX, firstViewportY, 
 
164
                                                                     viewportWidth, viewportHeight);
 
165
                        
 
166
                        return viewpRect.IntersectsWith (secondGeo);
 
167
                }
 
168
                
 
169
                public static void CloseWindows (IEnumerable<Window> windows)
 
170
                {
 
171
                        foreach (Window window in windows.Where (w => !w.IsSkipTasklist))
 
172
                                window.Close (Gtk.Global.CurrentEventTime);
 
173
                }
 
174
                
 
175
                public static void CloseWindows (Window window)
 
176
                {
 
177
                        CloseWindows (new [] {window});
 
178
                }
 
179
                
 
180
                public static void MinimizeRestoreWindows (Window window)
 
181
                {
 
182
                        MinimizeRestoreWindows (new [] {window});
 
183
                }
 
184
                
 
185
                public static void MaximizeWindow (Window window)
 
186
                {
 
187
                        window.Maximize ();
 
188
                }
 
189
                
 
190
                /// <summary>
 
191
                /// Moves the current viewport to the selected window and then raises it
 
192
                /// </summary>
 
193
                /// <param name="w">
 
194
                /// A <see cref="Window"/>
 
195
                /// </param>
 
196
                public static void CenterAndFocusWindow (this Window w) 
 
197
                {
 
198
                        if (w == null)
 
199
                                return;
 
200
 
 
201
                        if (!w.IsInViewport (w.Screen.ActiveWorkspace)) {
 
202
                                int viewX, viewY, viewW, viewH;
 
203
                                int midX, midY;
 
204
                                Screen scrn = w.Screen;
 
205
                                Workspace wsp = scrn.ActiveWorkspace;
 
206
                                
 
207
                                //get our windows geometry
 
208
                                w.GetGeometry (out viewX, out viewY, out viewW, out viewH);
 
209
                                
 
210
                                //we want to focus on where the middle of the window is
 
211
                                midX = viewX + (viewW / 2);
 
212
                                midY = viewY + (viewH / 2);
 
213
                                
 
214
                                //The positions given above are relative to the current viewport
 
215
                                //This makes them absolute
 
216
                                midX += wsp.ViewportX;
 
217
                                midY += wsp.ViewportY;
 
218
                                
 
219
                                //Check to make sure our middle didn't wrap
 
220
                                if (midX > wsp.Width) {
 
221
                                        midX %= wsp.Width;
 
222
                                }
 
223
                                
 
224
                                if (midY > wsp.Height) {
 
225
                                        midY %= wsp.Height;
 
226
                                }
 
227
                                
 
228
                                //take care of negative numbers (happens?)
 
229
                                while (midX < 0)
 
230
                                        midX += wsp.Width;
 
231
                        
 
232
                                while (midY < 0)
 
233
                                        midX += wsp.Height;
 
234
                                
 
235
                                scrn.MoveViewport (midX, midY);
 
236
                        }
 
237
 
 
238
                        if (w.Workspace != null && w.Workspace != w.Screen.ActiveWorkspace) 
 
239
                                w.Workspace.Activate (Gtk.Global.CurrentEventTime);
 
240
                        
 
241
                        
 
242
                        if (w.IsMinimized) 
 
243
                                w.Unminimize (Gtk.Global.CurrentEventTime);
 
244
                        
 
245
                        w.Activate (Gtk.Global.CurrentEventTime);
 
246
                }
 
247
        }
 
248
}