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_FOCUS_EXTERNAL_FOCUS_TRACKER_H_ 6 #define UI_VIEWS_FOCUS_EXTERNAL_FOCUS_TRACKER_H_ 7 8 #include "base/compiler_specific.h" 9 #include "ui/views/focus/focus_manager.h" 10 11 namespace views { 12 13 class View; 14 class ViewStorage; 15 16 // ExternalFocusTracker tracks the last focused view which belongs to the 17 // provided focus manager and is not either the provided parent view or one of 18 // its descendants. This is generally used if the parent view want to return 19 // focus to some other view once it is dismissed. The parent view and the focus 20 // manager must exist for the duration of the tracking. If the focus manager 21 // must be deleted before this object is deleted, make sure to call 22 // SetFocusManager(NULL) first. 23 // 24 // Typical use: When a view is added to the view hierarchy, it instantiates an 25 // ExternalFocusTracker and passes in itself and its focus manager. Then, 26 // when that view wants to return focus to the last focused view which is not 27 // itself and not a descandant of itself, (usually when it is being closed) 28 // it calls FocusLastFocusedExternalView. 29 class VIEWS_EXPORT ExternalFocusTracker : public FocusChangeListener { 30 public: 31 ExternalFocusTracker(View* parent_view, FocusManager* focus_manager); 32 virtual ~ExternalFocusTracker(); 33 34 // FocusChangeListener: 35 virtual void OnWillChangeFocus(View* focused_before, 36 View* focused_now) OVERRIDE; 37 virtual void OnDidChangeFocus(View* focused_before, 38 View* focused_now) OVERRIDE; 39 40 // Focuses last focused view which is not a child of parent view and is not 41 // parent view itself. Returns true if focus for a view was requested, false 42 // otherwise. 43 void FocusLastFocusedExternalView(); 44 45 // Sets the focus manager whose focus we are tracking. |focus_manager| can 46 // be NULL, but no focus changes will be tracked. This is useful if the focus 47 // manager went away, but you might later want to start tracking with a new 48 // manager later, or call FocusLastFocusedExternalView to focus the previous 49 // view. 50 void SetFocusManager(FocusManager* focus_manager); 51 52 private: 53 // Store the provided view. This view will be focused when 54 // FocusLastFocusedExternalView is called. 55 void StoreLastFocusedView(View* view); 56 57 // Store the currently focused view for our view manager and register as a 58 // listener for future focus changes. 59 void StartTracking(); 60 61 // Focus manager which we are a listener for. 62 FocusManager* focus_manager_; 63 64 // ID of the last focused view, which we store in view_storage_. 65 int last_focused_view_storage_id_; 66 67 // Used to store the last focused view which is not a child of 68 // ExternalFocusTracker. 69 ViewStorage* view_storage_; 70 71 // The parent view of views which we should not track focus changes to. We 72 // also do not track changes to parent_view_ itself. 73 View* parent_view_; 74 75 DISALLOW_COPY_AND_ASSIGN(ExternalFocusTracker); 76 }; 77 78 } // namespace views 79 80 #endif // UI_VIEWS_FOCUS_EXTERNAL_FOCUS_TRACKER_H_ 81