OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "athena/task/public/task_manager.h" |
| 6 |
| 7 #include "athena/screen/public/screen_manager.h" |
| 8 #include "athena/task/public/task.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "ui/aura/window.h" |
| 11 #include "ui/views/background.h" |
| 12 #include "ui/views/controls/label.h" |
| 13 #include "ui/views/controls/native/native_view_host.h" |
| 14 #include "ui/views/layout/layout_manager.h" |
| 15 #include "ui/views/view.h" |
| 16 #include "ui/views/widget/widget.h" |
| 17 |
| 18 namespace athena { |
| 19 |
| 20 namespace { |
| 21 |
| 22 class TaskWidget : public views::LayoutManager { |
| 23 public: |
| 24 explicit TaskWidget(Task* task) |
| 25 : task_(task), |
| 26 container_(NULL), |
| 27 title_(NULL), |
| 28 host_(NULL), |
| 29 widget_(NULL) { |
| 30 container_ = new views::View; |
| 31 |
| 32 title_ = new views::Label(); |
| 33 title_->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 34 const gfx::FontList& font_list = title_->font_list(); |
| 35 title_->SetFontList(font_list.Derive(1, gfx::Font::BOLD)); |
| 36 title_->SetEnabledColor(SK_ColorBLACK); |
| 37 title_->SetShadowColors(SK_ColorWHITE, SK_ColorWHITE); |
| 38 title_->set_shadow_blur(.5); |
| 39 container_->AddChildView(title_); |
| 40 container_->SetLayoutManager(this); |
| 41 |
| 42 host_ = new views::NativeViewHost(); |
| 43 container_->AddChildView(host_); |
| 44 |
| 45 widget_ = new views::Widget; |
| 46 views::Widget::InitParams params( |
| 47 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 48 params.parent = ScreenManager::Get()->GetContext(); |
| 49 params.delegate = NULL; |
| 50 params.activatable = views::Widget::InitParams::ACTIVATABLE_YES; |
| 51 widget_->Init(params); |
| 52 widget_->SetContentsView(container_); |
| 53 |
| 54 host_->Attach(task->GetNativeWindow()); |
| 55 } |
| 56 |
| 57 virtual ~TaskWidget() {} |
| 58 |
| 59 void Show() { |
| 60 Update(); |
| 61 widget_->Show(); |
| 62 } |
| 63 |
| 64 void Update() { |
| 65 title_->SetText(base::UTF8ToUTF16(task_->GetTitle())); |
| 66 title_->set_background(views::Background::CreateSolidBackground( |
| 67 task_->GetRepresentativeColor())); |
| 68 } |
| 69 |
| 70 private: |
| 71 // views::LayoutManager: |
| 72 virtual void Layout(views::View* host) OVERRIDE { |
| 73 CHECK_EQ(container_, host); |
| 74 const gfx::Rect& host_bounds = host->bounds(); |
| 75 const int kTitleHeight = 25; |
| 76 title_->SetBounds(0, 0, host_bounds.width(), kTitleHeight); |
| 77 host_->SetBounds(0, kTitleHeight, host_bounds.width(), |
| 78 host_bounds.height() - kTitleHeight); |
| 79 } |
| 80 |
| 81 virtual gfx::Size GetPreferredSize(const views::View* host) const OVERRIDE { |
| 82 CHECK_EQ(container_, host); |
| 83 gfx::Size size; |
| 84 gfx::Size label_size = title_->GetPreferredSize(); |
| 85 gfx::Size host_size = host_->GetPreferredSize(); |
| 86 |
| 87 size.set_width(std::max(label_size.width(), host_size.width())); |
| 88 size.set_height(label_size.height() + host_size.height()); |
| 89 return size; |
| 90 } |
| 91 |
| 92 Task* task_; |
| 93 views::View* container_; |
| 94 views::Label* title_; |
| 95 views::NativeViewHost* host_; |
| 96 views::Widget* widget_; |
| 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(TaskWidget); |
| 99 }; |
| 100 |
| 101 class TaskManagerImpl : public TaskManager { |
| 102 public: |
| 103 TaskManagerImpl() {} |
| 104 virtual ~TaskManagerImpl() {} |
| 105 |
| 106 // TaskManager: |
| 107 virtual void AddTask(Task* task) OVERRIDE { |
| 108 CHECK(task_widgets_.end() == task_widgets_.find(task)); |
| 109 TaskWidget* container = new TaskWidget(task); |
| 110 task_widgets_[task] = container; |
| 111 container->Show(); |
| 112 } |
| 113 |
| 114 virtual void RemoveTask(Task* task) OVERRIDE { |
| 115 std::map<Task*, TaskWidget*>::iterator find = task_widgets_.find(task); |
| 116 if (find != task_widgets_.end()) |
| 117 task_widgets_.erase(task); |
| 118 } |
| 119 |
| 120 virtual void UpdateTask(Task* task) OVERRIDE { |
| 121 std::map<Task*, TaskWidget*>::iterator find = task_widgets_.find(task); |
| 122 if (find != task_widgets_.end()) |
| 123 find->second->Update(); |
| 124 } |
| 125 |
| 126 private: |
| 127 std::map<Task*, TaskWidget*> task_widgets_; |
| 128 |
| 129 DISALLOW_COPY_AND_ASSIGN(TaskManagerImpl); |
| 130 }; |
| 131 |
| 132 static TaskManagerImpl* g_instance; |
| 133 |
| 134 } // namespace |
| 135 |
| 136 // static |
| 137 TaskManager* TaskManager::Create() { |
| 138 CHECK(!g_instance); |
| 139 g_instance = new TaskManagerImpl(); |
| 140 return g_instance; |
| 141 } |
| 142 |
| 143 TaskManager* TaskManager::Get() { |
| 144 return g_instance; |
| 145 } |
| 146 |
| 147 void TaskManager::Shutdown() { |
| 148 CHECK(g_instance); |
| 149 delete g_instance; |
| 150 g_instance = NULL; |
| 151 } |
| 152 |
| 153 } // namespace athena |
OLD | NEW |