| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mash/catalog_viewer/catalog_viewer.h" | 5 #include "mash/catalog_viewer/catalog_viewer.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 class CatalogViewerContents : public views::WidgetDelegateView, | 35 class CatalogViewerContents : public views::WidgetDelegateView, |
| 36 public ui::TableModel { | 36 public ui::TableModel { |
| 37 public: | 37 public: |
| 38 CatalogViewerContents(CatalogViewer* catalog_viewer, | 38 CatalogViewerContents(CatalogViewer* catalog_viewer, |
| 39 catalog::mojom::CatalogPtr catalog) | 39 catalog::mojom::CatalogPtr catalog) |
| 40 : catalog_viewer_(catalog_viewer), | 40 : catalog_viewer_(catalog_viewer), |
| 41 catalog_(std::move(catalog)), | 41 catalog_(std::move(catalog)), |
| 42 table_view_(nullptr), | 42 table_view_(nullptr), |
| 43 table_view_parent_(nullptr), | 43 table_view_parent_(nullptr), |
| 44 observer_(nullptr), | 44 observer_(nullptr) { |
| 45 weak_ptr_factory_(this) { | |
| 46 table_view_ = new views::TableView(this, GetColumns(), views::TEXT_ONLY, | 45 table_view_ = new views::TableView(this, GetColumns(), views::TEXT_ONLY, |
| 47 false); | 46 false); |
| 48 set_background(views::Background::CreateStandardPanelBackground()); | 47 set_background(views::Background::CreateStandardPanelBackground()); |
| 49 | 48 |
| 50 table_view_parent_ = table_view_->CreateParentIfNecessary(); | 49 table_view_parent_ = table_view_->CreateParentIfNecessary(); |
| 51 AddChildView(table_view_parent_); | 50 AddChildView(table_view_parent_); |
| 52 | 51 |
| 53 catalog_->GetEntries(nullptr, | |
| 54 base::Bind(&CatalogViewerContents::OnGotCatalogEntries, | |
| 55 weak_ptr_factory_.GetWeakPtr())); | |
| 56 // We don't want to show an empty UI so we just block until we have all the | 52 // We don't want to show an empty UI so we just block until we have all the |
| 57 // data. | 53 // data. GetEntries is a sync call. |
| 58 catalog_.WaitForIncomingResponse(); | 54 mojo::Array<catalog::mojom::EntryPtr> entries; |
| 55 bool got = catalog_->GetEntries(nullptr, &entries); |
| 56 if (got) { |
| 57 for (auto& entry : entries) |
| 58 entries_.push_back(Entry(entry->display_name, entry->name)); |
| 59 observer_->OnModelChanged(); |
| 60 } |
| 59 } | 61 } |
| 60 ~CatalogViewerContents() override { | 62 ~CatalogViewerContents() override { |
| 61 table_view_->SetModel(nullptr); | 63 table_view_->SetModel(nullptr); |
| 62 catalog_viewer_->RemoveWindow(GetWidget()); | 64 catalog_viewer_->RemoveWindow(GetWidget()); |
| 63 } | 65 } |
| 64 | 66 |
| 65 private: | 67 private: |
| 66 struct Entry { | 68 struct Entry { |
| 67 Entry(const std::string& name, const std::string& url) | 69 Entry(const std::string& name, const std::string& url) |
| 68 : name(name), url(url) {} | 70 : name(name), url(url) {} |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 default: | 112 default: |
| 111 NOTREACHED(); | 113 NOTREACHED(); |
| 112 break; | 114 break; |
| 113 } | 115 } |
| 114 return base::string16(); | 116 return base::string16(); |
| 115 } | 117 } |
| 116 void SetObserver(ui::TableModelObserver* observer) override { | 118 void SetObserver(ui::TableModelObserver* observer) override { |
| 117 observer_ = observer; | 119 observer_ = observer; |
| 118 } | 120 } |
| 119 | 121 |
| 120 void OnGotCatalogEntries(mojo::Array<catalog::mojom::EntryPtr> entries) { | |
| 121 entries_.clear(); | |
| 122 for (auto& entry : entries) | |
| 123 entries_.push_back(Entry(entry->display_name, entry->name)); | |
| 124 observer_->OnModelChanged(); | |
| 125 } | |
| 126 | |
| 127 static std::vector<ui::TableColumn> GetColumns() { | 122 static std::vector<ui::TableColumn> GetColumns() { |
| 128 std::vector<ui::TableColumn> columns; | 123 std::vector<ui::TableColumn> columns; |
| 129 | 124 |
| 130 ui::TableColumn name_column; | 125 ui::TableColumn name_column; |
| 131 name_column.id = 0; | 126 name_column.id = 0; |
| 132 // TODO(beng): use resources. | 127 // TODO(beng): use resources. |
| 133 name_column.title = base::ASCIIToUTF16("Name"); | 128 name_column.title = base::ASCIIToUTF16("Name"); |
| 134 name_column.width = -1; | 129 name_column.width = -1; |
| 135 name_column.percent = 0.4f; | 130 name_column.percent = 0.4f; |
| 136 name_column.sortable = true; | 131 name_column.sortable = true; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 150 | 145 |
| 151 CatalogViewer* catalog_viewer_; | 146 CatalogViewer* catalog_viewer_; |
| 152 catalog::mojom::CatalogPtr catalog_; | 147 catalog::mojom::CatalogPtr catalog_; |
| 153 | 148 |
| 154 views::TableView* table_view_; | 149 views::TableView* table_view_; |
| 155 views::View* table_view_parent_; | 150 views::View* table_view_parent_; |
| 156 ui::TableModelObserver* observer_; | 151 ui::TableModelObserver* observer_; |
| 157 | 152 |
| 158 std::vector<Entry> entries_; | 153 std::vector<Entry> entries_; |
| 159 | 154 |
| 160 base::WeakPtrFactory<CatalogViewerContents> weak_ptr_factory_; | |
| 161 | |
| 162 DISALLOW_COPY_AND_ASSIGN(CatalogViewerContents); | 155 DISALLOW_COPY_AND_ASSIGN(CatalogViewerContents); |
| 163 }; | 156 }; |
| 164 | 157 |
| 165 } // namespace | 158 } // namespace |
| 166 | 159 |
| 167 CatalogViewer::CatalogViewer() {} | 160 CatalogViewer::CatalogViewer() {} |
| 168 CatalogViewer::~CatalogViewer() {} | 161 CatalogViewer::~CatalogViewer() {} |
| 169 | 162 |
| 170 void CatalogViewer::RemoveWindow(views::Widget* window) { | 163 void CatalogViewer::RemoveWindow(views::Widget* window) { |
| 171 auto it = std::find(windows_.begin(), windows_.end(), window); | 164 auto it = std::find(windows_.begin(), windows_.end(), window); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 windows_.push_back(window); | 201 windows_.push_back(window); |
| 209 } | 202 } |
| 210 | 203 |
| 211 void CatalogViewer::Create(shell::Connection* connection, | 204 void CatalogViewer::Create(shell::Connection* connection, |
| 212 mojom::LaunchableRequest request) { | 205 mojom::LaunchableRequest request) { |
| 213 bindings_.AddBinding(this, std::move(request)); | 206 bindings_.AddBinding(this, std::move(request)); |
| 214 } | 207 } |
| 215 | 208 |
| 216 } // namespace catalog_viewer | 209 } // namespace catalog_viewer |
| 217 } // namespace mash | 210 } // namespace mash |
| OLD | NEW |