| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac.h" | 5 #include "chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "base/mac/mac_util.h" | 9 #include "base/mac/mac_util.h" |
| 10 #import "base/mac/sdk_forward_declarations.h" | 10 #import "base/mac/sdk_forward_declarations.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 #include "ui/gfx/color_palette.h" | 29 #include "ui/gfx/color_palette.h" |
| 30 #include "ui/gfx/geometry/rect.h" | 30 #include "ui/gfx/geometry/rect.h" |
| 31 #include "ui/gfx/image/image_skia_util_mac.h" | 31 #include "ui/gfx/image/image_skia_util_mac.h" |
| 32 #include "ui/gfx/paint_vector_icon.h" | 32 #include "ui/gfx/paint_vector_icon.h" |
| 33 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 33 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
| 34 #include "ui/gfx/text_elider.h" | 34 #include "ui/gfx/text_elider.h" |
| 35 #include "ui/gfx/vector_icons_public.h" | 35 #include "ui/gfx/vector_icons_public.h" |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 const int kPopupPaddingVertical = 5; | |
| 40 const int kMaterialPopupPaddingVertical = 4; | 39 const int kMaterialPopupPaddingVertical = 4; |
| 41 | 40 |
| 42 // Padding between matrix and the top and bottom of the popup window. | 41 // Padding between matrix and the top and bottom of the popup window. |
| 43 CGFloat PopupPaddingVertical() { | 42 CGFloat PopupPaddingVertical() { |
| 44 if (!ui::MaterialDesignController::IsModeMaterial()) { | |
| 45 return kPopupPaddingVertical; | |
| 46 } | |
| 47 return kMaterialPopupPaddingVertical; | 43 return kMaterialPopupPaddingVertical; |
| 48 } | 44 } |
| 49 | 45 |
| 50 // Animation duration when animating the popup window smaller. | 46 // Animation duration when animating the popup window smaller. |
| 51 const NSTimeInterval kShrinkAnimationDuration = 0.1; | 47 const NSTimeInterval kShrinkAnimationDuration = 0.1; |
| 52 | 48 |
| 53 } // namespace | 49 } // namespace |
| 54 | 50 |
| 55 OmniboxPopupViewMac::OmniboxPopupViewMac(OmniboxView* omnibox_view, | 51 OmniboxPopupViewMac::OmniboxPopupViewMac(OmniboxView* omnibox_view, |
| 56 OmniboxEditModel* edit_model, | 52 OmniboxEditModel* edit_model, |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 defer:NO]); | 174 defer:NO]); |
| 179 [popup_ setBackgroundColor:[NSColor clearColor]]; | 175 [popup_ setBackgroundColor:[NSColor clearColor]]; |
| 180 [popup_ setOpaque:NO]; | 176 [popup_ setOpaque:NO]; |
| 181 | 177 |
| 182 // Use a flipped view to pin the matrix top the top left. This is needed | 178 // Use a flipped view to pin the matrix top the top left. This is needed |
| 183 // for animated resize. | 179 // for animated resize. |
| 184 base::scoped_nsobject<FlippedView> contentView( | 180 base::scoped_nsobject<FlippedView> contentView( |
| 185 [[FlippedView alloc] initWithFrame:NSZeroRect]); | 181 [[FlippedView alloc] initWithFrame:NSZeroRect]); |
| 186 [popup_ setContentView:contentView]; | 182 [popup_ setContentView:contentView]; |
| 187 | 183 |
| 188 BOOL is_dark_theme = ui::MaterialDesignController::IsModeMaterial() && | 184 BOOL is_dark_theme = [[field_ window] hasDarkTheme]; |
| 189 [[field_ window] hasDarkTheme]; | |
| 190 | 185 |
| 191 // View to draw a background beneath the matrix. | 186 // View to draw a background beneath the matrix. |
| 192 background_view_.reset([[NSBox alloc] initWithFrame:NSZeroRect]); | 187 background_view_.reset([[NSBox alloc] initWithFrame:NSZeroRect]); |
| 193 [background_view_ setBoxType:NSBoxCustom]; | 188 [background_view_ setBoxType:NSBoxCustom]; |
| 194 [background_view_ setBorderType:NSNoBorder]; | 189 [background_view_ setBorderType:NSNoBorder]; |
| 195 [background_view_ setFillColor:BackgroundColor(is_dark_theme)]; | 190 [background_view_ setFillColor:BackgroundColor(is_dark_theme)]; |
| 196 [background_view_ setContentViewMargins:NSZeroSize]; | 191 [background_view_ setContentViewMargins:NSZeroSize]; |
| 197 [contentView addSubview:background_view_]; | 192 [contentView addSubview:background_view_]; |
| 198 | 193 |
| 199 matrix_.reset([[OmniboxPopupMatrix alloc] initWithObserver:this | 194 matrix_.reset([[OmniboxPopupMatrix alloc] initWithObserver:this |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 background_rect.size.width = NSWidth(popup_frame); | 249 background_rect.size.width = NSWidth(popup_frame); |
| 255 background_rect.size.height = NSHeight(popup_frame) - | 250 background_rect.size.height = NSHeight(popup_frame) - |
| 256 NSHeight(top_separator_frame) - NSHeight(bottom_separator_frame); | 251 NSHeight(top_separator_frame) - NSHeight(bottom_separator_frame); |
| 257 background_rect.origin.y = NSMaxY(top_separator_frame); | 252 background_rect.origin.y = NSMaxY(top_separator_frame); |
| 258 [background_view_ setFrame:background_rect]; | 253 [background_view_ setFrame:background_rect]; |
| 259 | 254 |
| 260 // In Material Design, the table is the width of the window. In non-MD, | 255 // In Material Design, the table is the width of the window. In non-MD, |
| 261 // calculate the width of the table based on backing out the popup's border | 256 // calculate the width of the table based on backing out the popup's border |
| 262 // from the width of the field. | 257 // from the width of the field. |
| 263 CGFloat table_width = NSWidth([[[field_ window] contentView] bounds]); | 258 CGFloat table_width = NSWidth([[[field_ window] contentView] bounds]); |
| 264 bool is_mode_material = ui::MaterialDesignController::IsModeMaterial(); | |
| 265 if (!is_mode_material) { | |
| 266 table_width = NSWidth([field_ bounds]); | |
| 267 } | |
| 268 DCHECK_GT(table_width, 0.0); | 259 DCHECK_GT(table_width, 0.0); |
| 269 | 260 |
| 270 // Matrix. | 261 // Matrix. |
| 271 NSPoint field_origin_base = | 262 NSPoint field_origin_base = |
| 272 [field_ convertPoint:[field_ bounds].origin toView:nil]; | 263 [field_ convertPoint:[field_ bounds].origin toView:nil]; |
| 273 NSRect matrix_frame = NSZeroRect; | 264 NSRect matrix_frame = NSZeroRect; |
| 274 matrix_frame.origin.x = 0; | 265 matrix_frame.origin.x = 0; |
| 275 if (!is_mode_material) { | 266 [matrix_ setContentLeftPadding:field_origin_base.x]; |
| 276 matrix_frame.origin.x = field_origin_base.x - NSMinX(anchor_rect_base); | |
| 277 } else { | |
| 278 [matrix_ setContentLeftPadding:field_origin_base.x]; | |
| 279 } | |
| 280 matrix_frame.origin.y = PopupPaddingVertical(); | 267 matrix_frame.origin.y = PopupPaddingVertical(); |
| 281 matrix_frame.size.width = table_width; | 268 matrix_frame.size.width = table_width; |
| 282 matrix_frame.size.height = matrixHeight; | 269 matrix_frame.size.height = matrixHeight; |
| 283 [matrix_ setFrame:matrix_frame]; | 270 [matrix_ setFrame:matrix_frame]; |
| 284 [[[matrix_ tableColumns] objectAtIndex:0] setWidth:table_width]; | 271 [[[matrix_ tableColumns] objectAtIndex:0] setWidth:table_width]; |
| 285 | 272 |
| 286 // Don't play animation games on first display. | 273 // Don't play animation games on first display. |
| 287 target_popup_frame_ = popup_frame; | 274 target_popup_frame_ = popup_frame; |
| 288 if (![popup_ parentWindow]) { | 275 if (![popup_ parentWindow]) { |
| 289 DCHECK(![popup_ isVisible]); | 276 DCHECK(![popup_ isVisible]); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 [popup_ setAnimations:savedAnimations]; | 322 [popup_ setAnimations:savedAnimations]; |
| 336 } | 323 } |
| 337 } | 324 } |
| 338 | 325 |
| 339 NSImage* OmniboxPopupViewMac::ImageForMatch( | 326 NSImage* OmniboxPopupViewMac::ImageForMatch( |
| 340 const AutocompleteMatch& match) const { | 327 const AutocompleteMatch& match) const { |
| 341 gfx::Image image = model_->GetIconIfExtensionMatch(match); | 328 gfx::Image image = model_->GetIconIfExtensionMatch(match); |
| 342 if (!image.IsEmpty()) | 329 if (!image.IsEmpty()) |
| 343 return image.AsNSImage(); | 330 return image.AsNSImage(); |
| 344 | 331 |
| 345 if (!ui::MaterialDesignController::IsModeMaterial()) { | |
| 346 const int resource_id = model_->IsStarredMatch(match) | |
| 347 ? IDR_OMNIBOX_STAR | |
| 348 : AutocompleteMatch::TypeToIcon(match.type); | |
| 349 return OmniboxViewMac::ImageForResource(resource_id); | |
| 350 } | |
| 351 bool is_dark_mode = [matrix_ hasDarkTheme]; | 332 bool is_dark_mode = [matrix_ hasDarkTheme]; |
| 352 const SkColor icon_color = | 333 const SkColor icon_color = |
| 353 is_dark_mode ? SkColorSetA(SK_ColorWHITE, 0xCC) : gfx::kChromeIconGrey; | 334 is_dark_mode ? SkColorSetA(SK_ColorWHITE, 0xCC) : gfx::kChromeIconGrey; |
| 354 const gfx::VectorIconId vector_icon_id = | 335 const gfx::VectorIconId vector_icon_id = |
| 355 model_->IsStarredMatch(match) | 336 model_->IsStarredMatch(match) |
| 356 ? gfx::VectorIconId::LOCATION_BAR_STAR | 337 ? gfx::VectorIconId::LOCATION_BAR_STAR |
| 357 : AutocompleteMatch::TypeToVectorIcon(match.type); | 338 : AutocompleteMatch::TypeToVectorIcon(match.type); |
| 358 const int kIconSize = 16; | 339 const int kIconSize = 16; |
| 359 return NSImageFromImageSkia( | 340 return NSImageFromImageSkia( |
| 360 gfx::CreateVectorIcon(vector_icon_id, kIconSize, icon_color)); | 341 gfx::CreateVectorIcon(vector_icon_id, kIconSize, icon_color)); |
| 361 } | 342 } |
| 362 | 343 |
| 363 void OmniboxPopupViewMac::OpenURLForRow(size_t row, | 344 void OmniboxPopupViewMac::OpenURLForRow(size_t row, |
| 364 WindowOpenDisposition disposition) { | 345 WindowOpenDisposition disposition) { |
| 365 DCHECK_LT(row, GetResult().size()); | 346 DCHECK_LT(row, GetResult().size()); |
| 366 omnibox_view_->OpenMatch(GetResult().match_at(row), disposition, GURL(), | 347 omnibox_view_->OpenMatch(GetResult().match_at(row), disposition, GURL(), |
| 367 base::string16(), row); | 348 base::string16(), row); |
| 368 } | 349 } |
| OLD | NEW |