forked from scrod/nv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AutoMoveCentral.m
766 lines (522 loc) · 17 KB
/
AutoMoveCentral.m
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
//
// AutoMoveCentral.m
// WindowOpener
//
// Created by Agl on 11.02.10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "AutoMoveCentral.h"
@implementation AutoMoveCentral
/**
* initiates this and creates the windows array
*/
- (id) init {
windows = [[NSMutableArray alloc] init];
return self;
}
/**
* adds the given window to the registered windows
*/
- (void) registerWindow:(AutoMoveWindow *)window {
[windows addObject:window];
}
/**
* removes the given window from the registered windows
*/
- (void) unregisterWindow:(AutoMoveWindow *)window {
[windows removeObject:window];
}
/**
* alters the given frame to "clip" AutoMoveWindows to each other, when they
* are moved close to each other
*/
- (NSRect) alterFrameChange:(NSRect)frame :(AutoMoveWindow *)window {
int clip = 14;
NSRect screenFrame = [[NSScreen mainScreen] visibleFrame];//[window getUsableScreen];
int x = frame.origin.x;
int y = frame.origin.y;
int xUpdated = x;
int yUpdated = y;
int width = frame.size.width;
int height = frame.size.height;
int rightX = x + width;
int upperY = y + height;
int screenHeight = screenFrame.size.height;
int screenWidth = screenFrame.size.width;
// we are close to the left screen edge
if (x < clip && x > -clip) {
xUpdated = 0;
}
// we are close to the bottom edge
if (y < clip && y > -clip) {
yUpdated = 0;
}
int tmp = screenWidth - rightX;
// we are close to the right edge
if (tmp < clip && tmp > -clip) {
xUpdated = screenWidth - width;
}
// we are close to the top edge
tmp = screenHeight - upperY;
if (tmp < clip && tmp > -clip) {
y = screenHeight - height;
}
unsigned count = [windows count];
int closestX = clip * 3;
int closestY = clip * 3;
BOOL clippedX = NO;
BOOL clippedY = NO;
while (count--) { //for all windows:
id obj = [windows objectAtIndex:count];
//only if it's not the window to be altered
if (obj != window) {
AutoMoveWindow *amw = obj;
BOOL xOverlap = NO;
BOOL yOverlap = NO;
NSRect testFrame = [amw frame];
int xLeftTest = testFrame.origin.x;
int xRightTest = testFrame.origin.x + testFrame.size.width;
int yDownTest = testFrame.origin.y;
int yUpTest = testFrame.origin.y + testFrame.size.height;
if ( x >= xLeftTest && x <= xRightTest ) { // -> left corner x is enclosed by other window x
xOverlap = YES;
}
if (rightX >= xLeftTest && rightX <= xRightTest) { // -> right corner x is enclosed by other window x
xOverlap = YES;
}
if (x <= xLeftTest && rightX >= xRightTest) { // -> we are bigger than the other window but still overlap
xOverlap = YES;
}
if ( y >= yDownTest && y <= yUpTest ) {
yOverlap = YES;
}
if ( upperY >= yDownTest && upperY <= yUpTest ) {
yOverlap = YES;
}
if (y <= yDownTest && upperY >= yUpTest) {
yOverlap = YES;
}
if (!(xOverlap && yOverlap) && (xOverlap || yOverlap)) { //if windows themselfes are not overlapping but there is a x- or y-overlap (i.e. not both)
if (xOverlap) {
//no yOverlap -> this window must be to the above or bellow the other one
BOOL didUpdate = NO;
if ( y < yDownTest ) { // we are bellow
int distance = yDownTest - upperY;
if (distance < closestY && distance < clip) { // -> bring them close
yUpdated = (y + distance) - 1;
//xUpdated = x;
closestY = distance;
clippedY = YES;
didUpdate = YES;
}
}
else { // we are above
int distance = y - yUpTest;
if (distance < closestY && distance < clip) { // -> bring them close
yUpdated = (y - distance) + 1;
//xUpdated = x;
closestY = distance;
clippedY = YES;
didUpdate = YES;
}
}
if (didUpdate && !clippedX) { // -> alligne Edges?
int distLeft = xLeftTest - x;
int distRight = xRightTest - rightX;
if ( distLeft * distLeft < clip * clip) {
xUpdated = x + distLeft;
}
if (distRight * distRight < clip * clip && distRight * distRight < distLeft * distLeft) {
xUpdated = x + distRight;
}
}/* */
}
else { // must be yOverlap
BOOL didUpdate = NO;
if ( x < xLeftTest) { //we are at the left side
int distance = xLeftTest - rightX;
if (distance < closestX && distance < clip) { // -> bring them close
xUpdated = x + distance -1;
//yUpdated = y;
closestX = distance;
clippedX = YES;
didUpdate = YES;
}
}
else { // we are on the right side
int distance = x - xRightTest;
if (distance < closestX && distance < clip) { // -> bring them close
xUpdated = (x - distance) + 1;
//yUpdated = y;
closestX = distance;
clippedX = YES;
didUpdate = YES;
}
}
if (didUpdate && !clippedY) { // -> alligne Edges?
int distDown = yDownTest - y;
int distUp = yUpTest - upperY;
if ( distDown * distDown < clip * clip) {
yUpdated = y + distDown;
}
if (distUp * distUp < clip * clip && distUp * distUp < distDown * distDown) {
yUpdated = y + distUp;
}
}
}
}
}
}
NSRect ret = NSMakeRect(xUpdated, yUpdated, width, height);
return ret;
}
/**
* Sets a (new) window wo its initial position. If an other window is allready
* at this position, a new position is computed and so on . . .
*/
- (void)setInitialPosition:(AutoMoveWindow *) window {
NSRect visibleScreen = [[NSScreen mainScreen] visibleFrame];
int screenHeight = visibleScreen.size.height + visibleScreen.origin.y;
int screenWidth = visibleScreen.size.width + visibleScreen.origin.x;
int screenLeftStart = visibleScreen.origin.x;
int screenLowerStart = visibleScreen.origin.y;
int windowHeight = [window frame].size.height;
NSRect frame = [window frame];
int startX = screenLeftStart;
int startY = screenHeight - windowHeight;
frame.origin.x = startX;
frame.origin.y = startY;
int iterateX = startX;
int iterateY = startY;
while (iterateX < (screenWidth - 30)) {
if (iterateY < screenLowerStart) {
startX += 100;
iterateX = startX;
iterateY = startY;
}
frame.origin.x = iterateX;
frame.origin.y = iterateY;
[window setFrameNoCall:frame display:NO animate:NO];
if ([self isAnyBodyAtThisPosition:window]) {
iterateX += 30;
iterateY -= 30;
}
else {
return;
}
}
}
/**
* returnes wether thers is allready another window at this position
*/
- (BOOL)isAnyBodyAtThisPosition:(AutoMoveWindow *) window {
int count = [windows count];
while (count--) {
id obj = [windows objectAtIndex:count];
if (window != obj) { //only if it's not the given window
AutoMoveWindow *w = obj;
NSPoint wRec1 = [window frame].origin;
NSPoint wRec2 = [w frame].origin;
if (wRec1.x == wRec2.x && wRec1.y == wRec2.y) {
return YES;
}
}
}
return NO;
}
/**
* moves all windows to the top of the screen. If a line is full the next line
* is set bellow the highest one of the previous one.
*/
- (void) moveAllToTop {
int count = [windows count];
if (count == 0) {
return;
}
AutoMoveWindow * win = nil;
NSRect screen = [[NSScreen mainScreen]visibleFrame];//[win getUsableScreen];
int screenHight = screen.size.height + screen.origin.y;
int currentUpperHeight = screenHight;
int maxWindowHeight = 0;
int xStart = screen.origin.x;
int maxRightX = screen.origin.x + screen.size.width;//xStart + screen.size.height;
int currentLeftX = xStart;
ManhattenAnimator * animator = [[ManhattenAnimator alloc] init];
int daIndex = 0;
// set the windows
while (count > daIndex) {
win = [windows objectAtIndex:daIndex];
daIndex++;
NSRect frame = [win frame];
//if the current window is to broad for the current line, start a new one
if (frame.size.width + currentLeftX >= maxRightX) {
currentUpperHeight = currentUpperHeight - maxWindowHeight;
//if we have almost reached the bottom of the screen, jump back to
//top and start again from a little x-offset
if (currentUpperHeight - frame.size.height <= screen.origin.y) {
currentUpperHeight = screenHight;
xStart = xStart + 20;
if (xStart >= 100) {
xStart = screen.origin.x;
}
}
currentLeftX = xStart;
maxWindowHeight = 0;
}
//set the windows new position
frame.origin.x = currentLeftX;
frame.origin.y = currentUpperHeight - frame.size.height;
//[win setFrameNoCall:frame display:YES animate:YES];
// AXELS_TODO [win setFrameManhattenAnimation:frame];
[animator registerForAnimation:win withEndFrame:frame];
//check if this is the currently highest window in the current line
if (frame.size.height > maxWindowHeight) {
maxWindowHeight = frame.size.height;
}
//itterate the x position
currentLeftX = currentLeftX + frame.size.width;
}
[animator animateAll];
}
/**
* moves all windows to the bottom of the screen and starts new lines if not all
* of them fit there
*/
- (void) moveAllToBottom {
int count = [windows count];
if (count == 0) { //no windows. Quick end
return;
}
NSRect screen = [[NSScreen mainScreen]visibleFrame];//[[windows objectAtIndex:0] getUsableScreen];
int currentLowerY = screen.origin.y;
int maxWindowHeight = 0;
int xStart = screen.origin.x;
int maxRightX = xStart + screen.size.width;
int currentLeftX = xStart;
int daIndex = 0;
ManhattenAnimator * animator = [[ManhattenAnimator alloc] init];
// for all windows ...
while (count > daIndex) {
AutoMoveWindow * win = [windows objectAtIndex:daIndex];
daIndex++;
NSRect frame = [win frame];
//if the current window is to broad for the rest of the line...
if (frame.size.width + currentLeftX >= maxRightX) {
//one line higher
currentLowerY = currentLowerY + maxWindowHeight;
// if we almost reached the top of the screen, jump back to bottom
// and continue with a little x offset
if (currentLowerY + 50 >= screen.origin.y + screen.size.height) {
currentLowerY = screen.origin.y;
xStart = xStart + 20;
if (xStart >= 100) {
xStart = screen.origin.x;
}
}
//reset to left edge
currentLeftX = xStart;
maxWindowHeight = 0;
}
//set new window position
frame.origin.x = currentLeftX;
frame.origin.y = currentLowerY;
//if we're to high (so the title bar would be out of the visible space)
if (currentLowerY + frame.size.height >= screen.origin.y + screen.size.height) {
frame.origin.y = (screen.origin.y + screen.size.height) - frame.size.height;
}
//[win setFrameNoCall:frame display:YES animate:YES];
// AXELS_TODO [win setFrameManhattenAnimation:frame];
[animator registerForAnimation:win withEndFrame:frame];
// check if this is the highest window in the current line
if (frame.size.height > maxWindowHeight) {
maxWindowHeight = frame.size.height;
}
// itterate x position
currentLeftX = currentLeftX + frame.size.width;
}
[animator animateAll];
}
- (void) moveAllToRight {
int count = [windows count];
if (count == 0) {
return;
}
//AutoMoveWindow * win = nil;
NSRect screen = [[NSScreen mainScreen]visibleFrame];
int screenHeight = screen.size.height + screen.origin.y;
int minLeftX = screen.origin.x;
int minLowerY = screen.origin.y;
int maxWindowWidth = 0;
int currentUpperY = screenHeight;
int xStart = screen.origin.x;
int currentRightX = xStart;
ManhattenAnimator * animator = [[ManhattenAnimator alloc] init];
int daIndex = 0;
while (daIndex < count) {
AutoMoveWindow * win = [windows objectAtIndex:daIndex];
daIndex++;
NSRect frame = [win frame];
//if the current window is to high for the rest of the collumn
if (currentUpperY - frame.size.height <= minLowerY) {
//one column to the left
currentRightX -= maxWindowWidth;
currentUpperY = screenHeight;
maxWindowWidth = 0;
}
//this window would hit the left border . . .
if ( currentRightX - frame.size.width < minLeftX) {
xStart -= 30;
if (xStart <= (screen.size.width + screen.origin.x) - 100) {
xStart = screen.size.width + screen.origin.x;
}
currentRightX = xStart;
currentUpperY = screenHeight;
maxWindowWidth = 0;
}
//set new window position
frame.origin.x = currentRightX - frame.size.width;
frame.origin.y = currentUpperY - frame.size.height;
[animator registerForAnimation:win withEndFrame:frame];
if (frame.size.width > maxWindowWidth) {
maxWindowWidth = frame.size.width;
}
//itterate y position
currentUpperY -= frame.size.height + 1;
}
[animator animateAll];
}
- (void) moveAllToLeft {
int count = [windows count];
if (count == 0) {
return;
}
NSRect screen = [[NSScreen mainScreen]visibleFrame];
int screenHeight = screen.size.height + screen.origin.y;
int maxRightX = screen.size.width + screen.origin.x;
int minLowerY = screen.origin.y;
int maxWindowWidth = 0;
int currentUpperY = screenHeight;
int xStart = screen.origin.x;
int currentLeftX = xStart;
ManhattenAnimator * animator = [[ManhattenAnimator alloc] init];
int daIndex = 0;
while (daIndex < count) {
AutoMoveWindow * win = [windows objectAtIndex:daIndex];
daIndex++;
NSRect frame = [win frame];
//if current window is to high for the rest of the collumn
if (currentUpperY - frame.size.height <= minLowerY) {
//one column to to the right and up
currentLeftX += maxWindowWidth;
currentUpperY = screenHeight;
maxWindowWidth = 0;
}
//if this window would hit the right border
if (currentLeftX + frame.size.width >= maxRightX) {
xStart += 30;
if (xStart >= screen.origin.x + 120) {
xStart = screen.origin.x;
}
currentLeftX = xStart;
currentUpperY = screenHeight;
maxWindowWidth = 0;
}
//set new window position
frame.origin.x = currentLeftX;
frame.origin.y = currentUpperY - frame.size.height;
[animator registerForAnimation:win withEndFrame:frame];
if (frame.size.width > maxWindowWidth) {
maxWindowWidth = frame.size.width;
}
//itterate y positon
currentUpperY -= frame.size.height + 1;
}
[animator animateAll];
}
// stacks the windows in the current order
- (void) stackWindowsInCurrentOrder {
int count = [windows count];
if (count == 0) {
return;
}
NSRect screen = [[NSScreen mainScreen]visibleFrame];//[[windows objectAtIndex:0] getUsableScreen];
int daIndex = 0;
int maxY = screen.origin.y + screen.size.height;
int minY = screen.origin.y;
int maxX = screen.origin.x + screen.size.width;
int minX = screen.origin.x;
int xStart = minX;
int currentLeftX = xStart;
int currentUpperY = maxY;
ManhattenAnimator * animator = [[ManhattenAnimator alloc] init];
// for every window
while (daIndex < count) {
AutoMoveWindow * win = [windows objectAtIndex:daIndex];
daIndex++;
NSRect frame = [win frame];
// if we reached the bottom or the right edge, reset to the next
// top-left starting point
if ( currentUpperY - frame.size.height < minY ||
currentLeftX + frame.size.width > maxX) {
xStart = xStart + 100;
currentLeftX = xStart;
currentUpperY = maxY;
}
//set the new frames position
frame.origin.y = currentUpperY - frame.size.height;
frame.origin.x = currentLeftX;
[win orderFront:self];
/*[win setFrameNoCall:frame display:YES animate:YES];
frame = [win frame];
frame.origin.x = currentLeftX;
[win setFrameNoCall:frame display:YES animate:YES];*/
// AXELS_TODO [win setFrameManhattenAnimation:frame];
[animator registerForAnimation:win withEndFrame:frame];
/*FrameWrapper * wr = [[FrameWrapper alloc]init];
[wr setFrame:frame];
[NSThread detachNewThreadSelector:@selector(setFrameManhattenAnimationWithWrapper:) toTarget:win withObject:wr];
/* */
//itterate for next window
//currentLeftX += 15;
currentUpperY -= 18;
}
[animator animateAll];
}
/*- (void) orderAccordingToColor {
NSMutableArray * ordereredWindows = [[NSMutableArray alloc] init];
NSEnumerator * enu = [windows objectEnumerator];
AutoMoveWindow * sticky = nil;
while ((sticky = [enu nextObject])) {
if ([[sticky controler] isYellow]) {
[ordereredWindows insertObject:sticky atIndex:0];
}
}
while ((sticky = [enu nextObject])) {
if ([[sticky controler] isViolet]) {
[ordereredWindows insertObject:sticky atIndex:0];
}
}
while ((sticky = [enu nextObject])) {
if ([[sticky controler] isPink]) {
[ordereredWindows insertObject:sticky atIndex:0];
}
}
while ((sticky = [enu nextObject])) {
if ([[sticky controler] isGrey]) {
[ordereredWindows insertObject:sticky atIndex:0];
}
}
while ((sticky = [enu nextObject])) {
if ([[sticky controler] isGreen]) {
[ordereredWindows insertObject:sticky atIndex:0];
}
}
while ((sticky = [enu nextObject])) {
if ([[sticky controler] isBlue]) {
[ordereredWindows insertObject:sticky atIndex:0];
}
}
[windows release];
windows = ordereredWindows;
} */
@end