Archived
1

Cleaned Code

This commit is contained in:
Kim Wittenburg
2014-10-21 15:06:57 +02:00
parent 5592128926
commit 91e7dbe9f2
20 changed files with 126 additions and 92 deletions

View File

@@ -12,6 +12,7 @@
#import "MPFunctionLayout.h"
#import "MPPowerFunction.h"
#import "MPFractionFunction.h"
#import "MPRangePath.h"
@@ -91,10 +92,8 @@
- (void)restartCaretTimer
{
if (self.caretTimer) {
if ([self.caretTimer isValid]) {
[self.caretTimer invalidate];
}
if (self.caretTimer && [self.caretTimer isValid]) {
[self.caretTimer invalidate];
}
self.caretTimer = [NSTimer scheduledTimerWithTimeInterval:self.caretBlinkRate/2 target:self selector:@selector(updateCaret:) userInfo:nil repeats:YES];
self.caretVisible = NO;
@@ -104,7 +103,11 @@
- (void)updateCaret:(NSTimer *)timer
{
self.caretVisible = !self.caretVisible;
self.needsDisplay = YES;
NSRect updatedRect = self.selectionRect;
NSPoint expressionOrigin = self.expressionOrigin;
updatedRect.origin.x += expressionOrigin.x;
updatedRect.origin.y += expressionOrigin.y;
[self setNeedsDisplayInRect:updatedRect];
}
- (NSRect)selectionRect
@@ -576,6 +579,10 @@
[self insertPowerFunction];
return;
}
if ([characters isEqualToString:@"/"]) {
[self insertFractionFunction];
return;
}
NSString *decimalSeparator = [NSRegularExpression escapedPatternForString:[[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator]];
NSMutableCharacterSet *allowedCharacters = [NSMutableCharacterSet alphanumericCharacterSet];
@@ -630,6 +637,23 @@
self.selection = MPMakeRangePath([[functionPath indexPathByAddingIndex:0] indexPathByAddingIndex:0], self.selection.length);
}
- (void)insertFractionFunction
{
MPExpression *selectedElementsExpression = [self.expressionStorage subexpressionWithRangePath:self.selection
referenceFrame:MPSymbolReferenceFrame];
MPFractionFunction *function = [[MPFractionFunction alloc] init];
function.nominatorExpression = selectedElementsExpression;
[self.expressionStorage replaceItemsInRangePath:self.selection
referenceFrame:MPSymbolReferenceFrame
withElements:@[function]];
MPExpression *targetExpression = [self.expressionStorage elementAtIndexPath:[self.selection.location indexPathByRemovingLastIndex]];
NSUInteger functionElementIndex = [targetExpression convertIndex:self.selection.location.lastIndex
fromReferenceFrame:MPSymbolReferenceFrame
toReferenceFrame:MPElementReferenceFrame];
NSIndexPath *functionPath = [self.selection.location indexPathByReplacingLastIndexWithIndex:functionElementIndex];
self.selection = MPMakeRangePath([[functionPath indexPathByAddingIndex:1] indexPathByAddingIndex:0], 0);
}
- (void)insertNewline:(id)sender
{
if (self.target && self.action) {
@@ -900,22 +924,22 @@
[transform concat];
// Draw the error
if (self.error) {
[[NSColor redColor] set];
NSRect rect = [self.expressionStorage.rootLayout boundingRectForRangePath:self.error.rangePath];
if (self.error.rangePath.length == 0) {
NSBezierPath *bezierPath = [NSBezierPath bezierPath];
[bezierPath moveToPoint:rect.origin];
[bezierPath lineToPoint:NSMakePoint(rect.origin.x - 5, rect.origin.y - 5)];
[bezierPath moveToPoint:rect.origin];
[bezierPath lineToPoint:NSMakePoint(rect.origin.x + 5, rect.origin.y - 5)];
bezierPath.lineWidth = 2.0;
[bezierPath stroke];
} else {
NSRect underlineRect = NSMakeRect(rect.origin.x, rect.origin.y + 2, rect.size.width, 2);
NSRectFill(underlineRect);
}
}
// if (self.error) {
// [[NSColor redColor] set];
// NSRect rect = [self.expressionStorage.rootLayout boundingRectForRangePath:self.error.rangePath];
// if (self.error.rangePath.length == 0) {
// NSBezierPath *bezierPath = [NSBezierPath bezierPath];
// [bezierPath moveToPoint:rect.origin];
// [bezierPath lineToPoint:NSMakePoint(rect.origin.x - 5, rect.origin.y - 5)];
// [bezierPath moveToPoint:rect.origin];
// [bezierPath lineToPoint:NSMakePoint(rect.origin.x + 5, rect.origin.y - 5)];
// bezierPath.lineWidth = 2.0;
// [bezierPath stroke];
// } else {
// NSRect underlineRect = NSMakeRect(rect.origin.x, rect.origin.y + 2, rect.size.width, 2);
// NSRectFill(underlineRect);
// }
// }
// Draw the selection
if (self.caretVisible || self.selection.length > 0) {