Chromium Code Reviews| 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 #ifndef UI_VIEWS_FOCUS_FOCUS_MANAGER_H_ | 5 #ifndef UI_VIEWS_FOCUS_FOCUS_MANAGER_H_ |
| 6 #define UI_VIEWS_FOCUS_FOCUS_MANAGER_H_ | 6 #define UI_VIEWS_FOCUS_FOCUS_MANAGER_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <map> | 9 #include <map> |
| 10 | 10 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 // No change to focus state has occurred yet when this function is called. | 113 // No change to focus state has occurred yet when this function is called. |
| 114 virtual void OnWillChangeFocus(View* focused_before, View* focused_now) = 0; | 114 virtual void OnWillChangeFocus(View* focused_before, View* focused_now) = 0; |
| 115 | 115 |
| 116 // Called after focus state has changed. | 116 // Called after focus state has changed. |
| 117 virtual void OnDidChangeFocus(View* focused_before, View* focused_now) = 0; | 117 virtual void OnDidChangeFocus(View* focused_before, View* focused_now) = 0; |
| 118 | 118 |
| 119 protected: | 119 protected: |
| 120 virtual ~FocusChangeListener() {} | 120 virtual ~FocusChangeListener() {} |
| 121 }; | 121 }; |
| 122 | 122 |
| 123 class VIEWS_EXPORT FocusManager { | 123 class VIEWS_EXPORT FocusManager : public ui::AcceleratorProcessor { |
| 124 public: | 124 public: |
| 125 // The reason why the focus changed. | 125 // The reason why the focus changed. |
| 126 enum FocusChangeReason { | 126 enum FocusChangeReason { |
| 127 // The focus changed because the user traversed focusable views using | 127 // The focus changed because the user traversed focusable views using |
| 128 // keys like Tab or Shift+Tab. | 128 // keys like Tab or Shift+Tab. |
| 129 kReasonFocusTraversal, | 129 kReasonFocusTraversal, |
| 130 | 130 |
| 131 // The focus changed due to restoring the focus. | 131 // The focus changed due to restoring the focus. |
| 132 kReasonFocusRestore, | 132 kReasonFocusRestore, |
| 133 | 133 |
| 134 // The focus changed due to a click or a shortcut to jump directly to | 134 // The focus changed due to a click or a shortcut to jump directly to |
| 135 // a particular view. | 135 // a particular view. |
| 136 kReasonDirectFocusChange | 136 kReasonDirectFocusChange |
| 137 }; | 137 }; |
| 138 | 138 |
| 139 // TODO: use Direction in place of bool reverse throughout. | 139 // TODO: use Direction in place of bool reverse throughout. |
| 140 enum Direction { | 140 enum Direction { |
| 141 kForward, | 141 kForward, |
| 142 kBackward | 142 kBackward |
| 143 }; | 143 }; |
| 144 | 144 |
| 145 enum FocusCycleWrappingBehavior { | 145 enum FocusCycleWrappingBehavior { |
| 146 kWrap, | 146 kWrap, |
| 147 kNoWrap | 147 kNoWrap |
| 148 }; | 148 }; |
| 149 | 149 |
| 150 FocusManager(Widget* widget, FocusManagerDelegate* delegate); | 150 FocusManager(Widget* widget); |
|
sky
2015/01/08 23:39:26
explicit
Andre
2015/01/09 00:06:15
Done.
| |
| 151 virtual ~FocusManager(); | 151 ~FocusManager() override; |
| 152 | 152 |
| 153 // Processes the passed key event for accelerators and keyboard traversal. | 153 // Processes the passed key event for accelerators and keyboard traversal. |
| 154 // Returns false if the event has been consumed and should not be processed | 154 // Returns false if the event has been consumed and should not be processed |
| 155 // further. | 155 // further. |
| 156 bool OnKeyEvent(const ui::KeyEvent& event); | 156 bool OnKeyEvent(const ui::KeyEvent& event); |
| 157 | 157 |
| 158 // Returns true is the specified is part of the hierarchy of the window | 158 // Returns true is the specified is part of the hierarchy of the window |
| 159 // associated with this FocusManager. | 159 // associated with this FocusManager. |
| 160 bool ContainsView(View* view); | 160 bool ContainsView(View* view); |
| 161 | 161 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 | 218 |
| 219 // Changes the text input focus to |view->GetTextInputClient()| iff |view| | 219 // Changes the text input focus to |view->GetTextInputClient()| iff |view| |
| 220 // is focused. Views must call this method when their internal | 220 // is focused. Views must call this method when their internal |
| 221 // TextInputClient instance changes. | 221 // TextInputClient instance changes. |
| 222 void OnTextInputClientChanged(View* view); | 222 void OnTextInputClientChanged(View* view); |
| 223 | 223 |
| 224 // Moves the text input focus into/out from |view|. | 224 // Moves the text input focus into/out from |view|. |
| 225 void FocusTextInputClient(View* view); | 225 void FocusTextInputClient(View* view); |
| 226 void BlurTextInputClient(View* view); | 226 void BlurTextInputClient(View* view); |
| 227 | 227 |
| 228 // Disable shortcut handling. | |
| 229 static void set_shortcut_handling_suspended(bool suspended) { | |
| 230 shortcut_handling_suspended_ = suspended; | |
| 231 } | |
| 232 // Returns whether shortcut handling is currently suspended. | |
| 233 bool shortcut_handling_suspended() { return shortcut_handling_suspended_; } | |
| 234 | |
| 235 // Register a keyboard accelerator for the specified target. If multiple | 228 // Register a keyboard accelerator for the specified target. If multiple |
| 236 // targets are registered for an accelerator, a target registered later has | 229 // targets are registered for an accelerator, a target registered later has |
| 237 // higher priority. | 230 // higher priority. |
| 238 // |accelerator| is the accelerator to register. | 231 // |accelerator| is the accelerator to register. |
| 239 // |priority| denotes the priority of the handler. | 232 // |priority| denotes the priority of the handler. |
| 240 // NOTE: In almost all cases, you should specify kNormalPriority for this | 233 // NOTE: In almost all cases, you should specify kNormalPriority for this |
| 241 // parameter. Setting it to kHighPriority prevents Chrome from sending the | 234 // parameter. Setting it to kHighPriority prevents Chrome from sending the |
| 242 // shortcut to the webpage if the renderer has focus, which is not desirable | 235 // shortcut to the webpage if the renderer has focus, which is not desirable |
| 243 // except for very isolated cases. | 236 // except for very isolated cases. |
| 244 // |target| is the AcceleratorTarget that handles the event once the | 237 // |target| is the AcceleratorTarget that handles the event once the |
| 245 // accelerator is pressed. | 238 // accelerator is pressed. |
| 246 // Note that we are currently limited to accelerators that are either: | 239 // Note that we are currently limited to accelerators that are either: |
| 247 // - a key combination including Ctrl or Alt | 240 // - a key combination including Ctrl or Alt |
| 248 // - the escape key | 241 // - the escape key |
| 249 // - the enter key | 242 // - the enter key |
| 250 // - any F key (F1, F2, F3 ...) | 243 // - any F key (F1, F2, F3 ...) |
| 251 // - any browser specific keys (as available on special keyboards) | 244 // - any browser specific keys (as available on special keyboards) |
| 252 void RegisterAccelerator(const ui::Accelerator& accelerator, | 245 void RegisterAccelerator(const ui::Accelerator& accelerator, |
| 253 ui::AcceleratorManager::HandlerPriority priority, | 246 ui::AcceleratorManager::HandlerPriority priority, |
| 254 ui::AcceleratorTarget* target); | 247 ui::AcceleratorTarget* target); |
| 255 | 248 |
| 256 // Unregister the specified keyboard accelerator for the specified target. | 249 // Unregister the specified keyboard accelerator for the specified target. |
| 257 void UnregisterAccelerator(const ui::Accelerator& accelerator, | 250 void UnregisterAccelerator(const ui::Accelerator& accelerator, |
| 258 ui::AcceleratorTarget* target); | 251 ui::AcceleratorTarget* target); |
| 259 | 252 |
| 260 // Unregister all keyboard accelerator for the specified target. | 253 // Unregister all keyboard accelerator for the specified target. |
| 261 void UnregisterAccelerators(ui::AcceleratorTarget* target); | 254 void UnregisterAccelerators(ui::AcceleratorTarget* target); |
| 262 | 255 |
| 263 // Activate the target associated with the specified accelerator. | |
| 264 // First, AcceleratorPressed handler of the most recently registered target | |
| 265 // is called, and if that handler processes the event (i.e. returns true), | |
| 266 // this method immediately returns. If not, we do the same thing on the next | |
| 267 // target, and so on. | |
| 268 // Returns true if an accelerator was activated. | |
| 269 bool ProcessAccelerator(const ui::Accelerator& accelerator); | |
| 270 | |
| 271 // Resets menu key state if |event| is not menu key release. | 256 // Resets menu key state if |event| is not menu key release. |
| 272 // This is effective only on x11. | 257 // This is effective only on x11. |
| 273 void MaybeResetMenuKeyState(const ui::KeyEvent& key); | 258 void MaybeResetMenuKeyState(const ui::KeyEvent& key); |
| 274 | 259 |
| 275 // Called by a RootView when a view within its hierarchy is removed | 260 // Called by a RootView when a view within its hierarchy is removed |
| 276 // from its parent. This will only be called by a RootView in a | 261 // from its parent. This will only be called by a RootView in a |
| 277 // hierarchy of Widgets that this FocusManager is attached to the | 262 // hierarchy of Widgets that this FocusManager is attached to the |
| 278 // parent Widget of. | 263 // parent Widget of. |
| 279 void ViewRemoved(View* removed); | 264 void ViewRemoved(View* removed); |
| 280 | 265 |
| 281 // Adds/removes a listener. The FocusChangeListener is notified every time | 266 // Adds/removes a listener. The FocusChangeListener is notified every time |
| 282 // the focused view is about to change. | 267 // the focused view is about to change. |
| 283 void AddFocusChangeListener(FocusChangeListener* listener); | 268 void AddFocusChangeListener(FocusChangeListener* listener); |
| 284 void RemoveFocusChangeListener(FocusChangeListener* listener); | 269 void RemoveFocusChangeListener(FocusChangeListener* listener); |
| 285 | 270 |
| 286 // Returns the AcceleratorTarget that should be activated for the specified | 271 // Adds/removes an accelerator processor. |
|
sky
2015/01/08 23:39:26
How about test coverage of some of this new functi
| |
| 287 // keyboard accelerator, or NULL if no view is registered for that keyboard | 272 // PreProcessors are given a chance to process an accelerator before normal |
| 288 // accelerator. | 273 // processing. |
| 289 ui::AcceleratorTarget* GetCurrentTargetForAccelerator( | 274 // PostProcessors are given a chance to process unhandled accelerators. |
| 290 const ui::Accelerator& accelerator) const; | 275 void AddAcceleratorPreProcessor(ui::AcceleratorProcessor*); |
|
sky
2015/01/08 23:39:26
style guide says you should have names here.
Andre
2015/01/09 00:06:15
Done.
| |
| 276 void AddAcceleratorPostProcessor(ui::AcceleratorProcessor*); | |
| 277 void RemoveAcceleratorProcessor(ui::AcceleratorProcessor*); | |
| 291 | 278 |
| 292 // Whether the given |accelerator| has a priority handler associated with it. | 279 // Whether the given |accelerator| has a priority handler associated with it. |
| 293 bool HasPriorityHandler(const ui::Accelerator& accelerator) const; | 280 bool HasPriorityHandler(const ui::Accelerator& accelerator) const; |
| 294 | 281 |
| 295 // Clears the native view having the focus. | 282 // Clears the native view having the focus. |
| 296 virtual void ClearNativeFocus(); | 283 virtual void ClearNativeFocus(); |
| 297 | 284 |
| 298 // Focuses the next keyboard-accessible pane, taken from the list of | 285 // Focuses the next keyboard-accessible pane, taken from the list of |
| 299 // views returned by WidgetDelegate::GetAccessiblePanes(). If there are | 286 // views returned by WidgetDelegate::GetAccessiblePanes(). If there are |
| 300 // no panes, the widget's root view is treated as a single pane. | 287 // no panes, the widget's root view is treated as a single pane. |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 324 // |starting_view| is NULL |starting_widget| is consuled to determine which | 311 // |starting_view| is NULL |starting_widget| is consuled to determine which |
| 325 // Widget to start from. See | 312 // Widget to start from. See |
| 326 // WidgetDelegate::ShouldAdvanceFocusToTopLevelWidget() for details. If both | 313 // WidgetDelegate::ShouldAdvanceFocusToTopLevelWidget() for details. If both |
| 327 // |starting_view| and |starting_widget| are NULL, traversal starts at | 314 // |starting_view| and |starting_widget| are NULL, traversal starts at |
| 328 // |widget_|. | 315 // |widget_|. |
| 329 View* GetNextFocusableView(View* starting_view, | 316 View* GetNextFocusableView(View* starting_view, |
| 330 Widget* starting_widget, | 317 Widget* starting_widget, |
| 331 bool reverse, | 318 bool reverse, |
| 332 bool dont_loop); | 319 bool dont_loop); |
| 333 | 320 |
| 321 // Overridden from ui::AcceleratorProcessor: | |
| 322 bool ProcessAccelerator(const ui::Accelerator& accelerator) override; | |
| 323 ui::AcceleratorTarget* GetTargetForAccelerator( | |
| 324 const ui::Accelerator& accelerator) const override; | |
| 325 | |
| 334 private: | 326 private: |
| 335 // Returns the focusable view found in the FocusTraversable specified starting | 327 // Returns the focusable view found in the FocusTraversable specified starting |
| 336 // at the specified view. This traverses down along the FocusTraversable | 328 // at the specified view. This traverses down along the FocusTraversable |
| 337 // hierarchy. | 329 // hierarchy. |
| 338 // Returns NULL if no focusable view were found. | 330 // Returns NULL if no focusable view were found. |
| 339 View* FindFocusableView(FocusTraversable* focus_traversable, | 331 View* FindFocusableView(FocusTraversable* focus_traversable, |
| 340 View* starting_view, | 332 View* starting_view, |
| 341 bool reverse); | 333 bool reverse); |
| 342 | 334 |
| 343 // Process arrow key traversal. Returns true if the event has been consumed | 335 // Process arrow key traversal. Returns true if the event has been consumed |
| 344 // and should not be processed further. | 336 // and should not be processed further. |
| 345 bool ProcessArrowKeyTraversal(const ui::KeyEvent& event); | 337 bool ProcessArrowKeyTraversal(const ui::KeyEvent& event); |
| 346 | 338 |
| 347 // Keeps track of whether shortcut handling is currently suspended. | |
| 348 static bool shortcut_handling_suspended_; | |
| 349 | |
| 350 // Whether arrow key traversal is enabled. | 339 // Whether arrow key traversal is enabled. |
| 351 static bool arrow_key_traversal_enabled_; | 340 static bool arrow_key_traversal_enabled_; |
| 352 | 341 |
| 353 // The top-level Widget this FocusManager is associated with. | 342 // The top-level Widget this FocusManager is associated with. |
| 354 Widget* widget_; | 343 Widget* widget_; |
| 355 | 344 |
| 356 // The object which handles an accelerator when |accelerator_manager_| doesn't | |
| 357 // handle it. | |
| 358 scoped_ptr<FocusManagerDelegate> delegate_; | |
| 359 | |
| 360 // The view that currently is focused. | 345 // The view that currently is focused. |
| 361 View* focused_view_; | 346 View* focused_view_; |
| 362 | 347 |
| 363 // The AcceleratorManager this FocusManager is associated with. | 348 // The AcceleratorManager this FocusManager is associated with. |
| 364 scoped_ptr<ui::AcceleratorManager> accelerator_manager_; | 349 scoped_ptr<ui::AcceleratorManager> accelerator_manager_; |
| 365 | 350 |
| 351 std::list<ui::AcceleratorProcessor*> accelerator_processors_; | |
| 352 | |
| 366 // The storage id used in the ViewStorage to store/restore the view that last | 353 // The storage id used in the ViewStorage to store/restore the view that last |
| 367 // had focus. | 354 // had focus. |
| 368 int stored_focused_view_storage_id_; | 355 int stored_focused_view_storage_id_; |
| 369 | 356 |
| 370 // The reason why the focus most recently changed. | 357 // The reason why the focus most recently changed. |
| 371 FocusChangeReason focus_change_reason_; | 358 FocusChangeReason focus_change_reason_; |
| 372 | 359 |
| 373 // The list of registered FocusChange listeners. | 360 // The list of registered FocusChange listeners. |
| 374 ObserverList<FocusChangeListener, true> focus_change_listeners_; | 361 ObserverList<FocusChangeListener, true> focus_change_listeners_; |
| 375 | 362 |
| 376 // See description above getter. | 363 // See description above getter. |
| 377 bool is_changing_focus_; | 364 bool is_changing_focus_; |
| 378 | 365 |
| 379 DISALLOW_COPY_AND_ASSIGN(FocusManager); | 366 DISALLOW_COPY_AND_ASSIGN(FocusManager); |
| 380 }; | 367 }; |
| 381 | 368 |
| 382 } // namespace views | 369 } // namespace views |
| 383 | 370 |
| 384 #endif // UI_VIEWS_FOCUS_FOCUS_MANAGER_H_ | 371 #endif // UI_VIEWS_FOCUS_FOCUS_MANAGER_H_ |
| OLD | NEW |