custom icons can now be selected.

This commit is contained in:
michael starke
2017-09-07 21:26:12 +02:00
parent 6410cc905b
commit 348da78e23
4 changed files with 115 additions and 26 deletions

View File

@@ -8,6 +8,7 @@
<customObject id="-2" userLabel="File's Owner" customClass="MPIconSelectViewController"> <customObject id="-2" userLabel="File's Owner" customClass="MPIconSelectViewController">
<connections> <connections>
<outlet property="iconCollectionView" destination="58" id="77"/> <outlet property="iconCollectionView" destination="58" id="77"/>
<outlet property="typeSelectionButton" destination="QWR-lI-xJr" id="U95-Gu-0UQ"/>
<outlet property="view" destination="1" id="56"/> <outlet property="view" destination="1" id="56"/>
</connections> </connections>
</customObject> </customObject>
@@ -18,13 +19,13 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews> <subviews>
<scrollView focusRingType="none" borderType="none" autohidesScrollers="YES" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="57" customClass="HNHUIScrollView"> <scrollView focusRingType="none" borderType="none" autohidesScrollers="YES" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="57" customClass="HNHUIScrollView">
<rect key="frame" x="10" y="50" width="380" height="280"/> <rect key="frame" x="10" y="50" width="380" height="240"/>
<clipView key="contentView" drawsBackground="NO" copiesOnScroll="NO" id="Gwf-zs-lPT"> <clipView key="contentView" drawsBackground="NO" copiesOnScroll="NO" id="Gwf-zs-lPT">
<rect key="frame" x="0.0" y="0.0" width="380" height="280"/> <rect key="frame" x="0.0" y="0.0" width="380" height="240"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews> <subviews>
<collectionView focusRingType="none" id="58"> <collectionView focusRingType="none" id="58">
<rect key="frame" x="0.0" y="0.0" width="380" height="280"/> <rect key="frame" x="0.0" y="0.0" width="380" height="240"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="primaryBackgroundColor" name="windowBackgroundColor" catalog="System" colorSpace="catalog"/> <color key="primaryBackgroundColor" name="windowBackgroundColor" catalog="System" colorSpace="catalog"/>
<connections> <connections>
@@ -38,12 +39,12 @@
<autoresizingMask key="autoresizingMask"/> <autoresizingMask key="autoresizingMask"/>
</scroller> </scroller>
<scroller key="verticalScroller" hidden="YES" verticalHuggingPriority="750" doubleValue="1" horizontal="NO" id="60"> <scroller key="verticalScroller" hidden="YES" verticalHuggingPriority="750" doubleValue="1" horizontal="NO" id="60">
<rect key="frame" x="234" y="1" width="15" height="143"/> <rect key="frame" x="364" y="0.0" width="16" height="240"/>
<autoresizingMask key="autoresizingMask"/> <autoresizingMask key="autoresizingMask"/>
</scroller> </scroller>
</scrollView> </scrollView>
<button verticalHuggingPriority="750" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="101"> <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="101">
<rect key="frame" x="263" y="18" width="117" height="25"/> <rect key="frame" x="265" y="18" width="115" height="25"/>
<buttonCell key="cell" type="roundTextured" title="Use Default Icon" bezelStyle="texturedRounded" alignment="center" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="102"> <buttonCell key="cell" type="roundTextured" title="Use Default Icon" bezelStyle="texturedRounded" alignment="center" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="102">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/> <behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/> <font key="font" metaFont="system"/>
@@ -52,8 +53,28 @@
<action selector="useDefault:" target="-2" id="129"/> <action selector="useDefault:" target="-2" id="129"/>
</connections> </connections>
</button> </button>
<button verticalHuggingPriority="750" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="His-4A-hcY"> <segmentedControl verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="QWR-lI-xJr">
<rect key="frame" x="198" y="18" width="57" height="25"/> <rect key="frame" x="132" y="296" width="136" height="25"/>
<segmentedCell key="cell" borderStyle="border" alignment="left" style="texturedRounded" trackingMode="selectOne" id="0BD-eH-GR9">
<font key="font" metaFont="system"/>
<segments>
<segment label="Built in"/>
<segment label="Custom" selected="YES" tag="1"/>
</segments>
</segmentedCell>
<connections>
<action selector="toggleIcons:" target="-2" id="mke-RW-XXE"/>
</connections>
</segmentedControl>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="8kv-BJ-IEk">
<rect key="frame" x="153" y="18" width="104" height="25"/>
<buttonCell key="cell" type="roundTextured" title="Download Icon" bezelStyle="texturedRounded" alignment="center" lineBreakMode="truncatingTail" enabled="NO" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="iaf-XW-XUo">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="His-4A-hcY">
<rect key="frame" x="89" y="18" width="56" height="25"/>
<buttonCell key="cell" type="roundTextured" title="Cancel" bezelStyle="texturedRounded" alignment="center" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="1kM-cI-P1o"> <buttonCell key="cell" type="roundTextured" title="Cancel" bezelStyle="texturedRounded" alignment="center" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="1kM-cI-P1o">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/> <behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/> <font key="font" metaFont="system"/>
@@ -64,17 +85,21 @@
</button> </button>
</subviews> </subviews>
<constraints> <constraints>
<constraint firstItem="57" firstAttribute="top" secondItem="1" secondAttribute="top" constant="10" id="88"/>
<constraint firstAttribute="bottom" secondItem="101" secondAttribute="bottom" constant="20" symbolic="YES" id="107"/> <constraint firstAttribute="bottom" secondItem="101" secondAttribute="bottom" constant="20" symbolic="YES" id="107"/>
<constraint firstAttribute="trailing" secondItem="101" secondAttribute="trailing" constant="20" symbolic="YES" id="108"/> <constraint firstAttribute="trailing" secondItem="101" secondAttribute="trailing" constant="20" symbolic="YES" id="108"/>
<constraint firstAttribute="trailing" secondItem="57" secondAttribute="trailing" constant="10" id="110"/> <constraint firstAttribute="trailing" secondItem="57" secondAttribute="trailing" constant="10" id="110"/>
<constraint firstItem="57" firstAttribute="leading" secondItem="1" secondAttribute="leading" constant="10" id="111"/> <constraint firstItem="57" firstAttribute="leading" secondItem="1" secondAttribute="leading" constant="10" id="111"/>
<constraint firstItem="101" firstAttribute="top" secondItem="57" secondAttribute="bottom" constant="8" symbolic="YES" id="112"/> <constraint firstItem="101" firstAttribute="top" secondItem="57" secondAttribute="bottom" constant="8" symbolic="YES" id="112"/>
<constraint firstItem="His-4A-hcY" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="1" secondAttribute="leading" constant="20" symbolic="YES" id="69a-Sb-jSj"/> <constraint firstItem="His-4A-hcY" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="1" secondAttribute="leading" constant="20" symbolic="YES" id="69a-Sb-jSj"/>
<constraint firstItem="101" firstAttribute="leading" secondItem="His-4A-hcY" secondAttribute="trailing" constant="8" id="EHs-2w-CJq"/> <constraint firstItem="57" firstAttribute="top" secondItem="QWR-lI-xJr" secondAttribute="bottom" constant="8" symbolic="YES" id="DIj-jT-MV1"/>
<constraint firstItem="8kv-BJ-IEk" firstAttribute="baseline" secondItem="101" secondAttribute="baseline" id="Knp-g8-7FQ"/>
<constraint firstItem="8kv-BJ-IEk" firstAttribute="leading" secondItem="His-4A-hcY" secondAttribute="trailing" constant="8" symbolic="YES" id="MZ0-p6-6MH"/>
<constraint firstItem="QWR-lI-xJr" firstAttribute="top" secondItem="1" secondAttribute="top" constant="20" symbolic="YES" id="agZ-dA-Otd"/>
<constraint firstItem="His-4A-hcY" firstAttribute="baseline" secondItem="101" secondAttribute="baseline" id="fq3-rS-gvQ"/> <constraint firstItem="His-4A-hcY" firstAttribute="baseline" secondItem="101" secondAttribute="baseline" id="fq3-rS-gvQ"/>
<constraint firstItem="101" firstAttribute="leading" secondItem="8kv-BJ-IEk" secondAttribute="trailing" constant="8" symbolic="YES" id="nvi-CZ-a6L"/>
<constraint firstItem="QWR-lI-xJr" firstAttribute="centerX" secondItem="1" secondAttribute="centerX" id="vp8-Zm-JHV"/>
</constraints> </constraints>
<point key="canvasLocation" x="-26" y="-60"/> <point key="canvasLocation" x="-43" y="90"/>
</customView> </customView>
<collectionViewItem id="61"> <collectionViewItem id="61">
<connections> <connections>
@@ -96,8 +121,19 @@
<font key="font" metaFont="system"/> <font key="font" metaFont="system"/>
</buttonCell> </buttonCell>
<connections> <connections>
<action selector="_selectImage:" target="-2" id="eFg-h5-9jy"/> <binding destination="61" name="argument" keyPath="representedObject" id="GTE-gb-8ra">
<binding destination="61" name="image" keyPath="representedObject" id="139"/> <dictionary key="options">
<bool key="NSConditionallySetsEnabled" value="NO"/>
<string key="NSSelectorName">_selectIcon:</string>
</dictionary>
</binding>
<binding destination="61" name="image" keyPath="representedObject.image" id="LKC-A5-b3U"/>
<binding destination="-2" name="target" keyPath="self" previousBinding="GTE-gb-8ra" id="ujd-pu-j5v">
<dictionary key="options">
<bool key="NSConditionallySetsEnabled" value="NO"/>
<string key="NSSelectorName">_selectIcon:</string>
</dictionary>
</binding>
</connections> </connections>
</button> </button>
</subviews> </subviews>

View File

@@ -20,7 +20,10 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
@class KPKIcon;
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
/** /**
* Available IconTypes * Available IconTypes
* Every Icon after MPCustomIconTypeBegin * Every Icon after MPCustomIconTypeBegin
@@ -132,7 +135,7 @@ typedef NS_ENUM(NSUInteger, MPIconType) {
* List of all available DatabaseIcons as an array of Images. Sorted by IconIndex. * List of all available DatabaseIcons as an array of Images. Sorted by IconIndex.
* @return Array of Icons as NSImage objects * @return Array of Icons as NSImage objects
*/ */
+ (NSArray *)databaseIcons; + (NSArray<KPKIcon *> *)databaseIcons;
/** /**
* List of all available DatabaseIcons as an array of MPIconType. Sorted by IconIndex. * List of all available DatabaseIcons as an array of MPIconType. Sorted by IconIndex.

View File

@@ -21,6 +21,7 @@
// //
#import "MPIconHelper.h" #import "MPIconHelper.h"
#import "KeePassKit/KeePassKit.h"
@implementation MPIconHelper @implementation MPIconHelper
@@ -53,7 +54,9 @@
continue; // Skip all non-db Keys continue; // Skip all non-db Keys
} }
MPIconType iconType = (MPIconType)iconNumber.integerValue; MPIconType iconType = (MPIconType)iconNumber.integerValue;
[mutableIcons addObject:[MPIconHelper icon:iconType]]; KPKIcon *icon = [[KPKIcon alloc] init];
icon.image = [MPIconHelper icon:iconType];
[mutableIcons addObject:icon];
} }
icons = [mutableIcons copy]; icons = [mutableIcons copy];
}); });

View File

@@ -24,11 +24,18 @@
#import "MPIconHelper.h" #import "MPIconHelper.h"
#import "MPDocument.h" #import "MPDocument.h"
typedef NS_ENUM(NSUInteger, MPIconeSelectionType) {
MPIconSelectionDefault,
MPIconSelectionCustom
};
@interface MPIconSelectViewController () @interface MPIconSelectViewController ()
/* UI properties */ /* UI properties */
@property (weak) IBOutlet NSCollectionView *iconCollectionView; @property (weak) IBOutlet NSCollectionView *iconCollectionView;
@property (weak) IBOutlet NSButton *imageButton; @property (weak) IBOutlet NSButton *imageButton;
@property (weak) IBOutlet NSSegmentedControl *typeSelectionButton;
@property MPIconeSelectionType selectionType;
@end @end
@@ -39,10 +46,35 @@
} }
- (void)viewDidLoad { - (void)viewDidLoad {
KPKNode *node = self.representedObject;
if(!node.iconUUID) {
self.selectionType = MPIconSelectionDefault;
}
else {
self.selectionType = MPIconSelectionCustom;
}
self.typeSelectionButton.selectedSegment = self.selectionType;
self.iconCollectionView.backgroundColors = @[NSColor.clearColor]; self.iconCollectionView.backgroundColors = @[NSColor.clearColor];
self.iconCollectionView.selectable = YES; self.iconCollectionView.selectable = YES;
self.iconCollectionView.allowsMultipleSelection = NO; self.iconCollectionView.allowsMultipleSelection = NO;
self.iconCollectionView.content = [MPIconHelper databaseIcons]; [self _updateContent];
}
- (void)_updateContent {
MPDocument *document = [NSDocumentController sharedDocumentController].currentDocument;
switch(self.selectionType) {
case MPIconSelectionCustom:
self.iconCollectionView.content = document.tree.metaData.customIcons;
break;
case MPIconSelectionDefault:
default:
self.iconCollectionView.content = [MPIconHelper databaseIcons];
}
}
- (IBAction)toggleIcons:(id)sender {
self.selectionType = self.typeSelectionButton.selectedSegment;
[self _updateContent];
} }
- (IBAction)useDefault:(id)sender { - (IBAction)useDefault:(id)sender {
@@ -53,21 +85,36 @@
[self.view.window performClose:sender]; [self.view.window performClose:sender];
} }
- (IBAction)cancel:(id)sender { - (IBAction)downloadIcon:(id)sender {
[self.view.window performClose:sender];
}
- (IBAction)_selectImage:(id)sender {
NSButton *button = sender;
NSImage *image = button.image;
NSUInteger buttonIndex = [self.iconCollectionView.content indexOfObject:image];
NSInteger newIconId = ((NSNumber *)[MPIconHelper databaseIconTypes][buttonIndex]).integerValue;
KPKNode *node = self.representedObject; KPKNode *node = self.representedObject;
[self.observer willChangeModelProperty]; [self.observer willChangeModelProperty];
node.iconId = newIconId;
[self.observer didChangeModelProperty]; [self.observer didChangeModelProperty];
[self.view.window performClose:sender]; [self.view.window performClose:sender];
} }
- (IBAction)cancel:(id)sender {
[self.view.window performClose:sender];
}
- (void)_selectIcon:(KPKIcon *)icon {
KPKNode *node = self.representedObject;
[self.observer willChangeModelProperty];
switch(self.selectionType) {
case MPIconSelectionCustom:
node.iconUUID = icon.uuid;
break;
default:
case MPIconSelectionDefault: {
NSUInteger iconIndex = [self.iconCollectionView.content indexOfObject:icon];
NSInteger newIconId = ((NSNumber *)[MPIconHelper databaseIconTypes][iconIndex]).integerValue;
node.iconId = newIconId;
node.iconUUID = nil;
break;
}
}
[self.observer didChangeModelProperty];
[self.view.window performClose:nil];
}
@end @end