forked from eczarny/spectacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpectacleHistory.m
92 lines (63 loc) · 2.24 KB
/
SpectacleHistory.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#import "SpectacleHistory.h"
#import "SpectacleHistoryItem.h"
#import "SpectacleConstants.h"
@interface SpectacleHistory (SpectacleHistoryPrivate)
- (SpectacleHistoryItem *)moveCurrentHistoryItemToHistoryItem: (SpectacleHistoryItem *)historyItem;
@end
#pragma mark -
@implementation SpectacleHistory
- (id)init {
if ((self = [super init])) {
firstHistoryItem = nil;
lastHistoryItem = firstHistoryItem;
currentHistoryItem = firstHistoryItem;
size = 0;
}
return self;
}
#pragma mark -
- (void)addHistoryItem: (SpectacleHistoryItem *)historyItem {
if ([self isEmpty]) {
currentHistoryItem = historyItem;
firstHistoryItem = currentHistoryItem;
lastHistoryItem = firstHistoryItem;
} else {
[historyItem setNextHistoryItem: [currentHistoryItem nextHistoryItem]];
[historyItem setPreviousHistoryItem: currentHistoryItem];
[[currentHistoryItem nextHistoryItem] setPreviousHistoryItem: historyItem];
[currentHistoryItem setNextHistoryItem: historyItem];
if (![historyItem nextHistoryItem]) {
firstHistoryItem = historyItem;
}
if (![historyItem previousHistoryItem]) {
lastHistoryItem = historyItem;
}
currentHistoryItem = historyItem;
}
if (++size >= SpectacleWindowActionHistorySize) {
[[lastHistoryItem nextHistoryItem] setPreviousHistoryItem: [lastHistoryItem previousHistoryItem]];
lastHistoryItem = [lastHistoryItem nextHistoryItem];
size--;
}
}
#pragma mark -
- (SpectacleHistoryItem *)nextHistoryItem {
return [self moveCurrentHistoryItemToHistoryItem: [currentHistoryItem nextHistoryItem]];
}
- (SpectacleHistoryItem *)previousHistoryItem {
return [self moveCurrentHistoryItemToHistoryItem: [currentHistoryItem previousHistoryItem]];
}
#pragma mark -
- (BOOL)isEmpty {
return size == 0;
}
@end
#pragma mark -
@implementation SpectacleHistory (SpectacleHistoryPrivate)
- (SpectacleHistoryItem *)moveCurrentHistoryItemToHistoryItem: (SpectacleHistoryItem *)historyItem {
if (historyItem) {
currentHistoryItem = historyItem;
}
return historyItem;
}
@end