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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.h"
#include "base/logging.h"
#import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_cell.h"
namespace {
// NSEvent -buttonNumber for middle mouse button.
const NSInteger kMiddleButtonNumber = 2;
} // namespace
@interface OmniboxPopupMatrix()
- (void)resetTrackingArea;
- (void)highlightRowAt:(NSInteger)rowIndex;
- (void)highlightRowUnder:(NSEvent*)theEvent;
- (BOOL)selectCellForEvent:(NSEvent*)theEvent;
@end
@implementation OmniboxPopupMatrix
- (id)initWithDelegate:(OmniboxPopupMatrixDelegate*)delegate {
if ((self = [super initWithFrame:NSZeroRect])) {
delegate_ = delegate;
[self setCellClass:[OmniboxPopupCell class]];
// Cells pack with no spacing.
[self setIntercellSpacing:NSMakeSize(0.0, 0.0)];
[self setDrawsBackground:YES];
[self setBackgroundColor:[NSColor controlBackgroundColor]];
[self renewRows:0 columns:1];
[self setAllowsEmptySelection:YES];
[self setMode:NSRadioModeMatrix];
[self deselectAllCells];
[self resetTrackingArea];
}
return self;
}
- (void)setDelegate:(OmniboxPopupMatrixDelegate*)delegate {
delegate_ = delegate;
}
- (NSInteger)highlightedRow {
NSArray* cells = [self cells];
const NSUInteger count = [cells count];
for(NSUInteger i = 0; i < count; ++i) {
if ([[cells objectAtIndex:i] isHighlighted]) {
return i;
}
}
return -1;
}
- (void)updateTrackingAreas {
[self resetTrackingArea];
[super updateTrackingAreas];
}
// Callbacks from tracking area.
- (void)mouseEntered:(NSEvent*)theEvent {
[self highlightRowUnder:theEvent];
}
- (void)mouseMoved:(NSEvent*)theEvent {
[self highlightRowUnder:theEvent];
}
- (void)mouseExited:(NSEvent*)theEvent {
[self highlightRowAt:-1];
}
// The tracking area events aren't forwarded during a drag, so handle
// highlighting manually for middle-click and middle-drag.
- (void)otherMouseDown:(NSEvent*)theEvent {
if ([theEvent buttonNumber] == kMiddleButtonNumber) {
[self highlightRowUnder:theEvent];
}
[super otherMouseDown:theEvent];
}
- (void)otherMouseDragged:(NSEvent*)theEvent {
if ([theEvent buttonNumber] == kMiddleButtonNumber) {
[self highlightRowUnder:theEvent];
}
[super otherMouseDragged:theEvent];
}
- (void)otherMouseUp:(NSEvent*)theEvent {
// Only intercept middle button.
if ([theEvent buttonNumber] != kMiddleButtonNumber) {
[super otherMouseUp:theEvent];
return;
}
// -otherMouseDragged: should always have been called at this location, but
// make sure the user is getting the right feedback.
[self highlightRowUnder:theEvent];
const NSInteger highlightedRow = [self highlightedRow];
if (highlightedRow != -1) {
DCHECK(delegate_);
delegate_->OnMatrixRowMiddleClicked(self, highlightedRow);
}
}
// Track the mouse until released, keeping the cell under the mouse selected.
// If the mouse wanders off-view, revert to the originally-selected cell. If
// the mouse is released over a cell, call the delegate to open the row's URL.
- (void)mouseDown:(NSEvent*)theEvent {
NSCell* selectedCell = [self selectedCell];
// Clear any existing highlight.
[self highlightRowAt:-1];
do {
if (![self selectCellForEvent:theEvent]) {
[self selectCell:selectedCell];
}
const NSUInteger mask = NSLeftMouseUpMask | NSLeftMouseDraggedMask;
theEvent = [[self window] nextEventMatchingMask:mask];
} while ([theEvent type] == NSLeftMouseDragged);
// Do not message the delegate if released outside view.
if (![self selectCellForEvent:theEvent]) {
[self selectCell:selectedCell];
} else {
const NSInteger selectedRow = [self selectedRow];
// No row could be selected if the model failed to update.
if (selectedRow == -1) {
NOTREACHED();
return;
}
DCHECK(delegate_);
delegate_->OnMatrixRowClicked(self, selectedRow);
}
}
- (void)resetTrackingArea {
if (trackingArea_.get())
[self removeTrackingArea:trackingArea_.get()];
trackingArea_.reset([[CrTrackingArea alloc]
initWithRect:[self frame]
options:NSTrackingMouseEnteredAndExited |
NSTrackingMouseMoved |
NSTrackingActiveInActiveApp |
NSTrackingInVisibleRect
owner:self
userInfo:nil]);
[self addTrackingArea:trackingArea_.get()];
}
- (void)highlightRowAt:(NSInteger)rowIndex {
// highlightCell will be nil if rowIndex is out of range, so no cell will be
// highlighted.
NSCell* highlightCell = [self cellAtRow:rowIndex column:0];
for (NSCell* cell in [self cells]) {
[cell setHighlighted:(cell == highlightCell)];
}
}
- (void)highlightRowUnder:(NSEvent*)theEvent {
NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil];
NSInteger row, column;
if ([self getRow:&row column:&column forPoint:point]) {
[self highlightRowAt:row];
} else {
[self highlightRowAt:-1];
}
}
// Select cell under |theEvent|, returning YES if a selection is made.
- (BOOL)selectCellForEvent:(NSEvent*)theEvent {
NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil];
NSInteger row, column;
if ([self getRow:&row column:&column forPoint:point]) {
DCHECK_EQ(column, 0);
DCHECK(delegate_);
delegate_->OnMatrixRowSelected(self, row);
return YES;
}
return NO;
}
@end
|