Skip to content

Commit d460725

Browse files
committed
Add a button to reset all filters (key: cmd+esc) and a possibility to remove a single filter (press shift+backspace when inside an input field of the row) which will also trigger a reset if the last row was removed (thus closely resembles the old single filter behaviour) #3302
1 parent 1a29647 commit d460725

File tree

5 files changed

+78
-7
lines changed

5 files changed

+78
-7
lines changed

Interfaces/English.lproj/DBView.xib

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@
12841284
<userDefinedRuntimeAttribute type="string" keyPath="systemColorOfName" value="gridColor"/>
12851285
</userDefinedRuntimeAttributes>
12861286
</customView>
1287-
<button verticalHuggingPriority="750" misplaced="YES" id="4676">
1287+
<button toolTip="Query the current table using the filter conditions defined above" verticalHuggingPriority="750" misplaced="YES" id="4676">
12881288
<rect key="frame" x="616" y="5" width="54" height="19"/>
12891289
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxY="YES"/>
12901290
<buttonCell key="cell" type="roundRect" title="Filter" bezelStyle="roundedRect" alignment="center" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="4677">
@@ -1295,6 +1295,21 @@
12951295
<action selector="filterTable:" target="ki9-Po-bdr" id="eAC-YD-du3"/>
12961296
</connections>
12971297
</button>
1298+
<button toolTip="Delete any defined filter conditions and reload the table (⌘⎋)" verticalHuggingPriority="750" id="jxG-hC-fxV">
1299+
<rect key="frame" x="20" y="5" width="34" height="19"/>
1300+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
1301+
<buttonCell key="cell" type="roundRect" bezelStyle="roundedRect" image="button_clear" imagePosition="overlaps" alignment="center" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="ZFV-bp-3r6">
1302+
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
1303+
<font key="font" metaFont="smallSystem"/>
1304+
<string key="keyEquivalent" base64-UTF8="YES">
1305+
Gw
1306+
</string>
1307+
<modifierMask key="keyEquivalentModifierMask" command="YES"/>
1308+
</buttonCell>
1309+
<connections>
1310+
<action selector="resetFilter:" target="ki9-Po-bdr" id="g9u-T3-TZd"/>
1311+
</connections>
1312+
</button>
12981313
</subviews>
12991314
</customView>
13001315
</subviews>
@@ -4445,6 +4460,7 @@ Gw
44454460
<connections>
44464461
<outlet property="filterButton" destination="4676" id="9tZ-dW-BR3"/>
44474462
<outlet property="filterRuleEditor" destination="FF9-z2-9od" id="RW4-XM-XQS"/>
4463+
<outlet property="resetButton" destination="jxG-hC-fxV" id="7mm-bC-Kd3"/>
44484464
<outlet property="tableContentViewBelow" destination="36" id="ZGh-dM-J6C"/>
44494465
<outlet property="tableDataInstance" destination="4702" id="e69-W6-UwN"/>
44504466
<outlet property="tableDocumentInstance" destination="-2" id="2Xe-WU-zRw"/>
@@ -5009,6 +5025,7 @@ Gw
50095025
<image name="button_action" width="30" height="22"/>
50105026
<image name="button_add" width="30" height="22"/>
50115027
<image name="button_bar_handle" width="11" height="23"/>
5028+
<image name="button_clear" width="30" height="22"/>
50125029
<image name="button_duplicate" width="30" height="22"/>
50135030
<image name="button_edit" width="30" height="22"/>
50145031
<image name="button_edit_mode" width="30" height="22"/>

Source/SPCompatibility.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@
8686
typedef NSUInteger NSCellHitResult;
8787
// This bitfield is available since 10.0 but only got a "name" in 10.10
8888
typedef NSUInteger NSAutoresizingMaskOptions;
89+
// This enum has been around since 10.0 but only got a "name" in 10.10
90+
typedef NSUInteger NSEventModifierFlags;
8991

9092
@compatibility_alias NSTitlebarAccessoryViewController NSViewController;
9193

@@ -162,6 +164,9 @@ typedef struct {
162164
#define NSAlertStyleWarning NSWarningAlertStyle
163165
#define NSAlertStyleCritical NSCriticalAlertStyle
164166

167+
#define NSEventModifierFlagShift NSShiftKeyMask
168+
#define NSEventModifierFlagDeviceIndependentFlagsMask NSDeviceIndependentModifierFlagsMask
169+
165170
@interface NSWindow (Sierra)
166171
+ (void)setAllowsAutomaticWindowTabbing:(BOOL)arg;
167172
@end

Source/SPRuleFilterController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ NSString * const SPRuleFilterHeightChangedNotification;
4242
IBOutlet SPTablesList *tablesListInstance;
4343
IBOutlet NSView *tableContentViewBelow;
4444
IBOutlet NSButton *filterButton;
45+
IBOutlet NSButton *resetButton;
4546

4647
NSMutableArray *columns;
4748
NSMutableDictionary *contentFilters;

Source/SPRuleFilterController.m

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ @interface ModelContainer : NSObject
203203

204204
#pragma mark -
205205

206-
@interface SPRuleFilterController () <NSRuleEditorDelegate>
206+
@interface SPRuleFilterController () <NSRuleEditorDelegate, NSTextFieldDelegate>
207207

208208
@property (readwrite, assign, nonatomic) CGFloat preferredHeight;
209209

@@ -223,6 +223,7 @@ - (BOOL)_focusOnFieldInSubtree:(NSDictionary *)dict;
223223
- (void)_resize;
224224
- (void)openContentFilterManagerForFilterType:(NSString *)filterType;
225225
- (IBAction)filterTable:(id)sender;
226+
- (IBAction)resetFilter:(id)sender;
226227
- (IBAction)_menuItemInRuleEditorClicked:(id)sender;
227228
- (void)_pretendPlayRuleEditorForCriteria:(NSMutableArray *)criteria displayValues:(NSMutableArray *)displayValues inRow:(NSInteger)row;
228229
- (void)_ensureValidOperatorCache:(ColumnNode *)col;
@@ -346,8 +347,8 @@ - (void)setColumns:(NSArray *)dataColumns;
346347
// make the rule editor reload the criteria
347348
[filterRuleEditor reloadCriteria];
348349

349-
// disable UI if no criteria exist
350-
[self setEnabled:([columns count] != 0)];
350+
// disable UI if no criteria exist (enable otherwise)
351+
[self setEnabled:YES];
351352
}
352353

353354
- (NSInteger)ruleEditor:(NSRuleEditor *)editor numberOfChildrenForCriterion:(nullable id)criterion withRowType:(NSRuleEditorRowType)rowType
@@ -495,6 +496,8 @@ - (id)ruleEditor:(NSRuleEditor *)editor displayValueForCriterion:(id)criterion i
495496
[textField sizeToFit];
496497
[textField setTarget:self];
497498
[textField setAction:@selector(_textFieldAction:)];
499+
[textField setDelegate:self]; // see -control:textView:doCommandBySelector:
500+
[textField setToolTip:NSLocalizedString(@"Enter the value to apply the filter condition with.\nPress ↩ to apply the filter or ⇧⌫ to remove this rule.", @"table content : rule filter editor : text input field : tooltip")];
498501
if([node initialValue]) [textField setStringValue:[node initialValue]];
499502
NSRect frame = [textField frame];
500503
//adjust width, to make the field wider
@@ -513,6 +516,37 @@ - (id)ruleEditor:(NSRuleEditor *)editor displayValueForCriterion:(id)criterion i
513516
return nil;
514517
}
515518

519+
- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
520+
{
521+
// if the user presses shift+backspace or shift+delete we'll try to remove the whole rule
522+
NSEvent *event = [NSApp currentEvent];
523+
if(
524+
( commandSelector == @selector(deleteBackward:) || commandSelector == @selector(deleteForward:) ) &&
525+
[event type] == NSKeyDown &&
526+
([event modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask) == NSEventModifierFlagShift
527+
) {
528+
NSInteger row = [filterRuleEditor rowForDisplayValue:control];
529+
530+
if(row != NSNotFound) {
531+
// we'll do the actual processing async, because we are currently in the stack of the object which will be dropped by this action.
532+
// so we want the delegate method to have finished first, just to be safe
533+
SPMainLoopAsync(^{
534+
// if we are about to remove the only row in existance, treat it as a reset instead
535+
if([[_modelContainer model] count] == 1) {
536+
[self resetFilter:nil];
537+
}
538+
else {
539+
[filterRuleEditor removeRowAtIndex:row];
540+
[self filterTable:nil]; // trigger a new filtering for convenience. I don't know if that is always the preferred approach...
541+
}
542+
});
543+
return YES;
544+
}
545+
}
546+
547+
return NO;
548+
}
549+
516550
- (IBAction)_textFieldAction:(id)sender
517551
{
518552
// if the action was caused by pressing return or enter, trigger filtering
@@ -652,6 +686,13 @@ - (IBAction)filterTable:(id)sender
652686
if(target && action) [target performSelector:action withObject:self];
653687
}
654688

689+
- (IBAction)resetFilter:(id)sender
690+
{
691+
[[_modelContainer mutableArrayValueForKey:@"model"] removeAllObjects];
692+
[self addFilterExpression];
693+
if(target && action) [target performSelector:action withObject:nil];
694+
}
695+
655696
- (void)_resize
656697
{
657698
// The situation with the sizing is a bit f'ed up:
@@ -939,9 +980,10 @@ - (BOOL)isEnabled
939980

940981
- (void)setEnabled:(BOOL)_enabled
941982
{
942-
enabled = _enabled;
943-
[filterButton setEnabled:_enabled];
944-
[filterRuleEditor setEnabled:_enabled];
983+
enabled = _enabled && [columns count] != 0;
984+
[filterButton setEnabled:enabled];
985+
[resetButton setEnabled:enabled];
986+
[filterRuleEditor setEnabled:enabled];
945987
}
946988

947989
- (NSString *)sqlWhereExpressionWithBinary:(BOOL)isBINARY error:(NSError **)err

Source/SPTableContent.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,10 @@ - (IBAction)filterTable:(id)sender
12711271
activeFilter = SPTableContentFilterSourceRuleFilter;
12721272
resetPaging = YES;
12731273
}
1274+
else if (sender == nil) {
1275+
activeFilter = SPTableContentFilterSourceNone;
1276+
resetPaging = YES;
1277+
}
12741278
#endif
12751279

12761280
NSString *taskString;
@@ -1362,6 +1366,7 @@ - (void)setRuleEditorVisible:(BOOL)show animate:(BOOL)animate
13621366
{
13631367
// we can't change the state of the button here, because the mouse click already changed it
13641368
if(show) {
1369+
[ruleFilterController setEnabled:YES];
13651370
if([ruleFilterController isEmpty]) {
13661371
[ruleFilterController addFilterExpression];
13671372
// the sizing will be updated automatically by adding a row
@@ -1371,6 +1376,7 @@ - (void)setRuleEditorVisible:(BOOL)show animate:(BOOL)animate
13711376
}
13721377
}
13731378
else {
1379+
[ruleFilterController setEnabled:NO]; // disable it to not trigger any key bindings when hidden
13741380
[self updateFilterRuleEditorSize:0.0 animate:animate];
13751381
}
13761382
showFilterRuleEditor = show;

0 commit comments

Comments
 (0)