diff options
author | rowanbeentje <rowan@beent.je> | 2013-03-31 23:05:36 +0000 |
---|---|---|
committer | rowanbeentje <rowan@beent.je> | 2013-03-31 23:05:36 +0000 |
commit | cca030a98637bec8e8a731efb7ad15b3f458b115 (patch) | |
tree | d2573148088d98d3ab7e765bf8933499169cc119 /Source/SPCustomQuery.m | |
parent | fb59ce3e1a183c8406912b9b21c5b5621497d54e (diff) | |
download | sequelpro-cca030a98637bec8e8a731efb7ad15b3f458b115.tar.gz sequelpro-cca030a98637bec8e8a731efb7ad15b3f458b115.tar.bz2 sequelpro-cca030a98637bec8e8a731efb7ad15b3f458b115.zip |
Combine the "Run All" and "Run Current/Previous/Selection" buttons into a single button with dropdown menu with ability to change the default action:
- Create a new SPComboPopupButton class; this subclasses NSPopupButton to retain the ability to show the popup menu, but only when the right-hand side of the button is pressed, allowing the rest of the button to perform the click action
- Combine the previous two "Run" buttons on the Custom Query view into a new SPComboPopupButton
- Move the Run menu items from the gear menu into the Run button popup menu so they can be discovered more easily (and the shortcuts seen more easily)
- Add a menu item to switch the button's default action, which also swaps all associated shortcuts
- Clean up associated logic
This implements Issue #1569
Diffstat (limited to 'Source/SPCustomQuery.m')
-rw-r--r-- | Source/SPCustomQuery.m | 193 |
1 files changed, 128 insertions, 65 deletions
diff --git a/Source/SPCustomQuery.m b/Source/SPCustomQuery.m index 08b5838c..26f66831 100644 --- a/Source/SPCustomQuery.m +++ b/Source/SPCustomQuery.m @@ -58,6 +58,7 @@ #import "SPTextView.h" #import "RegexKitLite.h" #import "SPThreadAdditions.h" +#import "SPConstants.h" #ifndef SP_CODA /* headers */ #import "SPAppController.h" #import "SPBundleHTMLOutputController.h" @@ -79,16 +80,50 @@ #ifdef SP_CODA @synthesize textView; @synthesize customQueryView; -@synthesize runAllButton; @synthesize tableDocumentInstance; @synthesize tablesListInstance; @synthesize affectedRowsText; #endif +@synthesize runAllButton; @synthesize textViewWasChanged; #pragma mark IBAction methods +/** + * Run the primary query task, as per the user preference + */ +- (IBAction)runPrimaryQueryAction:(id)sender +{ + if ([prefs boolForKey:SPQueryPrimaryControlRunsAll]) { + [self runAllQueries:sender]; + } else { + [self runSelectedQueries:sender]; + } +} + +/** + * Run the secondary query task, as per the user preference + */ +- (IBAction)runSecondaryQueryAction:(id)sender +{ + if ([prefs boolForKey:SPQueryPrimaryControlRunsAll]) { + [self runSelectedQueries:sender]; + } else { + [self runAllQueries:sender]; + } +} + +/** + * Swap the primary and secondary query run actions + */ +- (IBAction)switchDefaultQueryAction:(id)sender +{ + BOOL newValue = ![prefs boolForKey:SPQueryPrimaryControlRunsAll]; + [prefs setBool:newValue forKey:SPQueryPrimaryControlRunsAll]; + [self updateQueryInteractionInterface]; +} + /* * Split all the queries in the text view, split them into individual queries, * and run sequentially. @@ -1341,6 +1376,68 @@ } #pragma mark - +#pragma mark Interface setup + +/** + * Update the Run Selection/Query/All button and menu item state according + * to the user preferences. + */ +- (void)updateQueryInteractionInterface +{ + NSString *runAllTitle = NSLocalizedString(@"Run All Queries", @"Run All button and menu item title"); + + // By default, the interface uses Run Query/Run Selection as the primary interface, + // but the user can switch this. + BOOL primaryActionIsRunAll = [prefs boolForKey:SPQueryPrimaryControlRunsAll]; + + // Update the links as appropriate + if (primaryActionIsRunAll) { + runPrimaryActionButtonAsSelection = nil; + [runPrimaryActionButton setTitle:runAllTitle]; + [runPrimaryActionMenuItem setTitle:runAllTitle]; + } else { + runPrimaryActionButtonAsSelection = runPrimaryActionButton; + [runSecondaryActionMenuItem setTitle:runAllTitle]; + } + + // Update the Run Current/Previous/Selection menu item (and button if appropriate) + [self updateContextualRunInterface]; +} + +/** + * Update the selection-sensitive "Run Current" / "Run Previous" / "Run Selection" + * button and menu items based on the current interface state + */ +- (void)updateContextualRunInterface +{ + NSMenuItem *runSelectionMenuItem; + + // Determine the menu item to change + if (runPrimaryActionButtonAsSelection) { + runSelectionMenuItem = runPrimaryActionMenuItem; + } else { + runSelectionMenuItem = runSecondaryActionMenuItem; + } + + // If the current selection is a single caret position, update the button based on + // whether the caret is inside a valid query. + if (![textView selectedRange].length) { + if (currentQueryBeforeCaret) { + [runPrimaryActionButtonAsSelection setTitle:NSLocalizedString(@"Run Previous", @"Title of button to run query just before text caret in custom query view")]; + [runSelectionMenuItem setTitle:NSLocalizedString(@"Run Previous Query", @"Title of action menu item to run query just before text caret in custom query view")]; + } else { + [runPrimaryActionButtonAsSelection setTitle:NSLocalizedString(@"Run Current", @"Title of button to run current query in custom query view")]; + [runSelectionMenuItem setTitle:NSLocalizedString(@"Run Current Query", @"Title of action menu item to run current query in custom query view")]; + } + + // Otherwise, reflect the active selection + } else { + [runPrimaryActionButtonAsSelection setTitle:NSLocalizedString(@"Run Selection", @"Title of button to run selected text in custom query view")]; + [runSelectionMenuItem setTitle:NSLocalizedString(@"Run Selected Text", @"Title of action menu item to run selected text in custom query view")]; + } +} + +#pragma mark - #pragma mark Table load actions /** @@ -1506,9 +1603,6 @@ // Populate query favorites [self queryFavoritesHaveBeenUpdated:nil]; #endif - - // Disable runSelectionMenuItem in the gear menu - [runSelectionMenuItem setEnabled:NO]; } /** @@ -2568,7 +2662,7 @@ if (aTextView == textView) { if ([aTextView methodForSelector:aSelector] == [aTextView methodForSelector:@selector(insertNewline:)] && [[[NSApp currentEvent] characters] isEqualToString:@"\003"]) { - [self runSelectedQueries:self]; + [self runPrimaryQueryAction:self]; return YES; } @@ -2611,69 +2705,37 @@ // Ensure that the notification is from the custom query text view if ( [aNotification object] != textView ) return; - BOOL isLookBehind = YES; NSRange currentSelection = [textView selectedRange]; NSUInteger caretPosition = currentSelection.location; - NSRange qRange = [self queryRangeAtPosition:caretPosition lookBehind:&isLookBehind]; + // Detect the current query range, allowing the search to occur if the caret is after + // a semicolon + currentQueryBeforeCaret = YES; + currentQueryRange = [self queryRangeAtPosition:caretPosition lookBehind:¤tQueryBeforeCaret]; - if(qRange.length) - currentQueryRange = qRange; - else - currentQueryRange = NSMakeRange(0, 0); - - [textView setQueryRange:qRange]; + [textView setQueryRange:currentQueryRange]; [textView setNeedsDisplayInRect:[textView bounds]]; - // disable "Comment Current Query" menu item if no current query is selectable - [commentCurrentQueryMenuItem setEnabled:(currentQueryRange.length) ? YES : NO]; - - // If no text is selected, disable the button and action menu. - if ( caretPosition == NSNotFound ) { - selectionButtonCanBeEnabled = NO; - [runSelectionButton setEnabled:NO]; - [runSelectionMenuItem setEnabled:NO]; - return; + // If a query range is selected, update the current query range and menu actions + if (currentQueryRange.length) { + [commentCurrentQueryMenuItem setEnabled:YES]; + } else { + currentQueryRange = NSMakeRange(0, 0); + [commentCurrentQueryMenuItem setEnabled:NO]; } - // If the current selection is a single caret position, update the button based on - // whether the caret is inside a valid query. - if (!currentSelection.length) { - [runSelectionButton setTitle:NSLocalizedString(@"Run Current", @"Title of button to run current query in custom query view")]; - [runSelectionMenuItem setTitle:NSLocalizedString(@"Run Current Query", @"Title of action menu item to run current query in custom query view")]; - - // If a valid query is present at the cursor position, enable the button - if (qRange.length) { - if (isLookBehind) { - [runSelectionButton setTitle:NSLocalizedString(@"Run Previous", @"Title of button to run query just before text caret in custom query view")]; - [runSelectionMenuItem setTitle:NSLocalizedString(@"Run Previous Query", @"Title of action menu item to run query just before text caret in custom query view")]; - } - selectionButtonCanBeEnabled = YES; - if (![tableDocumentInstance isWorking]) { - [runSelectionButton setEnabled:YES]; - [runSelectionMenuItem setEnabled:YES]; - } - } else { - selectionButtonCanBeEnabled = NO; - [runSelectionButton setEnabled:NO]; - [runSelectionMenuItem setEnabled:NO]; - } - [commentLineOrSelectionMenuItem setTitle:NSLocalizedString(@"Comment Line", @"Title of action menu item to comment line")]; - - // For selection ranges, enable the button. - } else { - selectionButtonCanBeEnabled = YES; - [runSelectionButton setTitle:NSLocalizedString(@"Run Selection", @"Title of button to run selected text in custom query view")]; - [runSelectionMenuItem setTitle:NSLocalizedString(@"Run Selected Text", @"Title of action menu item to run selected text in custom query view")]; + // Vary the comment/line selection menu item according to whether a section is present + if (currentSelection.length) { [commentLineOrSelectionMenuItem setTitle:NSLocalizedString(@"Comment Selection", @"Title of action menu item to comment selection")]; - if (![tableDocumentInstance isWorking]) { - [runSelectionButton setEnabled:YES]; - [runSelectionMenuItem setEnabled:YES]; - } + } else { + [commentLineOrSelectionMenuItem setTitle:NSLocalizedString(@"Comment Line", @"Title of action menu item to comment line")]; } - if(!historyItemWasJustInserted) + if (!historyItemWasJustInserted) currentHistoryOffsetIndex = -1; + + // Update the text of the contextual run current/previous/selection button and menu item + [self updateContextualRunInterface]; } #pragma mark - @@ -3383,10 +3445,10 @@ #endif tableRowsSelectable = NO; - [runSelectionButton setEnabled:NO]; - [runSelectionMenuItem setEnabled:NO]; [runAllButton setEnabled:NO]; - [runAllMenuItem setEnabled:NO]; + [runPrimaryActionButton setEnabled:NO]; + [runPrimaryActionMenuItem setEnabled:NO]; + [runSecondaryActionMenuItem setEnabled:NO]; } /** @@ -3402,13 +3464,11 @@ return; #endif - if (selectionButtonCanBeEnabled) { - [runSelectionButton setEnabled:YES]; - [runSelectionMenuItem setEnabled:YES]; - } tableRowsSelectable = YES; [runAllButton setEnabled:YES]; - [runAllMenuItem setEnabled:YES]; + [runPrimaryActionButton setEnabled:YES]; + [runPrimaryActionMenuItem setEnabled:YES]; + [runSecondaryActionMenuItem setEnabled:YES]; } #pragma mark - @@ -3655,7 +3715,6 @@ sortField = nil; isDesc = NO; sortColumn = nil; - selectionButtonCanBeEnabled = NO; isFieldEditable = NO; cqColumnDefinition = nil; favoritesManager = nil; @@ -3691,6 +3750,9 @@ currentHistoryOffsetIndex = -1; historyItemWasJustInserted = NO; + currentQueryRange = NSMakeRange(0, 0); + currentQueryBeforeCaret = NO; + runPrimaryActionButtonAsSelection = nil; queryLoadTimer = nil; @@ -3874,6 +3936,7 @@ { [customQueryView setFieldEditorSelectedRange:NSMakeRange(0,0)]; + [self updateQueryInteractionInterface]; #ifndef SP_CODA // Set pre-defined menu tags |