Skip to content

Commit f2d7d77

Browse files
committed
DesktopKit tests: initial implementation of alert panel with countdown timer.
1 parent 01317ce commit f2d7d77

File tree

4 files changed

+124
-26
lines changed

4 files changed

+124
-26
lines changed

Tests/DesktopKit/AppController.m

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#import <DesktopKit/NXTOpenPanel.h>
2626
#import <DesktopKit/NXTHelpPanel.h>
2727
#import "AppController.h"
28+
#import "NXTCountdownAlert.h"
2829

2930
@implementation AppController : NSObject
3031

@@ -145,31 +146,39 @@ - (void)showSinglelineAlert:(id)sender
145146

146147
- (void)showCustomAlert:(id)sender
147148
{
148-
dispatch_queue_t alert_q = dispatch_queue_create("kit.desktop.ns", DISPATCH_QUEUE_CONCURRENT);
149-
dispatch_async(alert_q, ^{
150-
alert = [[NXTAlert alloc]
151-
initWithTitle:@"Login"
152-
message:@"Session finished with error.\n\n"
153-
"Click \"Restart\" to return to Workspace, "
154-
"click \"Quit\" to close your applications, "
155-
"click \"Explain\" to get more information about session failure."
156-
defaultButton:@"Restart"
157-
alternateButton:@"Quit"
158-
otherButton:@"Explain"];
159-
160-
[alert setButtonsTarget:self];
161-
[alert setButtonsAction:@selector(alertButtonPressed:)];
162-
163-
sleep(1);
164-
[alert show];
165-
// [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
166-
// [NSApp performSelectorOnMainThread:@selector(runModalForWindow:)
167-
// withObject:[alert panel]
168-
// waitUntilDone:YES];
169-
[alert performSelectorOnMainThread:@selector(runModalForWindow:)
170-
withObject:[alert panel]
171-
waitUntilDone:YES];
172-
});
149+
NXTCountdownAlert *customAlert =
150+
[[NXTCountdownAlert alloc] initWithTitle:@"Countdown Alert"
151+
message:@"Here should be countdown number - %i."
152+
defaultButton:@"Agree"
153+
alternateButton:@"Deny"
154+
otherButton:@"Dismiss"];
155+
customAlert.countDownPeriod = 5;
156+
[customAlert runModal];
157+
// dispatch_queue_t alert_q = dispatch_queue_create("kit.desktop.ns", DISPATCH_QUEUE_CONCURRENT);
158+
// dispatch_async(alert_q, ^{
159+
// alert = [[NXTAlert alloc]
160+
// initWithTitle:@"Login"
161+
// message:@"Session finished with error.\n\n"
162+
// "Click \"Restart\" to return to Workspace, "
163+
// "click \"Quit\" to close your applications, "
164+
// "click \"Explain\" to get more information about session failure."
165+
// defaultButton:@"Restart"
166+
// alternateButton:@"Quit"
167+
// otherButton:@"Explain"];
168+
169+
// [alert setButtonsTarget:self];
170+
// [alert setButtonsAction:@selector(alertButtonPressed:)];
171+
172+
// sleep(1);
173+
// [alert show];
174+
// // [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
175+
// // [NSApp performSelectorOnMainThread:@selector(runModalForWindow:)
176+
// // withObject:[alert panel]
177+
// // waitUntilDone:YES];
178+
// [alert performSelectorOnMainThread:@selector(runModalForWindow:)
179+
// withObject:[alert panel]
180+
// waitUntilDone:YES];
181+
// });
173182
}
174183

175184
- (void)alertButtonPressed:(id)sender

Tests/DesktopKit/GNUmakefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ $(APP_NAME)_OBJC_FILES += \
5151
TextTest.m \
5252
DrawingTest.m \
5353
ListViewTest.m \
54-
TabViewTest.m
54+
TabViewTest.m \
55+
NXTCountdownAlert.m
5556

5657
#
5758
# Makefiles
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#import <AppKit/AppKit.h>
2+
#import <DesktopKit/NXTAlert.h>
3+
4+
@interface NXTCountdownAlert : NXTAlert
5+
{
6+
NSString *messageTextFormat;
7+
NSTimer *countDownTimer;
8+
}
9+
10+
@property (readwrite, nonatomic) int countDownPeriod;
11+
12+
- (NSTextView *)message;
13+
14+
@end
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#import "NXTCountdownAlert.h"
2+
3+
@implementation NXTCountdownAlert
4+
5+
- (id)initWithTitle:(NSString *)titleText
6+
message:(NSString *)messageText
7+
defaultButton:(NSString *)defaultText
8+
alternateButton:(NSString *)alternateText
9+
otherButton:(NSString *)otherText
10+
{
11+
[super initWithTitle:titleText
12+
message:messageText
13+
defaultButton:defaultText
14+
alternateButton:alternateText
15+
otherButton:otherText];
16+
17+
if ([messageText rangeOfString:@"%"].location == NSNotFound) {
18+
NSLog(
19+
@"NXTCountdownAlert: message doesn't contain formatting characher - using default message");
20+
messageTextFormat = @"Panel will be closed after %i seconds";
21+
} else {
22+
messageTextFormat = [messageText copy];
23+
}
24+
25+
return self;
26+
}
27+
28+
- (void)setCountDownPeriod:(int)countDownPeriod
29+
{
30+
_countDownPeriod = countDownPeriod;
31+
[messageView setText:[NSString stringWithFormat:messageTextFormat, _countDownPeriod]];
32+
}
33+
34+
- (NSTextView *)message
35+
{
36+
return messageView;
37+
}
38+
39+
- (void)update:(NSTimer *)timer
40+
{
41+
NSLog(@"%s - %i", __func__, _countDownPeriod);
42+
[messageView setText:[NSString stringWithFormat:messageTextFormat, _countDownPeriod]];
43+
if (_countDownPeriod <= 0) {
44+
[countDownTimer invalidate];
45+
[panel orderOut:self];
46+
[NSApp stopModalWithCode:0];
47+
} else {
48+
_countDownPeriod--;
49+
}
50+
}
51+
52+
- (NSInteger)runModal
53+
{
54+
NSInteger result;
55+
56+
countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
57+
target:self
58+
selector:@selector(update:)
59+
userInfo:nil
60+
repeats:YES];
61+
[[NSRunLoop currentRunLoop] addTimer:countDownTimer forMode:NSModalPanelRunLoopMode];
62+
[countDownTimer fire];
63+
[self show];
64+
65+
result = [NSApp runModalForWindow:panel];
66+
if ([countDownTimer isValid]) {
67+
[countDownTimer invalidate];
68+
}
69+
[panel orderOut:self];
70+
71+
return result;
72+
}
73+
74+
@end

0 commit comments

Comments
 (0)