forked from fjolnir/afloat
-
Notifications
You must be signed in to change notification settings - Fork 23
/
AfloatBadgeController.m
192 lines (148 loc) · 5.1 KB
/
AfloatBadgeController.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
//
// AfloatBadgeController.m
// Afloat
//
// Created by ∞ on 13/03/08.
// Copyright 2008 Emanuele Vulcano. All rights reserved.
//
#import "AfloatBadgeController.h"
#import "AfloatStorage.h"
#import <QuartzCore/QuartzCore.h>
#define kAfloatBadgeControllerKey @"AfloatBadgeController"
@implementation AfloatBadgeController
+ (id) badgeControllerForWindow:(NSWindow*) w {
id panel = [AfloatStorage sharedValueForWindow:w key:kAfloatBadgeControllerKey];
if (panel)
return panel;
else
return [[self alloc] initAttachedToWindow:w];
}
- (id) initAttachedToWindow:(NSWindow*) parentWindow {
if (self = [self initWithWindowNibName:@"AfloatBadge" owner:self]) {
self.parentWindow = parentWindow;
}
return self;
}
- (void) windowDidLoad {
[[self window] setIgnoresMouseEvents:YES];
[[self window] setLevel:NSStatusWindowLevel];
CABasicAnimation* ani = [CABasicAnimation animation];
ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[[self window] setAnimations:
[NSDictionary dictionaryWithObject:ani forKey:@"frameOrigin"]];
L0Log(@"badge view = %@", badgeView);
}
- (NSWindow*) getParentWindow {
return _parentWindow;
}
- (void) setParentWindow:(NSWindow*) newParent {
if (newParent != _parentWindow) {
if (_parentWindow) {
[AfloatStorage removeSharedValueForWindow:_parentWindow key:kAfloatBadgeControllerKey];
}
if (newParent) {
[AfloatStorage setSharedValue:self window:newParent key:kAfloatBadgeControllerKey];
// [[NSNotificationCenter defaultCenter]
// addObserver:self selector:@selector(parentWindowDidResize:) name:NSWindowDidResizeNotification object:_parentWindow];
}
_parentWindow = newParent;
}
}
- (NSPoint) middleOriginForParentWindow {
if (!self.parentWindow) return NSZeroPoint;
NSRect parentFrame = [self.parentWindow frame];
NSRect windowFrame = [[self window] frame];
NSRect screenFrame = [[self.parentWindow screen] visibleFrame];
parentFrame = NSIntersectionRect(parentFrame, screenFrame);
NSPoint origin = parentFrame.origin;
origin.x += parentFrame.size.width / 2;
origin.x -= windowFrame.size.width / 2;
origin.y += parentFrame.size.height / 2;
origin.y -= windowFrame.size.height / 2;
return origin;
}
- (void) animateWithBadgeType:(AfloatBadgeType) type {
[self window]; // so the window is loaded.
switch (type) {
case AfloatBadgeDidBeginKeepingAfloat:
[badgeView setImage:[[self class] didBeginKeepingAfloatBadge]];
{
NSPoint targetOrigin = [self middleOriginForParentWindow];
NSPoint startingOrigin = targetOrigin;
startingOrigin.y -= 20;
NSRect r = [[self window] frame];
r.origin = startingOrigin;
[[self window] setFrame:r display:NO];
[[self window] makeKeyAndOrderFront:self];
[NSAnimationContext beginGrouping];
{
[[NSAnimationContext currentContext] setDuration:0.4];
r.origin = targetOrigin;
[[[self window] animator] setFrame:r display:YES];
}
[NSAnimationContext endGrouping];
enqueuedFades++;
[self performSelector:@selector(_fadeOut) withObject:nil afterDelay:0.7];
}
break;
case AfloatBadgeDidEndKeepingAfloat:
[badgeView setImage:[[self class] didEndKeepingAfloatBadge]];
{
NSPoint targetOrigin = [self middleOriginForParentWindow];
NSPoint startingOrigin = targetOrigin;
targetOrigin.y -= 30;
NSRect r = [[self window] frame];
r.origin = startingOrigin;
[[self window] setFrame:r display:NO];
[[self window] makeKeyAndOrderFront:self];
[NSAnimationContext beginGrouping];
{
[[NSAnimationContext currentContext] setDuration:0.4];
r.origin = targetOrigin;
[[[self window] animator] setFrame:r display:YES];
}
[NSAnimationContext endGrouping];
enqueuedFades++;
[self performSelector:@selector(_fadeOut) withObject:nil afterDelay:0.7];
}
break;
}
}
- (void) _fadeOut {
if (enqueuedFades <= 1)
enqueuedFades = 0;
else {
enqueuedFades--;
return;
}
if (fadingOut || ![[self window] isVisible]) return;
fadingOut = YES;
[[NSAnimationContext currentContext] setDuration:0.2];
[[[self window] animator] setAlphaValue:0];
[NSAnimationContext endGrouping];
[AfloatStorage removeSharedValueForWindow:self.parentWindow key:kAfloatBadgeControllerKey];
[self performSelector:@selector(_endAnimation) withObject:nil afterDelay:0.3];
}
- (void) _endAnimation {
fadingOut = NO;
[[self window] orderOut:self];
}
+ (NSImage*) didBeginKeepingAfloatBadge {
static NSImage* image = nil; if (!image) {
NSBundle* bundle = [NSBundle bundleForClass:self];
L0Log(@"loading %@", [bundle pathForImageResource:@"AfloatFloatingBadge"]);
image = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"AfloatFloatingBadge"]];
L0Log(@"%@", image);
}
return image;
}
+ (NSImage*) didEndKeepingAfloatBadge {
static NSImage* image = nil; if (!image) {
NSBundle* bundle = [NSBundle bundleForClass:self];
L0Log(@"loading %@", [bundle pathForImageResource:@"AfloatSinkingBadge"]);
image = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"AfloatSinkingBadge"]];
L0Log(@"%@", image);
}
return image;
}
@end