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>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSUInteger, MPPasteboardOverlayInfoType) {
MPPasteboardOverlayInfoPassword,
MPPasteboardOverlayInfoUsername,
@@ -30,6 +32,22 @@ typedef NS_ENUM(NSUInteger, MPPasteboardOverlayInfoType) {
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
/**
@@ -70,6 +88,9 @@ FOUNDATION_EXPORT NSString *const MPPasteBoardControllerDidClearClipboard;
@param name a custom name
@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
NS_ASSUME_NONNULL_END

View File

@@ -22,6 +22,7 @@
#import "MPPasteBoardController.h"
#import "MPSettingsHelper.h"
#import "MPIconHelper.h"
#import "MPOverlayWindowController.h"
/* Notifications */
@@ -126,6 +127,10 @@ NSString *const MPPasteBoardTypeSource = @"org.nspasteboard.source";
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{
if(!object) {
return;
@@ -135,27 +140,27 @@ NSString *const MPPasteBoardTypeSource = @"org.nspasteboard.source";
NSString *infoText = nil;
switch(overlayInfoType) {
case MPPasteboardOverlayInfoPassword:
infoImage = [NSBundle.mainBundle imageForResource:@"00_PasswordTemplate"];
infoImage = [MPIconHelper icon:MPIconPassword];
infoText = NSLocalizedString(@"COPIED_PASSWORD", @"Password was copied to the pasteboard");
break;
case MPPasteboardOverlayInfoURL:
infoImage = [NSBundle.mainBundle imageForResource:@"01_PackageNetworkTemplate"];
infoImage = [MPIconHelper icon:MPIconPackageNetwork];
infoText = NSLocalizedString(@"COPIED_URL", @"URL was copied to the pasteboard");
break;
case MPPasteboardOverlayInfoUsername:
infoImage = [NSBundle.mainBundle imageForResource:@"09_IdentityTemplate"];
infoImage = [MPIconHelper icon:MPIconIdentity];
infoText = NSLocalizedString(@"COPIED_USERNAME", @"Username was copied to the pasteboard");
break;
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];
break;
case MPPasteboardOverlayInfoReference:
infoImage = [NSBundle.mainBundle imageForResource:@"04_KlipperTemplate"];
infoImage = [MPIconHelper icon:MPIconKlipper];
infoText = name;
break;

View File

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

View File

@@ -10,6 +10,7 @@
#import <KeePassKit/KeePassKit.h>
#import "MPEntryInspectorViewController.h"
#import "MPPasteBoardController.h"
@interface MPTOTPViewController ()
@@ -45,6 +46,25 @@
[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 {
[self _updateDisplay];
}

View File

@@ -36,11 +36,14 @@
<subviews>
<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"/>
<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"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<connections>
<outlet property="delegate" destination="-2" id="pfM-gT-aXU"/>
</connections>
</textField>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Hxg-bd-l1O">
<rect key="frame" x="414" y="2" width="26" height="18"/>