Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ash/common/system/chromeos/power/power_status_view.h" | 5 #include "ash/common/system/chromeos/power/power_status_view.h" |
| 6 | 6 |
| 7 #include "ash/common/material_design/material_design_controller.h" | |
| 8 #include "ash/common/system/chromeos/power/power_status.h" | |
| 9 #include "ash/common/system/chromeos/power/tray_power.h" | 7 #include "ash/common/system/chromeos/power/tray_power.h" |
| 10 #include "ash/common/system/tray/fixed_sized_image_view.h" | 8 #include "ash/common/system/tray/fixed_sized_image_view.h" |
| 11 #include "ash/common/system/tray/tray_constants.h" | 9 #include "ash/common/system/tray/tray_constants.h" |
| 12 #include "ash/common/system/tray/tray_popup_item_style.h" | 10 #include "ash/common/system/tray/tray_popup_item_style.h" |
| 13 #include "ash/common/system/tray/tray_popup_utils.h" | 11 #include "ash/common/system/tray/tray_popup_utils.h" |
| 14 #include "ash/strings/grit/ash_strings.h" | 12 #include "ash/strings/grit/ash_strings.h" |
| 15 #include "base/i18n/number_formatting.h" | 13 #include "base/i18n/number_formatting.h" |
| 16 #include "base/i18n/time_formatting.h" | 14 #include "base/i18n/time_formatting.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 18 #include "ui/accessibility/ax_node_data.h" | 16 #include "ui/accessibility/ax_node_data.h" |
| 19 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 20 #include "ui/native_theme/native_theme.h" | 18 #include "ui/native_theme/native_theme.h" |
| 21 #include "ui/views/controls/image_view.h" | 19 #include "ui/views/controls/image_view.h" |
| 22 #include "ui/views/controls/label.h" | 20 #include "ui/views/controls/label.h" |
| 23 #include "ui/views/layout/box_layout.h" | 21 #include "ui/views/layout/box_layout.h" |
| 24 #include "ui/views/layout/grid_layout.h" | 22 #include "ui/views/layout/grid_layout.h" |
| 25 | 23 |
| 26 namespace ash { | 24 namespace ash { |
| 27 | 25 |
| 28 // Padding between battery status text and battery icon on default view. | 26 PowerStatusView::PowerStatusView() |
| 29 const int kPaddingBetweenBatteryStatusAndIcon = 3; | 27 : percentage_label_(new views::Label), |
| 30 | |
| 31 PowerStatusView::PowerStatusView(bool default_view_right_align) | |
| 32 : default_view_right_align_(default_view_right_align), | |
| 33 percentage_label_(new views::Label), | |
| 34 separator_label_(new views::Label), | 28 separator_label_(new views::Label), |
| 35 time_status_label_(new views::Label) { | 29 time_status_label_(new views::Label) { |
| 36 if (MaterialDesignController::IsSystemTrayMenuMaterial()) | 30 SetFocusBehavior(FocusBehavior::ALWAYS); |
| 37 SetFocusBehavior(FocusBehavior::ALWAYS); | |
| 38 | 31 |
| 39 percentage_label_->SetEnabledColor(kHeaderTextColorNormal); | 32 percentage_label_->SetEnabledColor(kHeaderTextColorNormal); |
| 40 separator_label_->SetEnabledColor(kHeaderTextColorNormal); | 33 separator_label_->SetEnabledColor(kHeaderTextColorNormal); |
| 41 separator_label_->SetText(base::ASCIIToUTF16(" - ")); | 34 separator_label_->SetText(base::ASCIIToUTF16(" - ")); |
| 42 LayoutView(); | 35 |
| 36 // TODO(tdanderson): Tweak padding values for material design. | |
|
tdanderson
2017/04/04 14:51:15
This TODO can be removed, the layout is to spec.
| |
| 37 views::BoxLayout* layout = new views::BoxLayout( | |
| 38 views::BoxLayout::kHorizontal, 12, 0, kTrayPopupPaddingBetweenItems); | |
| 39 SetLayoutManager(layout); | |
| 40 | |
| 41 AddChildView(percentage_label_); | |
| 42 AddChildView(separator_label_); | |
| 43 AddChildView(time_status_label_); | |
| 44 | |
| 43 PowerStatus::Get()->AddObserver(this); | 45 PowerStatus::Get()->AddObserver(this); |
| 44 OnPowerStatusChanged(); | 46 OnPowerStatusChanged(); |
| 45 } | 47 } |
| 46 | 48 |
| 47 PowerStatusView::~PowerStatusView() { | 49 PowerStatusView::~PowerStatusView() { |
| 48 PowerStatus::Get()->RemoveObserver(this); | 50 PowerStatus::Get()->RemoveObserver(this); |
| 49 } | 51 } |
| 50 | 52 |
| 51 void PowerStatusView::OnPowerStatusChanged() { | 53 void PowerStatusView::OnPowerStatusChanged() { |
| 52 UpdateText(); | 54 UpdateText(); |
| 53 } | 55 } |
| 54 | 56 |
| 55 void PowerStatusView::LayoutView() { | |
| 56 if (default_view_right_align_) { | |
| 57 views::BoxLayout* layout = | |
| 58 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, | |
| 59 kPaddingBetweenBatteryStatusAndIcon); | |
| 60 SetLayoutManager(layout); | |
| 61 | |
| 62 AddChildView(percentage_label_); | |
| 63 AddChildView(separator_label_); | |
| 64 AddChildView(time_status_label_); | |
| 65 AddChildView(new views::ImageView); | |
| 66 | |
| 67 return; | |
| 68 } | |
| 69 | |
| 70 // TODO(tdanderson): Tweak padding values for material design. | |
| 71 const bool material_design = | |
| 72 MaterialDesignController::IsSystemTrayMenuMaterial(); | |
| 73 views::BoxLayout* layout = new views::BoxLayout( | |
| 74 views::BoxLayout::kHorizontal, material_design ? 12 : 0, 0, | |
| 75 kTrayPopupPaddingBetweenItems); | |
| 76 SetLayoutManager(layout); | |
| 77 | |
| 78 if (!material_design) | |
| 79 AddChildView(TrayPopupUtils::CreateMainImageView()); | |
| 80 | |
| 81 AddChildView(percentage_label_); | |
| 82 AddChildView(separator_label_); | |
| 83 AddChildView(time_status_label_); | |
| 84 } | |
| 85 | |
| 86 void PowerStatusView::UpdateText() { | 57 void PowerStatusView::UpdateText() { |
| 87 const PowerStatus& status = *PowerStatus::Get(); | 58 const PowerStatus& status = *PowerStatus::Get(); |
| 88 base::string16 battery_percentage; | 59 base::string16 battery_percentage; |
| 89 base::string16 battery_time_status; | 60 base::string16 battery_time_status; |
| 90 | 61 |
| 91 if (status.IsBatteryFull()) { | 62 if (status.IsBatteryFull()) { |
| 92 battery_time_status = | 63 battery_time_status = |
| 93 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_FULL); | 64 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_FULL); |
| 94 } else { | 65 } else { |
| 95 battery_percentage = base::FormatPercent(status.GetRoundedBatteryPercent()); | 66 battery_percentage = base::FormatPercent(status.GetRoundedBatteryPercent()); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 117 } | 88 } |
| 118 } | 89 } |
| 119 } | 90 } |
| 120 percentage_label_->SetVisible(!battery_percentage.empty()); | 91 percentage_label_->SetVisible(!battery_percentage.empty()); |
| 121 percentage_label_->SetText(battery_percentage); | 92 percentage_label_->SetText(battery_percentage); |
| 122 separator_label_->SetVisible(!battery_percentage.empty() && | 93 separator_label_->SetVisible(!battery_percentage.empty() && |
| 123 !battery_time_status.empty()); | 94 !battery_time_status.empty()); |
| 124 time_status_label_->SetVisible(!battery_time_status.empty()); | 95 time_status_label_->SetVisible(!battery_time_status.empty()); |
| 125 time_status_label_->SetText(battery_time_status); | 96 time_status_label_->SetText(battery_time_status); |
| 126 | 97 |
| 127 if (MaterialDesignController::IsSystemTrayMenuMaterial()) { | 98 accessible_name_ = PowerStatus::Get()->GetAccessibleNameString(true); |
| 128 accessible_name_ = PowerStatus::Get()->GetAccessibleNameString(true); | |
| 129 | 99 |
| 130 TrayPopupItemStyle style(TrayPopupItemStyle::FontStyle::SYSTEM_INFO); | 100 TrayPopupItemStyle style(TrayPopupItemStyle::FontStyle::SYSTEM_INFO); |
| 131 style.SetupLabel(percentage_label_); | 101 style.SetupLabel(percentage_label_); |
| 132 style.SetupLabel(separator_label_); | 102 style.SetupLabel(separator_label_); |
| 133 style.SetupLabel(time_status_label_); | 103 style.SetupLabel(time_status_label_); |
| 134 } | |
| 135 } | 104 } |
| 136 | 105 |
| 137 void PowerStatusView::ChildPreferredSizeChanged(views::View* child) { | 106 void PowerStatusView::ChildPreferredSizeChanged(views::View* child) { |
| 138 PreferredSizeChanged(); | 107 PreferredSizeChanged(); |
| 139 } | 108 } |
| 140 | 109 |
| 141 void PowerStatusView::Layout() { | 110 void PowerStatusView::Layout() { |
| 142 views::View::Layout(); | 111 views::View::Layout(); |
| 143 | 112 |
| 144 // Move the time_status_label_, separator_label_, and percentage_label_ | 113 // Move the time_status_label_, separator_label_, and percentage_label_ |
| 145 // closer to each other. | 114 // closer to each other. |
| 146 if (percentage_label_ && separator_label_ && time_status_label_ && | 115 if (percentage_label_ && separator_label_ && time_status_label_ && |
| 147 percentage_label_->visible() && time_status_label_->visible()) { | 116 percentage_label_->visible() && time_status_label_->visible()) { |
| 148 separator_label_->SetX(percentage_label_->bounds().right() + 1); | 117 separator_label_->SetX(percentage_label_->bounds().right() + 1); |
| 149 time_status_label_->SetX(separator_label_->bounds().right() + 1); | 118 time_status_label_->SetX(separator_label_->bounds().right() + 1); |
| 150 } | 119 } |
| 151 } | 120 } |
| 152 | 121 |
| 153 void PowerStatusView::GetAccessibleNodeData(ui::AXNodeData* node_data) { | 122 void PowerStatusView::GetAccessibleNodeData(ui::AXNodeData* node_data) { |
| 154 if (!MaterialDesignController::IsSystemTrayMenuMaterial()) | |
| 155 return; | |
| 156 | |
| 157 node_data->role = ui::AX_ROLE_LABEL_TEXT; | 123 node_data->role = ui::AX_ROLE_LABEL_TEXT; |
| 158 node_data->SetName(accessible_name_); | 124 node_data->SetName(accessible_name_); |
| 159 } | 125 } |
| 160 | 126 |
| 161 } // namespace ash | 127 } // namespace ash |
| OLD | NEW |