| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/ui/views/toolbar/app_menu_button.h" | 5 #include "chrome/browser/ui/views/toolbar/app_menu_button.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" |
| 7 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/memory/ptr_util.h" |
| 8 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "cc/paint/paint_flags.h" |
| 12 #include "chrome/app/vector_icons/vector_icons.h" | 15 #include "chrome/app/vector_icons/vector_icons.h" |
| 13 #include "chrome/browser/themes/theme_properties.h" | 16 #include "chrome/browser/themes/theme_properties.h" |
| 14 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/browser_otr_state.h" | 18 #include "chrome/browser/ui/browser_otr_state.h" |
| 16 #include "chrome/browser/ui/layout_constants.h" | 19 #include "chrome/browser/ui/layout_constants.h" |
| 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "chrome/browser/ui/toolbar/app_menu_model.h" | 21 #include "chrome/browser/ui/toolbar/app_menu_model.h" |
| 18 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h" | 22 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h" |
| 19 #include "chrome/browser/ui/views/toolbar/app_menu.h" | 23 #include "chrome/browser/ui/views/toolbar/app_menu.h" |
| 24 #include "chrome/browser/ui/views/toolbar/app_menu_animation.h" |
| 20 #include "chrome/browser/ui/views/toolbar/toolbar_button.h" | 25 #include "chrome/browser/ui/views/toolbar/toolbar_button.h" |
| 21 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" | 26 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
| 27 #include "chrome/common/chrome_switches.h" |
| 22 #include "chrome/grit/theme_resources.h" | 28 #include "chrome/grit/theme_resources.h" |
| 23 #include "ui/base/resource/resource_bundle.h" | 29 #include "ui/base/resource/resource_bundle.h" |
| 24 #include "ui/base/theme_provider.h" | 30 #include "ui/base/theme_provider.h" |
| 25 #include "ui/gfx/color_palette.h" | 31 #include "ui/gfx/color_palette.h" |
| 26 #include "ui/gfx/paint_vector_icon.h" | 32 #include "ui/gfx/paint_vector_icon.h" |
| 27 #include "ui/keyboard/keyboard_controller.h" | 33 #include "ui/keyboard/keyboard_controller.h" |
| 28 #include "ui/views/controls/button/label_button_border.h" | 34 #include "ui/views/controls/button/label_button_border.h" |
| 29 #include "ui/views/controls/menu/menu_listener.h" | 35 #include "ui/views/controls/menu/menu_listener.h" |
| 30 #include "ui/views/metrics.h" | 36 #include "ui/views/metrics.h" |
| 31 | 37 |
| 38 namespace { |
| 39 const float kIconSize = 16; |
| 40 } // namespace |
| 41 |
| 32 // static | 42 // static |
| 33 bool AppMenuButton::g_open_app_immediately_for_testing = false; | 43 bool AppMenuButton::g_open_app_immediately_for_testing = false; |
| 34 | 44 |
| 35 AppMenuButton::AppMenuButton(ToolbarView* toolbar_view) | 45 AppMenuButton::AppMenuButton(ToolbarView* toolbar_view) |
| 36 : views::MenuButton(base::string16(), toolbar_view, false), | 46 : views::MenuButton(base::string16(), toolbar_view, false), |
| 37 severity_(AppMenuIconController::Severity::NONE), | 47 severity_(AppMenuIconController::Severity::NONE), |
| 38 type_(AppMenuIconController::IconType::NONE), | 48 type_(AppMenuIconController::IconType::NONE), |
| 39 toolbar_view_(toolbar_view), | 49 toolbar_view_(toolbar_view), |
| 40 margin_trailing_(0), | 50 margin_trailing_(0), |
| 41 weak_factory_(this) { | 51 weak_factory_(this) { |
| 42 SetInkDropMode(InkDropMode::ON); | 52 SetInkDropMode(InkDropMode::ON); |
| 43 SetFocusPainter(nullptr); | 53 SetFocusPainter(nullptr); |
| 54 |
| 55 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 56 if (command_line->HasSwitch(switches::kGlobalErrorMenuIcon)) { |
| 57 std::string flag = |
| 58 command_line->GetSwitchValueASCII(switches::kGlobalErrorMenuIcon); |
| 59 if (flag != switches::kGlobalErrorMenuIconOldBehavior) { |
| 60 if (flag == switches::kGlobalErrorMenuIconPersistentClosedState) { |
| 61 Browser* browser = toolbar_view_->browser(); |
| 62 browser->tab_strip_model()->AddObserver(this); |
| 63 animation_ = base::MakeUnique<AppMenuAnimation>(this, true); |
| 64 } else { |
| 65 animation_ = base::MakeUnique<AppMenuAnimation>(this, false); |
| 66 } |
| 67 } |
| 68 } |
| 44 } | 69 } |
| 45 | 70 |
| 46 AppMenuButton::~AppMenuButton() {} | 71 AppMenuButton::~AppMenuButton() {} |
| 47 | 72 |
| 48 void AppMenuButton::SetSeverity(AppMenuIconController::IconType type, | 73 void AppMenuButton::SetSeverity(AppMenuIconController::IconType type, |
| 49 AppMenuIconController::Severity severity, | 74 AppMenuIconController::Severity severity, |
| 50 bool animate) { | 75 bool animate) { |
| 51 type_ = type; | 76 type_ = type; |
| 52 severity_ = severity; | 77 severity_ = severity; |
| 53 UpdateIcon(); | 78 UpdateIcon(animate); |
| 54 } | 79 } |
| 55 | 80 |
| 56 void AppMenuButton::ShowMenu(bool for_drop) { | 81 void AppMenuButton::ShowMenu(bool for_drop) { |
| 57 if (menu_ && menu_->IsShowing()) | 82 if (menu_ && menu_->IsShowing()) |
| 58 return; | 83 return; |
| 59 | 84 |
| 60 #if defined(USE_AURA) | 85 #if defined(USE_AURA) |
| 61 keyboard::KeyboardController* keyboard_controller = | 86 keyboard::KeyboardController* keyboard_controller = |
| 62 keyboard::KeyboardController::GetInstance(); | 87 keyboard::KeyboardController::GetInstance(); |
| 63 if (keyboard_controller && keyboard_controller->keyboard_visible()) { | 88 if (keyboard_controller && keyboard_controller->keyboard_visible()) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 78 base::TimeTicks menu_open_time = base::TimeTicks::Now(); | 103 base::TimeTicks menu_open_time = base::TimeTicks::Now(); |
| 79 menu_->RunMenu(this); | 104 menu_->RunMenu(this); |
| 80 | 105 |
| 81 if (!for_drop) { | 106 if (!for_drop) { |
| 82 // Record the time-to-action for the menu. We don't record in the case of a | 107 // Record the time-to-action for the menu. We don't record in the case of a |
| 83 // drag-and-drop command because menus opened for drag-and-drop don't block | 108 // drag-and-drop command because menus opened for drag-and-drop don't block |
| 84 // the message loop. | 109 // the message loop. |
| 85 UMA_HISTOGRAM_TIMES("Toolbar.AppMenuTimeToAction", | 110 UMA_HISTOGRAM_TIMES("Toolbar.AppMenuTimeToAction", |
| 86 base::TimeTicks::Now() - menu_open_time); | 111 base::TimeTicks::Now() - menu_open_time); |
| 87 } | 112 } |
| 113 |
| 114 if (severity_ != AppMenuIconController::Severity::NONE) |
| 115 animation_->StartAnimation(); |
| 88 } | 116 } |
| 89 | 117 |
| 90 void AppMenuButton::CloseMenu() { | 118 void AppMenuButton::CloseMenu() { |
| 91 if (menu_) | 119 if (menu_) |
| 92 menu_->CloseMenu(); | 120 menu_->CloseMenu(); |
| 93 menu_.reset(); | 121 menu_.reset(); |
| 94 } | 122 } |
| 95 | 123 |
| 96 bool AppMenuButton::IsMenuShowing() const { | 124 bool AppMenuButton::IsMenuShowing() const { |
| 97 return menu_ && menu_->IsShowing(); | 125 return menu_ && menu_->IsShowing(); |
| 98 } | 126 } |
| 99 | 127 |
| 100 void AppMenuButton::AddMenuListener(views::MenuListener* listener) { | 128 void AppMenuButton::AddMenuListener(views::MenuListener* listener) { |
| 101 menu_listeners_.AddObserver(listener); | 129 menu_listeners_.AddObserver(listener); |
| 102 } | 130 } |
| 103 | 131 |
| 104 void AppMenuButton::RemoveMenuListener(views::MenuListener* listener) { | 132 void AppMenuButton::RemoveMenuListener(views::MenuListener* listener) { |
| 105 menu_listeners_.RemoveObserver(listener); | 133 menu_listeners_.RemoveObserver(listener); |
| 106 } | 134 } |
| 107 | 135 |
| 108 gfx::Size AppMenuButton::GetPreferredSize() const { | 136 gfx::Size AppMenuButton::GetPreferredSize() const { |
| 109 gfx::Rect rect(image()->GetPreferredSize()); | 137 gfx::Rect rect(gfx::Size(kIconSize, kIconSize)); |
| 110 rect.Inset(gfx::Insets(-ToolbarButton::kInteriorPadding)); | 138 rect.Inset(gfx::Insets(-ToolbarButton::kInteriorPadding)); |
| 111 return rect.size(); | 139 return rect.size(); |
| 112 } | 140 } |
| 113 | 141 |
| 114 void AppMenuButton::UpdateIcon() { | 142 void AppMenuButton::Layout() { |
| 115 SkColor color = gfx::kPlaceholderColor; | 143 if (animation_) { |
| 144 ink_drop_container()->SetBoundsRect(GetLocalBounds()); |
| 145 image()->SetBoundsRect(GetLocalBounds()); |
| 146 return; |
| 147 } |
| 148 |
| 149 views::MenuButton::Layout(); |
| 150 } |
| 151 |
| 152 void AppMenuButton::OnPaint(gfx::Canvas* canvas) { |
| 153 if (!animation_) { |
| 154 views::MenuButton::OnPaint(canvas); |
| 155 return; |
| 156 } |
| 157 |
| 158 gfx::Rect bounds = GetLocalBounds(); |
| 159 bounds.Inset(gfx::Insets(ToolbarButton::kInteriorPadding)); |
| 160 animation_->PaintAppMenu(canvas, bounds); |
| 161 } |
| 162 |
| 163 void AppMenuButton::TabInsertedAt(TabStripModel* tab_strip_model, |
| 164 content::WebContents* contents, |
| 165 int index, |
| 166 bool foreground) { |
| 167 if (severity_ != AppMenuIconController::Severity::NONE) |
| 168 animation_->StartAnimation(); |
| 169 } |
| 170 |
| 171 void AppMenuButton::UpdateIcon(bool should_animate) { |
| 172 SkColor severity_color = gfx::kPlaceholderColor; |
| 173 SkColor toolbar_icon_color = |
| 174 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON); |
| 116 const ui::NativeTheme* native_theme = GetNativeTheme(); | 175 const ui::NativeTheme* native_theme = GetNativeTheme(); |
| 117 switch (severity_) { | 176 switch (severity_) { |
| 118 case AppMenuIconController::Severity::NONE: | 177 case AppMenuIconController::Severity::NONE: |
| 119 color = GetThemeProvider()->GetColor( | 178 severity_color = toolbar_icon_color; |
| 120 ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON); | |
| 121 break; | 179 break; |
| 122 case AppMenuIconController::Severity::LOW: | 180 case AppMenuIconController::Severity::LOW: |
| 123 color = native_theme->GetSystemColor( | 181 severity_color = native_theme->GetSystemColor( |
| 124 ui::NativeTheme::kColorId_AlertSeverityLow); | 182 ui::NativeTheme::kColorId_AlertSeverityLow); |
| 125 break; | 183 break; |
| 126 case AppMenuIconController::Severity::MEDIUM: | 184 case AppMenuIconController::Severity::MEDIUM: |
| 127 color = native_theme->GetSystemColor( | 185 severity_color = native_theme->GetSystemColor( |
| 128 ui::NativeTheme::kColorId_AlertSeverityMedium); | 186 ui::NativeTheme::kColorId_AlertSeverityMedium); |
| 129 break; | 187 break; |
| 130 case AppMenuIconController::Severity::HIGH: | 188 case AppMenuIconController::Severity::HIGH: |
| 131 color = native_theme->GetSystemColor( | 189 severity_color = native_theme->GetSystemColor( |
| 132 ui::NativeTheme::kColorId_AlertSeverityHigh); | 190 ui::NativeTheme::kColorId_AlertSeverityHigh); |
| 133 break; | 191 break; |
| 134 } | 192 } |
| 135 | 193 |
| 194 if (animation_) { |
| 195 animation_->SetIconColors(toolbar_icon_color, severity_color); |
| 196 if (should_animate) |
| 197 animation_->StartAnimation(); |
| 198 return; |
| 199 } |
| 200 |
| 136 const gfx::VectorIcon* icon_id = nullptr; | 201 const gfx::VectorIcon* icon_id = nullptr; |
| 137 switch (type_) { | 202 switch (type_) { |
| 138 case AppMenuIconController::IconType::NONE: | 203 case AppMenuIconController::IconType::NONE: |
| 139 icon_id = &kBrowserToolsIcon; | 204 icon_id = &kBrowserToolsIcon; |
| 140 DCHECK_EQ(AppMenuIconController::Severity::NONE, severity_); | 205 DCHECK_EQ(AppMenuIconController::Severity::NONE, severity_); |
| 141 break; | 206 break; |
| 142 case AppMenuIconController::IconType::UPGRADE_NOTIFICATION: | 207 case AppMenuIconController::IconType::UPGRADE_NOTIFICATION: |
| 143 icon_id = &kBrowserToolsUpdateIcon; | 208 icon_id = &kBrowserToolsUpdateIcon; |
| 144 break; | 209 break; |
| 145 case AppMenuIconController::IconType::GLOBAL_ERROR: | 210 case AppMenuIconController::IconType::GLOBAL_ERROR: |
| 146 case AppMenuIconController::IconType::INCOMPATIBILITY_WARNING: | 211 case AppMenuIconController::IconType::INCOMPATIBILITY_WARNING: |
| 147 icon_id = &kBrowserToolsErrorIcon; | 212 icon_id = &kBrowserToolsErrorIcon; |
| 148 break; | 213 break; |
| 149 } | 214 } |
| 150 | 215 |
| 151 SetImage(views::Button::STATE_NORMAL, gfx::CreateVectorIcon(*icon_id, color)); | 216 SetImage(views::Button::STATE_NORMAL, |
| 217 gfx::CreateVectorIcon(*icon_id, severity_color)); |
| 152 } | 218 } |
| 153 | 219 |
| 154 void AppMenuButton::SetTrailingMargin(int margin) { | 220 void AppMenuButton::SetTrailingMargin(int margin) { |
| 155 margin_trailing_ = margin; | 221 margin_trailing_ = margin; |
| 156 UpdateThemedBorder(); | 222 UpdateThemedBorder(); |
| 157 InvalidateLayout(); | 223 InvalidateLayout(); |
| 158 } | 224 } |
| 159 | 225 |
| 226 void AppMenuButton::AppMenuAnimationStarted() { |
| 227 SetPaintToLayer(); |
| 228 layer()->SetFillsBoundsOpaquely(false); |
| 229 } |
| 230 |
| 231 void AppMenuButton::AppMenuAnimationEnded() { |
| 232 DestroyLayer(); |
| 233 } |
| 234 |
| 160 const char* AppMenuButton::GetClassName() const { | 235 const char* AppMenuButton::GetClassName() const { |
| 161 return "AppMenuButton"; | 236 return "AppMenuButton"; |
| 162 } | 237 } |
| 163 | 238 |
| 164 std::unique_ptr<views::LabelButtonBorder> AppMenuButton::CreateDefaultBorder() | 239 std::unique_ptr<views::LabelButtonBorder> AppMenuButton::CreateDefaultBorder() |
| 165 const { | 240 const { |
| 166 std::unique_ptr<views::LabelButtonBorder> border = | 241 std::unique_ptr<views::LabelButtonBorder> border = |
| 167 MenuButton::CreateDefaultBorder(); | 242 MenuButton::CreateDefaultBorder(); |
| 168 | 243 |
| 169 // Adjust border insets to follow the margin change, | 244 // Adjust border insets to follow the margin change, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 return ui::DragDropTypes::DRAG_MOVE; | 288 return ui::DragDropTypes::DRAG_MOVE; |
| 214 } | 289 } |
| 215 | 290 |
| 216 void AppMenuButton::OnDragExited() { | 291 void AppMenuButton::OnDragExited() { |
| 217 weak_factory_.InvalidateWeakPtrs(); | 292 weak_factory_.InvalidateWeakPtrs(); |
| 218 } | 293 } |
| 219 | 294 |
| 220 int AppMenuButton::OnPerformDrop(const ui::DropTargetEvent& event) { | 295 int AppMenuButton::OnPerformDrop(const ui::DropTargetEvent& event) { |
| 221 return ui::DragDropTypes::DRAG_MOVE; | 296 return ui::DragDropTypes::DRAG_MOVE; |
| 222 } | 297 } |
| OLD | NEW |