1 // Copyright (c) 2011 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 UI_VIEWS_ACCESSIBLE_PANE_VIEW_H_ 6 #define UI_VIEWS_ACCESSIBLE_PANE_VIEW_H_ 7 8 #include "base/containers/hash_tables.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/weak_ptr.h" 11 #include "ui/base/accelerators/accelerator.h" 12 #include "ui/views/focus/focus_manager.h" 13 #include "ui/views/view.h" 14 15 namespace views { 16 class FocusSearch; 17 18 // This class provides keyboard access to any view that extends it, typically 19 // a toolbar. The user sets focus to a control in this view by pressing 20 // F6 to traverse all panes, or by pressing a shortcut that jumps directly 21 // to this pane. 22 class VIEWS_EXPORT AccessiblePaneView : public View, 23 public FocusChangeListener, 24 public FocusTraversable { 25 public: 26 AccessiblePaneView(); 27 virtual ~AccessiblePaneView(); 28 29 // Set focus to the pane with complete keyboard access. 30 // Focus will be restored to the last focused view if the user escapes. 31 // If |initial_focus| is not NULL, that control will get 32 // the initial focus, if it's enabled and focusable. Returns true if 33 // the pane was able to receive focus. 34 virtual bool SetPaneFocus(View* initial_focus); 35 36 // Set focus to the pane with complete keyboard access, with the 37 // focus initially set to the default child. Focus will be restored 38 // to the last focused view if the user escapes. 39 // Returns true if the pane was able to receive focus. 40 virtual bool SetPaneFocusAndFocusDefault(); 41 42 // Overridden from View: 43 virtual FocusTraversable* GetPaneFocusTraversable() OVERRIDE; 44 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) 45 OVERRIDE; 46 virtual void SetVisible(bool flag) OVERRIDE; 47 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 48 virtual void RequestFocus() OVERRIDE; 49 50 // Overridden from FocusChangeListener: 51 virtual void OnWillChangeFocus(View* focused_before, 52 View* focused_now) OVERRIDE; 53 virtual void OnDidChangeFocus(View* focused_before, 54 View* focused_now) OVERRIDE; 55 56 // Overridden from FocusTraversable: 57 virtual FocusSearch* GetFocusSearch() OVERRIDE; 58 virtual FocusTraversable* GetFocusTraversableParent() OVERRIDE; 59 virtual View* GetFocusTraversableParentView() OVERRIDE; 60 61 // For testing only. 62 const ui::Accelerator& home_key() const { return home_key_; } 63 const ui::Accelerator& end_key() const { return end_key_; } 64 const ui::Accelerator& escape_key() const { return escape_key_; } 65 const ui::Accelerator& left_key() const { return left_key_; } 66 const ui::Accelerator& right_key() const { return right_key_; } 67 68 protected: 69 // A subclass can override this to provide a default focusable child 70 // other than the first focusable child. 71 virtual View* GetDefaultFocusableChild(); 72 73 // Returns the parent of |v|. Subclasses can override this if 74 // they need custom focus search behavior. 75 virtual View* GetParentForFocusSearch(View* v); 76 77 // Returns true if |v| is contained within the hierarchy rooted at |root| 78 // for the purpose of focus searching. Subclasses can override this if 79 // they need custom focus search behavior. 80 virtual bool ContainsForFocusSearch(View* root, const View* v); 81 82 // Remove pane focus. 83 virtual void RemovePaneFocus(); 84 85 View* GetFirstFocusableChild(); 86 View* GetLastFocusableChild(); 87 88 FocusManager* focus_manager() const { return focus_manager_; } 89 90 // When finishing navigation by pressing ESC, it is allowed to surrender the 91 // focus to another window if if |allow| is set and no previous view can be 92 // found. 93 void set_allow_deactivate_on_esc(bool allow) { 94 allow_deactivate_on_esc_ = allow; 95 } 96 97 private: 98 bool pane_has_focus_; 99 100 // If true, the panel should be de-activated upon escape when no active view 101 // is known where to return to. 102 bool allow_deactivate_on_esc_; 103 104 base::WeakPtrFactory<AccessiblePaneView> method_factory_; 105 106 // Save the focus manager rather than calling GetFocusManager(), 107 // so that we can remove focus listeners in the destructor. 108 FocusManager* focus_manager_; 109 110 // Our custom focus search implementation that traps focus in this 111 // pane and traverses all views that are focusable for accessibility, 112 // not just those that are normally focusable. 113 scoped_ptr<FocusSearch> focus_search_; 114 115 // Registered accelerators 116 ui::Accelerator home_key_; 117 ui::Accelerator end_key_; 118 ui::Accelerator escape_key_; 119 ui::Accelerator left_key_; 120 ui::Accelerator right_key_; 121 122 // View storage id for the last focused view that's not within this pane. 123 int last_focused_view_storage_id_; 124 125 friend class AccessiblePaneViewFocusSearch; 126 127 DISALLOW_COPY_AND_ASSIGN(AccessiblePaneView); 128 }; 129 130 } // namespace views 131 132 #endif // UI_VIEWS_ACCESSIBLE_PANE_VIEW_H_ 133