| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/aura_shell/launcher/launcher_view.h" | 5 #include "ui/aura_shell/launcher/launcher_view.h" |
| 6 | 6 |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "grit/ui_resources.h" |
| 8 #include "ui/aura/desktop.h" | 9 #include "ui/aura/desktop.h" |
| 9 #include "ui/aura_shell/aura_shell_export.h" | 10 #include "ui/aura_shell/aura_shell_export.h" |
| 10 #include "ui/aura_shell/launcher/launcher_model.h" | 11 #include "ui/aura_shell/launcher/launcher_model.h" |
| 11 #include "ui/aura_shell/shell.h" | 12 #include "ui/aura_shell/shell.h" |
| 13 #include "ui/aura_shell/shell_delegate.h" |
| 12 #include "ui/aura_shell/shell_window_ids.h" | 14 #include "ui/aura_shell/shell_window_ids.h" |
| 15 #include "ui/base/resource/resource_bundle.h" |
| 13 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
| 14 #include "ui/gfx/compositor/layer.h" | 17 #include "ui/gfx/compositor/layer.h" |
| 18 #include "ui/gfx/image/image.h" |
| 19 #include "views/controls/button/image_button.h" |
| 15 #include "views/widget/widget.h" | 20 #include "views/widget/widget.h" |
| 16 | 21 |
| 17 using views::View; | 22 using views::View; |
| 18 | 23 |
| 19 namespace aura_shell { | 24 namespace aura_shell { |
| 20 namespace internal { | 25 namespace internal { |
| 21 | 26 |
| 22 // Padding between each view. | 27 // Padding between each view. |
| 23 static const int kHorizontalPadding = 12; | 28 static const int kHorizontalPadding = 12; |
| 24 | 29 |
| 25 // Height of the LauncherView. Hard coded to avoid resizing as items are | 30 // Height of the LauncherView. Hard coded to avoid resizing as items are |
| 26 // added/removed. | 31 // added/removed. |
| 27 static const int kPreferredHeight = 64; | 32 static const int kPreferredHeight = 64; |
| 28 | 33 |
| 29 LauncherView::LauncherView() : model_(NULL) { | 34 LauncherView::LauncherView() |
| 35 : model_(NULL), |
| 36 new_browser_button_(NULL), |
| 37 show_apps_button_(NULL) { |
| 30 } | 38 } |
| 31 | 39 |
| 32 LauncherView::~LauncherView() { | 40 LauncherView::~LauncherView() { |
| 33 // The model owns the views. | |
| 34 RemoveAllChildViews(false); | |
| 35 | |
| 36 model_->RemoveObserver(this); | 41 model_->RemoveObserver(this); |
| 37 } | 42 } |
| 38 | 43 |
| 39 void LauncherView::Init(LauncherModel* model) { | 44 void LauncherView::Init() { |
| 40 DCHECK(!model_); | 45 model_.reset(new LauncherModel); |
| 41 model_ = model; | |
| 42 model_->AddObserver(this); | 46 model_->AddObserver(this); |
| 43 for (int i = 0; i < model_->item_count(); ++i) | 47 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 44 AddChildView(model_->view_at(i)); | 48 new_browser_button_ = new views::ImageButton(this); |
| 49 new_browser_button_->SetImage( |
| 50 views::CustomButton::BS_NORMAL, |
| 51 rb.GetImageNamed(IDR_AURA_LAUNCHER_ICON_CHROME).ToSkBitmap()); |
| 52 AddChildView(new_browser_button_); |
| 53 |
| 54 const LauncherItems& items(model_->items()); |
| 55 for (LauncherItems::const_iterator i = items.begin(); i != items.end(); ++i) |
| 56 AddChildView(CreateViewForItem(*i)); |
| 57 |
| 58 show_apps_button_ = new views::ImageButton(this); |
| 59 show_apps_button_->SetImage( |
| 60 views::CustomButton::BS_NORMAL, |
| 61 rb.GetImageNamed(IDR_AURA_LAUNCHER_ICON_APPLIST).ToSkBitmap()); |
| 62 AddChildView(show_apps_button_); |
| 63 } |
| 64 |
| 65 views::View* LauncherView::CreateViewForItem(const LauncherItem& item) { |
| 66 // TODO: need to support images. |
| 67 views::ImageButton* button = new views::ImageButton(this); |
| 68 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 69 button->SetImage( |
| 70 views::CustomButton::BS_NORMAL, |
| 71 rb.GetImageNamed(IDR_AURA_LAUNCHER_TABBED_BROWSER).ToSkBitmap()); |
| 72 return button; |
| 73 } |
| 74 |
| 75 void LauncherView::Resize() { |
| 76 int y = GetWidget()->GetClientAreaScreenBounds().y(); |
| 77 gfx::Size pref(GetPreferredSize()); |
| 78 GetWidget()->SetBounds(gfx::Rect(0, y, pref.width(), pref.height())); |
| 45 } | 79 } |
| 46 | 80 |
| 47 void LauncherView::Layout() { | 81 void LauncherView::Layout() { |
| 48 // TODO: need to deal with overflow. | 82 // TODO: need to deal with overflow. |
| 49 int x = 0; | 83 int x = kHorizontalPadding; |
| 50 for (int i = 0; i < child_count(); ++i) { | 84 for (int i = 0; i < child_count(); ++i) { |
| 51 View* child = child_at(i); | 85 View* child = child_at(i); |
| 52 if (child->IsVisible()) { | 86 if (child->IsVisible()) { |
| 53 gfx::Size pref_size = child->GetPreferredSize(); | 87 gfx::Size pref_size = child->GetPreferredSize(); |
| 54 int y = (height() - pref_size.height()) / 2; | 88 int y = (height() - pref_size.height()) / 2; |
| 55 child->SetBounds(x, y, pref_size.width(), pref_size.height()); | 89 child->SetBounds(x, y, pref_size.width(), pref_size.height()); |
| 56 x += child->width() + kHorizontalPadding; | 90 x += child->width() + kHorizontalPadding; |
| 57 } | 91 } |
| 58 } | 92 } |
| 59 } | 93 } |
| 60 | 94 |
| 61 gfx::Size LauncherView::GetPreferredSize() { | 95 gfx::Size LauncherView::GetPreferredSize() { |
| 62 int x = 0; | 96 int x = kHorizontalPadding; |
| 63 for (int i = 0; i < child_count(); ++i) { | 97 for (int i = 0; i < child_count(); ++i) { |
| 64 View* child = child_at(i); | 98 View* child = child_at(i); |
| 65 if (child->IsVisible()) { | 99 if (child->IsVisible()) { |
| 66 gfx::Size pref_size = child->GetPreferredSize(); | 100 gfx::Size pref_size = child->GetPreferredSize(); |
| 67 x += pref_size.width() + kHorizontalPadding; | 101 x += pref_size.width() + kHorizontalPadding; |
| 68 } | 102 } |
| 69 } | 103 } |
| 70 if (x > 0) | |
| 71 x -= kHorizontalPadding; | |
| 72 return gfx::Size(x, kPreferredHeight); | 104 return gfx::Size(x, kPreferredHeight); |
| 73 } | 105 } |
| 74 | 106 |
| 75 void LauncherView::LauncherItemAdded(int index) { | 107 void LauncherView::LauncherItemAdded(int index) { |
| 76 // TODO: to support animations is going to require coordinate conversions. | 108 // TODO: to support animations is going to require coordinate conversions. |
| 77 AddChildViewAt(model_->view_at(index), index); | 109 AddChildViewAt(CreateViewForItem(model_->items()[index]), index + 1); |
| 110 Resize(); |
| 78 } | 111 } |
| 79 | 112 |
| 80 void LauncherView::LauncherItemRemoved(int index) { | 113 void LauncherView::LauncherItemRemoved(int index) { |
| 81 // TODO: to support animations is going to require coordinate conversions. | 114 // TODO: to support animations is going to require coordinate conversions. |
| 82 RemoveChildView(child_at(index)); | 115 RemoveChildView(child_at(index + 1)); |
| 116 Resize(); |
| 83 } | 117 } |
| 84 | 118 |
| 85 void LauncherView::LauncherSelectionChanged() { | 119 void LauncherView::LauncherItemImagesChanged(int index) { |
| 120 // TODO: implement me. |
| 86 } | 121 } |
| 87 | 122 |
| 88 views::Widget* CreateLauncher(LauncherModel* model) { | 123 void LauncherView::ButtonPressed(views::Button* sender, |
| 124 const views::Event& event) { |
| 125 ShellDelegate* delegate = Shell::GetInstance()->delegate(); |
| 126 if (!delegate) |
| 127 return; |
| 128 if (sender == new_browser_button_) { |
| 129 delegate->CreateNewWindow(); |
| 130 } else if (sender == show_apps_button_) { |
| 131 delegate->ShowApps(); |
| 132 } else { |
| 133 int index = GetIndexOf(sender); |
| 134 DCHECK_NE(-1, index); |
| 135 // TODO: animations will require coordinate transforms. |
| 136 delegate->LauncherItemClicked(model_->items()[index - 1]); |
| 137 } |
| 138 } |
| 139 |
| 140 views::Widget* CreateLauncher() { |
| 89 views::Widget* launcher_widget = new views::Widget; | 141 views::Widget* launcher_widget = new views::Widget; |
| 90 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); | 142 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); |
| 91 params.bounds = gfx::Rect(0, 0, 300, kPreferredHeight); | 143 params.bounds = gfx::Rect(0, 0, 300, kPreferredHeight); |
| 92 params.parent = Shell::GetInstance()->GetContainer( | 144 params.parent = Shell::GetInstance()->GetContainer( |
| 93 aura_shell::internal::kShellWindowId_LauncherContainer); | 145 aura_shell::internal::kShellWindowId_LauncherContainer); |
| 94 LauncherView* launcher_view = new LauncherView; | 146 LauncherView* launcher_view = new LauncherView; |
| 95 launcher_view->Init(model); | 147 launcher_view->Init(); |
| 96 params.delegate = launcher_view; | 148 params.delegate = launcher_view; |
| 97 launcher_widget->Init(params); | 149 launcher_widget->Init(params); |
| 98 launcher_widget->GetNativeWindow()->layer()->SetOpacity(0.8f); | 150 launcher_widget->GetNativeWindow()->layer()->SetOpacity(0.8f); |
| 99 gfx::Size pref = static_cast<views::View*>(launcher_view)->GetPreferredSize(); | 151 gfx::Size pref = static_cast<views::View*>(launcher_view)->GetPreferredSize(); |
| 100 launcher_widget->SetBounds(gfx::Rect(0, 0, pref.width(), pref.height())); | 152 launcher_widget->SetBounds(gfx::Rect(0, 0, pref.width(), pref.height())); |
| 101 launcher_widget->SetContentsView(launcher_view); | 153 launcher_widget->SetContentsView(launcher_view); |
| 102 launcher_widget->Show(); | 154 launcher_widget->Show(); |
| 103 launcher_widget->GetNativeView()->set_name("LauncherView"); | 155 launcher_widget->GetNativeView()->set_name("LauncherView"); |
| 104 return launcher_widget; | 156 return launcher_widget; |
| 105 } | 157 } |
| 106 | 158 |
| 107 } // namespace internal | 159 } // namespace internal |
| 108 } // namespace aura_shell | 160 } // namespace aura_shell |
| OLD | NEW |