Skip to content

Commit

Permalink
Add a button to reset all filters (key: cmd+esc) and a possibility to…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
dmoagx committed Jun 14, 2019
1 parent 1a29647 commit d460725
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
19 changes: 18 additions & 1 deletion Interfaces/English.lproj/DBView.xib
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@
<userDefinedRuntimeAttribute type="string" keyPath="systemColorOfName" value="gridColor"/>
</userDefinedRuntimeAttributes>
</customView>
<button verticalHuggingPriority="750" misplaced="YES" id="4676">
<button toolTip="Query the current table using the filter conditions defined above" verticalHuggingPriority="750" misplaced="YES" id="4676">
<rect key="frame" x="616" y="5" width="54" height="19"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxY="YES"/>
<buttonCell key="cell" type="roundRect" title="Filter" bezelStyle="roundedRect" alignment="center" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="4677">
Expand All @@ -1295,6 +1295,21 @@
<action selector="filterTable:" target="ki9-Po-bdr" id="eAC-YD-du3"/>
</connections>
</button>
<button toolTip="Delete any defined filter conditions and reload the table (⌘⎋)" verticalHuggingPriority="750" id="jxG-hC-fxV">
<rect key="frame" x="20" y="5" width="34" height="19"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<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">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="smallSystem"/>
<string key="keyEquivalent" base64-UTF8="YES">
Gw
</string>
<modifierMask key="keyEquivalentModifierMask" command="YES"/>
</buttonCell>
<connections>
<action selector="resetFilter:" target="ki9-Po-bdr" id="g9u-T3-TZd"/>
</connections>
</button>
</subviews>
</customView>
</subviews>
Expand Down Expand Up @@ -4445,6 +4460,7 @@ Gw
<connections>
<outlet property="filterButton" destination="4676" id="9tZ-dW-BR3"/>
<outlet property="filterRuleEditor" destination="FF9-z2-9od" id="RW4-XM-XQS"/>
<outlet property="resetButton" destination="jxG-hC-fxV" id="7mm-bC-Kd3"/>
<outlet property="tableContentViewBelow" destination="36" id="ZGh-dM-J6C"/>
<outlet property="tableDataInstance" destination="4702" id="e69-W6-UwN"/>
<outlet property="tableDocumentInstance" destination="-2" id="2Xe-WU-zRw"/>
Expand Down Expand Up @@ -5009,6 +5025,7 @@ Gw
<image name="button_action" width="30" height="22"/>
<image name="button_add" width="30" height="22"/>
<image name="button_bar_handle" width="11" height="23"/>
<image name="button_clear" width="30" height="22"/>
<image name="button_duplicate" width="30" height="22"/>
<image name="button_edit" width="30" height="22"/>
<image name="button_edit_mode" width="30" height="22"/>
Expand Down
5 changes: 5 additions & 0 deletions Source/SPCompatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
typedef NSUInteger NSCellHitResult;
// This bitfield is available since 10.0 but only got a "name" in 10.10
typedef NSUInteger NSAutoresizingMaskOptions;
// This enum has been around since 10.0 but only got a "name" in 10.10
typedef NSUInteger NSEventModifierFlags;

@compatibility_alias NSTitlebarAccessoryViewController NSViewController;

Expand Down Expand Up @@ -162,6 +164,9 @@ typedef struct {
#define NSAlertStyleWarning NSWarningAlertStyle
#define NSAlertStyleCritical NSCriticalAlertStyle

#define NSEventModifierFlagShift NSShiftKeyMask
#define NSEventModifierFlagDeviceIndependentFlagsMask NSDeviceIndependentModifierFlagsMask

@interface NSWindow (Sierra)
+ (void)setAllowsAutomaticWindowTabbing:(BOOL)arg;
@end
Expand Down
1 change: 1 addition & 0 deletions Source/SPRuleFilterController.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ NSString * const SPRuleFilterHeightChangedNotification;
IBOutlet SPTablesList *tablesListInstance;
IBOutlet NSView *tableContentViewBelow;
IBOutlet NSButton *filterButton;
IBOutlet NSButton *resetButton;

NSMutableArray *columns;
NSMutableDictionary *contentFilters;
Expand Down
54 changes: 48 additions & 6 deletions Source/SPRuleFilterController.m
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ @interface ModelContainer : NSObject

#pragma mark -

@interface SPRuleFilterController () <NSRuleEditorDelegate>
@interface SPRuleFilterController () <NSRuleEditorDelegate, NSTextFieldDelegate>

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

Expand All @@ -223,6 +223,7 @@ - (BOOL)_focusOnFieldInSubtree:(NSDictionary *)dict;
- (void)_resize;
- (void)openContentFilterManagerForFilterType:(NSString *)filterType;
- (IBAction)filterTable:(id)sender;
- (IBAction)resetFilter:(id)sender;
- (IBAction)_menuItemInRuleEditorClicked:(id)sender;
- (void)_pretendPlayRuleEditorForCriteria:(NSMutableArray *)criteria displayValues:(NSMutableArray *)displayValues inRow:(NSInteger)row;
- (void)_ensureValidOperatorCache:(ColumnNode *)col;
Expand Down Expand Up @@ -346,8 +347,8 @@ - (void)setColumns:(NSArray *)dataColumns;
// make the rule editor reload the criteria
[filterRuleEditor reloadCriteria];

// disable UI if no criteria exist
[self setEnabled:([columns count] != 0)];
// disable UI if no criteria exist (enable otherwise)
[self setEnabled:YES];
}

- (NSInteger)ruleEditor:(NSRuleEditor *)editor numberOfChildrenForCriterion:(nullable id)criterion withRowType:(NSRuleEditorRowType)rowType
Expand Down Expand Up @@ -495,6 +496,8 @@ - (id)ruleEditor:(NSRuleEditor *)editor displayValueForCriterion:(id)criterion i
[textField sizeToFit];
[textField setTarget:self];
[textField setAction:@selector(_textFieldAction:)];
[textField setDelegate:self]; // see -control:textView:doCommandBySelector:
[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")];
if([node initialValue]) [textField setStringValue:[node initialValue]];
NSRect frame = [textField frame];
//adjust width, to make the field wider
Expand All @@ -513,6 +516,37 @@ - (id)ruleEditor:(NSRuleEditor *)editor displayValueForCriterion:(id)criterion i
return nil;
}

- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
{
// if the user presses shift+backspace or shift+delete we'll try to remove the whole rule
NSEvent *event = [NSApp currentEvent];
if(
( commandSelector == @selector(deleteBackward:) || commandSelector == @selector(deleteForward:) ) &&
[event type] == NSKeyDown &&
([event modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask) == NSEventModifierFlagShift
) {
NSInteger row = [filterRuleEditor rowForDisplayValue:control];

if(row != NSNotFound) {
// we'll do the actual processing async, because we are currently in the stack of the object which will be dropped by this action.
// so we want the delegate method to have finished first, just to be safe
SPMainLoopAsync(^{
// if we are about to remove the only row in existance, treat it as a reset instead
if([[_modelContainer model] count] == 1) {
[self resetFilter:nil];
}
else {
[filterRuleEditor removeRowAtIndex:row];
[self filterTable:nil]; // trigger a new filtering for convenience. I don't know if that is always the preferred approach...
}
});
return YES;
}
}

return NO;
}

- (IBAction)_textFieldAction:(id)sender
{
// if the action was caused by pressing return or enter, trigger filtering
Expand Down Expand Up @@ -652,6 +686,13 @@ - (IBAction)filterTable:(id)sender
if(target && action) [target performSelector:action withObject:self];
}

- (IBAction)resetFilter:(id)sender
{
[[_modelContainer mutableArrayValueForKey:@"model"] removeAllObjects];
[self addFilterExpression];
if(target && action) [target performSelector:action withObject:nil];
}

- (void)_resize
{
// The situation with the sizing is a bit f'ed up:
Expand Down Expand Up @@ -939,9 +980,10 @@ - (BOOL)isEnabled

- (void)setEnabled:(BOOL)_enabled
{
enabled = _enabled;
[filterButton setEnabled:_enabled];
[filterRuleEditor setEnabled:_enabled];
enabled = _enabled && [columns count] != 0;
[filterButton setEnabled:enabled];
[resetButton setEnabled:enabled];
[filterRuleEditor setEnabled:enabled];
}

- (NSString *)sqlWhereExpressionWithBinary:(BOOL)isBINARY error:(NSError **)err
Expand Down
6 changes: 6 additions & 0 deletions Source/SPTableContent.m
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,10 @@ - (IBAction)filterTable:(id)sender
activeFilter = SPTableContentFilterSourceRuleFilter;
resetPaging = YES;
}
else if (sender == nil) {
activeFilter = SPTableContentFilterSourceNone;
resetPaging = YES;
}
#endif

NSString *taskString;
Expand Down Expand Up @@ -1362,6 +1366,7 @@ - (void)setRuleEditorVisible:(BOOL)show animate:(BOOL)animate
{
// we can't change the state of the button here, because the mouse click already changed it
if(show) {
[ruleFilterController setEnabled:YES];
if([ruleFilterController isEmpty]) {
[ruleFilterController addFilterExpression];
// the sizing will be updated automatically by adding a row
Expand All @@ -1371,6 +1376,7 @@ - (void)setRuleEditorVisible:(BOOL)show animate:(BOOL)animate
}
}
else {
[ruleFilterController setEnabled:NO]; // disable it to not trigger any key bindings when hidden
[self updateFilterRuleEditorSize:0.0 animate:animate];
}
showFilterRuleEditor = show;
Expand Down

0 comments on commit d460725

Please sign in to comment.