| 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/location.h" | 7 #include "base/location.h" |
| 8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "chrome/browser/themes/theme_properties.h" | 12 #include "chrome/browser/themes/theme_properties.h" |
| 13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_otr_state.h" | 14 #include "chrome/browser/ui/browser_otr_state.h" |
| 15 #include "chrome/browser/ui/layout_constants.h" | 15 #include "chrome/browser/ui/layout_constants.h" |
| 16 #include "chrome/browser/ui/toolbar/app_menu_model.h" | 16 #include "chrome/browser/ui/toolbar/app_menu_model.h" |
| 17 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h" | 17 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h" |
| 18 #include "chrome/browser/ui/views/toolbar/app_menu.h" | 18 #include "chrome/browser/ui/views/toolbar/app_menu.h" |
| 19 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" | 19 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
| 20 #include "chrome/grit/theme_resources.h" | 20 #include "chrome/grit/theme_resources.h" |
| 21 #include "extensions/common/feature_switch.h" | 21 #include "extensions/common/feature_switch.h" |
| 22 #include "ui/base/material_design/material_design_controller.h" | |
| 23 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
| 24 #include "ui/base/theme_provider.h" | 23 #include "ui/base/theme_provider.h" |
| 25 #include "ui/gfx/color_palette.h" | 24 #include "ui/gfx/color_palette.h" |
| 26 #include "ui/gfx/paint_vector_icon.h" | 25 #include "ui/gfx/paint_vector_icon.h" |
| 27 #include "ui/gfx/vector_icons_public.h" | 26 #include "ui/gfx/vector_icons_public.h" |
| 28 #include "ui/keyboard/keyboard_controller.h" | 27 #include "ui/keyboard/keyboard_controller.h" |
| 29 #include "ui/views/controls/button/label_button_border.h" | 28 #include "ui/views/controls/button/label_button_border.h" |
| 30 #include "ui/views/controls/menu/menu_listener.h" | 29 #include "ui/views/controls/menu/menu_listener.h" |
| 31 #include "ui/views/metrics.h" | 30 #include "ui/views/metrics.h" |
| 32 #include "ui/views/painter.h" | |
| 33 | 31 |
| 34 // static | 32 // static |
| 35 bool AppMenuButton::g_open_app_immediately_for_testing = false; | 33 bool AppMenuButton::g_open_app_immediately_for_testing = false; |
| 36 | 34 |
| 37 AppMenuButton::AppMenuButton(ToolbarView* toolbar_view) | 35 AppMenuButton::AppMenuButton(ToolbarView* toolbar_view) |
| 38 : views::MenuButton(base::string16(), toolbar_view, false), | 36 : views::MenuButton(base::string16(), toolbar_view, false), |
| 39 severity_(AppMenuIconPainter::SEVERITY_NONE), | 37 severity_(AppMenuIconController::Severity::NONE), |
| 40 type_(AppMenuIconController::IconType::NONE), | 38 type_(AppMenuIconController::IconType::NONE), |
| 41 toolbar_view_(toolbar_view), | 39 toolbar_view_(toolbar_view), |
| 42 allow_extension_dragging_( | 40 allow_extension_dragging_( |
| 43 extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()), | 41 extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()), |
| 44 margin_trailing_(0), | 42 margin_trailing_(0), |
| 45 weak_factory_(this) { | 43 weak_factory_(this) { |
| 46 if (ui::MaterialDesignController::IsModeMaterial()) { | 44 SetInkDropMode(InkDropMode::ON); |
| 47 SetInkDropMode(InkDropMode::ON); | 45 SetFocusPainter(nullptr); |
| 48 SetFocusPainter(nullptr); | |
| 49 } else { | |
| 50 icon_painter_.reset(new AppMenuIconPainter(this)); | |
| 51 } | |
| 52 } | 46 } |
| 53 | 47 |
| 54 AppMenuButton::~AppMenuButton() {} | 48 AppMenuButton::~AppMenuButton() {} |
| 55 | 49 |
| 56 void AppMenuButton::SetSeverity(AppMenuIconController::IconType type, | 50 void AppMenuButton::SetSeverity(AppMenuIconController::IconType type, |
| 57 AppMenuIconPainter::Severity severity, | 51 AppMenuIconController::Severity severity, |
| 58 bool animate) { | 52 bool animate) { |
| 59 if (ui::MaterialDesignController::IsModeMaterial()) { | 53 severity_ = severity; |
| 60 severity_ = severity; | 54 type_ = type; |
| 61 type_ = type; | 55 UpdateIcon(); |
| 62 UpdateIcon(); | |
| 63 return; | |
| 64 } | |
| 65 | |
| 66 icon_painter_->SetSeverity(severity, animate); | |
| 67 SchedulePaint(); | |
| 68 } | 56 } |
| 69 | 57 |
| 70 void AppMenuButton::ShowMenu(bool for_drop) { | 58 void AppMenuButton::ShowMenu(bool for_drop) { |
| 71 if (menu_ && menu_->IsShowing()) | 59 if (menu_ && menu_->IsShowing()) |
| 72 return; | 60 return; |
| 73 | 61 |
| 74 #if defined(USE_AURA) | 62 #if defined(USE_AURA) |
| 75 keyboard::KeyboardController* keyboard_controller = | 63 keyboard::KeyboardController* keyboard_controller = |
| 76 keyboard::KeyboardController::GetInstance(); | 64 keyboard::KeyboardController::GetInstance(); |
| 77 if (keyboard_controller && keyboard_controller->keyboard_visible()) { | 65 if (keyboard_controller && keyboard_controller->keyboard_visible()) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 100 |
| 113 void AppMenuButton::AddMenuListener(views::MenuListener* listener) { | 101 void AppMenuButton::AddMenuListener(views::MenuListener* listener) { |
| 114 menu_listeners_.AddObserver(listener); | 102 menu_listeners_.AddObserver(listener); |
| 115 } | 103 } |
| 116 | 104 |
| 117 void AppMenuButton::RemoveMenuListener(views::MenuListener* listener) { | 105 void AppMenuButton::RemoveMenuListener(views::MenuListener* listener) { |
| 118 menu_listeners_.RemoveObserver(listener); | 106 menu_listeners_.RemoveObserver(listener); |
| 119 } | 107 } |
| 120 | 108 |
| 121 gfx::Size AppMenuButton::GetPreferredSize() const { | 109 gfx::Size AppMenuButton::GetPreferredSize() const { |
| 122 if (ui::MaterialDesignController::IsModeMaterial()) { | 110 gfx::Size size(image()->GetPreferredSize()); |
| 123 gfx::Size size(image()->GetPreferredSize()); | 111 gfx::Insets insets(GetLayoutInsets(TOOLBAR_BUTTON)); |
| 124 const ui::ThemeProvider* provider = GetThemeProvider(); | 112 size.Enlarge(insets.width(), insets.height()); |
| 125 if (provider) { | 113 return size; |
| 126 gfx::Insets insets(GetLayoutInsets(TOOLBAR_BUTTON)); | |
| 127 size.Enlarge(insets.width(), insets.height()); | |
| 128 } | |
| 129 return size; | |
| 130 } | |
| 131 | |
| 132 return ResourceBundle::GetSharedInstance(). | |
| 133 GetImageSkiaNamed(IDR_TOOLBAR_BEZEL_HOVER)->size(); | |
| 134 } | |
| 135 | |
| 136 void AppMenuButton::ScheduleAppMenuIconPaint() { | |
| 137 SchedulePaint(); | |
| 138 } | 114 } |
| 139 | 115 |
| 140 void AppMenuButton::UpdateIcon() { | 116 void AppMenuButton::UpdateIcon() { |
| 141 DCHECK(ui::MaterialDesignController::IsModeMaterial()); | |
| 142 SkColor color = gfx::kPlaceholderColor; | 117 SkColor color = gfx::kPlaceholderColor; |
| 143 const ui::NativeTheme* native_theme = GetNativeTheme(); | 118 const ui::NativeTheme* native_theme = GetNativeTheme(); |
| 144 switch (severity_) { | 119 switch (severity_) { |
| 145 case AppMenuIconPainter::SEVERITY_NONE: | 120 case AppMenuIconController::Severity::NONE: |
| 146 color = GetThemeProvider()->GetColor( | 121 color = GetThemeProvider()->GetColor( |
| 147 ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON); | 122 ThemeProperties::COLOR_TOOLBAR_BUTTON_ICON); |
| 148 break; | 123 break; |
| 149 case AppMenuIconPainter::SEVERITY_LOW: | 124 case AppMenuIconController::Severity::LOW: |
| 150 color = native_theme->GetSystemColor( | 125 color = native_theme->GetSystemColor( |
| 151 ui::NativeTheme::kColorId_AlertSeverityLow); | 126 ui::NativeTheme::kColorId_AlertSeverityLow); |
| 152 break; | 127 break; |
| 153 case AppMenuIconPainter::SEVERITY_MEDIUM: | 128 case AppMenuIconController::Severity::MEDIUM: |
| 154 color = native_theme->GetSystemColor( | 129 color = native_theme->GetSystemColor( |
| 155 ui::NativeTheme::kColorId_AlertSeverityMedium); | 130 ui::NativeTheme::kColorId_AlertSeverityMedium); |
| 156 break; | 131 break; |
| 157 case AppMenuIconPainter::SEVERITY_HIGH: | 132 case AppMenuIconController::Severity::HIGH: |
| 158 color = native_theme->GetSystemColor( | 133 color = native_theme->GetSystemColor( |
| 159 ui::NativeTheme::kColorId_AlertSeverityHigh); | 134 ui::NativeTheme::kColorId_AlertSeverityHigh); |
| 160 break; | 135 break; |
| 161 } | 136 } |
| 162 | 137 |
| 163 gfx::VectorIconId icon_id = gfx::VectorIconId::VECTOR_ICON_NONE; | 138 gfx::VectorIconId icon_id = gfx::VectorIconId::VECTOR_ICON_NONE; |
| 164 switch (type_) { | 139 switch (type_) { |
| 165 case AppMenuIconController::IconType::NONE: | 140 case AppMenuIconController::IconType::NONE: |
| 166 icon_id = gfx::VectorIconId::BROWSER_TOOLS; | 141 icon_id = gfx::VectorIconId::BROWSER_TOOLS; |
| 167 DCHECK_EQ(severity_, AppMenuIconPainter::SEVERITY_NONE); | 142 DCHECK_EQ(severity_, AppMenuIconController::Severity::NONE); |
| 168 break; | 143 break; |
| 169 case AppMenuIconController::IconType::UPGRADE_NOTIFICATION: | 144 case AppMenuIconController::IconType::UPGRADE_NOTIFICATION: |
| 170 icon_id = gfx::VectorIconId::BROWSER_TOOLS_UPDATE; | 145 icon_id = gfx::VectorIconId::BROWSER_TOOLS_UPDATE; |
| 171 break; | 146 break; |
| 172 case AppMenuIconController::IconType::GLOBAL_ERROR: | 147 case AppMenuIconController::IconType::GLOBAL_ERROR: |
| 173 case AppMenuIconController::IconType::INCOMPATIBILITY_WARNING: | 148 case AppMenuIconController::IconType::INCOMPATIBILITY_WARNING: |
| 174 icon_id = gfx::VectorIconId::BROWSER_TOOLS_ERROR; | 149 icon_id = gfx::VectorIconId::BROWSER_TOOLS_ERROR; |
| 175 break; | 150 break; |
| 176 } | 151 } |
| 177 | 152 |
| 178 SetImage(views::Button::STATE_NORMAL, gfx::CreateVectorIcon(icon_id, color)); | 153 SetImage(views::Button::STATE_NORMAL, gfx::CreateVectorIcon(icon_id, color)); |
| 179 } | 154 } |
| 180 | 155 |
| 181 void AppMenuButton::SetTrailingMargin(int margin) { | 156 void AppMenuButton::SetTrailingMargin(int margin) { |
| 182 margin_trailing_ = margin; | 157 margin_trailing_ = margin; |
| 183 | |
| 184 UpdateThemedBorder(); | 158 UpdateThemedBorder(); |
| 185 | |
| 186 if (!ui::MaterialDesignController::IsModeMaterial()) { | |
| 187 const int inset = LabelButton::kFocusRectInset; | |
| 188 SetFocusPainter(views::Painter::CreateDashedFocusPainterWithInsets( | |
| 189 gfx::Insets(inset, inset, inset, inset + margin))); | |
| 190 } | |
| 191 | |
| 192 InvalidateLayout(); | 159 InvalidateLayout(); |
| 193 } | 160 } |
| 194 | 161 |
| 195 const char* AppMenuButton::GetClassName() const { | 162 const char* AppMenuButton::GetClassName() const { |
| 196 return "AppMenuButton"; | 163 return "AppMenuButton"; |
| 197 } | 164 } |
| 198 | 165 |
| 199 std::unique_ptr<views::LabelButtonBorder> AppMenuButton::CreateDefaultBorder() | 166 std::unique_ptr<views::LabelButtonBorder> AppMenuButton::CreateDefaultBorder() |
| 200 const { | 167 const { |
| 201 std::unique_ptr<views::LabelButtonBorder> border = | 168 std::unique_ptr<views::LabelButtonBorder> border = |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 | 225 |
| 259 void AppMenuButton::OnDragExited() { | 226 void AppMenuButton::OnDragExited() { |
| 260 DCHECK(allow_extension_dragging_); | 227 DCHECK(allow_extension_dragging_); |
| 261 weak_factory_.InvalidateWeakPtrs(); | 228 weak_factory_.InvalidateWeakPtrs(); |
| 262 } | 229 } |
| 263 | 230 |
| 264 int AppMenuButton::OnPerformDrop(const ui::DropTargetEvent& event) { | 231 int AppMenuButton::OnPerformDrop(const ui::DropTargetEvent& event) { |
| 265 DCHECK(allow_extension_dragging_); | 232 DCHECK(allow_extension_dragging_); |
| 266 return ui::DragDropTypes::DRAG_MOVE; | 233 return ui::DragDropTypes::DRAG_MOVE; |
| 267 } | 234 } |
| 268 | |
| 269 void AppMenuButton::OnPaint(gfx::Canvas* canvas) { | |
| 270 views::MenuButton::OnPaint(canvas); | |
| 271 if (ui::MaterialDesignController::IsModeMaterial()) | |
| 272 return; | |
| 273 // Use GetPreferredSize() to center the icon inside the visible bounds rather | |
| 274 // than the whole size() (which may refer to hit test region extended to the | |
| 275 // end of the toolbar in maximized mode). | |
| 276 icon_painter_->Paint(canvas, GetThemeProvider(), | |
| 277 gfx::Rect(GetPreferredSize()), | |
| 278 AppMenuIconPainter::BEZEL_NONE); | |
| 279 } | |
| OLD | NEW |