Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / views / focus / external_focus_tracker.cc
blob293c2e0f5b914b96f8dc93748a7035b2d3dc777a
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.
5 #include "ui/views/focus/external_focus_tracker.h"
7 #include "base/logging.h"
8 #include "ui/views/focus/view_storage.h"
9 #include "ui/views/view.h"
11 namespace views {
13 ExternalFocusTracker::ExternalFocusTracker(View* parent_view,
14 FocusManager* focus_manager)
15 : focus_manager_(focus_manager),
16 parent_view_(parent_view) {
17 DCHECK(parent_view);
18 view_storage_ = ViewStorage::GetInstance();
19 last_focused_view_storage_id_ = view_storage_->CreateStorageID();
20 // Store the view which is focused when we're created.
21 if (focus_manager_)
22 StartTracking();
25 ExternalFocusTracker::~ExternalFocusTracker() {
26 view_storage_->RemoveView(last_focused_view_storage_id_);
27 if (focus_manager_)
28 focus_manager_->RemoveFocusChangeListener(this);
31 void ExternalFocusTracker::OnWillChangeFocus(View* focused_before,
32 View* focused_now) {
33 if (focused_now && !parent_view_->Contains(focused_now) &&
34 parent_view_ != focused_now) {
35 // Store the newly focused view.
36 StoreLastFocusedView(focused_now);
40 void ExternalFocusTracker::OnDidChangeFocus(View* focused_before,
41 View* focused_now) {
44 void ExternalFocusTracker::FocusLastFocusedExternalView() {
45 View* last_focused_view =
46 view_storage_->RetrieveView(last_focused_view_storage_id_);
47 if (last_focused_view)
48 last_focused_view->RequestFocus();
51 void ExternalFocusTracker::SetFocusManager(FocusManager* focus_manager) {
52 if (focus_manager_)
53 focus_manager_->RemoveFocusChangeListener(this);
54 focus_manager_ = focus_manager;
55 if (focus_manager_)
56 StartTracking();
59 void ExternalFocusTracker::StoreLastFocusedView(View* view) {
60 view_storage_->RemoveView(last_focused_view_storage_id_);
61 // If the view is NULL, remove the last focused view from storage, but don't
62 // try to store NULL.
63 if (view != NULL)
64 view_storage_->StoreView(last_focused_view_storage_id_, view);
67 void ExternalFocusTracker::StartTracking() {
68 StoreLastFocusedView(focus_manager_->GetFocusedView());
69 focus_manager_->AddFocusChangeListener(this);
72 } // namespace views