Move isCommand and isModified to base class and use in transformer

This commit is contained in:
Andrew Kitchen 2025-01-17 13:38:58 -08:00
parent 3bf60f5400
commit 0046799a5f
5 changed files with 19 additions and 21 deletions

View file

@ -219,8 +219,8 @@ static NSString* kLeftTabString = @"\xe2\x87\xa4";
NSEventModifierFlags _modifiers = event.modifierFlags;
BOOL hasOptionModifier = (_modifiers & NSEventModifierFlagOption) != 0;
BOOL hasShiftModifier = (_modifiers & NSEventModifierFlagShift) != 0;
BOOL isCommand = (_modifiers & (NSEventModifierFlagControl | NSEventModifierFlagCommand)) != 0;
BOOL isCommand = event.isCommand;
BOOL needsShiftGlyph = NO;
NSMutableString *mutableResponse = [NSMutableString string];
@ -237,10 +237,8 @@ static NSString* kLeftTabString = @"\xe2\x87\xa4";
if (hasShiftModifier)
{
if (isCommand)
if (isCommand || (hasOptionModifier && !_displayModifiedCharacters))
[mutableResponse appendString:kShiftKeyString];
else if (hasOptionModifier && !_displayModifiedCharacters)
[mutableResponse appendString:kShiftKeyString];
else
needsShiftGlyph = !_displayModifiedCharacters;
}
@ -267,7 +265,7 @@ static NSString* kLeftTabString = @"\xe2\x87\xa4";
KCKeystroke *keystroke = (KCKeystroke *)event;
// check for bare shift-tab as left tab special case
if (hasShiftModifier && !keystroke.isCommand && !hasOptionModifier)
if (hasShiftModifier && !isCommand && !hasOptionModifier)
{
if (keystroke.keyCode == 48) {
[mutableResponse appendString:kLeftTabString];
@ -281,7 +279,7 @@ static NSString* kLeftTabString = @"\xe2\x87\xa4";
}
void(^appendModifiers)(BOOL) = ^(BOOL append) {
if (append && !keystroke.isCommand) {
if (append && !isCommand) {
if (hasOptionModifier) {
[mutableResponse appendString:kOptionKeyString];
}

View file

@ -32,6 +32,12 @@
@property (nonatomic, readonly) NSEventType type;
@property (nonatomic, readonly) NSEventModifierFlags modifierFlags;
/// An event is a command if it includes the Control or Command key; Option and Shift are only considered modifiers.
@property (nonatomic, readonly) BOOL isCommand;
/// Indicates whether a Keystroke has any of the Control, Command, Option or Shift modifiers applied.
@property (nonatomic, readonly) BOOL isModified;
+ (instancetype)eventWithNSEvent:(NSEvent *)event;
- (instancetype)initWithNSEvent:(NSEvent *)event NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;

View file

@ -52,6 +52,14 @@
return self;
}
- (BOOL)isCommand {
return (self.modifierFlags & (NSEventModifierFlagControl | NSEventModifierFlagCommand)) != 0;
}
- (BOOL)isModified {
return (self.modifierFlags & (NSEventModifierFlagControl | NSEventModifierFlagCommand | NSEventModifierFlagOption | NSEventModifierFlagShift)) != 0;
}
- (NSString *)convertToString {
return [[KCEventTransformer currentTransformer] transformedValue:self];
}

View file

@ -36,10 +36,4 @@
@property (nonatomic, copy, readonly) NSString *characters;
@property (nonatomic, copy, readonly) NSString *charactersIgnoringModifiers;
/// A Keystroke is a command if it includes the Control or Command key; Option and Shift are only considered modifiers.
- (BOOL)isCommand;
/// Indicates whether a Keystroke has any of the Control, Command, Option or Shift modifiers applied.
- (BOOL)isModified;
@end

View file

@ -57,14 +57,6 @@
[super dealloc];
}
- (BOOL)isCommand {
return (self.modifierFlags & (NSEventModifierFlagControl | NSEventModifierFlagCommand)) != 0;
}
- (BOOL)isModified {
return (self.modifierFlags & (NSEventModifierFlagControl | NSEventModifierFlagCommand | NSEventModifierFlagOption | NSEventModifierFlagShift)) != 0;
}
- (NSString *)description {
return [NSString stringWithFormat:@"<KCKeystroke: keyCode: %hu, modifiers: %lu, characters: %@, charactersIgnoringModifiers: %@>",
self.keyCode, self.modifierFlags, _characters, _charactersIgnoringModifiers];