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 3// found in the LICENSE file. 4 5#ifndef CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_POPUP_VIEW_MAC_H_ 6#define CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_POPUP_VIEW_MAC_H_ 7 8#import <Cocoa/Cocoa.h> 9 10#include "base/basictypes.h" 11#include "base/mac/scoped_nsobject.h" 12#include "base/memory/scoped_ptr.h" 13#include "chrome/browser/autocomplete/autocomplete_match.h" 14#import "chrome/browser/ui/cocoa/omnibox/omnibox_popup_matrix.h" 15#include "chrome/browser/ui/omnibox/omnibox_popup_view.h" 16#include "ui/gfx/font.h" 17 18class AutocompleteResult; 19class OmniboxEditModel; 20class OmniboxPopupModel; 21class OmniboxView; 22 23// Implements OmniboxPopupView using a raw NSWindow containing an 24// NSTableView. 25class OmniboxPopupViewMac : public OmniboxPopupView, 26 public OmniboxPopupMatrixDelegate { 27 public: 28 OmniboxPopupViewMac(OmniboxView* omnibox_view, 29 OmniboxEditModel* edit_model, 30 NSTextField* field); 31 virtual ~OmniboxPopupViewMac(); 32 33 // Overridden from OmniboxPopupView: 34 virtual bool IsOpen() const OVERRIDE; 35 virtual void InvalidateLine(size_t line) OVERRIDE {} 36 virtual void UpdatePopupAppearance() OVERRIDE; 37 virtual gfx::Rect GetTargetBounds() OVERRIDE; 38 // This is only called by model in SetSelectedLine() after updating 39 // everything. Popup should already be visible. 40 virtual void PaintUpdatesNow() OVERRIDE; 41 virtual void OnDragCanceled() OVERRIDE {} 42 43 // Overridden from OmniboxPopupMatrixDelegate: 44 virtual void OnMatrixRowSelected(OmniboxPopupMatrix* matrix, 45 size_t row) OVERRIDE; 46 virtual void OnMatrixRowClicked(OmniboxPopupMatrix* matrix, 47 size_t row) OVERRIDE; 48 virtual void OnMatrixRowMiddleClicked(OmniboxPopupMatrix* matrix, 49 size_t row) OVERRIDE; 50 51 OmniboxPopupMatrix* matrix() { return matrix_; } 52 53 // Return the text to show for the match, based on the match's 54 // contents and description. Result will be in |font|, with the 55 // boldfaced version used for matches. 56 static NSAttributedString* MatchText(const AutocompleteMatch& match, 57 gfx::Font& font, 58 float cell_width); 59 60 // Applies the given font and colors to the match string based on 61 // classifications. 62 static NSMutableAttributedString* DecorateMatchedString( 63 const base::string16& match_string, 64 const AutocompleteMatch::ACMatchClassifications& classifications, 65 NSColor* text_color, 66 NSColor* dim_text_color, 67 gfx::Font& font); 68 69 // Helper for MatchText() to elide a marked-up string using 70 // gfx::ElideText() as a model. Modifies |a_string| in place. 71 // TODO(shess): Consider breaking AutocompleteButtonCell out of this 72 // code, and modifying it to have something like -setMatch:, so that 73 // these convolutions to expose internals for testing can be 74 // cleaner. 75 static NSMutableAttributedString* ElideString( 76 NSMutableAttributedString* a_string, 77 const base::string16& original_string, 78 const gfx::Font& font, 79 const float cell_width); 80 81 protected: 82 // Gets the autocomplete results. This is virtual so that it can be overriden 83 // by tests. 84 virtual const AutocompleteResult& GetResult() const; 85 86 private: 87 // Create the popup_ instance if needed. 88 void CreatePopupIfNeeded(); 89 90 // Calculate the appropriate position for the popup based on the 91 // field's screen position and the given target for the matrix 92 // height, and makes the popup visible. Animates to the new frame 93 // if the popup shrinks, snaps to the new frame if the popup grows, 94 // allows existing animations to continue if the size doesn't 95 // change. 96 void PositionPopup(const CGFloat matrixHeight); 97 98 // Returns the NSImage that should be used as an icon for the given match. 99 NSImage* ImageForMatch(const AutocompleteMatch& match); 100 101 // Opens the URL at the given row. 102 void OpenURLForRow(size_t row, WindowOpenDisposition disposition); 103 104 OmniboxView* omnibox_view_; 105 scoped_ptr<OmniboxPopupModel> model_; 106 NSTextField* field_; // owned by tab controller 107 108 // Child window containing a matrix which implements the popup. 109 base::scoped_nsobject<NSWindow> popup_; 110 NSRect target_popup_frame_; 111 112 base::scoped_nsobject<OmniboxPopupMatrix> matrix_; 113 base::scoped_nsobject<NSView> top_separator_view_; 114 base::scoped_nsobject<NSView> bottom_separator_view_; 115 base::scoped_nsobject<NSBox> background_view_; 116 117 DISALLOW_COPY_AND_ASSIGN(OmniboxPopupViewMac); 118}; 119 120#endif // CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_POPUP_VIEW_MAC_H_ 121