NJKeyInputField descends from NSControl and has a text field, rather than being one.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Tue, 12 Mar 2013 13:00:38 +0000 (14:00 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Tue, 12 Mar 2013 13:00:38 +0000 (14:00 +0100)
Classes/NJKeyInputField.h
Classes/NJKeyInputField.m
Info.plist

index 84498ff9351f07fb96b8e056b31261ea236958b6..c17fdaa712ee33022f40112d7ed2bbdedcd95f79 100644 (file)
@@ -11,7 +11,7 @@ extern const CGKeyCode NJKeyInputFieldEmpty;
 
 @protocol NJKeyInputFieldDelegate;
 
-@interface NJKeyInputField : NSTextField
+@interface NJKeyInputField : NSControl
     // An NJKeyInputField is a NSTextField-like widget that receives
     // exactly one key press, and displays the name of that key, then
     // resigns its first responder status. It can also inform a
@@ -20,7 +20,7 @@ extern const CGKeyCode NJKeyInputFieldEmpty;
 + (NSString *)stringForKeyCode:(CGKeyCode)keyCode;
     // Give the string name for a virtual key code.
 
-@property (nonatomic, weak) IBOutlet id <NJKeyInputFieldDelegate, NSTextFieldDelegate> delegate;
+@property (nonatomic, weak) IBOutlet id <NJKeyInputFieldDelegate> delegate;
 
 @property (nonatomic, assign) CGKeyCode keyCode;
     // The currently displayed key code, or NJKeyInputFieldEmpty if no
@@ -37,7 +37,6 @@ extern const CGKeyCode NJKeyInputFieldEmpty;
 
 @protocol NJKeyInputFieldDelegate <NSObject>
 
-@optional
 - (void)keyInputField:(NJKeyInputField *)keyInput
          didChangeKey:(CGKeyCode)keyCode;
 - (void)keyInputFieldDidClear:(NJKeyInputField *)keyInput;
index 2bdbfc6c7a64840e83b953f36506fa4fb059b88e..4e5513a2f472ad1436001ed026a29be845664b28 100644 (file)
@@ -17,20 +17,23 @@ enum {
 
 const CGKeyCode NJKeyInputFieldEmpty = kVK_MAX;
 
-@implementation NJKeyInputField
+@implementation NJKeyInputField {
+    NSTextField *field;
+}
 
 - (id)initWithFrame:(NSRect)frameRect {
     if ((self = [super initWithFrame:frameRect])) {
-        self.alignment = NSCenterTextAlignment;
-        self.editable = NO;
-        self.selectable = NO;
+        field = [[NSTextField alloc] initWithFrame:self.bounds];
+        field.alignment = NSCenterTextAlignment;
+        field.editable = NO;
+        field.selectable = NO;
+        [self addSubview:field];
     }
     return self;
 }
 
 - (void)clear {
     self.keyCode = NJKeyInputFieldEmpty;
-    if ([self.delegate respondsToSelector:@selector(keyInputFieldDidClear:)])
         [self.delegate keyInputFieldDidClear:self];
     [self resignIfFirstResponder];
 }
@@ -189,18 +192,18 @@ const CGKeyCode NJKeyInputFieldEmpty = kVK_MAX;
 }
 
 - (BOOL)becomeFirstResponder {
-    self.backgroundColor = NSColor.selectedTextBackgroundColor;
+    field.backgroundColor = NSColor.selectedTextBackgroundColor;
     return [super becomeFirstResponder];
 }
 
 - (BOOL)resignFirstResponder {
-    self.backgroundColor = NSColor.textBackgroundColor;
+    field.backgroundColor = NSColor.textBackgroundColor;
     return [super resignFirstResponder];
 }
 
 - (void)setKeyCode:(CGKeyCode)keyCode {
     _keyCode = keyCode;
-    self.stringValue = [NJKeyInputField stringForKeyCode:keyCode];
+    field.stringValue = [NJKeyInputField stringForKeyCode:keyCode];
 }
 
 - (void)keyDown:(NSEvent *)event {
@@ -209,12 +212,10 @@ const CGKeyCode NJKeyInputFieldEmpty = kVK_MAX;
         if ((event.modifierFlags & IGNORE) && event.keyCode == kVK_Delete) {
             // Allow Alt/Command+Delete to clear the field.
             self.keyCode = NJKeyInputFieldEmpty;
-            if ([self.delegate respondsToSelector:@selector(keyInputFieldDidClear:)])
-                [self.delegate keyInputFieldDidClear:self];
+            [self.delegate keyInputFieldDidClear:self];
         } else if (!(event.modifierFlags & IGNORE)) {
             self.keyCode = event.keyCode;
-            if ([self.delegate respondsToSelector:@selector(keyInputField:didChangeKey:)])
-                [self.delegate keyInputField:self didChangeKey:self.keyCode];
+            [self.delegate keyInputField:self didChangeKey:self.keyCode];
         }
         [self resignIfFirstResponder];
     }
@@ -236,17 +237,8 @@ const CGKeyCode NJKeyInputFieldEmpty = kVK_MAX;
     // modifiers are still down.
     if (!(theEvent.modifierFlags & NSDeviceIndependentModifierFlagsMask)) {
         self.keyCode = theEvent.keyCode;
-        if ([self.delegate respondsToSelector:@selector(keyInputField:didChangeKey:)])
-            [self.delegate keyInputField:self didChangeKey:_keyCode];
+        [self.delegate keyInputField:self didChangeKey:_keyCode];
     }
 }
 
-- (void)setDelegate:(id<NJKeyInputFieldDelegate, NSTextFieldDelegate>)delegate {
-    [super setDelegate:delegate];
-}
-
-- (id <NJKeyInputFieldDelegate, NSTextFieldDelegate>)delegate {
-    return (id)[super delegate];
-}
-
 @end
index a8c5eb5be187c4f91a687132eb8ab22fc801cc03..9bb0e57bd5122877481a3bdcf16d877d5fc50107 100644 (file)
@@ -46,7 +46,7 @@
        <key>CFBundleSignature</key>
        <string>????</string>
        <key>CFBundleVersion</key>
-       <string>183</string>
+       <string>184</string>
        <key>LSApplicationCategoryType</key>
        <string>public.app-category.utilities</string>
        <key>NSHumanReadableCopyright</key>