Archived
1

Improved Functions Template Chooser

This commit is contained in:
Kim Wittenburg
2014-09-30 22:19:08 +02:00
parent a382b1f10b
commit 8f2f773909
3 changed files with 79 additions and 65 deletions

View File

@@ -24,7 +24,7 @@
@interface MPExpressionView ()
@property (nonatomic, weak) NSButton *functionsButton;
@property (nonatomic, strong) NSButton *functionsButton;
@property (nonatomic, strong) NSPopover *functionsPopover;
@property (nonatomic, strong) MPFunctionsViewController *functionsViewController;
@@ -312,30 +312,31 @@
expressionStorage.expressionView = self;
_expressionStorage = expressionStorage;
NSBundle *frameworkBundle = [NSBundle bundleForClass:[self class]];
NSImage *image = [frameworkBundle imageForResource:@"FunctionsButtonDisclosure"];
[image setName:@"FunctionsButtonDisclosure"];
// Setup the Functions Button
NSButton *button = [[NSButton alloc] initWithFrame:NSZeroRect];
button.target = self;
button.action = @selector(showFunctions:);
button.buttonType = NSMomentaryChangeButton;
button.bezelStyle = NSShadowlessSquareBezelStyle;
button.bordered = NO;
NSFont *font = [NSFont fontWithName:@"Times New Roman" size:25.0];
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:@"Σ" attributes:@{NSFontAttributeName: font, NSForegroundColorAttributeName: [NSColor colorWithWhite:.61 alpha:1]}];
button.attributedTitle = attributedTitle;
button.imagePosition = NSImageLeft;
button.image = image;
self.functionsButton = button;
[self addSubview:self.functionsButton];
[self initializeButtons];
// Setup Selection
self.selection = [[MPRangePath alloc] initWithRange:NSMakeRange(0, 0)];
self.caretBlinkRate = 1.0;
[self restartCaretTimer];
}
- (void)initializeButtons
{
NSButton *functionsButton = self.functionsButton;
[self addSubview:functionsButton];
NSDictionary *variableBindings = NSDictionaryOfVariableBindings(functionsButton);
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[functionsButton]-10-|"
options:0
metrics:nil
views:variableBindings]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:functionsButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];
}
#pragma mark Properties
- (void)setExpressionStorage:(MPExpressionStorage *)expressionStorage
{
@@ -358,6 +359,29 @@
self.needsDisplay = YES;
}
- (NSButton *)functionsButton
{
if (!_functionsButton) {
NSBundle *frameworkBundle = [NSBundle bundleForClass:[self class]];
NSImage *image = [frameworkBundle imageForResource:@"FunctionsButtonDisclosure"];
[image setName:@"FunctionsButtonDisclosure"];
NSButton *button = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
button.translatesAutoresizingMaskIntoConstraints = NO;
button.target = self;
button.action = @selector(showFunctions:);
button.buttonType = NSMomentaryChangeButton;
button.bezelStyle = NSShadowlessSquareBezelStyle;
button.bordered = NO;
NSFont *font = [NSFont fontWithName:@"Times New Roman" size:25.0];
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:@"Σ" attributes:@{NSFontAttributeName: font, NSForegroundColorAttributeName: [NSColor colorWithWhite:.61 alpha:1]}];
button.attributedTitle = attributedTitle;
button.imagePosition = NSImageLeft;
button.image = image;
_functionsButton = button;
}
return _functionsButton;
}
#pragma mark Actions
- (void)showFunctions:(id)sender
{
@@ -429,16 +453,6 @@
[super setFrame:frameRect];
}
- (void)layout
{
NSSize buttonSize = [self.functionsButton fittingSize];
self.functionsButton.frame = NSMakeRect(self.bounds.size.width - buttonSize.width - 10,
(self.bounds.size.height - buttonSize.height) / 2,
buttonSize.width,
buttonSize.height);
[super layout];
}
- (NSSize)intrinsicContentSize
{
NSSize size = self.expressionStorage.rootLayout.bounds.size;
@@ -458,7 +472,7 @@
NSString *characters = theEvent.characters;
if ([characters isEqualToString:@"("]) {
[self insertParenthesisFunction];
[self insertParenthesisFunction:nil];
return;
}
@@ -466,7 +480,7 @@
NSString *decimalSeparator = [NSRegularExpression escapedPatternForString:[[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator]];
NSMutableCharacterSet *allowedCharacters = [NSMutableCharacterSet alphanumericCharacterSet];
[allowedCharacters addCharactersInString:[NSString stringWithFormat:@"+-*= %@", decimalSeparator]];
[allowedCharacters addCharactersInString:[NSString stringWithFormat:@"+-*= !%@", decimalSeparator]];
if (characters.length == 1 && [characters stringByTrimmingCharactersInSet:allowedCharacters].length == 0) {
[self.expressionStorage replaceSymbolsInRangePath:self.selection withElements:@[characters]];
@@ -476,7 +490,7 @@
}
}
- (void)insertParenthesisFunction
- (void)insertParenthesisFunction:(id)sender
{
MPExpression *selectedElementsExpression = [self.expressionStorage subexpressionWithRangePath:self.selection];
MPParenthesisFunction *function = [[MPParenthesisFunction alloc] init];
@@ -700,7 +714,7 @@
MPExpression *newTargetExpression = [self.expressionStorage elementAtIndexPath:newTargetExpressionPath];
NSIndexPath *newSelectionLocation = [functionPath indexPathByReplacingLastIndexWithIndex:[newTargetExpression locationOfElementAtIndex:functionPath.lastIndex]];
[self.expressionStorage replaceSymbolsInRangePath:MPMakeRangePath(functionPath, 1) withElements:remainder];
[self.expressionStorage replaceSymbolsInRangePath:MPMakeRangePath(newSelectionLocation, 1) withElements:remainder];
self.selection = MPMakeRangePath(newSelectionLocation, 0);
}
}