Skip to content

Commit

Permalink
Allow the query to still be forwarded when no matching string exists …
Browse files Browse the repository at this point in the history
…in the choices list
  • Loading branch information
halfwit committed Jun 12, 2022
1 parent 7f34baa commit 56aa797
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 2 additions & 0 deletions extensions/chooser/HSChooser.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
@property(nonatomic, retain) NSString *fontName;
@property(nonatomic) CGFloat fontSize;
@property(nonatomic) BOOL searchSubText;
@property(nonatomic) BOOL enableDefaultForQuery;

@property(nonatomic) NSColor *fgColor;
@property(nonatomic) NSColor *subTextColor;

Expand Down
15 changes: 14 additions & 1 deletion extensions/chooser/HSChooser.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ - (id)initWithRefTable:(LSRefTable)refTable completionCallbackRef:(int)completio
self.currentStaticChoices = nil;
self.currentCallbackChoices = nil;
self.filteredChoices = nil;
self.enableDefaultForQuery = NO;

self.hideCallbackRef = LUA_NOREF;
self.showCallbackRef = LUA_NOREF;
Expand Down Expand Up @@ -393,7 +394,19 @@ - (void)tableView:(NSTableView *)tableView didClickedRow:(NSInteger)row {
[skin pushNSObject:choice];
[skin protectedCallAndError:@"hs.chooser:completionCallback" nargs:1 nresults:0];
}


_lua_stackguard_exit(skin.L);
} else if (self.enableDefaultForQuery != NO) {
// No row remaining in choices, return just query
self.hasChosen = YES;
LuaSkin *skin = [LuaSkin sharedWithState:NULL];
_lua_stackguard_entry(skin.L);
NSDictionary<NSString*, NSString*> *choice = @{@"text": self.queryField.stringValue};
[self hide];
[skin pushLuaRef:self.refTable ref:self.completionCallbackRef];
[skin pushNSObject:choice];
[skin protectedCallAndError:@"hs.chooser:completionCallback" nargs:1 nresults:0];

_lua_stackguard_exit(skin.L);
}
}
Expand Down
38 changes: 38 additions & 0 deletions extensions/chooser/libchooser.m
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,43 @@ static int chooserSetBgDark(lua_State *L) {
return 1;
}

/// hs.chooser:enableDefaultForQuery([]) -> hs.chooser object or boolean
/// Method
/// Gets/Sets whether the chooser should run the callback on a query when it does not match any on the list
///
/// Parameters:
/// * enableDefaultForQuery - An optional boolean, true to return query string, false to not. If this parametr is omitted, the current configuration value will be returned
///
/// Returns:
/// * the `hs.chooser` object if a value was set, or a boolean if no parameter was passed
///
/// Notes:
/// * This should be used before a chooser has been displayed
static int chooserSetEnableDefaultForQuery(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK];

HSChooser *chooser = [skin toNSObjectAtIndex:1];

switch (lua_type(L, 2)) {
case LUA_TBOOLEAN:
chooser.enableDefaultForQuery = lua_toboolean(L, 2);
lua_pushvalue(L, 1);
break;

case LUA_TNONE:
lua_pushboolean(L, chooser.enableDefaultForQuery);
return 1;

default:
NSLog(@"ERROR: Unknown type passed to hs.chooser:enableDefaultForQuery(). This should not be possible");
lua_pushnil(L);
break;
}

return 1;
}

/// hs.chooser:searchSubText([searchSubText]) -> hs.chooser object or boolean
/// Method
/// Gets/Sets whether the chooser should search in the sub-text of each item
Expand Down Expand Up @@ -894,6 +931,7 @@ static int userdata_gc(lua_State* L) {
{"bgDark", chooserSetBgDark},
{"placeholderText", chooserPlaceholder},
{"searchSubText", chooserSetSearchSubText},
{"enableDefaultForQuery", chooserSetEnableDefaultForQuery},
{"width", chooserSetWidth},
{"rows", chooserSetNumRows},

Expand Down

0 comments on commit 56aa797

Please sign in to comment.