Skip to content

Commit

Permalink
NXTAlert: added methods to set custom buttons target/action.
Browse files Browse the repository at this point in the history
  • Loading branch information
trunkmaster committed Aug 5, 2024
1 parent 93ad26d commit 42bdabd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Frameworks/DesktopKit/NXTAlert.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
- (void)show;
- (NSInteger)runModal;

- (void)setButtonsTarget:(id)target;
- (void)setButtonsAction:(SEL)action;
- (void)buttonPressed:(id)sender;
@end

Expand Down
15 changes: 15 additions & 0 deletions Frameworks/DesktopKit/NXTAlert.m
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,23 @@ - (NSInteger)runModal
return result;
}


- (void)setButtonsTarget:(id)target
{
for (NSButton *button in buttons) {
[button setTarget:target];
}
}
- (void)setButtonsAction:(SEL)action
{
for (NSButton *button in buttons) {
[button setAction:action];
}
}

- (void)buttonPressed:(id)sender
{

if ([NSApp modalWindow] != panel) {
NSLog(@"NXAalert panel button pressed when not in modal loop.");
return;
Expand Down
38 changes: 18 additions & 20 deletions Frameworks/DesktopKit/Tests/AppController.m
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,27 @@ - (void)showSinglelineAlert:(id)sender
- (void)showCustomAlert:(id)sender
{
NXTAlert *alert;
NSInteger choice;

alert = [[NXTAlert alloc]
initWithTitle:@"Login"
message:@"Session finished with error. "
"Please select action below.\n"
"\"Restart\" - returns to Workspace\n"
"\"Cleanup\" - kill all running applications and return to Lohgin\n"
"\"Desribe\" - see console log contents to understand cause of error\n"
message:@"Session finished with error.\n\n"
"Do you want to return to Workspace (Restart), "
"quit session closing all applications (Quit) "
"or show log file to get more information about session failure (Explain)?"
defaultButton:@"Restart"
alternateButton:@"Cleanup"
otherButton:@"Describe"];
alternateButton:@"Quit"
otherButton:@"Explain..."];

[alert setButtonsTarget:self];
[alert setButtonsAction:@selector(alertButtonPressed:)];

// [panel setTarget:self];
// [panel setAction:@selector(alertButtonPressed:)];
// [[alert panel] orderFront:self];
[alert show];
choice = [NSApp runModalForWindow:[alert panel]];
switch (choice) {
[NSApp runModalForWindow:[alert panel]];
}

- (void)alertButtonPressed:(id)sender
{
switch ([sender tag]) {
case NSAlertDefaultReturn:
NSLog(@"Alert Panel: start from scratch.");
break;
Expand All @@ -149,16 +151,12 @@ - (void)showCustomAlert:(id)sender
break;
case NSAlertOtherReturn:
NSLog(@"Alert Panel: show console.log contents.");
break;
return;
default:
NSLog(@"Alert Panel: user has made a strange choice!");
}
[[alert panel] close];
}

- (void)alertButtonPressed:(id)sender
{

[NSApp stopModalWithCode:[sender tag]];
[[sender window] close];
}

# pragma mark - Open and Save panels
Expand Down

0 comments on commit 42bdabd

Please sign in to comment.