| 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/command_line.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "cc/paint/paint_flags.h" | 14 #include "cc/paint/paint_flags.h" |
| 15 #include "chrome/app/vector_icons/vector_icons.h" | 15 #include "chrome/app/vector_icons/vector_icons.h" |
| 16 #include "chrome/browser/themes/theme_properties.h" | 16 #include "chrome/browser/themes/theme_properties.h" |
| 17 #include "chrome/browser/themes/theme_service.h" |
| 18 #include "chrome/browser/themes/theme_service_factory.h" |
| 17 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
| 18 #include "chrome/browser/ui/browser_otr_state.h" | 20 #include "chrome/browser/ui/browser_otr_state.h" |
| 19 #include "chrome/browser/ui/layout_constants.h" | 21 #include "chrome/browser/ui/layout_constants.h" |
| 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 22 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 21 #include "chrome/browser/ui/toolbar/app_menu_animation.h" | |
| 22 #include "chrome/browser/ui/toolbar/app_menu_model.h" | 23 #include "chrome/browser/ui/toolbar/app_menu_model.h" |
| 23 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h" | 24 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h" |
| 24 #include "chrome/browser/ui/views/toolbar/app_menu.h" | 25 #include "chrome/browser/ui/views/toolbar/app_menu.h" |
| 25 #include "chrome/browser/ui/views/toolbar/toolbar_button.h" | 26 #include "chrome/browser/ui/views/toolbar/toolbar_button.h" |
| 26 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" | 27 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
| 27 #include "chrome/common/chrome_switches.h" | 28 #include "chrome/common/chrome_switches.h" |
| 28 #include "ui/base/resource/resource_bundle.h" | 29 #include "ui/base/resource/resource_bundle.h" |
| 29 #include "ui/base/theme_provider.h" | 30 #include "ui/base/theme_provider.h" |
| 30 #include "ui/gfx/canvas.h" | 31 #include "ui/gfx/canvas.h" |
| 31 #include "ui/gfx/color_palette.h" | 32 #include "ui/gfx/color_palette.h" |
| 32 #include "ui/gfx/paint_vector_icon.h" | 33 #include "ui/gfx/paint_vector_icon.h" |
| 33 #include "ui/keyboard/keyboard_controller.h" | 34 #include "ui/keyboard/keyboard_controller.h" |
| 34 #include "ui/views/controls/button/label_button_border.h" | 35 #include "ui/views/controls/button/label_button_border.h" |
| 35 #include "ui/views/controls/menu/menu_listener.h" | 36 #include "ui/views/controls/menu/menu_listener.h" |
| 36 #include "ui/views/metrics.h" | 37 #include "ui/views/metrics.h" |
| 37 | 38 |
| 38 namespace { | 39 namespace { |
| 39 | 40 |
| 40 constexpr float kIconSize = 16; | 41 constexpr float kIconSize = 16; |
| 41 | 42 |
| 42 } // namespace | 43 } // namespace |
| 43 | 44 |
| 44 // static | 45 // static |
| 45 bool AppMenuButton::g_open_app_immediately_for_testing = false; | 46 bool AppMenuButton::g_open_app_immediately_for_testing = false; |
| 46 | 47 |
| 47 AppMenuButton::AppMenuButton(ToolbarView* toolbar_view) | 48 AppMenuButton::AppMenuButton(ToolbarView* toolbar_view) |
| 48 : views::MenuButton(base::string16(), toolbar_view, false), | 49 : views::MenuButton(base::string16(), toolbar_view, false), |
| 49 severity_(AppMenuIconController::Severity::NONE), | 50 toolbar_view_(toolbar_view) { |
| 50 type_(AppMenuIconController::IconType::NONE), | |
| 51 toolbar_view_(toolbar_view), | |
| 52 should_use_new_icon_(false), | |
| 53 margin_trailing_(0), | |
| 54 weak_factory_(this) { | |
| 55 SetInkDropMode(InkDropMode::ON); | 51 SetInkDropMode(InkDropMode::ON); |
| 56 SetFocusPainter(nullptr); | 52 SetFocusPainter(nullptr); |
| 57 | 53 |
| 58 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 54 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 59 if (command_line->HasSwitch(switches::kEnableNewAppMenuIcon)) { | 55 if (command_line->HasSwitch(switches::kEnableNewAppMenuIcon)) { |
| 60 toolbar_view_->browser()->tab_strip_model()->AddObserver(this); | 56 toolbar_view_->browser()->tab_strip_model()->AddObserver(this); |
| 61 should_use_new_icon_ = true; | 57 should_use_new_icon_ = true; |
| 62 } | 58 } |
| 63 } | 59 } |
| 64 | 60 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 menu_listeners_.RemoveObserver(listener); | 122 menu_listeners_.RemoveObserver(listener); |
| 127 } | 123 } |
| 128 | 124 |
| 129 gfx::Size AppMenuButton::CalculatePreferredSize() const { | 125 gfx::Size AppMenuButton::CalculatePreferredSize() const { |
| 130 gfx::Rect rect(gfx::Size(kIconSize, kIconSize)); | 126 gfx::Rect rect(gfx::Size(kIconSize, kIconSize)); |
| 131 rect.Inset(gfx::Insets(-ToolbarButton::kInteriorPadding)); | 127 rect.Inset(gfx::Insets(-ToolbarButton::kInteriorPadding)); |
| 132 return rect.size(); | 128 return rect.size(); |
| 133 } | 129 } |
| 134 | 130 |
| 135 void AppMenuButton::Layout() { | 131 void AppMenuButton::Layout() { |
| 136 if (animation_) { | 132 if (new_icon_) { |
| 133 new_icon_->SetBoundsRect(GetContentsBounds()); |
| 137 ink_drop_container()->SetBoundsRect(GetLocalBounds()); | 134 ink_drop_container()->SetBoundsRect(GetLocalBounds()); |
| 138 image()->SetBoundsRect(GetLocalBounds()); | 135 image()->SetBoundsRect(GetLocalBounds()); |
| 139 return; | 136 return; |
| 140 } | 137 } |
| 141 | 138 |
| 142 views::MenuButton::Layout(); | 139 views::MenuButton::Layout(); |
| 143 } | 140 } |
| 144 | 141 |
| 145 void AppMenuButton::PaintButtonContents(gfx::Canvas* canvas) { | 142 void AppMenuButton::OnThemeChanged() { |
| 146 if (!animation_) { | 143 UpdateIcon(false); |
| 147 views::MenuButton::PaintButtonContents(canvas); | |
| 148 return; | |
| 149 } | |
| 150 | |
| 151 gfx::Rect bounds = GetLocalBounds(); | |
| 152 bounds.Inset(gfx::Insets(ToolbarButton::kInteriorPadding)); | |
| 153 animation_->PaintAppMenu(canvas, bounds); | |
| 154 } | 144 } |
| 155 | 145 |
| 156 void AppMenuButton::TabInsertedAt(TabStripModel* tab_strip_model, | 146 void AppMenuButton::TabInsertedAt(TabStripModel* tab_strip_model, |
| 157 content::WebContents* contents, | 147 content::WebContents* contents, |
| 158 int index, | 148 int index, |
| 159 bool foreground) { | 149 bool foreground) { |
| 160 AnimateIconIfPossible(); | 150 AnimateIconIfPossible(); |
| 161 } | 151 } |
| 162 | 152 |
| 163 void AppMenuButton::AppMenuAnimationStarted() { | |
| 164 SetPaintToLayer(); | |
| 165 layer()->SetFillsBoundsOpaquely(false); | |
| 166 } | |
| 167 | |
| 168 void AppMenuButton::AppMenuAnimationEnded() { | |
| 169 DestroyLayer(); | |
| 170 } | |
| 171 | |
| 172 void AppMenuButton::InvalidateIcon() { | |
| 173 SchedulePaint(); | |
| 174 } | |
| 175 | |
| 176 void AppMenuButton::UpdateIcon(bool should_animate) { | 153 void AppMenuButton::UpdateIcon(bool should_animate) { |
| 177 SkColor severity_color = gfx::kPlaceholderColor; | 154 SkColor severity_color = gfx::kPlaceholderColor; |
| 178 SkColor toolbar_icon_color = | 155 SkColor toolbar_icon_color = |
| 179 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON); | 156 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON); |
| 180 const ui::NativeTheme* native_theme = GetNativeTheme(); | 157 const ui::NativeTheme* native_theme = GetNativeTheme(); |
| 181 switch (severity_) { | 158 switch (severity_) { |
| 182 case AppMenuIconController::Severity::NONE: | 159 case AppMenuIconController::Severity::NONE: |
| 183 severity_color = toolbar_icon_color; | 160 severity_color = toolbar_icon_color; |
| 184 break; | 161 break; |
| 185 case AppMenuIconController::Severity::LOW: | 162 case AppMenuIconController::Severity::LOW: |
| 186 severity_color = native_theme->GetSystemColor( | 163 severity_color = native_theme->GetSystemColor( |
| 187 ui::NativeTheme::kColorId_AlertSeverityLow); | 164 ui::NativeTheme::kColorId_AlertSeverityLow); |
| 188 break; | 165 break; |
| 189 case AppMenuIconController::Severity::MEDIUM: | 166 case AppMenuIconController::Severity::MEDIUM: |
| 190 severity_color = native_theme->GetSystemColor( | 167 severity_color = native_theme->GetSystemColor( |
| 191 ui::NativeTheme::kColorId_AlertSeverityMedium); | 168 ui::NativeTheme::kColorId_AlertSeverityMedium); |
| 192 break; | 169 break; |
| 193 case AppMenuIconController::Severity::HIGH: | 170 case AppMenuIconController::Severity::HIGH: |
| 194 severity_color = native_theme->GetSystemColor( | 171 severity_color = native_theme->GetSystemColor( |
| 195 ui::NativeTheme::kColorId_AlertSeverityHigh); | 172 ui::NativeTheme::kColorId_AlertSeverityHigh); |
| 196 break; | 173 break; |
| 197 } | 174 } |
| 198 | 175 |
| 199 if (should_use_new_icon_) { | 176 if (should_use_new_icon_) { |
| 200 if (!animation_) | 177 if (!new_icon_) { |
| 201 animation_ = base::MakeUnique<AppMenuAnimation>(this, toolbar_icon_color); | 178 new_icon_ = new views::AnimatedIconView(kBrowserToolsAnimatedIcon); |
| 179 new_icon_->set_can_process_events_within_subtree(false); |
| 180 AddChildView(new_icon_); |
| 181 } |
| 202 | 182 |
| 203 animation_->set_target_color(severity_color); | 183 // Only show a special color for severity when using the classic Chrome |
| 184 // theme. Otherwise, we can't be sure that it contrasts with the toolbar |
| 185 // background. |
| 186 new_icon_->set_color( |
| 187 ThemeServiceFactory::GetForProfile(toolbar_view_->browser()->profile()) |
| 188 ->UsingDefaultTheme() |
| 189 ? severity_color |
| 190 : toolbar_icon_color); |
| 191 |
| 204 if (should_animate) | 192 if (should_animate) |
| 205 AnimateIconIfPossible(); | 193 AnimateIconIfPossible(); |
| 206 | 194 |
| 207 return; | 195 return; |
| 208 } | 196 } |
| 209 | 197 |
| 210 const gfx::VectorIcon* icon_id = nullptr; | 198 const gfx::VectorIcon* icon_id = nullptr; |
| 211 switch (type_) { | 199 switch (type_) { |
| 212 case AppMenuIconController::IconType::NONE: | 200 case AppMenuIconController::IconType::NONE: |
| 213 icon_id = &kBrowserToolsIcon; | 201 icon_id = &kBrowserToolsIcon; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 226 gfx::CreateVectorIcon(*icon_id, severity_color)); | 214 gfx::CreateVectorIcon(*icon_id, severity_color)); |
| 227 } | 215 } |
| 228 | 216 |
| 229 void AppMenuButton::SetTrailingMargin(int margin) { | 217 void AppMenuButton::SetTrailingMargin(int margin) { |
| 230 margin_trailing_ = margin; | 218 margin_trailing_ = margin; |
| 231 UpdateThemedBorder(); | 219 UpdateThemedBorder(); |
| 232 InvalidateLayout(); | 220 InvalidateLayout(); |
| 233 } | 221 } |
| 234 | 222 |
| 235 void AppMenuButton::AnimateIconIfPossible() { | 223 void AppMenuButton::AnimateIconIfPossible() { |
| 236 if (!animation_ || !should_use_new_icon_ || | 224 if (!new_icon_ || !should_use_new_icon_ || |
| 237 severity_ == AppMenuIconController::Severity::NONE) { | 225 severity_ == AppMenuIconController::Severity::NONE) { |
| 238 return; | 226 return; |
| 239 } | 227 } |
| 240 | 228 |
| 241 animation_->StartAnimation(); | 229 new_icon_->Animate(views::AnimatedIconView::END); |
| 242 } | 230 } |
| 243 | 231 |
| 244 const char* AppMenuButton::GetClassName() const { | 232 const char* AppMenuButton::GetClassName() const { |
| 245 return "AppMenuButton"; | 233 return "AppMenuButton"; |
| 246 } | 234 } |
| 247 | 235 |
| 248 std::unique_ptr<views::LabelButtonBorder> AppMenuButton::CreateDefaultBorder() | 236 std::unique_ptr<views::LabelButtonBorder> AppMenuButton::CreateDefaultBorder() |
| 249 const { | 237 const { |
| 250 std::unique_ptr<views::LabelButtonBorder> border = | 238 std::unique_ptr<views::LabelButtonBorder> border = |
| 251 MenuButton::CreateDefaultBorder(); | 239 MenuButton::CreateDefaultBorder(); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 return ui::DragDropTypes::DRAG_MOVE; | 286 return ui::DragDropTypes::DRAG_MOVE; |
| 299 } | 287 } |
| 300 | 288 |
| 301 void AppMenuButton::OnDragExited() { | 289 void AppMenuButton::OnDragExited() { |
| 302 weak_factory_.InvalidateWeakPtrs(); | 290 weak_factory_.InvalidateWeakPtrs(); |
| 303 } | 291 } |
| 304 | 292 |
| 305 int AppMenuButton::OnPerformDrop(const ui::DropTargetEvent& event) { | 293 int AppMenuButton::OnPerformDrop(const ui::DropTargetEvent& event) { |
| 306 return ui::DragDropTypes::DRAG_MOVE; | 294 return ui::DragDropTypes::DRAG_MOVE; |
| 307 } | 295 } |
| OLD | NEW |