Started converting MPPastBoardController API to use more flexible interface.

This commit is contained in:
Michael Starke
2021-02-15 22:03:22 +01:00
parent 1b04e5cfa2
commit 9e14e8301d
5 changed files with 57 additions and 8 deletions

View File

@@ -22,6 +22,8 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSUInteger, MPPasteboardOverlayInfoType) { typedef NS_ENUM(NSUInteger, MPPasteboardOverlayInfoType) {
MPPasteboardOverlayInfoPassword, MPPasteboardOverlayInfoPassword,
MPPasteboardOverlayInfoUsername, MPPasteboardOverlayInfoUsername,
@@ -30,6 +32,22 @@ typedef NS_ENUM(NSUInteger, MPPasteboardOverlayInfoType) {
MPPasteboardOverlayInfoReference // overlay info that a reference that was copied MPPasteboardOverlayInfoReference // overlay info that a reference that was copied
}; };
typedef MPPasteboardOverlayInfoType MPPasteboardContentInfoType;
@interface MPPasteBoardContentInfo : NSObject
@property (readonly, strong) NSImage *image;
@property (readonly, strong) NSString *label;
+ (instancetype)contentInforForCustomField:(NSString *)name;
+ (instancetype)passwordContentInfo; // creates a content info approporate for passwords
+ (instancetype)urlContentInfo; // creates a content info apprpriate for urls
- (instancetype)initWithImage:(NSImage * _Nullable)image label:(NSString * _Nullable)label NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithType:(MPPasteboardContentInfoType)type;
@end
@interface MPPasteBoardController : NSObject @interface MPPasteBoardController : NSObject
/** /**
@@ -70,6 +88,9 @@ FOUNDATION_EXPORT NSString *const MPPasteBoardControllerDidClearClipboard;
@param name a custom name @param name a custom name
@param view the view that initiated the copy action @param view the view that initiated the copy action
*/ */
- (void)copyObject:(id<NSPasteboardWriting>)object overlayInfo:(MPPasteboardOverlayInfoType)overlayInfoType name:(NSString *)name atView:(NSView *)view; - (void)copyObject:(id<NSPasteboardWriting>)object overlayInfo:(MPPasteboardOverlayInfoType)overlayInfoType name:(NSString * _Nullable)name atView:(NSView *)view;
- (void)copyObject:(id<NSPasteboardWriting>)object contentInfo:(MPPasteBoardContentInfo *)info atView:(NSView *)view;
@end @end
NS_ASSUME_NONNULL_END

View File

@@ -22,6 +22,7 @@
#import "MPPasteBoardController.h" #import "MPPasteBoardController.h"
#import "MPSettingsHelper.h" #import "MPSettingsHelper.h"
#import "MPIconHelper.h"
#import "MPOverlayWindowController.h" #import "MPOverlayWindowController.h"
/* Notifications */ /* Notifications */
@@ -126,6 +127,10 @@ NSString *const MPPasteBoardTypeSource = @"org.nspasteboard.source";
self.isEmpty = NO; self.isEmpty = NO;
} }
- (void)copyObject:(id<NSPasteboardWriting>)object contentInfo:(MPPasteBoardContentInfo *)info atView:(nonnull NSView *)view{
[self copyObject:object overlayInfo:MPPasteboardOverlayInfoCustom name:info.label atView:view];
}
- (void)copyObject:(id<NSPasteboardWriting>)object overlayInfo:(MPPasteboardOverlayInfoType)overlayInfoType name:(NSString *)name atView:(NSView *)view{ - (void)copyObject:(id<NSPasteboardWriting>)object overlayInfo:(MPPasteboardOverlayInfoType)overlayInfoType name:(NSString *)name atView:(NSView *)view{
if(!object) { if(!object) {
return; return;
@@ -135,27 +140,27 @@ NSString *const MPPasteBoardTypeSource = @"org.nspasteboard.source";
NSString *infoText = nil; NSString *infoText = nil;
switch(overlayInfoType) { switch(overlayInfoType) {
case MPPasteboardOverlayInfoPassword: case MPPasteboardOverlayInfoPassword:
infoImage = [NSBundle.mainBundle imageForResource:@"00_PasswordTemplate"]; infoImage = [MPIconHelper icon:MPIconPassword];
infoText = NSLocalizedString(@"COPIED_PASSWORD", @"Password was copied to the pasteboard"); infoText = NSLocalizedString(@"COPIED_PASSWORD", @"Password was copied to the pasteboard");
break; break;
case MPPasteboardOverlayInfoURL: case MPPasteboardOverlayInfoURL:
infoImage = [NSBundle.mainBundle imageForResource:@"01_PackageNetworkTemplate"]; infoImage = [MPIconHelper icon:MPIconPackageNetwork];
infoText = NSLocalizedString(@"COPIED_URL", @"URL was copied to the pasteboard"); infoText = NSLocalizedString(@"COPIED_URL", @"URL was copied to the pasteboard");
break; break;
case MPPasteboardOverlayInfoUsername: case MPPasteboardOverlayInfoUsername:
infoImage = [NSBundle.mainBundle imageForResource:@"09_IdentityTemplate"]; infoImage = [MPIconHelper icon:MPIconIdentity];
infoText = NSLocalizedString(@"COPIED_USERNAME", @"Username was copied to the pasteboard"); infoText = NSLocalizedString(@"COPIED_USERNAME", @"Username was copied to the pasteboard");
break; break;
case MPPasteboardOverlayInfoCustom: case MPPasteboardOverlayInfoCustom:
infoImage = [NSBundle.mainBundle imageForResource:@"00_PasswordTemplate"]; infoImage = [MPIconHelper icon:MPIconPassword];
infoText = [NSString stringWithFormat:NSLocalizedString(@"COPIED_FIELD_%@", "Field name that was copied to the pasteboard"), name]; infoText = [NSString stringWithFormat:NSLocalizedString(@"COPIED_FIELD_%@", "Field name that was copied to the pasteboard"), name];
break; break;
case MPPasteboardOverlayInfoReference: case MPPasteboardOverlayInfoReference:
infoImage = [NSBundle.mainBundle imageForResource:@"04_KlipperTemplate"]; infoImage = [MPIconHelper icon:MPIconKlipper];
infoText = name; infoText = name;
break; break;

View File

@@ -11,7 +11,7 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@interface MPTOTPViewController : NSViewController @interface MPTOTPViewController : NSViewController <HNHUITextFieldDelegate>
@property (strong) IBOutlet HNHUITextField *toptValueTextField; @property (strong) IBOutlet HNHUITextField *toptValueTextField;
@property (strong) IBOutlet NSButton *remainingTimeButton; @property (strong) IBOutlet NSButton *remainingTimeButton;
@property (strong) IBOutlet NSButton *showSetupButton; @property (strong) IBOutlet NSButton *showSetupButton;

View File

@@ -10,6 +10,7 @@
#import <KeePassKit/KeePassKit.h> #import <KeePassKit/KeePassKit.h>
#import "MPEntryInspectorViewController.h" #import "MPEntryInspectorViewController.h"
#import "MPPasteBoardController.h"
@interface MPTOTPViewController () @interface MPTOTPViewController ()
@@ -45,6 +46,25 @@
[self _didChangeAttribute:nil]; [self _didChangeAttribute:nil];
} }
- (BOOL)textField:(NSTextField *)textField textView:(NSTextView *)textView performAction:(SEL)action {
if(action == @selector(copy:)) {
MPPasteboardOverlayInfoType info = MPPasteboardOverlayInfoCustom;
NSMutableString *selectedValue = [[NSMutableString alloc] init];
for(NSValue *rangeValue in textView.selectedRanges) {
[selectedValue appendString:[textView.string substringWithRange:rangeValue.rangeValue]];
}
NSString *name = NSLocalizedString(@"TOTP", "Field TOTP was copied to the pasteboard");
if(selectedValue.length == 0) {
return YES;
}
[MPPasteBoardController.defaultController copyObject:selectedValue overlayInfo:info name:name atView:self.view];
return NO;
}
return YES;
}
- (void)_didChangeAttribute:(NSNotification *)notification { - (void)_didChangeAttribute:(NSNotification *)notification {
[self _updateDisplay]; [self _updateDisplay];
} }

View File

@@ -36,11 +36,14 @@
<subviews> <subviews>
<textField horizontalHuggingPriority="249" verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="YES" translatesAutoresizingMaskIntoConstraints="NO" id="xBL-Jz-VQO" customClass="HNHUITextField"> <textField horizontalHuggingPriority="249" verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="YES" translatesAutoresizingMaskIntoConstraints="NO" id="xBL-Jz-VQO" customClass="HNHUITextField">
<rect key="frame" x="0.0" y="0.0" width="406" height="21"/> <rect key="frame" x="0.0" y="0.0" width="406" height="21"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" truncatesLastVisibleLine="YES" selectable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" usesSingleLineMode="YES" id="Vuy-HC-UhI"> <textFieldCell key="cell" lineBreakMode="truncatingTail" truncatesLastVisibleLine="YES" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" usesSingleLineMode="YES" id="Vuy-HC-UhI">
<font key="font" metaFont="system"/> <font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/> <color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/> <color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell> </textFieldCell>
<connections>
<outlet property="delegate" destination="-2" id="pfM-gT-aXU"/>
</connections>
</textField> </textField>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Hxg-bd-l1O"> <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Hxg-bd-l1O">
<rect key="frame" x="414" y="2" width="26" height="18"/> <rect key="frame" x="414" y="2" width="26" height="18"/>