-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskBoard.cs
More file actions
496 lines (428 loc) · 18.7 KB
/
TaskBoard.cs
File metadata and controls
496 lines (428 loc) · 18.7 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
namespace TaskBoardWf
{
public partial class TaskBoard : Form
{
// TODO: Consider to write rubber band above the task icon
// TODO: Make it scalable and scrollable
// TODO: Implement keyboard interface
// TODO: Make HOTKEY and colors configurable
//
// Variables
//
// Rubber Band (Left button drag)
bool isSelecting;
Point rubberBandStart;
Color lineColor = Color.Purple; // or Gray
int lineBorder = 1;
Graphics gRubberBand;
// Scrolling (Right button drag)
bool isScrolling = false;
private Point scrollStart;
// Global Hot Key
HotKey hotKey;
// Window Image
private IntPtr thumbHandle;
private int deltaOpacity;
// Edge Controller
private EdgeGuideController edgeGuideController;
//
// Constructor
//
public TaskBoard()
{
InitializeComponent();
edgeGuideController = new EdgeGuideController(this);
}
//
// Method for Key event
//
void hotKey_HotKeyPush(object sender, EventArgs e)
{
if (Form.ActiveForm != this) {
Activate();
BringToFront();
WindowState = FormWindowState.Maximized;
}
else {
Logger.LogError("HotKey pushed when TaskBoard is active");
// Write code for command of M-q here
ForegroundSelectedTask();
}
}
private void SelectNextTask(IntPtr handle)
{
throw new NotImplementedException();
}
private void ForegroundSelectedTask()
{
foreach (var taskControl in Controls.OfType<TaskUserControl>()) {
if (taskControl.IsSelected) {
WinAPI.SetForegroundTask(taskControl.WindowHandle);
break;
}
}
}
private void SelectRightTask()
{
var selectedControl = Controls.OfType<TaskUserControl>().FirstOrDefault(c => c.IsSelected);
if (selectedControl is null) {
// TODO: If nothing selected, select most recent Task
var topControl = Controls.OfType<TaskUserControl>().First().IsSelected = true;
return;
}
var targetControl = Controls.OfType<TaskUserControl>()
.Where(c => c.Right > selectedControl.Right)
// Select controls nearly on the same line
.Where(c => Math.Abs(c.Top - selectedControl.Top) < selectedControl.Height)
// Do not select controls out of the screen area
.Where(c => c.Left < ClientSize.Width && c.Right > 0)
.Where(c => c.Top < ClientSize.Height && c.Bottom > 0)
.OrderBy(c => c.Right)
.FirstOrDefault();
if (targetControl is null) return;
selectedControl.IsSelected = false;
targetControl.IsSelected = true;
}
private void SelectLeftTask()
{
var selectedControl = Controls.OfType<TaskUserControl>().FirstOrDefault(c => c.IsSelected);
if (selectedControl is null) {
var topControl = Controls.OfType<TaskUserControl>().First().IsSelected = true;
return;
}
var targetControl = Controls.OfType<TaskUserControl>()
.Where(c => c.Right < selectedControl.Right)
.Where(c => Math.Abs(c.Top - selectedControl.Top) < selectedControl.Height)
.Where(c => c.Left < ClientSize.Width && c.Right > 0)
.Where(c => c.Top < ClientSize.Height && c.Bottom > 0)
.OrderBy(c => c.Right)
.OrderByDescending(c => c.Right)
.FirstOrDefault();
if (targetControl is null) return;
selectedControl.IsSelected = false;
targetControl.IsSelected = true;
}
private void SelectUpperTask()
{
var selectedControl = Controls.OfType<TaskUserControl>().FirstOrDefault(c => c.IsSelected);
if (selectedControl is null) {
var topControl = Controls.OfType<TaskUserControl>().First().IsSelected = true;
return;
}
var targetControl = Controls.OfType<TaskUserControl>()
.Where(c => c.Top < selectedControl.Top)
.Where(c => c.Left < ClientSize.Width && c.Right > 0)
.Where(c => c.Top < ClientSize.Height && c.Bottom > 0)
.OrderBy(c => c.Right)
.OrderByDescending(c => c.Top)
.FirstOrDefault();
if (targetControl is null) return;
selectedControl.IsSelected = false;
targetControl.IsSelected = true;
}
private void SelectLowerTask()
{
var selectedControl = Controls.OfType<TaskUserControl>().FirstOrDefault(c => c.IsSelected);
if (selectedControl is null) {
var topControl = Controls.OfType<TaskUserControl>().First().IsSelected = true;
return;
}
var targetControl = Controls.OfType<TaskUserControl>()
.Where(c => c.Top > selectedControl.Top)
.Where(c => c.Left < ClientSize.Width && c.Right > 0)
.Where(c => c.Top < ClientSize.Height && c.Bottom > 0)
.OrderBy(c => c.Right)
.OrderBy(c => c.Top)
.FirstOrDefault();
if (targetControl is null) return;
selectedControl.IsSelected = false;
targetControl.IsSelected = true;
}
//
// Methods for display control
//
// Propose where to place new Task control
private Point ProposePosition()
{
Control baseCtrl = null;
// TODO: Consider where to place the new Task
// TODO: Consider to disallow overlapping controls
// Next to the most bottom and most right Task control
foreach (Control ctrl in Controls.OfType<TaskUserControl>()) {
if (baseCtrl == null || baseCtrl.Bottom < ctrl.Bottom || (baseCtrl.Bottom == ctrl.Bottom && baseCtrl.Right < ctrl.Right)) {
baseCtrl = ctrl;
}
}
if (baseCtrl == null) {
// The first one should be place at (0, 0)
return Point.Empty;
}
else if (baseCtrl.Right + baseCtrl.Width > Screen.PrimaryScreen.WorkingArea.Width) {
// If excessing Board width, place lower
return new Point(0, baseCtrl.Top + baseCtrl.Height);
}
else {
// Otherwise, place next to the one
return new Point(baseCtrl.Right, baseCtrl.Top);
}
}
// Update Task controls on the Board, delete obsolete Task controls and add new Task controls
public void Renew()
{
var runningTasks = WinAPI.GetTaskHwndList();
var taskToRemove = new List<TaskUserControl>();
foreach (var taskControl in Controls.OfType<TaskUserControl>()) {
if (runningTasks.Contains(taskControl.WindowHandle)) {
// Remove existing Task controls from the variable to extract new tasks
runningTasks.Remove(taskControl.WindowHandle);
taskControl.Renew();
}
else {
// Add obsolete tasks to the list
// Disposing control here makes "foreach" not work properly
taskToRemove.Add(taskControl);
}
}
// Dispose obsolete Task controls
foreach (var task in taskToRemove) {
task.Dispose();
}
// Add new tasks from old to new ones
runningTasks.Reverse();
foreach (var newTask in runningTasks) {
var newTaskControl = new TaskUserControl(newTask);
newTaskControl.Location = ProposePosition();
Controls.Add(newTaskControl);
newTaskControl.BringToFront();
}
// TODO: Save tasks positions to recover the layout when restarting after crashes
// TODO: Save tasks positions to avoid rearrange tasks every time logging in using short cut and/or MiLauncher
}
internal void DisplayWindowImage(IntPtr winHandle)
{
if (Program.appSettings.BackgroundThumbnail) {
DisplayThumbnail(winHandle, opaque: true);
Bitmap screenImage = WinAPI.CaptureWindow(Handle);
WinAPI.DwmUnregisterThumbnail(thumbHandle);
thumbHandle = IntPtr.Zero;
BackgroundImage = ConvertToGrayscale(ResizeImage(screenImage));
}
else {
DisplayThumbnail(winHandle);
}
}
private static Bitmap ConvertToGrayscale(Bitmap original)
{
Bitmap grayscaleBitmap = new Bitmap(original.Width, original.Height);
for (int y = 0; y < original.Height; y++) {
for (int x = 0; x < original.Width; x++) {
Color originalColor = original.GetPixel(x, y);
// グレースケールの計算(標準的な輝度法)
int grayScale = (int)(originalColor.R * 0.3 + originalColor.G * 0.59 + originalColor.B * 0.11);
// 新しい色を設定
Color grayColor = Color.FromArgb(originalColor.A, grayScale, grayScale, grayScale);
grayscaleBitmap.SetPixel(x, y, grayColor);
}
}
return grayscaleBitmap;
}
private static Bitmap ResizeImage(Bitmap image)
{
int newWidth = (int)(image.Width * 0.9);
int newHeight = (int)(image.Height * 0.9);
Bitmap resizedImage = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(resizedImage)) {
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(image, 0, 0, newWidth, newHeight);
}
return resizedImage;
}
internal void DisplayThumbnail(IntPtr winHandle, bool opaque = false)
{
// For safety, check and unregister thumbHandle before registering
if (thumbHandle != IntPtr.Zero) {
WinAPI.DwmUnregisterThumbnail(thumbHandle);
thumbHandle = IntPtr.Zero;
}
int result = WinAPI.DwmRegisterThumbnail(Handle, winHandle, out thumbHandle);
if (result != 0) {
Debug.WriteLine("Failed to register thumbnail.");
return;
}
WinAPI.DWM_THUMBNAIL_PROPERTIES props = new WinAPI.DWM_THUMBNAIL_PROPERTIES {
dwFlags = WinAPI.DWM_THUMBNAIL_PROPERTIES.DWM_TNP_RECTDESTINATION |
WinAPI.DWM_THUMBNAIL_PROPERTIES.DWM_TNP_VISIBLE |
WinAPI.DWM_THUMBNAIL_PROPERTIES.DWM_TNP_OPACITY,
// Set TaskBoard itself as destination screen
rcDestination = new WinAPI.RECT {
Left = ClientRectangle.Left,
Top = ClientRectangle.Top,
Right = ClientRectangle.Right,
Bottom = ClientRectangle.Bottom
},
fVisible = true,
opacity = opaque
? byte.MaxValue
: (byte)(Program.appSettings.ThumbnailOpacity + deltaOpacity)
//: Math.Max(Math.Min((byte)(Program.appSettings.ThumbnailOpacity + deltaOpacity), byte.MaxValue), byte.MinValue)
};
WinAPI.DwmUpdateThumbnailProperties(thumbHandle, ref props);
}
internal void ChangeThumbnailOpacity(IntPtr winHandle, bool increase)
{
// Debug.WriteLine("mouse wheel event " + (e.Delta > 0 ? "Up" : "Down"));
var delta = Program.appSettings.DeltaOpacity;
deltaOpacity += increase ? delta : -delta;
deltaOpacity = Math.Min(deltaOpacity, byte.MaxValue - Program.appSettings.ThumbnailOpacity);
deltaOpacity = Math.Max(deltaOpacity, byte.MinValue - Program.appSettings.ThumbnailOpacity);
DisplayThumbnail(winHandle);
}
internal void ClearWindowImage()
{
if (Program.appSettings.BackgroundThumbnail) {
BackgroundImage = null;
}
else {
WinAPI.DwmUnregisterThumbnail(thumbHandle);
thumbHandle = IntPtr.Zero;
deltaOpacity = 0;
}
}
//
// Event Handlers
//
private void TaskBoard_Load(object sender, EventArgs e)
{
WindowState = FormWindowState.Maximized;
// Initialize displaying Task controls on the Board using Renew()
Renew();
// Global Hot Key
hotKey = new HotKey(MOD_KEY.ALT, Keys.Q); // Keys.MButton not work
hotKey.HotKeyPush += new EventHandler(hotKey_HotKeyPush);
Logger.LogError("hotkey registered");
}
private void TaskBoard_FormClosing(object sender, FormClosingEventArgs e)
{
hotKey.Dispose();
}
private void TaskBoard_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
rubberBandStart = PointToClient(Cursor.Position);
isSelecting = true;
Renew();
foreach (var taskControl in Controls.OfType<TaskUserControl>()) {
taskControl.IsSelected = false;
}
}
else if (e.Button == MouseButtons.Right) {
scrollStart = e.Location;
isScrolling = true;
Cursor = Cursors.SizeAll;
edgeGuideController.ShowEdgeGuides(Controls.OfType<TaskUserControl>());
}
ClearWindowImage();
}
private void TaskBoard_MouseMove(object sender, MouseEventArgs e)
{
if (isSelecting) {
// Draw rubber band
Point rubberBandEnd = PointToClient(Cursor.Position);
RubberBandBox.Bounds = RectangleExt.Create(rubberBandStart, rubberBandEnd);
// Specifying nothing but the size creates noncolor canvas
// To avoid 0 width/height, which makes an error, add +1 to Width and Height
var rubberBandBitmap = new Bitmap(RubberBandBox.Width + 1, RubberBandBox.Height + 1);
// Create Graphics object for the rubber band
gRubberBand = Graphics.FromImage(rubberBandBitmap);
Pen linePen = new Pen(lineColor, lineBorder);
linePen.DashStyle = DashStyle.Dot;
// To show the right and bottom lines, DrawRectangle should be -1, which is not related to the size of Bitmap mentioned above
gRubberBand.DrawRectangle(linePen, 0, 0, RubberBandBox.Width - 1, RubberBandBox.Height - 1);
RubberBandBox.Image = rubberBandBitmap;
RubberBandBox.Enabled = true;
// Release resources
linePen.Dispose();
gRubberBand.Dispose();
// Check overlapped Task controls with rubber band
foreach (var taskControl in Controls.OfType<TaskUserControl>()) {
if (new Rectangle(taskControl.Location, taskControl.Size).IntersectsWith(RubberBandBox.Bounds)) {
taskControl.IsSelected = true;
}
else {
taskControl.IsSelected = false;
}
}
}
else if (isScrolling) {
// Move all controls instead of scrolling Form
foreach (var ctrl in Controls.OfType<TaskUserControl>()) {
ctrl.Location = new Point(ctrl.Location.X + e.Location.X - scrollStart.X, ctrl.Location.Y + e.Location.Y - scrollStart.Y);
}
// Update edge guides only when moved
if (Math.Abs(scrollStart.X - e.Location.X) > 0 || Math.Abs(scrollStart.Y - e.Location.Y) > 0) {
edgeGuideController.ClearGuides();
edgeGuideController.ShowEdgeGuides(Controls.OfType<TaskUserControl>());
}
scrollStart = e.Location;
}
}
private void TaskBoard_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
// Erase rubber band
// To avoid 0 width/height, which makes an error, add +1 to Width and Height
RubberBandBox.Image = new Bitmap(RubberBandBox.Width + 1, RubberBandBox.Height + 1);
// Disable to click Rubber Band Box
RubberBandBox.Enabled = false;
isSelecting = false;
}
else if (e.Button == MouseButtons.Right) {
isScrolling = false;
Cursor = Cursors.Default;
edgeGuideController.ClearGuides();
}
}
private void TaskBoard_Activated(object sender, EventArgs e)
{
Renew();
// Select the icon of the next window of TaskBoard in Z order
// SelectNextTask(Handle);
}
//
// Keyboard interfaces
//
// TODO: implement Key Map Controller class
private void TaskBoard_KeyDown(object sender, KeyEventArgs e)
{
// Since command of Hot Key does not work here, use hotKey_HotKeyPush instead
if (e.KeyCode == Keys.Enter) {
ForegroundSelectedTask();
}
if (e.KeyCode == Keys.Right) {
SelectRightTask();
}
if (e.KeyCode == Keys.Left) {
SelectLeftTask();
}
if (e.KeyCode == Keys.Up) {
SelectUpperTask();
}
if (e.KeyCode == Keys.Down) {
SelectLowerTask();
}
}
private void TaskBoard_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Right || e.KeyCode == Keys.Left) {
e.IsInputKey = true;
}
}
}
}