forked from ArchipelProject/GrowlCappuccino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TNGrowlCenter.j
364 lines (294 loc) · 11.8 KB
/
TNGrowlCenter.j
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
/*
* TNGrowlCenter.j
*
* Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/Foundation.j>
@import <AppKit/CPView.j>
@import <AppKit/CPViewAnimation.j>
@import "TNGrowlMessage.j"
@import "TNGrowlView.j"
/*! @global
@group TNGrowl
the defaultCenter pointer
*/
TNGrowlDefaultCenter = nil;
/*! @global
@group TNGrowlPlacement
the height of TNGrowlView
*/
TNGrowlPlacementWidth = 250.0
/*! @global
@group TNGrowlPlacement
the width of TNGrowlView
*/
TNGrowlPlacementHeight = 80.0
/*! @global
@group TNGrowlPlacement
the margin top value
*/
TNGrowlPlacementMarginTop = 10.0;
/*! @global
@group TNGrowlPlacement
the margin top right
*/
TNGrowlPlacementMarginRight = 10.0;
/*! @global
@group TNGrowlAnimation
Duration of fade in and fade out CPViewAnimation
*/
TNGrowlAnimationDuration = 0.3;
var _TNGrowlIconInfo,
_TNGrowlIconError,
_TNGrowlIconWarning;
/*! @ingroup growlcappuccino
this is the GrowlCappuccino notification center. This is from where you can post Growl notification.
it provide a class method defaultCenter: that return the default GrowlCappuccino center.
In the most of the case you should use this default center.
You can find two option to set in Info.plist
- TNGrowlDefaultLifeTime : in seconds. Deafult: 5. The lifeTime of notification.
*/
@implementation TNGrowlCenter : CPObject
{
CPArray _notificationsHistory @accessors(property=notificationsHistory);
CPTableView _tableForNotificationHistory @accessors(getter=tableForNotificationHistory);
CPView _view @accessors(property=view);
float _defaultLifeTime @accessors(property=lifeDefaultTime);
int _maxHistory @accessors(property=maxHistory);
CPArray _notifications;
CGRect _notificationFrame;
}
#pragma mark -
#pragma mark Class methods
/*! return the defaultCenter
@return TNGrowlCenter the default center;
*/
+ (id)defaultCenter
{
if (!TNGrowlDefaultCenter)
TNGrowlDefaultCenter = [[TNGrowlCenter alloc] init];
return TNGrowlDefaultCenter;
}
/*! display a notification with type TNGrowlIconInfo
@param aTitle the title of the notification
@param aMessage the mesage of the notification
*/
+ (void)pushNotificationWithTitle:(CPString)aTitle message:(CPString)aMessage
{
[[TNGrowlCenter defaultCenter] pushNotificationWithTitle:aTitle message:aMessage];
}
/*! display a notification with type TNGrowlIconError
@param aTitle the title of the notification
@param aMessage the mesage of the notification
*/
+ (void)pushErrorNotificationWithTitle:(CPString)aTitle message:(CPString)aMessage
{
[[TNGrowlCenter defaultCenter] pushNotificationWithTitle:aTitle message:aMessage icon:TNGrowlIconError];
}
/*! display a notification with type TNGrowlIconWarning
@param aTitle the title of the notification
@param aMessage the mesage of the notification
*/
+ (void)pushWarningNotificationWithTitle:(CPString)aTitle message:(CPString)aMessage
{
[[TNGrowlCenter defaultCenter] pushNotificationWithTitle:aTitle message:aMessage icon:TNGrowlIconWarning];
}
#pragma mark -
#pragma mark Initialization
/*! initialize the class
@return the initialized instance of TNGrowlCenter
*/
- (id)init
{
if (self = [super init])
{
var bundle = [CPBundle bundleForClass:[self class]];
_defaultLifeTime = [bundle objectForInfoDictionaryKey:@"TNGrowlDefaultLifeTime"];
_notifications = [CPArray array];
_notificationFrame = CGRectMake(10,10, TNGrowlPlacementWidth,TNGrowlPlacementHeight);
_notificationsHistory = [CPArray array];
_maxHistory = 50;
_TNGrowlIconInfo = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon-info.png"]];
_TNGrowlIconError = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon-error.png"]];
_TNGrowlIconWarning = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon-warning.png"]];
}
return self;
}
#pragma mark -
#pragma mark Notification handlers
/*! Responder of the message TNGrowlViewLifeTimeExpirationNotification
this will start fade out the TNGrowlView that sent the notification
@param aNotification the notification
*/
- (void)didReceivedNotificationEndLifeTime:(CPNotification)aNotification
{
var center = [CPNotificationCenter defaultCenter],
senderView = [aNotification object],
animView = [CPDictionary dictionaryWithObjectsAndKeys:senderView, CPViewAnimationTargetKey, CPViewAnimationFadeOutEffect, CPViewAnimationEffectKey],
anim = [[CPViewAnimation alloc] initWithViewAnimations:[animView]];
[center removeObserver:self name:TNGrowlViewLifeTimeExpirationNotification object:senderView];
[anim setDuration:TNGrowlAnimationDuration];
[anim setDelegate:self];
[anim startAnimation];
}
#pragma mark -
#pragma mark Messaging
/*! @deprecated
display a notification with type TNGrowlIconInfo
@param aTitle the title of the notification
@param aMessage the mesage of the notification
*/
- (void)pushNotificationWithTitle:(CPString)aTitle message:(CPString)aMessage
{
[self pushNotificationWithTitle:aTitle message:aMessage icon:TNGrowlIconInfo];
}
/*! display a notification with given type
anIconType can be:
- TNGrowlIconInfo
- TNGrowlIconError
- TNGrowlIconWarning
@param aTitle the title of the notification
@param aMessage the mesage of the notification
@param anIconType a type of icon.
*/
- (void)pushNotificationWithTitle:(CPString)aTitle message:(CPString)aMessage icon:(CPString)anIconType
{
[self pushNotificationWithTitle:aTitle message:aMessage customIcon:anIconType];
}
/*! display a notification with a CPImage as icon.
@param aTitle the title of the notification
@param aMessage the mesage of the notification
@param anIcon a CPImage representing the notification icon
*/
- (void)pushNotificationWithTitle:(CPString)aTitle message:(CPString)aMessage customIcon:(id)anIcon
{
[self pushNotificationWithTitle:aTitle message:aMessage customIcon:anIcon target:nil action:nil actionParameters:nil];
}
/*! display a notification with a CPImage as icon.
@param aTitle the title of the notification
@param aMessage the mesage of the notification
@param anIcon a CPImage representing the notification icon
@param aTarget the target of the click responder
@param anAction a selector of aTarget to perform on click
*/
- (void)pushNotificationWithTitle:(CPString)aTitle message:(CPString)aMessage customIcon:(id)anIcon target:(id)aTarget action:(SEL)anAction actionParameters:(id)anObject
{
var center = [CPNotificationCenter defaultCenter],
notifView = [[TNGrowlView alloc] initWithFrame:_notificationFrame title:aTitle message:aMessage icon:anIcon lifeTime:_defaultLifeTime],
frame = [_view frame],
notifFrame = CGRectCreateCopy(_notificationFrame),
animParams = [CPDictionary dictionaryWithObjectsAndKeys:notifView, CPViewAnimationTargetKey, CPViewAnimationFadeInEffect, CPViewAnimationEffectKey],
anim = [[CPViewAnimation alloc] initWithViewAnimations:[animParams]];
[center addObserver:self selector:@selector(didReceivedNotificationEndLifeTime:) name:TNGrowlViewLifeTimeExpirationNotification object:notifView];
notifFrame.origin.x = frame.size.width - _notificationFrame.size.width - TNGrowlPlacementMarginRight;
notifFrame.origin.y = TNGrowlPlacementMarginTop;
for (var i = 0; i < [_notifications count]; i++)
{
var isViewInThisFrame = NO,
tmpFrame;
for (var j = 0; j < [_notifications count]; j++)
{
tmpFrame = [[_notifications objectAtIndex:j] frame];
if ((notifFrame.origin.y == tmpFrame.origin.y) && (notifFrame.origin.x == tmpFrame.origin.x))
{
isViewInThisFrame = YES;
break;
}
}
if (!isViewInThisFrame)
break;
notifFrame.origin.y += tmpFrame.size.height + TNGrowlPlacementMarginTop;
if ((notifFrame.origin.y + notifFrame.size.height) >= frame.size.height)
{
notifFrame.origin.x -= (notifFrame.size.width + TNGrowlPlacementMarginRight);
notifFrame.origin.y = TNGrowlPlacementMarginTop;
}
}
[_notifications addObject:notifView];
[notifView setAutoresizingMask:CPViewMinXMargin];
[notifView setFrame:notifFrame];
[notifView setTarget:aTarget];
[notifView setAction:anAction];
[notifView setActionParameters:anObject];
[_view addSubview:notifView];
[anim setDuration:0.3];
[anim startAnimation];
var imageIcon;
switch (anIcon)
{
case TNGrowlIconWarning:
imageIcon = _TNGrowlIconWarning;
break;
case TNGrowlIconError:
imageIcon = _TNGrowlIconError;
break;
default:
imageIcon = _TNGrowlIconInfo;
break;
}
[_notificationsHistory addObject:[TNGrowlMessage growlMessageWithTitle:aTitle message:aMessage icon:imageIcon]];
if ([_notificationsHistory count] > _maxHistory)
[_notificationsHistory removeObjectAtIndex:0];
if (_tableForNotificationHistory)
[_tableForNotificationHistory reloadData];
}
#pragma mark -
#pragma mark History
/*! set the table view that will show the history
NOTE: This should only work with tables using
as data source TNTableViewDataSource from TNKit.
If you are not using this dataSource object, do
not use it.
The table view should have as identifiers:
- "title" (CPString)
- "message" (CPString)
- "icon" (CPImage)
- "date" (CPDate)
@param aTableView the tableView
*/
- (void)setTableForNotificationHistory:(CPTableView)aTableView
{
if (_tableForNotificationHistory === aTableView)
return;
if (![[_tableForNotificationHistory dataSource] className] === @"TNTableViewDataSource")
CPLog.warn("GrowlCappuccino: You should usie a table view as notification history that uses TNTableViewDataSource.");
_tableForNotificationHistory = aTableView;
[[_tableForNotificationHistory dataSource] setContent:[_notificationsHistory copy]];
_notificationsHistory = [_tableForNotificationHistory dataSource];
[_tableForNotificationHistory reloadData];
}
/*! flush the history array
*/
- (void)clearHistory
{
[_notificationsHistory removeAllObjects];
if (_tableForNotificationHistory)
[_tableForNotificationHistory reloadData];
}
#pragma mark -
#pragma mark Delegates
/*! delegate of CPAnimation. Will remove definitly the TNGrowlView
from the superview
@param anAnimation the animation that have ended
*/
- (void)animationDidEnd:(CPAnimation)anAnimation
{
var senderView = [[[anAnimation viewAnimations] objectAtIndex:0] objectForKey:CPViewAnimationTargetKey];
[_notifications removeObject:senderView];
[senderView removeFromSuperview];
}
@end