Archived
1
This repository has been archived on 2022-08-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
mathpad/MathPad/MPLayout.m
2014-08-22 00:54:13 +02:00

133 lines
2.5 KiB
Objective-C

//
// MPLayout.m
// MathPad
//
// Created by Kim Wittenburg on 11.08.14.
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
//
#import "MPLayout.h"
@interface MPLayout ()
// Querying and Storing Caches
- (BOOL)hasCacheForElementAtIndex:(NSUInteger)index;
- (void)ensureCacheSizeForIndex:(NSUInteger)index;
@end
@implementation MPLayout {
NSMutableArray *_cache;
NSSize _cachedSize;
}
#pragma mark Creation Methods
- (id)init
{
self = [super init];
if (self) {
_cache = [[NSMutableArray alloc] init];
_cachedSize = NSZeroSize;
}
return self;
}
- (id)initWithPath:(NSIndexPath *)path
parent:(MPLayout *)parent
{
self = [self init];
if (self) {
_parent = parent;
}
return self;
}
#pragma Text System Objects
- (MPExpressionStorage *)expressionStorage
{
return self.parent.expressionStorage;
}
#pragma mark Cache Tree
// Querying and Storing Caches
- (BOOL)hasCacheForElementAtIndex:(NSUInteger)index
{
if (index >= _cache.count) {
return NO;
}
return _cache[index] != MPNull;
}
- (id)cachableObjectForIndex:(NSUInteger)index
generator:(id (^)())generator
{
if ([self hasCacheForElementAtIndex:index]) {
return _cache[index];
}
id object = generator();
[self ensureCacheSizeForIndex:index];
_cache[index] = object;
return object;
}
- (void)ensureCacheSizeForIndex:(NSUInteger)index
{
while (index >= _cache.count) {
[_cache addObject:MPNull];
}
}
// Clearing Caches
- (void)clearCacheInRange:(NSRange)range
replacementLength:(NSUInteger)replacementLength
{
NSMutableArray *placeholders = [[NSMutableArray alloc] initWithCapacity:replacementLength];
while (placeholders.count < replacementLength) {
[placeholders addObject:MPNull];
}
[_cache replaceObjectsInRange:range
withObjectsFromArray:placeholders];
[self invalidate];
}
- (void)invalidate
{
_cachedSize = NSZeroSize;
[self.parent invalidate];
}
#pragma mark Calculation and Drawing Methods
- (NSSize)size
{
if (NSEqualSizes(_cachedSize, NSZeroSize)) {
_cachedSize = [self generateSize];
}
return _cachedSize;
}
- (void)drawAtPoint:(NSPoint)point
{
}
@end
@implementation MPLayout (MPSubclassImplement)
- (MPLayout *)childLayoutAtIndex:(NSUInteger)index
{
return nil;
}
- (NSSize)sizeForChildAtIndex:(NSUInteger)index
{
return NSZeroSize;
}
- (NSSize)generateSize
{
return NSZeroSize;
}
@end