forked from mikecsh/mbtablegrid
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MBAutoCompleteWindow.m
176 lines (135 loc) · 5.55 KB
/
MBAutoCompleteWindow.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// MBAutoCompleteWindow.m
// Tap Forms Mac
//
// Created by Brendan Duddridge on 2017-12-25.
// Copyright © 2017 Tap Zapp Software Inc. All rights reserved.
//
#import "MBAutoCompleteWindow.h"
@interface MBAutoCompleteWindow()<NSTableViewDelegate, NSTableViewDataSource>
@property (nonatomic, strong) NSTableView *tableView;
@property (nonatomic, strong) NSScrollView *scrollView;
@end
@implementation MBAutoCompleteWindow
@synthesize completions = _completions;
- (instancetype)initWithContentRect:(NSRect)contentRect styleMask:(NSWindowStyleMask)style backing:(NSBackingStoreType)backingStoreType defer:(BOOL)flag {
MBAutoCompleteWindow *this = [super initWithContentRect:contentRect styleMask:style backing:backingStoreType defer:flag];
this.hasShadow = YES;
this.backgroundColor = [NSColor windowBackgroundColor];
return this;
}
- (void)setCompletions:(NSArray<NSString *> *)completions {
_completions = completions;
if (!self.tableView) {
NSTableColumn *column1 = [[NSTableColumn alloc] initWithIdentifier:@"text"];
[column1 setEditable:NO];
[column1 setWidth:self.frame.size.width - 15];
NSRect bounds = self.frame;
bounds.origin.x = 0;
bounds.origin.y = 0;
NSTableView *tableView = [[NSTableView alloc] initWithFrame:bounds];
self.tableView = tableView;
[tableView setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleRegular];
[tableView setBackgroundColor:[NSColor clearColor]];
[tableView setIntercellSpacing:NSMakeSize(12, 0)];
[tableView setHeaderView:nil];
[tableView setRefusesFirstResponder:NO];
[tableView addTableColumn:column1];
[tableView setDelegate:self];
[tableView setDataSource:self];
NSScrollView *tableScrollView = [[NSScrollView alloc] initWithFrame:bounds];
[tableScrollView setDrawsBackground:NO];
[tableScrollView setDocumentView:tableView];
[tableScrollView setHasVerticalScroller:YES];
tableScrollView.autoresizesSubviews = YES;
self.scrollView = tableScrollView;
[self.contentView addSubview:tableScrollView];
self.contentView.autoresizesSubviews = YES;
[self.tableView scrollToBeginningOfDocument:nil];
}
[self.tableView setFrameSize:self.frame.size];
[self.scrollView setFrameSize:self.frame.size];
[self.tableView reloadData];
[self.scrollView flashScrollers];
}
- (void)moveRowDown:(id)sender {
NSInteger selectedRow = self.tableView.selectedRow;
if (selectedRow < 0 || selectedRow == NSNotFound) {
selectedRow = 0;
} else if (self.tableView.selectedRow < self.completions.count) {
selectedRow++;
}
if (selectedRow < self.completions.count) {
[self.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:selectedRow] byExtendingSelection:NO];
[self.tableView scrollRowToVisible:selectedRow];
[self makeFirstResponder:self.tableView];
}
NSLog(@"movedown in table");
}
- (void)moveRowUp:(id)sender {
NSInteger selectedRow = self.tableView.selectedRow;
if (selectedRow <= 0 || selectedRow == NSNotFound) {
selectedRow = 0;
} else if (self.tableView.selectedRow < self.completions.count) {
selectedRow--;
}
[self.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:selectedRow] byExtendingSelection:NO];
[self.tableView scrollRowToVisible:selectedRow];
NSLog(@"moveup in table");
}
#pragma mark - table delegates
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return self.completions.count;
}
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row {
return 21;
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSString *string = nil;
if (row < self.completions.count) {
string = self.completions[row];
}
NSTableCellView *cellView = [tableView makeViewWithIdentifier:@"AutoCompleteCell" owner:self];
if (cellView == nil) {
cellView = [[NSTableCellView alloc] initWithFrame:NSZeroRect];
NSTextField *textField = [[NSTextField alloc] initWithFrame:NSZeroRect];
textField.textColor = [NSColor controlTextColor];
textField.font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSControlSizeRegular]];
textField.bezeled = NO;
textField.drawsBackground = NO;
textField.editable = NO;
textField.selectable = NO;
textField.lineBreakMode = NSLineBreakByTruncatingTail;
[cellView addSubview:textField];
cellView.autoresizesSubviews = YES;
textField.translatesAutoresizingMaskIntoConstraints = NO;
[cellView.leadingAnchor constraintEqualToAnchor:textField.leadingAnchor].active = YES;
[cellView.topAnchor constraintEqualToAnchor:textField.topAnchor].active = YES;
[cellView.trailingAnchor constraintEqualToAnchor:textField.trailingAnchor].active = YES;
[cellView.bottomAnchor constraintEqualToAnchor:textField.bottomAnchor].active = YES;
cellView.textField = textField;
cellView.identifier = @"AutoCompleteCell";
}
cellView.textField.objectValue = string;
return cellView;
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification {
if ([self.selectionDelegate respondsToSelector:@selector(didSelectValue:)]) {
NSInteger completionIndex = self.tableView.selectedRow;
if (completionIndex < self.completions.count) {
NSString *value = self.completions[completionIndex];
[self.selectionDelegate didSelectValue:value];
}
}
}
#pragma mark - delegate methods
- (void)insert:(id)sender {
if ([self.selectionDelegate respondsToSelector:@selector(didSelectValue:)]) {
NSInteger completionIndex = self.tableView.selectedRow;
if (completionIndex < self.completions.count) {
NSString *value = self.completions[completionIndex];
[self.selectionDelegate didSelectValue:value];
}
}
}
@end