]> khamidou — GitHub mirrors - electron.git/commitdiff
Return button's index for [NSAlert runModalSheetForWindow].
authorCheng Zhao <zcbenz@gmail.com>
Fri, 7 Jun 2013 07:58:36 +0000 (15:58 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 7 Jun 2013 07:58:36 +0000 (15:58 +0800)
browser/nsalert_synchronous_sheet.mm

index 20e4f489c2ee3842ed5491a8ba2e626e861a9525..47e96557307c2e72304fb03987777f43d169d4af 100644 (file)
 @implementation NSAlert (SynchronousSheet)
 
 -(NSInteger) runModalSheetForWindow:(NSWindow *)aWindow {
-    // Set ourselves as the target for button clicks
-    for (NSButton *button in [self buttons]) {
-        [button setTarget:self];
-        [button setAction:@selector(BE_stopSynchronousSheet:)];
-    }
+  // Set ourselves as the target for button clicks
+  for (NSButton *button in [self buttons]) {
+    [button setTarget:self];
+    [button setAction:@selector(BE_stopSynchronousSheet:)];
+  }
 
-    // Bring up the sheet and wait until stopSynchronousSheet is triggered by a button click
-    [self performSelectorOnMainThread:@selector(BE_beginSheetModalForWindow:) withObject:aWindow waitUntilDone:YES];
-    NSInteger modalCode = [NSApp runModalForWindow:[self window]];
+  // Bring up the sheet and wait until stopSynchronousSheet is triggered by a button click
+  [self performSelectorOnMainThread:@selector(BE_beginSheetModalForWindow:) withObject:aWindow waitUntilDone:YES];
+  NSInteger modalCode = [NSApp runModalForWindow:[self window]];
 
-    // This is called only after stopSynchronousSheet is called (that is,
-    // one of the buttons is clicked)
-    [NSApp performSelectorOnMainThread:@selector(endSheet:) withObject:[self window] waitUntilDone:YES];
+  // This is called only after stopSynchronousSheet is called (that is,
+  // one of the buttons is clicked)
+  [NSApp performSelectorOnMainThread:@selector(endSheet:) withObject:[self window] waitUntilDone:YES];
 
-    // Remove the sheet from the screen
-    [[self window] performSelectorOnMainThread:@selector(orderOut:) withObject:self waitUntilDone:YES];
+  // Remove the sheet from the screen
+  [[self window] performSelectorOnMainThread:@selector(orderOut:) withObject:self waitUntilDone:YES];
 
-    return modalCode;
+  return modalCode;
 }
 
 -(NSInteger) runModalSheet {
-    return [self runModalSheetForWindow:[NSApp mainWindow]];
+  return [self runModalSheetForWindow:[NSApp mainWindow]];
 }
 
 
 #pragma mark Private methods
 
 -(IBAction) BE_stopSynchronousSheet:(id)sender {
-    // See which of the buttons was clicked
-    NSUInteger clickedButtonIndex = [[self buttons] indexOfObject:sender];
-
-    // Be consistent with Apple's documentation (see NSAlert's addButtonWithTitle) so that
-    // the fourth button is numbered NSAlertThirdButtonReturn + 1, and so on
-    NSInteger modalCode = 0;
-    if (clickedButtonIndex == NSAlertFirstButtonReturn)
-        modalCode = NSAlertFirstButtonReturn;
-    else if (clickedButtonIndex == NSAlertSecondButtonReturn)
-        modalCode = NSAlertSecondButtonReturn;
-    else if (clickedButtonIndex == NSAlertThirdButtonReturn)
-        modalCode = NSAlertThirdButtonReturn;
-    else
-        modalCode = NSAlertThirdButtonReturn + (clickedButtonIndex - 2);
-
-    [NSApp stopModalWithCode:modalCode];
+  [NSApp stopModalWithCode:[sender  tag]];
 }
 
 -(void) BE_beginSheetModalForWindow:(NSWindow *)aWindow {
-    [self beginSheetModalForWindow:aWindow modalDelegate:nil didEndSelector:nil contextInfo:nil];
+  [self beginSheetModalForWindow:aWindow modalDelegate:nil didEndSelector:nil contextInfo:nil];
 }
 
 @end