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.
5 #ifndef CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_VIEW_MAC_H_
6 #define CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_VIEW_MAC_H_
8 #import <Cocoa/Cocoa.h>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string16.h"
12 #include "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h"
13 #include "components/omnibox/browser/omnibox_view.h"
16 class OmniboxPopupView
;
27 // Implements OmniboxView on an AutocompleteTextField.
28 class OmniboxViewMac
: public OmniboxView
,
29 public AutocompleteTextFieldObserver
{
31 OmniboxViewMac(OmniboxEditController
* controller
,
33 CommandUpdater
* command_updater
,
34 AutocompleteTextField
* field
);
35 ~OmniboxViewMac() override
;
37 // For use when switching tabs, this saves the current state onto the tab so
38 // that it can be restored during a later call to Update().
39 void SaveStateToTab(content::WebContents
* tab
);
41 // Called when the window's active tab changes.
42 void OnTabChanged(const content::WebContents
* web_contents
);
44 // Called to clear the saved state for |web_contents|.
45 void ResetTabState(content::WebContents
* web_contents
);
48 void Update() override
;
49 void OpenMatch(const AutocompleteMatch
& match
,
50 WindowOpenDisposition disposition
,
51 const GURL
& alternate_nav_url
,
52 const base::string16
& pasted_text
,
53 size_t selected_line
) override
;
54 base::string16
GetText() const override
;
55 void SetWindowTextAndCaretPos(const base::string16
& text
,
58 bool notify_text_changed
) override
;
59 void SetForcedQuery() override
;
60 bool IsSelectAll() const override
;
61 bool DeleteAtEndPressed() override
;
62 void GetSelectionBounds(base::string16::size_type
* start
,
63 base::string16::size_type
* end
) const override
;
64 void SelectAll(bool reversed
) override
;
65 void RevertAll() override
;
66 void UpdatePopup() override
;
67 void CloseOmniboxPopup() override
;
68 void SetFocus() override
;
69 void ApplyCaretVisibility() override
;
70 void OnTemporaryTextMaybeChanged(const base::string16
& display_text
,
71 bool save_original_selection
,
72 bool notify_text_changed
) override
;
73 bool OnInlineAutocompleteTextMaybeChanged(const base::string16
& display_text
,
74 size_t user_text_length
) override
;
75 void OnInlineAutocompleteTextCleared() override
;
76 void OnRevertTemporaryText() override
;
77 void OnBeforePossibleChange() override
;
78 bool OnAfterPossibleChange() override
;
79 gfx::NativeView
GetNativeView() const override
;
80 gfx::NativeView
GetRelativeWindowForPopup() const override
;
81 void SetGrayTextAutocompletion(const base::string16
& input
) override
;
82 base::string16
GetGrayTextAutocompletion() const override
;
83 int GetTextWidth() const override
;
84 int GetWidth() const override
;
85 bool IsImeComposing() const override
;
87 // Implement the AutocompleteTextFieldObserver interface.
88 NSRange
SelectionRangeForProposedRange(NSRange proposed_range
) override
;
89 void OnControlKeyChanged(bool pressed
) override
;
90 bool CanCopy() override
;
91 void CopyToPasteboard(NSPasteboard
* pboard
) override
;
92 bool ShouldEnableShowURL() override
;
93 void ShowURL() override
;
94 void OnPaste() override
;
95 bool CanPasteAndGo() override
;
96 int GetPasteActionStringId() override
;
97 void OnPasteAndGo() override
;
98 void OnFrameChanged() override
;
99 void ClosePopup() override
;
100 void OnDidBeginEditing() override
;
101 void OnBeforeChange() override
;
102 void OnDidChange() override
;
103 void OnDidEndEditing() override
;
104 void OnInsertText() override
;
105 void OnDidDrawRect() override
;
106 bool OnDoCommandBySelector(SEL cmd
) override
;
107 void OnSetFocus(bool control_down
) override
;
108 void OnKillFocus() override
;
109 void OnMouseDown(NSInteger button_number
) override
;
110 bool ShouldSelectAllOnMouseDown() override
;
112 // Helper for LocationBarViewMac. Optionally selects all in |field_|.
113 void FocusLocation(bool select_all
);
115 // Helper to get the font to use in the field, exposed for the
117 // The style parameter specifies the new style for the font, and is a
118 // bitmask of the values: BOLD, ITALIC and UNDERLINE (see ui/gfx/font.h).
119 static NSFont
* GetFieldFont(int style
);
120 static NSFont
* GetLargeFont(int style
);
121 static NSFont
* GetSmallFont(int style
);
123 // If |resource_id| has a PDF image which can be used, return it.
124 // Otherwise return the PNG image from the resource bundle.
125 static NSImage
* ImageForResource(int resource_id
);
127 // Color used to draw suggest text.
128 static NSColor
* SuggestTextColor();
130 AutocompleteTextField
* field() const { return field_
; }
133 // Called when the user hits backspace in |field_|. Checks whether
134 // keyword search is being terminated. Returns true if the
135 // backspace should be intercepted (not forwarded on to the standard
137 bool OnBackspacePressed();
139 // Returns the field's currently selected range. Only valid if the
141 NSRange
GetSelectedRange() const;
143 // Returns the field's currently marked range. Only valid if the field has
145 NSRange
GetMarkedRange() const;
147 // Returns true if |field_| is first-responder in the window. Used
148 // in various DCHECKS to make sure code is running in appropriate
150 bool IsFirstResponder() const;
152 // If |model_| believes it has focus, grab focus if needed and set
153 // the selection to |range|. Otherwise does nothing.
154 void SetSelectedRange(const NSRange range
);
156 // Update the field with |display_text| and highlight the host and scheme (if
157 // it's an URL or URL-fragment). Resets any suggest text that may be present.
158 void SetText(const base::string16
& display_text
);
160 // Internal implementation of SetText. Does not reset the suggest text before
161 // setting the display text. Most callers should use |SetText()| instead.
162 void SetTextInternal(const base::string16
& display_text
);
164 // Update the field with |display_text| and set the selection.
165 void SetTextAndSelectedRange(const base::string16
& display_text
,
166 const NSRange range
);
168 // Pass the current content of |field_| to SetText(), maintaining
169 // any selection. Named to be consistent with GTK and Windows,
170 // though here we cannot really do the in-place operation they do.
171 void EmphasizeURLComponents() override
;
173 // Apply our font and paragraph style to |attributedString|.
174 void ApplyTextStyle(NSMutableAttributedString
* attributedString
);
176 // Calculates text attributes according to |display_text| and applies them
177 // to the given |attributedString| object.
178 void ApplyTextAttributes(const base::string16
& display_text
,
179 NSMutableAttributedString
* attributedString
);
181 // Return the number of UTF-16 units in the current buffer, excluding the
183 int GetOmniboxTextLength() const override
;
184 NSUInteger
GetTextLength() const;
186 // Returns true if the caret is at the end of the content.
187 bool IsCaretAtEnd() const;
191 scoped_ptr
<OmniboxPopupView
> popup_view_
;
193 AutocompleteTextField
* field_
; // owned by tab controller
195 // Selection at the point where the user started using the
196 // arrows to move around in the popup.
197 NSRange saved_temporary_selection_
;
199 // Tracking state before and after a possible change for reporting
201 NSRange selection_before_change_
;
202 base::string16 text_before_change_
;
203 NSRange marked_range_before_change_
;
205 // Was delete pressed?
206 bool delete_was_pressed_
;
208 // Was the delete key pressed with an empty selection at the end of the edit?
209 bool delete_at_end_pressed_
;
211 base::string16 suggest_text_
;
213 // State used to coalesce changes to text and selection to avoid drawing
215 bool in_coalesced_update_block_
;
216 bool do_coalesced_text_update_
;
217 base::string16 coalesced_text_update_
;
218 bool do_coalesced_range_update_
;
219 NSRange coalesced_range_update_
;
221 // The time of the first character insert operation that has not yet been
222 // painted. Used to measure omnibox responsiveness with a histogram.
223 base::TimeTicks insert_char_time_
;
225 DISALLOW_COPY_AND_ASSIGN(OmniboxViewMac
);
228 #endif // CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_VIEW_MAC_H_