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 "components/wug/view.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "components/wug/view_model.h" |
| 10 |
| 11 namespace wug { |
| 12 |
| 13 View::View(const std::string& id) |
| 14 : parent_(NULL), |
| 15 id_(id), |
| 16 ready_children_(0), |
| 17 view_model_ready_(false), |
| 18 ready_(false), |
| 19 weak_factory_(this) { |
| 20 } |
| 21 |
| 22 View::~View() { |
| 23 } |
| 24 |
| 25 void View::Init() { |
| 26 CreateAndAddChildren(); |
| 27 if (!IsRootView()) |
| 28 path_ = parent_->path() + "$"; |
| 29 path_ += id_; |
| 30 view_model_.reset(CreateViewModel()); |
| 31 view_model_->SetView(this); |
| 32 for (auto& id_child : children_) { |
| 33 id_child.second->Init(); |
| 34 } |
| 35 if (children_.empty()) |
| 36 OnChildrenReady(); |
| 37 } |
| 38 |
| 39 bool View::IsRootView() const { |
| 40 return !parent_; |
| 41 } |
| 42 |
| 43 ViewModel* View::GetViewModel() const { |
| 44 return view_model_.get(); |
| 45 } |
| 46 |
| 47 void View::OnViewModelReady() { |
| 48 view_model_ready_ = true; |
| 49 if (ready_children_ == static_cast<int>(children_.size())) |
| 50 OnReady(); |
| 51 } |
| 52 |
| 53 View* View::GetChild(const std::string& id) const { |
| 54 auto it = children_.find(id); |
| 55 if (it == children_.end()) |
| 56 return nullptr; |
| 57 return it->second; |
| 58 } |
| 59 |
| 60 void View::OnReady() { |
| 61 if (ready_) |
| 62 return; |
| 63 ready_ = true; |
| 64 if (!IsRootView()) |
| 65 parent_->OnChildReady(this); |
| 66 } |
| 67 |
| 68 void View::UpdateContext(const base::DictionaryValue& diff) { |
| 69 DCHECK(ready_); |
| 70 view_model_->UpdateContext(diff); |
| 71 } |
| 72 |
| 73 void View::HandleEvent(const std::string& event) { |
| 74 DCHECK(ready_); |
| 75 view_model_->OnEvent(event); |
| 76 } |
| 77 |
| 78 void View::AddChild(View* child) { |
| 79 DCHECK(children_.find(child->id()) == children_.end()); |
| 80 DCHECK(!child->id().empty()); |
| 81 children_.set(child->id(), make_scoped_ptr(child)); |
| 82 child->set_parent(this); |
| 83 } |
| 84 |
| 85 void View::OnChildReady(View* child) { |
| 86 ++ready_children_; |
| 87 if (ready_children_ == static_cast<int>(children_.size())) |
| 88 OnChildrenReady(); |
| 89 } |
| 90 |
| 91 void View::OnChildrenReady() { |
| 92 view_model_->OnChildrenReady(); |
| 93 if (view_model_ready_) |
| 94 OnReady(); |
| 95 } |
| 96 |
| 97 } // namespace wug |
OLD | NEW |