| 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_view_mac.h" | 5 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" |
| 6 | 6 |
| 7 #include <Carbon/Carbon.h> // kVK_Return | 7 #include <Carbon/Carbon.h> // kVK_Return |
| 8 | 8 |
| 9 #include "base/property_bag.h" | |
| 10 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 11 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
| 12 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete_input.h" | 12 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 14 #include "chrome/browser/autocomplete/autocomplete_match.h" | 13 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 15 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/ui/cocoa/event_utils.h" | 15 #include "chrome/browser/ui/cocoa/event_utils.h" |
| 17 #include "chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac.h" | 16 #include "chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac.h" |
| 18 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h" | 17 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h" |
| 19 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" | 18 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 NSColor* SuggestTextColor() { | 77 NSColor* SuggestTextColor() { |
| 79 return [NSColor grayColor]; | 78 return [NSColor grayColor]; |
| 80 } | 79 } |
| 81 NSColor* SecureSchemeColor() { | 80 NSColor* SecureSchemeColor() { |
| 82 return ColorWithRGBBytes(0x07, 0x95, 0x00); | 81 return ColorWithRGBBytes(0x07, 0x95, 0x00); |
| 83 } | 82 } |
| 84 NSColor* SecurityErrorSchemeColor() { | 83 NSColor* SecurityErrorSchemeColor() { |
| 85 return ColorWithRGBBytes(0xa2, 0x00, 0x00); | 84 return ColorWithRGBBytes(0xa2, 0x00, 0x00); |
| 86 } | 85 } |
| 87 | 86 |
| 87 const char kOmniboxViewMacStateKey[] = "OmniboxViewMacState"; |
| 88 |
| 88 // Store's the model and view state across tab switches. | 89 // Store's the model and view state across tab switches. |
| 89 struct OmniboxViewMacState { | 90 struct OmniboxViewMacState : public base::SupportsUserData::Data { |
| 90 OmniboxViewMacState(const OmniboxEditModel::State model_state, | 91 OmniboxViewMacState(const OmniboxEditModel::State model_state, |
| 91 const bool has_focus, | 92 const bool has_focus, |
| 92 const NSRange& selection) | 93 const NSRange& selection) |
| 93 : model_state(model_state), | 94 : model_state(model_state), |
| 94 has_focus(has_focus), | 95 has_focus(has_focus), |
| 95 selection(selection) { | 96 selection(selection) { |
| 96 } | 97 } |
| 98 virtual ~OmniboxViewMacState() {} |
| 97 | 99 |
| 98 const OmniboxEditModel::State model_state; | 100 const OmniboxEditModel::State model_state; |
| 99 const bool has_focus; | 101 const bool has_focus; |
| 100 const NSRange selection; | 102 const NSRange selection; |
| 101 }; | 103 }; |
| 102 | 104 |
| 103 // Returns a lazily initialized property bag accessor for saving our | |
| 104 // state in a WebContents. When constructed |accessor| generates a | |
| 105 // globally-unique id used to index into the per-tab PropertyBag used | |
| 106 // to store the state data. | |
| 107 base::PropertyAccessor<OmniboxViewMacState>* GetStateAccessor() { | |
| 108 CR_DEFINE_STATIC_LOCAL( | |
| 109 base::PropertyAccessor<OmniboxViewMacState>, accessor, ()); | |
| 110 return &accessor; | |
| 111 } | |
| 112 | |
| 113 // Accessors for storing and getting the state from the tab. | 105 // Accessors for storing and getting the state from the tab. |
| 114 void StoreStateToTab(WebContents* tab, | 106 void StoreStateToTab(WebContents* tab, |
| 115 const OmniboxViewMacState& state) { | 107 OmniboxViewMacState* state) { |
| 116 GetStateAccessor()->SetProperty(tab->GetPropertyBag(), state); | 108 tab->SetUserData(kOmniboxViewMacStateKey, state); |
| 117 } | 109 } |
| 118 const OmniboxViewMacState* GetStateFromTab(const WebContents* tab) { | 110 const OmniboxViewMacState* GetStateFromTab(const WebContents* tab) { |
| 119 return GetStateAccessor()->GetProperty(tab->GetPropertyBag()); | 111 return static_cast<OmniboxViewMacState*>( |
| 112 tab->GetUserData(&kOmniboxViewMacStateKey)); |
| 120 } | 113 } |
| 121 | 114 |
| 122 // Helper to make converting url_parse ranges to NSRange easier to | 115 // Helper to make converting url_parse ranges to NSRange easier to |
| 123 // read. | 116 // read. |
| 124 NSRange ComponentToNSRange(const url_parse::Component& component) { | 117 NSRange ComponentToNSRange(const url_parse::Component& component) { |
| 125 return NSMakeRange(static_cast<NSInteger>(component.begin), | 118 return NSMakeRange(static_cast<NSInteger>(component.begin), |
| 126 static_cast<NSInteger>(component.len)); | 119 static_cast<NSInteger>(component.len)); |
| 127 } | 120 } |
| 128 | 121 |
| 129 } // namespace | 122 } // namespace |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 173 |
| 181 NSRange range; | 174 NSRange range; |
| 182 if (hasFocus) { | 175 if (hasFocus) { |
| 183 range = GetSelectedRange(); | 176 range = GetSelectedRange(); |
| 184 } else { | 177 } else { |
| 185 // If we are not focussed, there is no selection. Manufacture | 178 // If we are not focussed, there is no selection. Manufacture |
| 186 // something reasonable in case it starts to matter in the future. | 179 // something reasonable in case it starts to matter in the future. |
| 187 range = NSMakeRange(0, GetTextLength()); | 180 range = NSMakeRange(0, GetTextLength()); |
| 188 } | 181 } |
| 189 | 182 |
| 190 OmniboxViewMacState state(model()->GetStateForTabSwitch(), hasFocus, range); | 183 OmniboxViewMacState* state = |
| 184 new OmniboxViewMacState(model()->GetStateForTabSwitch(), hasFocus, range); |
| 191 StoreStateToTab(tab, state); | 185 StoreStateToTab(tab, state); |
| 192 } | 186 } |
| 193 | 187 |
| 194 void OmniboxViewMac::Update(const WebContents* tab_for_state_restoring) { | 188 void OmniboxViewMac::Update(const WebContents* tab_for_state_restoring) { |
| 195 // TODO(shess): It seems like if the tab is non-NULL, then this code | 189 // TODO(shess): It seems like if the tab is non-NULL, then this code |
| 196 // shouldn't need to be called at all. When coded that way, I find | 190 // shouldn't need to be called at all. When coded that way, I find |
| 197 // that the field isn't always updated correctly. Figure out why | 191 // that the field isn't always updated correctly. Figure out why |
| 198 // this is. Maybe this method should be refactored into more | 192 // this is. Maybe this method should be refactored into more |
| 199 // specific cases. | 193 // specific cases. |
| 200 bool user_visible = model()->UpdatePermanentText(toolbar_model()->GetText()); | 194 bool user_visible = model()->UpdatePermanentText(toolbar_model()->GetText()); |
| (...skipping 783 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 984 NSUInteger OmniboxViewMac::GetTextLength() const { | 978 NSUInteger OmniboxViewMac::GetTextLength() const { |
| 985 return ([field_ currentEditor] ? | 979 return ([field_ currentEditor] ? |
| 986 [[[field_ currentEditor] string] length] : | 980 [[[field_ currentEditor] string] length] : |
| 987 [[field_ stringValue] length]) - suggest_text_length_; | 981 [[field_ stringValue] length]) - suggest_text_length_; |
| 988 } | 982 } |
| 989 | 983 |
| 990 bool OmniboxViewMac::IsCaretAtEnd() const { | 984 bool OmniboxViewMac::IsCaretAtEnd() const { |
| 991 const NSRange selection = GetSelectedRange(); | 985 const NSRange selection = GetSelectedRange(); |
| 992 return selection.length == 0 && selection.location == GetTextLength(); | 986 return selection.length == 0 && selection.location == GetTextLength(); |
| 993 } | 987 } |
| OLD | NEW |