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 <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 12 #include "ui/base/accelerators/accelerator_manager.h" | 12 #include "ui/base/accelerators/accelerator_manager.h" |
| 13 #include "ui/views/view_observer.h" | |
| 13 #include "ui/views/views_export.h" | 14 #include "ui/views/views_export.h" |
| 14 | 15 |
| 15 // FocusManager handles focus traversal, stores and restores focused views, and | 16 // FocusManager handles focus traversal, stores and restores focused views, and |
| 16 // handles keyboard accelerators. This class is an implementation detail of | 17 // handles keyboard accelerators. This class is an implementation detail of |
| 17 // views::. Most callers should use methods of views:: classes rather than using | 18 // views::. Most callers should use methods of views:: classes rather than using |
| 18 // FocusManager directly. | 19 // FocusManager directly. |
| 19 // | 20 // |
| 20 // There are 2 types of focus: | 21 // There are 2 types of focus: |
| 21 // | 22 // |
| 22 // - The native focus, which is the focus that a gfx::NativeView has. | 23 // - The native focus, which is the focus that a gfx::NativeView has. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 // the parent view that directly contains the native window. This is needed | 64 // the parent view that directly contains the native window. This is needed |
| 64 // when traversing up from the nested RootView to know which view to start | 65 // when traversing up from the nested RootView to know which view to start |
| 65 // with when going to the next/previous view. In the example: | 66 // with when going to the next/previous view. In the example: |
| 66 // | 67 // |
| 67 // hwnd_view_container_->GetWidget()->SetFocusTraversableParent( | 68 // hwnd_view_container_->GetWidget()->SetFocusTraversableParent( |
| 68 // native_control); | 69 // native_control); |
| 69 // | 70 // |
| 70 // Note that FocusTraversable views do not have to be RootViews: | 71 // Note that FocusTraversable views do not have to be RootViews: |
| 71 // AccessibleToolbarView is FocusTraversable. | 72 // AccessibleToolbarView is FocusTraversable. |
| 72 | 73 |
| 74 namespace base { | |
| 75 namespace debug { | |
| 76 class StackTrace; | |
| 77 } | |
| 78 } | |
| 73 namespace ui { | 79 namespace ui { |
|
msw
2017/03/13 18:22:16
nit: blank line above
| |
| 74 class Accelerator; | 80 class Accelerator; |
| 75 class AcceleratorTarget; | 81 class AcceleratorTarget; |
| 76 class KeyEvent; | 82 class KeyEvent; |
| 77 } | 83 } |
| 78 | 84 |
| 79 namespace views { | 85 namespace views { |
| 80 | 86 |
| 81 class FocusManagerDelegate; | 87 class FocusManagerDelegate; |
| 82 class FocusSearch; | 88 class FocusSearch; |
| 83 class View; | 89 class View; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 111 // No change to focus state has occurred yet when this function is called. | 117 // No change to focus state has occurred yet when this function is called. |
| 112 virtual void OnWillChangeFocus(View* focused_before, View* focused_now) = 0; | 118 virtual void OnWillChangeFocus(View* focused_before, View* focused_now) = 0; |
| 113 | 119 |
| 114 // Called after focus state has changed. | 120 // Called after focus state has changed. |
| 115 virtual void OnDidChangeFocus(View* focused_before, View* focused_now) = 0; | 121 virtual void OnDidChangeFocus(View* focused_before, View* focused_now) = 0; |
| 116 | 122 |
| 117 protected: | 123 protected: |
| 118 virtual ~FocusChangeListener() {} | 124 virtual ~FocusChangeListener() {} |
| 119 }; | 125 }; |
| 120 | 126 |
| 121 class VIEWS_EXPORT FocusManager { | 127 // FocusManager adds itself as a ViewObserver to the currently focused view. |
| 128 class VIEWS_EXPORT FocusManager : public ViewObserver { | |
| 122 public: | 129 public: |
| 123 // The reason why the focus changed. | 130 // The reason why the focus changed. |
| 124 enum FocusChangeReason { | 131 enum FocusChangeReason { |
| 125 // The focus changed because the user traversed focusable views using | 132 // The focus changed because the user traversed focusable views using |
| 126 // keys like Tab or Shift+Tab. | 133 // keys like Tab or Shift+Tab. |
| 127 kReasonFocusTraversal, | 134 kReasonFocusTraversal, |
| 128 | 135 |
| 129 // The focus changed due to restoring the focus. | 136 // The focus changed due to restoring the focus. |
| 130 kReasonFocusRestore, | 137 kReasonFocusRestore, |
| 131 | 138 |
| 132 // The focus changed due to a click or a shortcut to jump directly to | 139 // The focus changed due to a click or a shortcut to jump directly to |
| 133 // a particular view. | 140 // a particular view. |
| 134 kReasonDirectFocusChange | 141 kReasonDirectFocusChange |
| 135 }; | 142 }; |
| 136 | 143 |
| 137 // TODO: use Direction in place of bool reverse throughout. | 144 // TODO: use Direction in place of bool reverse throughout. |
| 138 enum Direction { | 145 enum Direction { |
| 139 kForward, | 146 kForward, |
| 140 kBackward | 147 kBackward |
| 141 }; | 148 }; |
| 142 | 149 |
| 143 enum FocusCycleWrappingBehavior { | 150 enum FocusCycleWrappingBehavior { |
| 144 kWrap, | 151 kWrap, |
| 145 kNoWrap | 152 kNoWrap |
| 146 }; | 153 }; |
| 147 | 154 |
| 148 FocusManager(Widget* widget, std::unique_ptr<FocusManagerDelegate> delegate); | 155 FocusManager(Widget* widget, std::unique_ptr<FocusManagerDelegate> delegate); |
| 149 virtual ~FocusManager(); | 156 ~FocusManager() override; |
| 150 | 157 |
| 151 // Processes the passed key event for accelerators and keyboard traversal. | 158 // Processes the passed key event for accelerators and keyboard traversal. |
| 152 // Returns false if the event has been consumed and should not be processed | 159 // Returns false if the event has been consumed and should not be processed |
| 153 // further. | 160 // further. |
| 154 bool OnKeyEvent(const ui::KeyEvent& event); | 161 bool OnKeyEvent(const ui::KeyEvent& event); |
| 155 | 162 |
| 156 // Returns true is the specified is part of the hierarchy of the window | 163 // Returns true is the specified is part of the hierarchy of the window |
| 157 // associated with this FocusManager. | 164 // associated with this FocusManager. |
| 158 bool ContainsView(View* view); | 165 bool ContainsView(View* view); |
| 159 | 166 |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 324 bool reverse); | 331 bool reverse); |
| 325 | 332 |
| 326 // Process arrow key traversal. Returns true if the event has been consumed | 333 // Process arrow key traversal. Returns true if the event has been consumed |
| 327 // and should not be processed further. | 334 // and should not be processed further. |
| 328 bool ProcessArrowKeyTraversal(const ui::KeyEvent& event); | 335 bool ProcessArrowKeyTraversal(const ui::KeyEvent& event); |
| 329 | 336 |
| 330 // Whether |view| is currently focusable as per the platform's interpretation | 337 // Whether |view| is currently focusable as per the platform's interpretation |
| 331 // of |keyboard_accesible_|. | 338 // of |keyboard_accesible_|. |
| 332 bool IsFocusable(View* view) const; | 339 bool IsFocusable(View* view) const; |
| 333 | 340 |
| 341 // ViewObserver: | |
| 342 void OnViewIsDeleting(View* view) override; | |
| 343 | |
| 334 // Whether arrow key traversal is enabled. | 344 // Whether arrow key traversal is enabled. |
| 335 static bool arrow_key_traversal_enabled_; | 345 static bool arrow_key_traversal_enabled_; |
| 336 | 346 |
| 337 // The top-level Widget this FocusManager is associated with. | 347 // The top-level Widget this FocusManager is associated with. |
| 338 Widget* widget_; | 348 Widget* widget_; |
| 339 | 349 |
| 340 // The object which handles an accelerator when |accelerator_manager_| doesn't | 350 // The object which handles an accelerator when |accelerator_manager_| doesn't |
| 341 // handle it. | 351 // handle it. |
| 342 std::unique_ptr<FocusManagerDelegate> delegate_; | 352 std::unique_ptr<FocusManagerDelegate> delegate_; |
| 343 | 353 |
| 344 // The view that currently is focused. | 354 // The view that currently is focused. |
| 345 View* focused_view_ = nullptr; | 355 View* focused_view_ = nullptr; |
| 346 | 356 |
| 357 // TODO(sky): remove, used for debugging 687232. | |
| 358 std::unique_ptr<base::debug::StackTrace> stack_when_focused_view_set_; | |
| 359 | |
| 347 // The AcceleratorManager this FocusManager is associated with. | 360 // The AcceleratorManager this FocusManager is associated with. |
| 348 ui::AcceleratorManager accelerator_manager_; | 361 ui::AcceleratorManager accelerator_manager_; |
| 349 | 362 |
| 350 // Keeps track of whether shortcut handling is currently suspended. | 363 // Keeps track of whether shortcut handling is currently suspended. |
| 351 bool shortcut_handling_suspended_ = false; | 364 bool shortcut_handling_suspended_ = false; |
| 352 | 365 |
| 353 // The storage id used in the ViewStorage to store/restore the view that last | 366 // The storage id used in the ViewStorage to store/restore the view that last |
| 354 // had focus. | 367 // had focus. |
| 355 const int stored_focused_view_storage_id_; | 368 const int stored_focused_view_storage_id_; |
| 356 | 369 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 367 // FocusTraversable level. Currently only used on Mac, when Full Keyboard | 380 // FocusTraversable level. Currently only used on Mac, when Full Keyboard |
| 368 // access is enabled. | 381 // access is enabled. |
| 369 bool keyboard_accessible_ = false; | 382 bool keyboard_accessible_ = false; |
| 370 | 383 |
| 371 DISALLOW_COPY_AND_ASSIGN(FocusManager); | 384 DISALLOW_COPY_AND_ASSIGN(FocusManager); |
| 372 }; | 385 }; |
| 373 | 386 |
| 374 } // namespace views | 387 } // namespace views |
| 375 | 388 |
| 376 #endif // UI_VIEWS_FOCUS_FOCUS_MANAGER_H_ | 389 #endif // UI_VIEWS_FOCUS_FOCUS_MANAGER_H_ |
| OLD | NEW |