[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
jamescook | 63936fe | 2016-06-13 19:11:25 | [diff] [blame] | 5 | #include "ash/common/system/tray/tray_image_item.h" |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 6 | |
tdanderson | 1bc521b5 | 2016-07-07 00:16:06 | [diff] [blame] | 7 | #include "ash/common/material_design/material_design_controller.h" |
jamescook | 1b474d6a | 2016-06-04 05:56:00 | [diff] [blame] | 8 | #include "ash/common/shelf/wm_shelf_util.h" |
jamescook | 625f791 | 2016-07-14 01:00:52 | [diff] [blame] | 9 | #include "ash/common/system/tray/system_tray.h" |
tdanderson | 1bc521b5 | 2016-07-07 00:16:06 | [diff] [blame] | 10 | #include "ash/common/system/tray/tray_constants.h" |
jamescook | 63936fe | 2016-06-13 19:11:25 | [diff] [blame] | 11 | #include "ash/common/system/tray/tray_item_view.h" |
| 12 | #include "ash/common/system/tray/tray_utils.h" |
tdanderson | 1bc521b5 | 2016-07-07 00:16:06 | [diff] [blame] | 13 | #include "grit/ash_resources.h" |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 14 | #include "ui/base/resource/resource_bundle.h" |
| 15 | #include "ui/gfx/image/image.h" |
tdanderson | 1bc521b5 | 2016-07-07 00:16:06 | [diff] [blame] | 16 | #include "ui/gfx/paint_vector_icon.h" |
| 17 | #include "ui/gfx/vector_icons_public.h" |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 18 | #include "ui/views/controls/image_view.h" |
[email protected] | d05a15a4 | 2013-08-14 05:48:31 | [diff] [blame] | 19 | #include "ui/views/layout/box_layout.h" |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 20 | |
| 21 | namespace ash { |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 22 | |
tdanderson | 1bc521b5 | 2016-07-07 00:16:06 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | // Maps a non-MD PNG resource id to its corresponding MD vector icon id. |
| 26 | // TODO(tdanderson): Remove this once material design is enabled by |
| 27 | // default. See crbug.com/614453. |
| 28 | gfx::VectorIconId ResourceIdToVectorIconId(int resource_id) { |
| 29 | gfx::VectorIconId vector_id = gfx::VectorIconId::VECTOR_ICON_NONE; |
| 30 | switch (resource_id) { |
tdanderson | 1bc521b5 | 2016-07-07 00:16:06 | [diff] [blame] | 31 | case IDR_AURA_UBER_TRAY_ACCESSIBILITY: |
| 32 | return gfx::VectorIconId::SYSTEM_TRAY_ACCESSIBILITY; |
| 33 | case IDR_AURA_UBER_TRAY_UPDATE: |
| 34 | return gfx::VectorIconId::SYSTEM_TRAY_UPDATE; |
thestig | 6db17dc9 | 2016-07-12 03:37:07 | [diff] [blame] | 35 | case IDR_AURA_UBER_TRAY_VOLUME_MUTE: |
| 36 | return gfx::VectorIconId::SYSTEM_TRAY_VOLUME_MUTE; |
| 37 | #if defined(OS_CHROMEOS) |
tdanderson | 1bc521b5 | 2016-07-07 00:16:06 | [diff] [blame] | 38 | case IDR_AURA_UBER_TRAY_AUTO_ROTATION_LOCKED: |
| 39 | return gfx::VectorIconId::SYSTEM_TRAY_ROTATION_LOCK_LOCKED; |
| 40 | case IDR_AURA_UBER_TRAY_CAPS_LOCK: |
| 41 | return gfx::VectorIconId::SYSTEM_TRAY_CAPS_LOCK; |
thestig | 6db17dc9 | 2016-07-12 03:37:07 | [diff] [blame] | 42 | case IDR_AURA_UBER_TRAY_TRACING: |
| 43 | // TODO(tdanderson): Update the icon used for tracing or remove it from |
| 44 | // the system tray. See crbug.com/625691. |
| 45 | return gfx::VectorIconId::CODE; |
| 46 | #endif |
tdanderson | 1bc521b5 | 2016-07-07 00:16:06 | [diff] [blame] | 47 | default: |
| 48 | NOTREACHED(); |
| 49 | break; |
| 50 | } |
| 51 | |
| 52 | return vector_id; |
| 53 | } |
| 54 | |
| 55 | } // namespace |
| 56 | |
bruthig | 5c85316 | 2016-07-25 20:23:26 | [diff] [blame] | 57 | TrayImageItem::TrayImageItem(SystemTray* system_tray, |
| 58 | int resource_id, |
| 59 | UmaType uma_type) |
| 60 | : SystemTrayItem(system_tray, uma_type), |
[email protected] | 1dbddf4 | 2012-11-21 21:41:59 | [diff] [blame] | 61 | resource_id_(resource_id), |
tdanderson | 7804394 | 2016-08-17 19:22:47 | [diff] [blame] | 62 | icon_color_(kTrayIconColor), |
thestig | 6db17dc9 | 2016-07-12 03:37:07 | [diff] [blame] | 63 | tray_view_(nullptr) {} |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 64 | |
| 65 | TrayImageItem::~TrayImageItem() {} |
| 66 | |
[email protected] | 5d1491d | 2012-04-23 21:51:00 | [diff] [blame] | 67 | views::View* TrayImageItem::tray_view() { |
[email protected] | 1d06b181 | 2012-05-01 16:53:10 | [diff] [blame] | 68 | return tray_view_; |
[email protected] | 5d1491d | 2012-04-23 21:51:00 | [diff] [blame] | 69 | } |
| 70 | |
sky | e79274a | 2016-06-08 05:39:02 | [diff] [blame] | 71 | views::View* TrayImageItem::CreateTrayView(LoginStatus status) { |
thestig | 6db17dc9 | 2016-07-12 03:37:07 | [diff] [blame] | 72 | CHECK(!tray_view_); |
[email protected] | 1dbddf4 | 2012-11-21 21:41:59 | [diff] [blame] | 73 | tray_view_ = new TrayItemView(this); |
[email protected] | 5d1491d | 2012-04-23 21:51:00 | [diff] [blame] | 74 | tray_view_->CreateImageView(); |
tdanderson | 7804394 | 2016-08-17 19:22:47 | [diff] [blame] | 75 | UpdateImageOnImageView(); |
[email protected] | 5d1491d | 2012-04-23 21:51:00 | [diff] [blame] | 76 | tray_view_->SetVisible(GetInitialVisibility()); |
[email protected] | d05a15a4 | 2013-08-14 05:48:31 | [diff] [blame] | 77 | SetItemAlignment(system_tray()->shelf_alignment()); |
[email protected] | 1d06b181 | 2012-05-01 16:53:10 | [diff] [blame] | 78 | return tray_view_; |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 79 | } |
| 80 | |
sky | e79274a | 2016-06-08 05:39:02 | [diff] [blame] | 81 | views::View* TrayImageItem::CreateDefaultView(LoginStatus status) { |
| 82 | return nullptr; |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 83 | } |
| 84 | |
sky | e79274a | 2016-06-08 05:39:02 | [diff] [blame] | 85 | views::View* TrayImageItem::CreateDetailedView(LoginStatus status) { |
| 86 | return nullptr; |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 87 | } |
| 88 | |
sky | e79274a | 2016-06-08 05:39:02 | [diff] [blame] | 89 | void TrayImageItem::UpdateAfterLoginStatusChange(LoginStatus status) {} |
[email protected] | eee3e774 | 2012-04-24 17:25:16 | [diff] [blame] | 90 | |
jamescook | 1b474d6a | 2016-06-04 05:56:00 | [diff] [blame] | 91 | void TrayImageItem::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) { |
[email protected] | b17f430b | 2012-07-23 22:12:14 | [diff] [blame] | 92 | SetTrayImageItemBorder(tray_view_, alignment); |
[email protected] | d05a15a4 | 2013-08-14 05:48:31 | [diff] [blame] | 93 | SetItemAlignment(alignment); |
[email protected] | b17f430b | 2012-07-23 22:12:14 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 96 | void TrayImageItem::DestroyTrayView() { |
thestig | 6db17dc9 | 2016-07-12 03:37:07 | [diff] [blame] | 97 | tray_view_ = nullptr; |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 98 | } |
| 99 | |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 100 | void TrayImageItem::DestroyDefaultView() {} |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 101 | |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 102 | void TrayImageItem::DestroyDetailedView() {} |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 103 | |
tdanderson | 7804394 | 2016-08-17 19:22:47 | [diff] [blame] | 104 | void TrayImageItem::SetIconColor(SkColor color) { |
| 105 | icon_color_ = color; |
| 106 | UpdateImageOnImageView(); |
| 107 | } |
| 108 | |
tdanderson | 1bc521b5 | 2016-07-07 00:16:06 | [diff] [blame] | 109 | void TrayImageItem::SetImageFromResourceId(int resource_id) { |
| 110 | resource_id_ = resource_id; |
tdanderson | 7804394 | 2016-08-17 19:22:47 | [diff] [blame] | 111 | UpdateImageOnImageView(); |
tdanderson | 1bc521b5 | 2016-07-07 00:16:06 | [diff] [blame] | 112 | } |
| 113 | |
jamescook | 1b474d6a | 2016-06-04 05:56:00 | [diff] [blame] | 114 | void TrayImageItem::SetItemAlignment(ShelfAlignment alignment) { |
[email protected] | d05a15a4 | 2013-08-14 05:48:31 | [diff] [blame] | 115 | // Center the item dependent on the orientation of the shelf. |
jamescook | 1b474d6a | 2016-06-04 05:56:00 | [diff] [blame] | 116 | views::BoxLayout::Orientation layout = IsHorizontalAlignment(alignment) |
msw | 67a57624 | 2016-04-18 21:06:13 | [diff] [blame] | 117 | ? views::BoxLayout::kHorizontal |
| 118 | : views::BoxLayout::kVertical; |
[email protected] | d05a15a4 | 2013-08-14 05:48:31 | [diff] [blame] | 119 | tray_view_->SetLayoutManager(new views::BoxLayout(layout, 0, 0, 0)); |
| 120 | tray_view_->Layout(); |
| 121 | } |
| 122 | |
tdanderson | 7804394 | 2016-08-17 19:22:47 | [diff] [blame] | 123 | void TrayImageItem::UpdateImageOnImageView() { |
| 124 | if (!tray_view_) |
| 125 | return; |
| 126 | |
| 127 | if (MaterialDesignController::UseMaterialDesignSystemIcons()) { |
| 128 | tray_view_->image_view()->SetImage(gfx::CreateVectorIcon( |
| 129 | ResourceIdToVectorIconId(resource_id_), kTrayIconSize, icon_color_)); |
| 130 | } else { |
| 131 | tray_view_->image_view()->SetImage(ui::ResourceBundle::GetSharedInstance() |
| 132 | .GetImageNamed(resource_id_) |
| 133 | .ToImageSkia()); |
| 134 | } |
| 135 | } |
| 136 | |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 137 | } // namespace ash |