Added copy API to attributeViewController to be used by subclasses

This commit is contained in:
Michael Starke
2022-03-09 00:07:17 +01:00
parent cea10e83b1
commit 401d0d1b2f
4 changed files with 28 additions and 56 deletions

View File

@@ -26,7 +26,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (strong) IBOutlet NSButton *actionButton;
- (void)updateValuesAndEditing;
- (void)performCopyForText:(NSString *)text;
@end

View File

@@ -46,7 +46,7 @@ NSString *nameForDefaultKey(NSString *key) {
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
// set editor to false?
_isEditor = NO;
return self;
}
@@ -66,6 +66,7 @@ NSString *nameForDefaultKey(NSString *key) {
self.actionButton.action = @selector(_copyText:);
self.actionButton.target = self;
self.actionButton.hidden = YES;
self.actionButton.title = NSLocalizedString(@"COPY", "Button title for copying an attribute value");
[self updateValuesAndEditing];
}
@@ -84,15 +85,10 @@ NSString *nameForDefaultKey(NSString *key) {
- (void)setRepresentedObject:(id)representedObject {
if(self.representedAttribute) {
[NSNotificationCenter.defaultCenter removeObserver:self name:KPKWillChangeAttributeNotification object:self.representedObject];
[NSNotificationCenter.defaultCenter removeObserver:self name:KPKDidChangeAttributeNotification object:self.representedObject];
}
super.representedObject = representedObject;
if(self.representedAttribute) {
[NSNotificationCenter.defaultCenter addObserver:self
selector:(@selector(_willChangeAttribute:))
name:KPKWillChangeAttributeNotification
object:self.representedAttribute];
[NSNotificationCenter.defaultCenter addObserver:self
selector:(@selector(_didChangeAttribute:))
name:KPKDidChangeAttributeNotification
@@ -104,10 +100,7 @@ NSString *nameForDefaultKey(NSString *key) {
}
- (void)_copyText:(id)sender {
NSText *text = [self.view.window fieldEditor:NO forObject:self.valueTextField];
if([text isKindOfClass:NSTextView.class]) {
[self textField:self.valueTextField textView:(NSTextView *)text performAction:@selector(copy:)];
}
[self performCopyForText:self.valueTextField.stringValue];
}
- (BOOL)textField:(NSTextField *)textField textView:(NSTextView *)textView performAction:(SEL)action {
@@ -116,7 +109,7 @@ NSString *nameForDefaultKey(NSString *key) {
}
// Only copy action
MPPasteboardOverlayInfoType info = MPPasteboardOverlayInfoCustom;
NSMutableString *selectedValue = [[NSMutableString alloc] init];
for(NSValue *rangeValue in textView.selectedRanges) {
[selectedValue appendString:[textView.string substringWithRange:rangeValue.rangeValue]];
@@ -124,7 +117,15 @@ NSString *nameForDefaultKey(NSString *key) {
if(selectedValue.length == 0) {
[selectedValue setString:textField.stringValue];
}
[self performCopyForText:selectedValue];
return NO;
}
- (void)performCopyForText:(NSString *)text {
MPPasteboardOverlayInfoType info = MPPasteboardOverlayInfoCustom;
NSString *name = @"";
if([self.representedAttribute.key isEqual:kKPKUsernameKey]) {
info = MPPasteboardOverlayInfoUsername;
}
@@ -140,13 +141,7 @@ NSString *nameForDefaultKey(NSString *key) {
else {
name = self.representedAttribute.key;
}
[MPPasteBoardController.defaultController copyObject:selectedValue overlayInfo:info name:name atView:self.view];
return NO;
}
- (void)_willChangeAttribute:(NSNotification *)notification {
// nothing to d
[MPPasteBoardController.defaultController copyObject:text overlayInfo:info name:name atView:self.view];
}
- (void)_didChangeAttribute:(NSNotification *)notification {
@@ -203,30 +198,5 @@ NSString *nameForDefaultKey(NSString *key) {
self.representedAttribute.value = self.valueTextField.stringValue;
}
- (void)objectDidBeginEditing:(id<NSEditor>)editor {
NSLog(@"%@: %@", NSStringFromClass(self.class), NSStringFromSelector(_cmd));
[super objectDidBeginEditing:editor];
}
- (void)objectDidEndEditing:(id<NSEditor>)editor {
NSLog(@"%@: %@", NSStringFromClass(self.class), NSStringFromSelector(_cmd));
[super objectDidEndEditing:editor];
}
- (BOOL)commitEditing {
NSLog(@"%@: %@", NSStringFromClass(self.class), NSStringFromSelector(_cmd));
return [super commitEditing];
}
- (BOOL)commitEditingAndReturnError:(NSError *__autoreleasing _Nullable *)error {
NSLog(@"%@: %@", NSStringFromClass(self.class), NSStringFromSelector(_cmd));
return [super commitEditingAndReturnError:error];
}
- (void)commitEditingWithDelegate:(id)delegate didCommitSelector:(SEL)didCommitSelector contextInfo:(void *)contextInfo {
NSLog(@"%@: %@", NSStringFromClass(self.class), NSStringFromSelector(_cmd));
[super commitEditingWithDelegate:delegate didCommitSelector:didCommitSelector contextInfo:contextInfo];
}
@end

View File

@@ -27,14 +27,6 @@
self.togglePasswordButton.action = @selector(toggleDisplay:);
self.togglePasswordButton.target = self.passwordTextField;
__weak MPEntryPasswordAttributeViewController *welf = self;
self.passwordTextField.buttonTitle = NSLocalizedString(@"COPY", "Button to copy the value of an Attribute");
self.passwordTextField.buttonActionBlock = ^void(NSTextField *tf) {
NSText *text = [welf.view.window fieldEditor:NO forObject:welf.passwordTextField];
if([text isKindOfClass:NSTextView.class]) {
[welf textField:welf.passwordTextField textView:(NSTextView *)text performAction:@selector(copy:)];
}
};
}
- (void)updateValuesAndEditing {

View File

@@ -8,6 +8,7 @@
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="MPEntryPasswordAttributeViewController">
<connections>
<outlet property="actionButton" destination="z9F-gY-cEs" id="65B-kh-9zh"/>
<outlet property="generatePasswordButton" destination="UlI-yI-Sqn" id="sKR-Rd-2LC"/>
<outlet property="keyTextField" destination="2aN-Ps-z4K" id="ua7-Km-K5o"/>
<outlet property="passwordTextField" destination="tpF-hp-29r" id="2pM-fr-9l0"/>
@@ -17,7 +18,7 @@
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView id="Hz6-mo-xeY">
<customView id="Hz6-mo-xeY" customClass="MPInspectorEditorView">
<rect key="frame" x="0.0" y="0.0" width="480" height="42"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
@@ -36,7 +37,7 @@
<rect key="frame" x="0.0" y="0.0" width="480" height="20"/>
<subviews>
<secureTextField horizontalHuggingPriority="249" verticalHuggingPriority="750" horizontalCompressionResistancePriority="249" allowsCharacterPickerTouchBarItem="YES" contentType="oneTimeCode" translatesAutoresizingMaskIntoConstraints="NO" id="tpF-hp-29r" customClass="HNHUISecureTextField">
<rect key="frame" x="0.0" y="0.0" width="352" height="20"/>
<rect key="frame" x="0.0" y="0.0" width="299" height="20"/>
<secureTextFieldCell key="cell" lineBreakMode="truncatingTail" truncatesLastVisibleLine="YES" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" drawsBackground="YES" usesSingleLineMode="YES" id="CeT-F7-Vb3">
<font key="font" size="13" name="Menlo-Regular"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
@@ -51,7 +52,7 @@
</connections>
</secureTextField>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="AY0-XE-8S7">
<rect key="frame" x="353" y="-7" width="50" height="32"/>
<rect key="frame" x="300" y="-7" width="51" height="32"/>
<buttonCell key="cell" type="push" bezelStyle="rounded" image="NSQuickLookTemplate" imagePosition="only" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="NUE-lS-hzr">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES" changeBackground="YES" changeGray="YES"/>
<font key="font" metaFont="system"/>
@@ -61,22 +62,31 @@
</connections>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="UlI-yI-Sqn">
<rect key="frame" x="397" y="-7" width="90" height="32"/>
<rect key="frame" x="345" y="-7" width="90" height="32"/>
<buttonCell key="cell" type="push" title="Generate" bezelStyle="rounded" alignment="center" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="Rx9-ck-eKo">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="z9F-gY-cEs">
<rect key="frame" x="430" y="-5" width="56" height="27"/>
<buttonCell key="cell" type="push" title="Copy" bezelStyle="rounded" alignment="center" controlSize="small" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="P6t-eT-Iug">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="smallSystem"/>
</buttonCell>
</button>
</subviews>
<visibilityPriorities>
<integer value="1000"/>
<integer value="1000"/>
<integer value="1000"/>
<integer value="1000"/>
</visibilityPriorities>
<customSpacing>
<real value="3.4028234663852886e+38"/>
<real value="3.4028234663852886e+38"/>
<real value="3.4028234663852886e+38"/>
<real value="3.4028234663852886e+38"/>
</customSpacing>
</stackView>
</subviews>