更新選字窗元件,與 OpenVanilla 同步

This commit is contained in:
ovadmin 2017-04-30 15:54:03 +08:00 committed by Mengjuei
parent dd6ba14da5
commit d6e1ba435c
3 changed files with 48 additions and 9 deletions

View File

@ -42,6 +42,7 @@
NSArray *_keyLabels; NSArray *_keyLabels;
NSFont *_keyLabelFont; NSFont *_keyLabelFont;
NSFont *_candidateFont; NSFont *_candidateFont;
BOOL _visible;
} }
- (void)reloadData; - (void)reloadData;

View File

@ -81,6 +81,17 @@
- (void)setWindowTopLeftPoint:(NSPoint)topLeftPoint bottomOutOfScreenAdjustmentHeight:(CGFloat)height - (void)setWindowTopLeftPoint:(NSPoint)topLeftPoint bottomOutOfScreenAdjustmentHeight:(CGFloat)height
{ {
// Since layout is now deferred, the origin setting should also be deferred so that
// the correct visible frame dimensions are used.
NSArray *params = [NSArray arrayWithObjects:[NSValue valueWithPoint:topLeftPoint], [NSNumber numberWithDouble:height], nil];
[self performSelector:@selector(deferredSetWindowTopLeftPoint:) withObject:params afterDelay:0.0];
}
- (void)deferredSetWindowTopLeftPoint:(NSArray *)params
{
NSPoint topLeftPoint = [[params objectAtIndex:0] pointValue];
CGFloat height = [[params objectAtIndex:1] doubleValue];
NSPoint adjustedPoint = topLeftPoint; NSPoint adjustedPoint = topLeftPoint;
CGFloat adjustedHeight = height; CGFloat adjustedHeight = height;
@ -132,11 +143,13 @@
- (BOOL)visible - (BOOL)visible
{ {
return [[self window] isVisible]; // Because setVisible: defers its action, we need to use our own visible. Do not use [[self window] isVisible].
return _visible;
} }
- (void)setVisible:(BOOL)visible - (void)setVisible:(BOOL)visible
{ {
_visible = visible;
if (visible) { if (visible) {
[[self window] performSelector:@selector(orderFront:) withObject:self afterDelay:0.0]; [[self window] performSelector:@selector(orderFront:) withObject:self afterDelay:0.0];
} }

View File

@ -162,15 +162,24 @@ static const CGFloat kCandidateTextLeftMargin = 8.0;
return (selectedRow == -1) ? NSUIntegerMax : selectedRow; return (selectedRow == -1) ? NSUIntegerMax : selectedRow;
} }
- (void)setSelectedCandidateIndex:(NSUInteger)newIndex - (void)setSelectedCandidateIndex:(NSUInteger)aNewIndex
{ {
NSUInteger newIndex = aNewIndex;
NSInteger selectedRow = [_tableView selectedRow]; NSInteger selectedRow = [_tableView selectedRow];
NSUInteger labelCount = [_keyLabels count]; NSUInteger labelCount = [_keyLabels count];
NSUInteger itemCount = [_delegate candidateCountForController:self]; NSUInteger itemCount = [_delegate candidateCountForController:self];
NSUInteger lastVisibleRow = newIndex; if (newIndex == NSUIntegerMax) {
if (itemCount == 0) {
[_tableView deselectAll:self];
return;
}
newIndex = 0;
}
NSUInteger lastVisibleRow = newIndex;
if (selectedRow != -1 && itemCount > 0 && itemCount > labelCount) { if (selectedRow != -1 && itemCount > 0 && itemCount > labelCount) {
if (newIndex > selectedRow && (newIndex - selectedRow) > 1) { if (newIndex > selectedRow && (newIndex - selectedRow) > 1) {
lastVisibleRow = min(newIndex + labelCount - 1, itemCount - 1); lastVisibleRow = min(newIndex + labelCount - 1, itemCount - 1);
@ -237,7 +246,7 @@ static const CGFloat kCandidateTextLeftMargin = 8.0;
} }
} }
if (newHilightIndex != _keyLabelStripView.highlightedIndex) { if (newHilightIndex != _keyLabelStripView.highlightedIndex && newHilightIndex >= 0) {
_keyLabelStripView.highlightedIndex = newHilightIndex; _keyLabelStripView.highlightedIndex = newHilightIndex;
[_keyLabelStripView setNeedsDisplay:YES]; [_keyLabelStripView setNeedsDisplay:YES];
} }
@ -341,6 +350,12 @@ static const CGFloat kCandidateTextLeftMargin = 8.0;
} }
- (void)layoutCandidateView - (void)layoutCandidateView
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(doLayoutCanaditeView) object:nil];
[self performSelector:@selector(doLayoutCanaditeView) withObject:nil afterDelay:0.0];
}
- (void)doLayoutCanaditeView
{ {
NSUInteger count = [_delegate candidateCountForController:self]; NSUInteger count = [_delegate candidateCountForController:self];
if (!count) { if (!count) {
@ -383,8 +398,18 @@ static const CGFloat kCandidateTextLeftMargin = 8.0;
CGFloat rowHeight = ceil(fontSize * 1.25); CGFloat rowHeight = ceil(fontSize * 1.25);
[_tableView setRowHeight:rowHeight]; [_tableView setRowHeight:rowHeight];
CGFloat maxKeyLabelWidth = keyLabelFontSize;
NSDictionary *textAttr = [NSDictionary dictionaryWithObjectsAndKeys:
_keyLabelFont, NSFontAttributeName,
nil];
NSSize boundingBox = NSMakeSize(1600.0, 1600.0);
for (NSString *label in _keyLabels) {
NSRect rect = [label boundingRectWithSize:boundingBox options:NSStringDrawingUsesLineFragmentOrigin attributes:textAttr];
maxKeyLabelWidth = max(rect.size.width, maxKeyLabelWidth);
}
CGFloat rowSpacing = [_tableView intercellSpacing].height; CGFloat rowSpacing = [_tableView intercellSpacing].height;
CGFloat stripWidth = ceil(keyLabelFontSize * 1.20); CGFloat stripWidth = ceil(maxKeyLabelWidth * 1.20);
CGFloat tableViewStartWidth = ceil(_maxCandidateAttrStringWidth + scrollerWidth);; CGFloat tableViewStartWidth = ceil(_maxCandidateAttrStringWidth + scrollerWidth);;
CGFloat windowWidth = stripWidth + 1.0 + tableViewStartWidth; CGFloat windowWidth = stripWidth + 1.0 + tableViewStartWidth;
CGFloat windowHeight = keyLabelCount * (rowHeight + rowSpacing); CGFloat windowHeight = keyLabelCount * (rowHeight + rowSpacing);