Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(391)

Side by Side Diff: chrome/browser/autocomplete/autocomplete_popup_view_mac.mm

Issue 63047: Revert "Initial implemention of Mac Omnibox." (Closed)
Patch Set: Created 11 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/autocomplete/autocomplete_popup_view_mac.h"
6
7 #include "base/sys_string_conversions.h"
8 #include "chrome/browser/autocomplete/autocomplete_edit.h"
9 #include "chrome/browser/autocomplete/autocomplete_edit_view_mac.h"
10 #include "chrome/browser/autocomplete/autocomplete_popup_model.h"
11
12 // Thin Obj-C bridge class between the target and data source of the
13 // popup window's NSTableView and the AutocompletePopupView
14 // implementation.
15
16 @interface AutocompleteTableTarget : NSObject {
17 @private
18 AutocompletePopupViewMac* popup_view_; // weak, owns us.
19 }
20 - initWithPopupView:(AutocompletePopupViewMac*)view;
21
22 // Tell popup model via popup_view_ about the selected row.
23 - (void)select:sender;
24
25 // NSTableDataSource methods, filled from data returned by
26 // the popup model via popup_view_.
27 - (NSInteger)numberOfRowsInTableView:(NSTableView*)aTableView;
28 - (id)tableView:(NSTableView*)aTableView
29 objectValueForTableColumn:(NSTableColumn*)aTableColumn
30 row:(int)ri;
31
32 // Placeholder for finding the star image.
33 - (NSImage*)starImage;
34 @end
35
36 AutocompletePopupViewMac::AutocompletePopupViewMac(
37 AutocompleteEditViewMac* edit_view,
38 AutocompleteEditModel* edit_model,
39 Profile* profile)
40 : model_(new AutocompletePopupModel(this, edit_model, profile)),
41 edit_view_(edit_view),
42 field_(nil),
43 table_target_([[AutocompleteTableTarget alloc] initWithPopupView:this]),
44 popup_(nil) {
45 DCHECK(edit_view);
46 DCHECK(edit_model);
47 DCHECK(profile);
48 edit_model->set_popup_model(model_.get());
49 }
50
51 AutocompletePopupViewMac::~AutocompletePopupViewMac() {
52 // TODO(shess): Having to be aware of destructor ordering in this
53 // way seems brittle. There must be a better way.
54
55 // Destroy the popup model before this object is destroyed, because
56 // it can call back to us in the destructor.
57 model_.reset();
58
59 // Break references to table_target_ before it is released.
60 NSTableView* table = [popup_ contentView];
61 [table setTarget:nil];
62 [table setDataSource:nil];
63 }
64
65 bool AutocompletePopupViewMac::IsOpen() const {
66 return [popup_ isVisible] ? true : false;
67 }
68
69 static NSTableColumn* MakeTableColumn(int tag, Class field_class) {
70 NSNumber* id = [NSNumber numberWithInt:tag];
71 NSTableColumn* col =
72 [[[NSTableColumn alloc] initWithIdentifier:id] autorelease];
73
74 [col setEditable:NO];
75 [col setResizingMask:NSTableColumnNoResizing];
76 [col setDataCell:[[[field_class alloc] init] autorelease]];
77
78 return col;
79 }
80
81 void AutocompletePopupViewMac::CreatePopupIfNeeded() {
82 if (!popup_) {
83 popup_.reset([[NSWindow alloc] initWithContentRect:NSZeroRect
84 styleMask:NSBorderlessWindowMask
85 backing:NSBackingStoreBuffered
86 defer:YES]);
87 [popup_ setMovableByWindowBackground:NO];
88 [popup_ setOpaque:YES];
89 [popup_ setHasShadow:YES];
90 [popup_ setLevel:NSNormalWindowLevel];
91
92 NSTableView* table =
93 [[[NSTableView alloc] initWithFrame:NSZeroRect] autorelease];
94 [popup_ setContentView:table];
95
96 [table setTarget:table_target_];
97 [table setAction:@selector(select:)];
98 [table setHeaderView:nil];
99 [table setDataSource:table_target_];
100 [table setIntercellSpacing:NSMakeSize(1.0, 0.0)];
101
102 [table addTableColumn:MakeTableColumn(0, [NSTextFieldCell class])];
103 [table addTableColumn:MakeTableColumn(1, [NSImageCell class])];
104 [table addTableColumn:MakeTableColumn(2, [NSTextFieldCell class])];
105 }
106 }
107
108 void AutocompletePopupViewMac::UpdatePopupAppearance() {
109 const AutocompleteResult& result = model_->result();
110 if (result.empty()) {
111 [[popup_ parentWindow] removeChildWindow:popup_];
112 [popup_ orderOut:nil];
113 return;
114 }
115
116 CreatePopupIfNeeded();
117
118 // Layout the popup and size it to land underneath the field.
119 // TODO(shess) Consider refactoring to remove this depenency,
120 // because the popup doesn't need any of the field-like aspects of
121 // field_. The edit view could expose helper methods for attaching
122 // the window to the field.
123 NSRect r = [field_ bounds];
124 r = [field_ convertRectToBase:r];
125 r.origin = [[field_ window] convertBaseToScreen:r.origin];
126
127 // TODO(shess): Derive this from the actual image size, once the
128 // image is in the project.
129 static const int kStarWidth = 25;
130
131 NSArray* cols = [[popup_ contentView] tableColumns];
132 [[cols objectAtIndex:0] setWidth:floor((r.size.width - kStarWidth) / 2)];
133 [[cols objectAtIndex:1] setWidth:kStarWidth];
134 [[cols objectAtIndex:2] setWidth:ceil((r.size.width - kStarWidth) / 2)];
135
136 [[popup_ contentView] reloadData];
137 [[popup_ contentView] tile];
138 r.size.height = [[popup_ contentView] frame].size.height;
139 r.origin.y -= r.size.height + 2;
140
141 // Update the selection.
142 PaintUpdatesNow();
143
144 [popup_ setFrame:r display:YES];
145
146 if (!IsOpen()) {
147 [[field_ window] addChildWindow:popup_ ordered:NSWindowAbove];
148 }
149 }
150
151 // This is only called by model in SetSelectedLine() after updating
152 // everything. Popup should already be visible.
153 void AutocompletePopupViewMac::PaintUpdatesNow() {
154 NSIndexSet* set = [NSIndexSet indexSetWithIndex:model_->selected_line()];
155 NSTableView* table = [popup_ contentView];
156 [table selectRowIndexes:set byExtendingSelection:NO];
157 }
158
159 void AutocompletePopupViewMac::StopAutocomplete() {
160 model_->StopAutocomplete();
161 }
162
163 size_t AutocompletePopupViewMac::ResultRowCount() {
164 return model_->result().size();
165 }
166
167 const std::wstring& AutocompletePopupViewMac::ResultContentsAt(size_t i) {
168 return model_->result().match_at(i).contents;
169 }
170
171 bool AutocompletePopupViewMac::ResultStarredAt(size_t i) {
172 return model_->result().match_at(i).starred;
173 }
174
175 const std::wstring& AutocompletePopupViewMac::ResultDescriptionAt(size_t i) {
176 return model_->result().match_at(i).description;
177 }
178
179 void AutocompletePopupViewMac::AcceptInput(
180 WindowOpenDisposition disposition, bool for_drop) {
181 edit_view_->AcceptInput(disposition, for_drop);
182 }
183
184 @implementation AutocompleteTableTarget
185
186 - initWithPopupView:(AutocompletePopupViewMac*)view {
187 self = [super init];
188 if (self) {
189 popup_view_ = view;
190 }
191 return self;
192 }
193
194 - (NSImage*)starImage {
195 return [NSImage imageNamed:@"starred.pdf"];
196 }
197
198 - (NSInteger)numberOfRowsInTableView:(NSTableView*)aTableView {
199 DCHECK(popup_view_);
200 return static_cast<NSInteger>(popup_view_->ResultRowCount());
201 }
202
203 - (id)tableView:(NSTableView*)aTableView
204 objectValueForTableColumn:(NSTableColumn*)aTableColumn
205 row:(int)ri {
206 int columnIndex = [[aTableColumn identifier] integerValue];
207 size_t rowIndex = static_cast<size_t>(ri);
208 DCHECK(popup_view_);
209 DCHECK_LT(rowIndex, popup_view_->ResultRowCount());
210 DCHECK_LT(columnIndex, 3);
211
212 if (columnIndex == 1) {
213 if (popup_view_->ResultStarredAt(rowIndex)) {
214 return [self starImage];
215 }
216 return nil;
217 }
218
219 NSString* s;
220 if (columnIndex == 0) {
221 s = base::SysWideToNSString(popup_view_->ResultContentsAt(rowIndex));
222 } else {
223 s = base::SysWideToNSString(popup_view_->ResultDescriptionAt(rowIndex));
224 }
225
226 NSMutableParagraphStyle* style =
227 [[[NSMutableParagraphStyle alloc] init] autorelease];
228 [style setLineBreakMode:NSLineBreakByTruncatingTail];
229
230 NSMutableAttributedString* as =
231 [[[NSMutableAttributedString alloc] initWithString:s] autorelease];
232 [as addAttribute:NSParagraphStyleAttributeName value:style
233 range:NSMakeRange(0, [s length])];
234
235 // TODO(shess): There is a ton more styling to be done, here, for
236 // instance URLs different from search suggestions different from secure
237 // URLs, etc. [See AutocompletePopupViewMac::UpdateAndStyleText().]
238 // Deferring in the interests of getting a minimal implementation in.
239
240 return as;
241 }
242
243 - (void)select:sender {
244 DCHECK(popup_view_);
245 popup_view_->AcceptInput(CURRENT_TAB, false);
246 }
247
248 @end
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/autocomplete_popup_view_mac.h ('k') | chrome/browser/cocoa/location_bar_view_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698