Improved Functions Template Chooser
This commit is contained in:
@@ -14,11 +14,11 @@
|
||||
#import "MPParenthesisFunction.h"
|
||||
|
||||
|
||||
@class MPFunctionTemplatePrototype;
|
||||
@class MPFunctionTemplateItem;
|
||||
|
||||
@interface MPFunctionsCollectionView : NSCollectionView
|
||||
|
||||
@property (nonatomic, weak) MPFunctionTemplatePrototype *hoverItem;
|
||||
@property (nonatomic, weak) MPFunctionTemplateItem *hoverItem;
|
||||
|
||||
@property (nonatomic, weak) id target;
|
||||
@property (nonatomic) SEL action;
|
||||
@@ -37,7 +37,8 @@
|
||||
|
||||
|
||||
|
||||
@interface MPFunctionTemplatePrototype : NSCollectionViewItem
|
||||
@interface MPFunctionTemplateItem : NSCollectionViewItem
|
||||
@property (nonatomic, copy) NSString *templateName;
|
||||
@end
|
||||
|
||||
|
||||
@@ -56,7 +57,7 @@
|
||||
if (NSMouseInRect(pointInView, viewItem.view.frame, self.isFlipped)) {
|
||||
if (self.target && self.action) {
|
||||
[self.target performSelector:self.action
|
||||
withObject:[viewItem.representedObject copy]
|
||||
withObject:[[viewItem.representedObject objectForKey:@"function" ] copy]
|
||||
afterDelay:0.0];
|
||||
}
|
||||
break;
|
||||
@@ -103,6 +104,7 @@
|
||||
@synthesize functionTemplateLayout = _functionTemplateLayout;
|
||||
- (MPFunctionLayout *)functionTemplateLayout
|
||||
{
|
||||
|
||||
if (!_functionTemplateLayout) {
|
||||
_functionTemplateLayout = [MPFunctionLayout functionLayoutForFunction:self.functionTemplate
|
||||
parent:nil];
|
||||
@@ -140,21 +142,13 @@
|
||||
|
||||
|
||||
|
||||
@implementation MPFunctionTemplatePrototype
|
||||
@implementation MPFunctionTemplateItem
|
||||
|
||||
static void *MPFunctionTemplateViewMouseOverContext = @"MPFunctionTemplateViewMouseOverContext";
|
||||
|
||||
- (id)initWithCoder:(NSCoder *)aDecoder
|
||||
- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder
|
||||
{
|
||||
self = [super initWithCoder:aDecoder];
|
||||
if (self) {
|
||||
MPFunctionTemplateView *view = (MPFunctionTemplateView *)self.view;
|
||||
[view addObserver:self
|
||||
forKeyPath:@"mouseOver"
|
||||
options:0
|
||||
context:MPFunctionTemplateViewMouseOverContext];
|
||||
}
|
||||
return self;
|
||||
return [super awakeAfterUsingCoder:aDecoder];
|
||||
}
|
||||
|
||||
- (void)observeValueForKeyPath:(NSString *)keyPath
|
||||
@@ -175,8 +169,14 @@ static void *MPFunctionTemplateViewMouseOverContext = @"MPFunctionTemplateViewMo
|
||||
|
||||
- (void)setRepresentedObject:(id)representedObject
|
||||
{
|
||||
MPFunctionTemplateView *view = (MPFunctionTemplateView *)self.view;
|
||||
view.functionTemplate = [representedObject objectForKey:@"function"];
|
||||
self.templateName = [representedObject objectForKey:@"name"];
|
||||
[view addObserver:self
|
||||
forKeyPath:@"mouseOver"
|
||||
options:0
|
||||
context:MPFunctionTemplateViewMouseOverContext];
|
||||
[super setRepresentedObject:representedObject];
|
||||
((MPFunctionTemplateView *)self.view).functionTemplate = representedObject;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -195,21 +195,14 @@ static void *MPFunctionTemplateViewMouseOverContext = @"MPFunctionTemplateViewMo
|
||||
bundle:[NSBundle bundleForClass:[self class]]];
|
||||
}
|
||||
|
||||
- (id)initWithNibName:(NSString *)nibNameOrNil
|
||||
bundle:(NSBundle *)nibBundleOrNil
|
||||
- (void)awakeFromNib
|
||||
{
|
||||
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
|
||||
if (self) {
|
||||
self.functionPrototypes = @[[[MPSumFunction alloc] init], [[MPParenthesisFunction alloc] init]];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
static void *MPCollectionViewHoverItemChangeContext = @"MPCollectionViewHoverItemChangeContext";
|
||||
|
||||
- (void)loadView
|
||||
{
|
||||
[super loadView];
|
||||
self.functionPrototypes = @[
|
||||
@{@"function": [[MPSumFunction alloc] init],
|
||||
@"name": NSLocalizedString(@"Sum", @"Sum Function Name")},
|
||||
@{@"function": [[MPParenthesisFunction alloc] init],
|
||||
@"name": NSLocalizedString(@"Parenthesis", @"Parenthesis Function Name")}
|
||||
];
|
||||
[self.collectionView addObserver:self
|
||||
forKeyPath:@"hoverItem"
|
||||
options:0
|
||||
@@ -218,6 +211,13 @@ static void *MPCollectionViewHoverItemChangeContext = @"MPCollectionViewHoverIte
|
||||
self.collectionView.action = self.action;
|
||||
}
|
||||
|
||||
static void *MPCollectionViewHoverItemChangeContext = @"MPCollectionViewHoverItemChangeContext";
|
||||
|
||||
- (void)setView:(NSView *)view
|
||||
{
|
||||
[super setView:view];
|
||||
}
|
||||
|
||||
- (void)setTarget:(id)target
|
||||
{
|
||||
_target = target;
|
||||
@@ -240,7 +240,7 @@ static void *MPCollectionViewHoverItemChangeContext = @"MPCollectionViewHoverIte
|
||||
context:(void *)context
|
||||
{
|
||||
if (context == MPCollectionViewHoverItemChangeContext) {
|
||||
self.currentDescription = [[self.collectionView.hoverItem.representedObject class] performSelector:@selector(localizedFunctionName)];
|
||||
self.currentDescription = self.collectionView.hoverItem.templateName;
|
||||
} else {
|
||||
[super observeValueForKeyPath:keyPath
|
||||
ofObject:object
|
||||
|
||||
Reference in New Issue
Block a user