-
Notifications
You must be signed in to change notification settings - Fork 24
/
UserNotification.pas
executable file
·232 lines (159 loc) · 8.52 KB
/
UserNotification.pas
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
unit UserNotification;
interface
{$ifdef Darwin}
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$linkframework Foundation}
uses
Classes, SysUtils, MacOSAll, CocoaAll, CocoaUtils, LCLType;//, CarbonProc;
const
NSUserNotificationActivationTypeNone = 0;
NSUserNotificationActivationTypeContentsClicked = 1;
NSUserNotificationActivationTypeActionButtonClicked = 2;
type
NSUserNotificationPtr = ^NSUserNotification;
NSUserNotificationCenterPtr = ^NSUserNotificationCenter;
NSUserNotificationCenterDelegateProtocolPtr = ^NSUserNotificationCenterDelegateProtocol;
NSUserNotificationActivationType = NSInteger;
NSUserNotificationActivationTypePtr = ^NSUserNotificationActivationType;
//NSUserNotification = objcclass external (NSObject, NSCopyingProtocol)
NSUserNotification = objcclass external (NSObject)
private
_internal: id;
public
procedure setTitle(newValue: NSString); message 'setTitle:';
function title: NSString; message 'title';
procedure setSubtitle(newValue: NSString); message 'setSubtitle:';
function subtitle: NSString; message 'subtitle';
procedure setInformativeText(newValue: NSString); message 'setInformativeText:';
function informativeText: NSString; message 'informativeText';
procedure setActionButtonTitle(newValue: NSString); message 'setActionButtonTitle:';
function actionButtonTitle: NSString; message 'actionButtonTitle';
procedure setUserInfo(newValue: NSDictionary); message 'setUserInfo:';
function userInfo: NSDictionary; message 'userInfo';
procedure setDeliveryDate(newValue: NSDate); message 'setDeliveryDate:';
function deliveryDate: NSDate; message 'deliveryDate';
procedure setDeliveryTimeZone(newValue: NSTimeZone); message 'setDeliveryTimeZone:';
function deliveryTimeZone: NSTimeZone; message 'deliveryTimeZone';
procedure setDeliveryRepeatInterval(newValue: NSDateComponents); message 'setDeliveryRepeatInterval:';
function deliveryRepeatInterval: NSDateComponents; message 'deliveryRepeatInterval';
function actualDeliveryDate: NSDate; message 'actualDeliveryDate';
function isPresented: boolean; message 'isPresented';
function isRemote: boolean; message 'isRemote';
procedure setSoundName(newValue: NSString); message 'setSoundName:';
function soundName: NSString; message 'soundName';
procedure setHasActionButton(newValue: boolean); message 'setHasActionButton:';
function hasActionButton: boolean; message 'hasActionButton';
function activationType: NSUserNotificationActivationType; message 'activationType';
procedure setOtherButtonTitle(newValue: NSString); message 'setOtherButtonTitle:';
function otherButtonTitle: NSString; message 'otherButtonTitle';
// Adopted protocols
//function copyWithZone (zone: NSZonePtr): id; message 'copyWithZone:';
end;
NSUserNotificationCenterDelegateProtocol = objcprotocol;
NSUserNotificationCenter = objcclass external (NSObject)
private
_internal: id;
public
class function defaultUserNotificationCenter: NSUserNotificationCenter; message 'defaultUserNotificationCenter';
//procedure setDelegate(newValue: NSUserNotificationCenterDelegateProtocol); message 'setDelegate:';
procedure setDelegate(newValue: NSObject); message 'setDelegate:';
function delegate: NSUserNotificationCenterDelegateProtocol; message 'delegate';
procedure setScheduledNotifications(newValue: NSArray); message 'setScheduledNotifications:';
function scheduledNotifications: NSArray; message 'scheduledNotifications';
procedure scheduleNotification (notification: NSUserNotification); message 'scheduleNotification:';
procedure removeScheduledNotification (notification: NSUserNotification); message 'removeScheduledNotification:';
function deliveredNotifications: NSArray; message 'deliveredNotifications';
procedure deliverNotification (notification: NSUserNotification); message 'deliverNotification:';
procedure removeDeliveredNotification (notification: NSUserNotification); message 'removeDeliveredNotification:';
procedure removeAllDeliveredNotifications; message 'removeAllDeliveredNotifications';
end;
NSUserNotificationCenterDelegateProtocol = objcprotocol external name 'NSUserNotificationCenterDelegate' (NSObjectProtocol)
optional
procedure userNotificationCenter_didDeliverNotification (center: NSUserNotificationCenter; notification: NSUserNotification); message 'userNotificationCenter:didDeliverNotification:';
procedure userNotificationCenter_didActivateNotification (center: NSUserNotificationCenter; notification: NSUserNotification); message 'userNotificationCenter:didActivateNotification:';
function userNotificationCenter_shouldPresentNotification (center: NSUserNotificationCenter; notification: NSUserNotification): boolean; message 'userNotificationCenter:shouldPresentNotification:';
end;
NSUserNotificationCenterAlwaysShowDelegate = objcclass(NSObject)
public
function shouldPresentNotification ({%H-}center: NSUserNotificationCenter; {%H-}notification: NSUserNotification): boolean; message 'userNotificationCenter:shouldPresentNotification:';
end;
procedure DeliverUserNotification(const Title, Subtitle, Message: string);
procedure DeliverUserNotification(const Title, Subtitle, Message, Sound: string);
procedure ShowAlertSheet(FormHandle: HWND; const TitleStr, MessageStr: string);
var
NSUserNotificationDefaultSoundName: NSString { available in 10_8, NA }; cvar; external;
{$endif}
implementation
{$ifdef Darwin}
procedure ShowAlertSheet(FormHandle: HWND; const TitleStr, MessageStr: string);
var
tNSStr,mNSStr, okNSStr, fNSStr: NSString;
theWindow : CocoaAll.NSWindow;
theID : id;
begin
theID := NSView(FormHandle).window;
theWindow := NSView(FormHandle).window;
tNSStr := NSString(CFStringCreateWithPascalString(kCFAllocatorDefault, TitleStr, kCFStringEncodingUTF8));//title
mNSStr := NSString(CFStringCreateWithPascalString(kCFAllocatorDefault, MessageStr, kCFStringEncodingUTF8));//message
okNSStr := NSString(CFStringCreateWithPascalString(kCFAllocatorDefault, 'OK', kCFStringEncodingUTF8));//button caption
fNSStr := NSString(CFStringCreateWithPascalString(kCFAllocatorDefault, '%@', kCFStringEncodingUTF8));//format
NSBeginAlertSheet(tNSStr,okNSStr,nil,nil,theWindow,theID,nil,nil,nil,fNSStr, [mNSStr]);
end;
var
UserNotificationCenterDelegate: NSUserNotificationCenterAlwaysShowDelegate;
function NSUserNotificationCenterAlwaysShowDelegate.shouldPresentNotification (center: NSUserNotificationCenter; notification: NSUserNotification): boolean;
begin
Result := True;
end;
procedure DeliverUserNotification(const Title, Subtitle, Message: string);
begin
DeliverUserNotification(Title, Subtitle, Message, CFStringToStr(CFStringRef(NSUserNotificationDefaultSoundName)));
end;
procedure DeliverUserNotification(const Title, Subtitle, Message, Sound: string);
var
Notification: NSUserNotification;
TmpNSStr: NSString;
begin
Notification := NSUserNotification.alloc.init;
try
TmpNSStr := NSString(CFStringCreateWithPascalString(kCFAllocatorDefault, Title, kCFStringEncodingUTF8));
try
Notification.setTitle(TmpNSStr);
finally
TmpNSStr.release;
end;
TmpNSStr := NSString(CFStringCreateWithPascalString(kCFAllocatorDefault, Subtitle, kCFStringEncodingUTF8));
try
Notification.setSubtitle(TmpNSStr);
finally
TmpNSStr.release;
end;
TmpNSStr := NSString(CFStringCreateWithPascalString(kCFAllocatorDefault, Message, kCFStringEncodingUTF8));
try
Notification.setInformativeText(TmpNSStr);
finally
TmpNSStr.release;
end;
if Sound <> '' then
begin
TmpNSStr := NSString(CFStringCreateWithPascalString(kCFAllocatorDefault, Sound, kCFStringEncodingUTF8));
try
Notification.setSoundName(TmpNSStr);
finally
TmpNSStr.release;
end;
end;
NSUserNotificationCenter.defaultUserNotificationCenter.deliverNotification(Notification);
finally
Notification.release;
end;
end;
initialization
UserNotificationCenterDelegate := NSUserNotificationCenterAlwaysShowDelegate.alloc.init;
NSUserNotificationCenter.defaultUserNotificationCenter.setDelegate(UserNotificationCenterDelegate);
finalization
NSUserNotificationCenter.defaultUserNotificationCenter.setDelegate(nil);
UserNotificationCenterDelegate.release;
{$endif}
end.