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 "mash/shelf/shelf_view.h" |
| 6 |
| 7 #include "base/strings/stringprintf.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "mojo/shell/public/cpp/application_impl.h" |
| 10 #include "ui/gfx/canvas.h" |
| 11 #include "ui/views/controls/button/label_button.h" |
| 12 #include "ui/views/layout/box_layout.h" |
| 13 |
| 14 namespace mash { |
| 15 namespace shelf { |
| 16 |
| 17 enum ShelfButtonID { |
| 18 SHELF_BUTTON_VIEWS_EXAMPLES, |
| 19 SHELF_BUTTON_TASK_VIEWER, |
| 20 }; |
| 21 |
| 22 ShelfView::ShelfView(mojo::ApplicationImpl* app) : app_(app), binding_(this) { |
| 23 app->ConnectToService("mojo:desktop_wm", &user_window_controller_); |
| 24 |
| 25 mash::wm::mojom::UserWindowObserverPtr observer; |
| 26 mojo::InterfaceRequest<mash::wm::mojom::UserWindowObserver> request = |
| 27 mojo::GetProxy(&observer); |
| 28 user_window_controller_->AddUserWindowObserver(std::move(observer)); |
| 29 binding_.Bind(std::move(request)); |
| 30 |
| 31 SetLayoutManager( |
| 32 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); |
| 33 |
| 34 views::LabelButton* views_examples = |
| 35 new views::LabelButton(this, base::ASCIIToUTF16("Views Examples")); |
| 36 views_examples->set_tag(SHELF_BUTTON_VIEWS_EXAMPLES); |
| 37 AddChildView(views_examples); |
| 38 |
| 39 views::LabelButton* task_viewer = |
| 40 new views::LabelButton(this, base::ASCIIToUTF16("Task Viewer")); |
| 41 task_viewer->set_tag(SHELF_BUTTON_TASK_VIEWER); |
| 42 AddChildView(task_viewer); |
| 43 } |
| 44 |
| 45 ShelfView::~ShelfView() {} |
| 46 |
| 47 void ShelfView::OnPaint(gfx::Canvas* canvas) { |
| 48 canvas->FillRect(GetLocalBounds(), SK_ColorYELLOW); |
| 49 views::View::OnPaint(canvas); |
| 50 } |
| 51 |
| 52 gfx::Size ShelfView::GetPreferredSize() const { |
| 53 return gfx::Size(1, 48); |
| 54 } |
| 55 |
| 56 views::View* ShelfView::GetContentsView() { |
| 57 return this; |
| 58 } |
| 59 |
| 60 void ShelfView::ButtonPressed(views::Button* sender, const ui::Event& event) { |
| 61 if (sender->tag() == SHELF_BUTTON_VIEWS_EXAMPLES) |
| 62 app_->ConnectToApplication("mojo:views_examples"); |
| 63 else if (sender->tag() == SHELF_BUTTON_TASK_VIEWER) |
| 64 app_->ConnectToApplication("mojo:task_viewer"); |
| 65 else |
| 66 user_window_controller_->FocusUserWindow(sender->tag()); |
| 67 } |
| 68 |
| 69 void ShelfView::OnUserWindowObserverAdded(mojo::Array<uint32_t> window_ids) { |
| 70 for (size_t i = 0; i < window_ids.size(); ++i) |
| 71 OnUserWindowAdded(window_ids[i]); |
| 72 } |
| 73 |
| 74 void ShelfView::OnUserWindowAdded(uint32_t window_id) { |
| 75 // TODO(msw): Get the actual window title and icon. |
| 76 views::LabelButton* open_window_button = new views::LabelButton( |
| 77 this, base::ASCIIToUTF16(base::StringPrintf("Window %d", window_id))); |
| 78 open_window_button->set_tag(window_id); |
| 79 open_window_buttons_.push_back(open_window_button); |
| 80 AddChildView(open_window_button); |
| 81 Layout(); |
| 82 SchedulePaint(); |
| 83 } |
| 84 |
| 85 void ShelfView::OnUserWindowRemoved(uint32_t window_id) { |
| 86 for (size_t i = 0; i < open_window_buttons_.size(); ++i) { |
| 87 if (static_cast<uint32_t>(open_window_buttons_[i]->tag()) == window_id) { |
| 88 views::LabelButton* button = open_window_buttons_[i]; |
| 89 open_window_buttons_.erase(open_window_buttons_.begin() + i); |
| 90 RemoveChildView(button); |
| 91 delete button; |
| 92 return; |
| 93 } |
| 94 } |
| 95 } |
| 96 |
| 97 } // namespace shelf |
| 98 } // namespace mash |
OLD | NEW |