forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ToolNotes.m
92 lines (76 loc) · 3 KB
/
ToolNotes.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
//
// ToolNotes.m
// iTerm
//
// Created by George Nachman on 9/19/11.
// Copyright 2011 Georgetech. All rights reserved.
//
#import "ToolNotes.h"
#import "NSFileManager+DirectoryLocations.h"
static NSString *kToolNotesSetTextNotification = @"kToolNotesSetTextNotification";
@interface ToolNotes ()
- (NSString *)filename;
@end
@implementation ToolNotes
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
filemanager_ = [[NSFileManager alloc] init];
NSScrollView *scrollview = [[NSScrollView alloc]
initWithFrame:NSMakeRect(0, 0, frame.size.width, frame.size.height)];
[scrollview setHasVerticalScroller:YES];
[scrollview setHasHorizontalScroller:NO];
[scrollview setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
NSSize contentSize = [scrollview contentSize];
textView_ = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, contentSize.width, contentSize.height)];
[textView_ setAllowsUndo:YES];
[textView_ setMinSize:NSMakeSize(0.0, contentSize.height)];
[textView_ setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[textView_ setVerticallyResizable:YES];
[textView_ setHorizontallyResizable:NO];
[textView_ setAutoresizingMask:NSViewWidthSizable];
[[textView_ textContainer] setContainerSize:NSMakeSize(contentSize.width, FLT_MAX)];
[[textView_ textContainer] setWidthTracksTextView:YES];
[textView_ setDelegate:self];
[textView_ readRTFDFromFile:[self filename]];
[scrollview setDocumentView:textView_];
[self addSubview:scrollview];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(setText:)
name:kToolNotesSetTextNotification
object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[textView_ writeRTFDToFile:[self filename] atomically:NO];
[filemanager_ release];
[super dealloc];
}
- (NSString *)filename
{
return [NSString stringWithFormat:@"%@/notes.rtfd", [filemanager_ applicationSupportDirectory]];
}
- (void)textDidChange:(NSNotification *)aNotification
{
// Avoid saving huge files because of the slowdown it would cause.
if ([[textView_ textStorage] length] < 100 * 1024) {
[textView_ writeRTFDToFile:[self filename] atomically:NO];
ignoreNotification_ = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:kToolNotesSetTextNotification
object:nil];
ignoreNotification_ = NO;
}
[textView_ breakUndoCoalescing];
}
- (void)setText:(NSNotification *)aNotification
{
if (!ignoreNotification_) {
[textView_ readRTFDFromFile:[self filename]];
}
}
- (void)shutdown {
}
@end