added MPCollectionView to track index for context menu

This commit is contained in:
michael starke
2017-09-19 16:05:35 +02:00
parent c444c27a98
commit c80daac682
6 changed files with 82 additions and 5 deletions

View File

@@ -23,7 +23,7 @@
<rect key="frame" x="0.0" y="0.0" width="380" height="270"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<collectionView focusRingType="none" selectable="YES" id="58">
<collectionView focusRingType="none" selectable="YES" id="58" customClass="MPCollectionView">
<rect key="frame" x="0.0" y="0.0" width="380" height="270"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="primaryBackgroundColor" name="windowBackgroundColor" catalog="System" colorSpace="catalog"/>

View File

@@ -46,8 +46,7 @@ typedef NS_ENUM(NSUInteger, MPActionType) {
MPActionShowEntryHistory, // show history
MPActionHideEntryHistory, // exit history
MPActionPerformAutotypeForSelectedEntry, // Perform Autotype for selected Entry
MPActionRemoveAttachment, // Remove an attachment
MPActionDeleteCustomIcon // delte a custom icon
MPActionRemoveAttachment // Remove an attachment
};
/**
* Helper to retrieve commonly used actions

View File

@@ -0,0 +1,15 @@
//
// MPCollectionView.h
// MacPass
//
// Created by Michael Starke on 18.09.17.
// Copyright © 2017 HicknHack Software GmbH. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface MPCollectionView : NSCollectionView
@property NSUInteger contextMenuIndex; // the index the context menu was last opened. NSNotFound if invalid
@end

View File

@@ -0,0 +1,44 @@
//
// MPCollectionView.m
// MacPass
//
// Created by Michael Starke on 18.09.17.
// Copyright © 2017 HicknHack Software GmbH. All rights reserved.
//
#import "MPCollectionView.h"
@implementation MPCollectionView
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if(self) {
_contextMenuIndex = NSNotFound;
}
return self;
}
- (instancetype)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if(self) {
_contextMenuIndex = NSNotFound;
}
return self;
}
- (NSMenu *)menuForEvent:(NSEvent *)event {
self.contextMenuIndex = NSNotFound;
NSPoint point = [self convertPoint:event.locationInWindow fromView:nil];
NSUInteger count = self.content.count;
for (NSUInteger i = 0; i < count; i++) {
NSRect itemFrame = [self frameForItemAtIndex:i];
if (NSMouseInRect(point, itemFrame, self.isFlipped)) {
self.contextMenuIndex = i;
break;
}
}
return [super menuForEvent:event];
}
@end

View File

@@ -23,12 +23,13 @@
#import "MPIconSelectViewController.h"
#import "MPIconHelper.h"
#import "MPDocument.h"
#import "MPCollectionView.h"
#import "MPCollectionViewItem.h"
@interface MPIconSelectViewController () <NSCollectionViewDelegate>
/* UI properties */
@property (weak) IBOutlet NSCollectionView *iconCollectionView;
@property (weak) IBOutlet MPCollectionView *iconCollectionView;
@property (weak) IBOutlet NSButton *imageButton;
@end
@@ -46,6 +47,10 @@
self.iconCollectionView.delegate = self;
[self.iconCollectionView registerForDraggedTypes:@[(NSString *)kUTTypeURL, (NSString *)kUTTypeFileURL]];
NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
[menu addItem:[[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"DELETE", @"") action:@selector(deleteIcon:) keyEquivalent:@""]];
self.iconCollectionView.menu = menu;
[self _updateCollectionViewContent];
}
@@ -95,7 +100,15 @@
}
- (void)deleteIcon:(id)sender {
NSUInteger index = self.iconCollectionView.contextMenuIndex;
NSUInteger firstCustomIndex = [MPIconHelper databaseIcons].count;
if(index < firstCustomIndex) {
return;
}
MPDocument *document = [NSDocumentController sharedDocumentController].currentDocument;
KPKIcon *icon = self.iconCollectionView.content[index];
[document.tree.metaData removeCustomIcon:icon];
[self _updateCollectionViewContent];
}