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