Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(245)

Side by Side Diff: mash/catalog_viewer/catalog_viewer.cc

Issue 2488873004: Filter CatalogViewer results by capability. (Closed)
Patch Set: Better UI. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "mojo/public/cpp/bindings/binding.h" 15 #include "mojo/public/cpp/bindings/binding.h"
16 #include "services/catalog/public/interfaces/catalog.mojom.h" 16 #include "services/catalog/public/interfaces/catalog.mojom.h"
17 #include "services/service_manager/public/cpp/connection.h" 17 #include "services/service_manager/public/cpp/connection.h"
18 #include "services/service_manager/public/cpp/connector.h" 18 #include "services/service_manager/public/cpp/connector.h"
19 #include "services/service_manager/public/cpp/interface_registry.h" 19 #include "services/service_manager/public/cpp/interface_registry.h"
20 #include "services/service_manager/public/cpp/service_context.h" 20 #include "services/service_manager/public/cpp/service_context.h"
21 #include "ui/base/models/table_model.h" 21 #include "ui/base/models/table_model.h"
22 #include "ui/base/resource/resource_bundle.h" 22 #include "ui/base/resource/resource_bundle.h"
23 #include "ui/resources/grit/ui_resources.h" 23 #include "ui/resources/grit/ui_resources.h"
24 #include "ui/views/background.h" 24 #include "ui/views/background.h"
25 #include "ui/views/controls/label.h"
25 #include "ui/views/controls/table/table_view.h" 26 #include "ui/views/controls/table/table_view.h"
26 #include "ui/views/controls/table/table_view_observer.h" 27 #include "ui/views/controls/table/table_view_observer.h"
28 #include "ui/views/controls/textfield/textfield.h"
29 #include "ui/views/controls/textfield/textfield_controller.h"
30 #include "ui/views/layout/grid_layout.h"
27 #include "ui/views/mus/aura_init.h" 31 #include "ui/views/mus/aura_init.h"
28 #include "ui/views/mus/window_manager_connection.h" 32 #include "ui/views/mus/window_manager_connection.h"
29 #include "ui/views/widget/widget_delegate.h" 33 #include "ui/views/widget/widget_delegate.h"
30 34
31 namespace mash { 35 namespace mash {
32 namespace catalog_viewer { 36 namespace catalog_viewer {
33 namespace { 37 namespace {
34 38
35 class CatalogViewerContents : public views::WidgetDelegateView, 39 class CatalogViewerContents : public views::WidgetDelegateView,
36 public ui::TableModel { 40 public ui::TableModel,
41 public views::TextfieldController {
37 public: 42 public:
38 CatalogViewerContents(CatalogViewer* catalog_viewer, 43 CatalogViewerContents(CatalogViewer* catalog_viewer,
39 catalog::mojom::CatalogPtr catalog) 44 catalog::mojom::CatalogPtr catalog)
40 : catalog_viewer_(catalog_viewer), 45 : catalog_viewer_(catalog_viewer),
41 catalog_(std::move(catalog)), 46 catalog_(std::move(catalog)),
42 table_view_(nullptr), 47 table_view_(nullptr),
43 table_view_parent_(nullptr), 48 table_view_parent_(nullptr),
44 observer_(nullptr) { 49 observer_(nullptr),
45 table_view_ = new views::TableView(this, GetColumns(), views::TEXT_ONLY, 50 capability_(new views::Textfield) {
46 false); 51 const int kPadding = 5;
47 set_background(views::Background::CreateStandardPanelBackground()); 52 set_background(views::Background::CreateStandardPanelBackground());
48 53
54 views::GridLayout* layout = new views::GridLayout(this);
55 layout->SetInsets(kPadding, kPadding, kPadding, kPadding);
56 SetLayoutManager(layout);
57
58 views::ColumnSet* columns = layout->AddColumnSet(0);
59 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 0,
60 views::GridLayout::USE_PREF, 0, 0);
61 columns->AddPaddingColumn(0, kPadding);
62 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
63 views::GridLayout::USE_PREF, 0, 0);
64
65 layout->StartRow(0, 0);
66 layout->AddView(new views::Label(base::WideToUTF16(L"Capability:")));
67 layout->AddView(capability_);
68 capability_->set_controller(this);
69
70 layout->StartRowWithPadding(1, 0, 0, kPadding);
71 table_view_ =
72 new views::TableView(this, GetColumns(), views::TEXT_ONLY, false);
49 table_view_parent_ = table_view_->CreateParentIfNecessary(); 73 table_view_parent_ = table_view_->CreateParentIfNecessary();
50 AddChildView(table_view_parent_); 74 layout->AddView(table_view_parent_, 3, 1, views::GridLayout::FILL,
75 views::GridLayout::FILL);
51 76
52 // We don't want to show an empty UI so we just block until we have all the 77 GetAllEntries();
53 // data. GetEntries is a sync call.
54 std::vector<catalog::mojom::EntryPtr> entries;
55 bool got = catalog_->GetEntries(base::nullopt, &entries);
56 if (got) {
57 for (auto& entry : entries)
58 entries_.push_back(Entry(entry->display_name, entry->name));
59 observer_->OnModelChanged();
60 }
61 } 78 }
62 ~CatalogViewerContents() override { 79 ~CatalogViewerContents() override {
63 table_view_->SetModel(nullptr); 80 table_view_->SetModel(nullptr);
64 catalog_viewer_->RemoveWindow(GetWidget()); 81 catalog_viewer_->RemoveWindow(GetWidget());
65 } 82 }
66 83
67 private: 84 private:
68 struct Entry { 85 struct Entry {
69 Entry(const std::string& name, const std::string& url) 86 Entry(const std::string& name, const std::string& url)
70 : name(name), url(url) {} 87 : name(name), url(url) {}
(...skipping 11 matching lines...) Expand all
82 bool CanMaximize() const override { return true; } 99 bool CanMaximize() const override { return true; }
83 bool CanMinimize() const override { return true; } 100 bool CanMinimize() const override { return true; }
84 101
85 gfx::ImageSkia GetWindowAppIcon() override { 102 gfx::ImageSkia GetWindowAppIcon() override {
86 // TODO(jamescook): Create a new .pak file for this app and make a custom 103 // TODO(jamescook): Create a new .pak file for this app and make a custom
87 // icon, perhaps one that looks like the Chrome OS task viewer icon. 104 // icon, perhaps one that looks like the Chrome OS task viewer icon.
88 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 105 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
89 return *rb.GetImageSkiaNamed(IDR_NOTIFICATION_SETTINGS); 106 return *rb.GetImageSkiaNamed(IDR_NOTIFICATION_SETTINGS);
90 } 107 }
91 108
92 // Overridden from views::View:
93 void Layout() override {
94 gfx::Rect bounds = GetLocalBounds();
95 bounds.Inset(10, 10);
96 table_view_parent_->SetBoundsRect(bounds);
97 }
98
99 // Overridden from ui::TableModel: 109 // Overridden from ui::TableModel:
100 int RowCount() override { 110 int RowCount() override {
101 return static_cast<int>(entries_.size()); 111 return static_cast<int>(entries_.size());
102 } 112 }
103 base::string16 GetText(int row, int column_id) override { 113 base::string16 GetText(int row, int column_id) override {
104 switch(column_id) { 114 switch(column_id) {
105 case 0: 115 case 0:
106 DCHECK(row < static_cast<int>(entries_.size())); 116 DCHECK(row < static_cast<int>(entries_.size()));
107 return base::UTF8ToUTF16(entries_[row].name); 117 return base::UTF8ToUTF16(entries_[row].name);
108 case 1: 118 case 1:
109 DCHECK(row < static_cast<int>(entries_.size())); 119 DCHECK(row < static_cast<int>(entries_.size()));
110 return base::UTF8ToUTF16(entries_[row].url); 120 return base::UTF8ToUTF16(entries_[row].url);
111 default: 121 default:
112 NOTREACHED(); 122 NOTREACHED();
113 break; 123 break;
114 } 124 }
115 return base::string16(); 125 return base::string16();
116 } 126 }
127
128 // Overriden from views::TextFieldController:
129 bool HandleKeyEvent(views::Textfield* sender,
130 const ui::KeyEvent& key_event) override {
131 if (key_event.type() != ui::ET_KEY_PRESSED ||
132 key_event.key_code() != ui::VKEY_RETURN)
133 return false;
134
135 if (sender->text().length()) {
136 catalog_->GetEntriesProvidingCapability(
137 base::UTF16ToUTF8(sender->text()),
138 base::Bind(&CatalogViewerContents::OnReceivedEntries,
139 base::Unretained(this)));
140 } else {
141 GetAllEntries();
142 }
143
144 return true;
145 }
146
147 void GetAllEntries() {
148 // We don't want to show an empty UI so we just block until we have all the
149 // data. GetEntries is a sync call.
150 std::vector<catalog::mojom::EntryPtr> entries;
151 if (catalog_->GetEntries(base::nullopt, &entries))
152 UpdateEntries(entries);
153 }
154
155 void OnReceivedEntries(std::vector<catalog::mojom::EntryPtr> entries) {
156 UpdateEntries(entries);
157 }
158
159 void UpdateEntries(const std::vector<catalog::mojom::EntryPtr>& entries) {
160 entries_.clear();
161 for (auto& entry : entries)
162 entries_.push_back(Entry(entry->display_name, entry->name));
163 observer_->OnModelChanged();
164 }
165
117 void SetObserver(ui::TableModelObserver* observer) override { 166 void SetObserver(ui::TableModelObserver* observer) override {
118 observer_ = observer; 167 observer_ = observer;
119 } 168 }
120 169
121 static std::vector<ui::TableColumn> GetColumns() { 170 static std::vector<ui::TableColumn> GetColumns() {
122 std::vector<ui::TableColumn> columns; 171 std::vector<ui::TableColumn> columns;
123 172
124 ui::TableColumn name_column; 173 ui::TableColumn name_column;
125 name_column.id = 0; 174 name_column.id = 0;
126 // TODO(beng): use resources. 175 // TODO(beng): use resources.
(...skipping 14 matching lines...) Expand all
141 190
142 return columns; 191 return columns;
143 } 192 }
144 193
145 CatalogViewer* catalog_viewer_; 194 CatalogViewer* catalog_viewer_;
146 catalog::mojom::CatalogPtr catalog_; 195 catalog::mojom::CatalogPtr catalog_;
147 196
148 views::TableView* table_view_; 197 views::TableView* table_view_;
149 views::View* table_view_parent_; 198 views::View* table_view_parent_;
150 ui::TableModelObserver* observer_; 199 ui::TableModelObserver* observer_;
200 views::Textfield* capability_;
151 201
152 std::vector<Entry> entries_; 202 std::vector<Entry> entries_;
153 203
154 DISALLOW_COPY_AND_ASSIGN(CatalogViewerContents); 204 DISALLOW_COPY_AND_ASSIGN(CatalogViewerContents);
155 }; 205 };
156 206
157 } // namespace 207 } // namespace
158 208
159 CatalogViewer::CatalogViewer() {} 209 CatalogViewer::CatalogViewer() {}
160 CatalogViewer::~CatalogViewer() {} 210 CatalogViewer::~CatalogViewer() {}
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 windows_.push_back(window); 250 windows_.push_back(window);
201 } 251 }
202 252
203 void CatalogViewer::Create(const service_manager::Identity& remote_identity, 253 void CatalogViewer::Create(const service_manager::Identity& remote_identity,
204 mojom::LaunchableRequest request) { 254 mojom::LaunchableRequest request) {
205 bindings_.AddBinding(this, std::move(request)); 255 bindings_.AddBinding(this, std::move(request));
206 } 256 }
207 257
208 } // namespace catalog_viewer 258 } // namespace catalog_viewer
209 } // namespace mash 259 } // namespace mash
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698