[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" |
tdanderson | 1bc521b5 | 2016-07-07 00:16:06 | [diff] [blame^] | 9 | #include "ash/common/system/tray/tray_constants.h" |
jamescook | 63936fe | 2016-06-13 19:11:25 | [diff] [blame] | 10 | #include "ash/common/system/tray/tray_item_view.h" |
| 11 | #include "ash/common/system/tray/tray_utils.h" |
[email protected] | d05a15a4 | 2013-08-14 05:48:31 | [diff] [blame] | 12 | #include "ash/system/tray/system_tray.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) { |
| 31 | case IDR_AURA_UBER_TRAY_VOLUME_MUTE: |
| 32 | return gfx::VectorIconId::SYSTEM_TRAY_VOLUME_MUTE; |
| 33 | case IDR_AURA_UBER_TRAY_TRACING: |
| 34 | // TODO(tdanderson): Update the icon used for tracing or remove it from |
| 35 | // the system tray. See crbug.com/625691. |
| 36 | return gfx::VectorIconId::CODE; |
| 37 | case IDR_AURA_UBER_TRAY_ACCESSIBILITY: |
| 38 | return gfx::VectorIconId::SYSTEM_TRAY_ACCESSIBILITY; |
| 39 | case IDR_AURA_UBER_TRAY_UPDATE: |
| 40 | return gfx::VectorIconId::SYSTEM_TRAY_UPDATE; |
| 41 | case IDR_AURA_UBER_TRAY_AUTO_ROTATION_LOCKED: |
| 42 | return gfx::VectorIconId::SYSTEM_TRAY_ROTATION_LOCK_LOCKED; |
| 43 | case IDR_AURA_UBER_TRAY_CAPS_LOCK: |
| 44 | return gfx::VectorIconId::SYSTEM_TRAY_CAPS_LOCK; |
| 45 | default: |
| 46 | NOTREACHED(); |
| 47 | break; |
| 48 | } |
| 49 | |
| 50 | return vector_id; |
| 51 | } |
| 52 | |
| 53 | } // namespace |
| 54 | |
[email protected] | 1dbddf4 | 2012-11-21 21:41:59 | [diff] [blame] | 55 | TrayImageItem::TrayImageItem(SystemTray* system_tray, int resource_id) |
| 56 | : SystemTrayItem(system_tray), |
| 57 | resource_id_(resource_id), |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 58 | tray_view_(NULL) {} |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 59 | |
| 60 | TrayImageItem::~TrayImageItem() {} |
| 61 | |
[email protected] | 5d1491d | 2012-04-23 21:51:00 | [diff] [blame] | 62 | views::View* TrayImageItem::tray_view() { |
[email protected] | 1d06b181 | 2012-05-01 16:53:10 | [diff] [blame] | 63 | return tray_view_; |
[email protected] | 5d1491d | 2012-04-23 21:51:00 | [diff] [blame] | 64 | } |
| 65 | |
sky | e79274a | 2016-06-08 05:39:02 | [diff] [blame] | 66 | views::View* TrayImageItem::CreateTrayView(LoginStatus status) { |
[email protected] | 1d06b181 | 2012-05-01 16:53:10 | [diff] [blame] | 67 | CHECK(tray_view_ == NULL); |
[email protected] | 1dbddf4 | 2012-11-21 21:41:59 | [diff] [blame] | 68 | tray_view_ = new TrayItemView(this); |
[email protected] | 5d1491d | 2012-04-23 21:51:00 | [diff] [blame] | 69 | tray_view_->CreateImageView(); |
tdanderson | 1bc521b5 | 2016-07-07 00:16:06 | [diff] [blame^] | 70 | |
| 71 | if (MaterialDesignController::UseMaterialDesignSystemIcons()) { |
| 72 | tray_view_->image_view()->SetImage(CreateVectorIcon( |
| 73 | ResourceIdToVectorIconId(resource_id_), kTrayIconSize, kTrayIconColor)); |
| 74 | } else { |
| 75 | tray_view_->image_view()->SetImage(ui::ResourceBundle::GetSharedInstance() |
| 76 | .GetImageNamed(resource_id_) |
| 77 | .ToImageSkia()); |
| 78 | } |
| 79 | |
[email protected] | 5d1491d | 2012-04-23 21:51:00 | [diff] [blame] | 80 | tray_view_->SetVisible(GetInitialVisibility()); |
[email protected] | d05a15a4 | 2013-08-14 05:48:31 | [diff] [blame] | 81 | SetItemAlignment(system_tray()->shelf_alignment()); |
[email protected] | 1d06b181 | 2012-05-01 16:53:10 | [diff] [blame] | 82 | return tray_view_; |
[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::CreateDefaultView(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 | views::View* TrayImageItem::CreateDetailedView(LoginStatus status) { |
| 90 | return nullptr; |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 91 | } |
| 92 | |
sky | e79274a | 2016-06-08 05:39:02 | [diff] [blame] | 93 | void TrayImageItem::UpdateAfterLoginStatusChange(LoginStatus status) {} |
[email protected] | eee3e774 | 2012-04-24 17:25:16 | [diff] [blame] | 94 | |
jamescook | 1b474d6a | 2016-06-04 05:56:00 | [diff] [blame] | 95 | void TrayImageItem::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) { |
[email protected] | b17f430b | 2012-07-23 22:12:14 | [diff] [blame] | 96 | SetTrayImageItemBorder(tray_view_, alignment); |
[email protected] | d05a15a4 | 2013-08-14 05:48:31 | [diff] [blame] | 97 | SetItemAlignment(alignment); |
[email protected] | b17f430b | 2012-07-23 22:12:14 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 100 | void TrayImageItem::DestroyTrayView() { |
[email protected] | 1d06b181 | 2012-05-01 16:53:10 | [diff] [blame] | 101 | tray_view_ = NULL; |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 102 | } |
| 103 | |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 104 | void TrayImageItem::DestroyDefaultView() {} |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 105 | |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 106 | void TrayImageItem::DestroyDetailedView() {} |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 107 | |
tdanderson | 1bc521b5 | 2016-07-07 00:16:06 | [diff] [blame^] | 108 | // TODO(tdanderson): Consider moving or renaming this function, as it is only |
| 109 | // used by TrayUpdate to modify the update severity. See crbug.com/625692. |
| 110 | void TrayImageItem::SetImageFromResourceId(int resource_id) { |
| 111 | resource_id_ = resource_id; |
| 112 | if (!tray_view_) |
| 113 | return; |
| 114 | tray_view_->image_view()->SetImage(ui::ResourceBundle::GetSharedInstance() |
| 115 | .GetImageNamed(resource_id_) |
| 116 | .ToImageSkia()); |
| 117 | } |
| 118 | |
jamescook | 1b474d6a | 2016-06-04 05:56:00 | [diff] [blame] | 119 | void TrayImageItem::SetItemAlignment(ShelfAlignment alignment) { |
[email protected] | d05a15a4 | 2013-08-14 05:48:31 | [diff] [blame] | 120 | // Center the item dependent on the orientation of the shelf. |
jamescook | 1b474d6a | 2016-06-04 05:56:00 | [diff] [blame] | 121 | views::BoxLayout::Orientation layout = IsHorizontalAlignment(alignment) |
msw | 67a57624 | 2016-04-18 21:06:13 | [diff] [blame] | 122 | ? views::BoxLayout::kHorizontal |
| 123 | : views::BoxLayout::kVertical; |
[email protected] | d05a15a4 | 2013-08-14 05:48:31 | [diff] [blame] | 124 | tray_view_->SetLayoutManager(new views::BoxLayout(layout, 0, 0, 0)); |
| 125 | tray_view_->Layout(); |
| 126 | } |
| 127 | |
[email protected] | 31a5e31 | 2012-03-19 16:23:14 | [diff] [blame] | 128 | } // namespace ash |