| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 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 #include "services/view_manager/server_view_drawn_tracker.h" |
| 6 |
| 7 #include "services/view_manager/server_view.h" |
| 8 #include "services/view_manager/server_view_drawn_tracker_observer.h" |
| 9 |
| 10 namespace view_manager { |
| 11 |
| 12 ServerViewDrawnTracker::ServerViewDrawnTracker( |
| 13 ServerView* root, |
| 14 ServerView* view, |
| 15 ServerViewDrawnTrackerObserver* observer) |
| 16 : root_(root), |
| 17 view_(view), |
| 18 observer_(observer), |
| 19 drawn_(view->IsDrawn(root)) { |
| 20 AddObservers(); |
| 21 } |
| 22 |
| 23 ServerViewDrawnTracker::~ServerViewDrawnTracker() { |
| 24 RemoveObservers(); |
| 25 } |
| 26 |
| 27 void ServerViewDrawnTracker::SetDrawn(ServerView* ancestor, bool drawn) { |
| 28 if (drawn == drawn_) |
| 29 return; |
| 30 |
| 31 drawn_ = drawn; |
| 32 observer_->OnDrawnStateChanged(ancestor, view_, drawn); |
| 33 } |
| 34 |
| 35 void ServerViewDrawnTracker::AddObservers() { |
| 36 if (!view_) |
| 37 return; |
| 38 |
| 39 for (ServerView* v = view_; v; v = v->parent()) { |
| 40 v->AddObserver(this); |
| 41 views_.insert(v); |
| 42 } |
| 43 } |
| 44 |
| 45 void ServerViewDrawnTracker::RemoveObservers() { |
| 46 for (ServerView* view : views_) |
| 47 view->RemoveObserver(this); |
| 48 |
| 49 views_.clear(); |
| 50 } |
| 51 |
| 52 void ServerViewDrawnTracker::OnViewDestroyed(ServerView* view) { |
| 53 // As views are removed before being destroyed, resulting in |
| 54 // OnViewHierarchyChanged() and us removing ourself as an observer, the only |
| 55 // view we should ever get notified of destruction on is |view_|. |
| 56 DCHECK_EQ(view, view_); |
| 57 RemoveObservers(); |
| 58 view_ = nullptr; |
| 59 SetDrawn(nullptr, false); |
| 60 } |
| 61 |
| 62 void ServerViewDrawnTracker::OnViewHierarchyChanged(ServerView* view, |
| 63 ServerView* new_parent, |
| 64 ServerView* old_parent) { |
| 65 RemoveObservers(); |
| 66 AddObservers(); |
| 67 const bool is_drawn = view_->IsDrawn(root_); |
| 68 SetDrawn(is_drawn ? nullptr : old_parent, is_drawn); |
| 69 } |
| 70 |
| 71 void ServerViewDrawnTracker::OnViewVisibilityChanged(ServerView* view) { |
| 72 const bool is_drawn = view_->IsDrawn(root_); |
| 73 SetDrawn(is_drawn ? nullptr : view->parent(), is_drawn); |
| 74 } |
| 75 |
| 76 } // namespace view_manager |
| OLD | NEW |