Chromium Code Reviews| Index: ash/common/system/chromeos/power/tray_power.cc |
| diff --git a/ash/common/system/chromeos/power/tray_power.cc b/ash/common/system/chromeos/power/tray_power.cc |
| index 8e694e15c41542f9c14ca7a21cdbd7323a2d7ac2..f280906a0a9a981358013bcbcbe0e05931e7b78b 100644 |
| --- a/ash/common/system/chromeos/power/tray_power.cc |
| +++ b/ash/common/system/chromeos/power/tray_power.cc |
| @@ -25,6 +25,7 @@ |
| #include "base/time/time.h" |
| #include "ui/accessibility/ax_node_data.h" |
| #include "ui/base/resource/resource_bundle.h" |
| +#include "ui/gfx/image/image_skia_source.h" |
| #include "ui/message_center/message_center.h" |
| #include "ui/message_center/notification.h" |
| #include "ui/message_center/notification_delegate.h" |
| @@ -90,6 +91,22 @@ void LogBatteryForNoCharger(TrayPower::NotificationState state, |
| namespace tray { |
| +class PowerTrayImageSource : public gfx::ImageSkiaSource { |
|
Peter Kasting
2017/03/25 00:22:30
Nit: I'm sorta inclined to declare this class insi
danakj
2017/03/31 20:49:04
More nesting :/ and it's in the .cc anyways. But d
|
| + public: |
| + PowerTrayImageSource(const PowerStatus::BatteryImageInfo& info) |
|
Peter Kasting
2017/03/25 00:22:30
Nit: explicit
Maybe also pass by value and move i
danakj
2017/03/31 20:49:04
Done.
Peter Kasting
2017/03/31 22:07:20
Wouldn't these both be at worst one deep copy? In
danakj
2017/04/03 14:43:13
If we pass by const&
- the caller doesn't invoke a
Peter Kasting
2017/04/03 20:32:51
It sounds like pass-by-value + move-into-place is
|
| + : info_(info) {} |
| + |
| + // gfx::ImageSkiaSource implementation. |
| + gfx::ImageSkiaRep GetImageForScale(float scale) override { |
| + return PowerStatus::Get()->GetBatteryImage(info_, scale); |
| + } |
| + |
| + const PowerStatus::BatteryImageInfo& info() const { return info_; } |
| + |
| + private: |
| + PowerStatus::BatteryImageInfo info_; |
| +}; |
|
Peter Kasting
2017/03/25 00:22:30
Nit: DISALLOW_COPY_AND_ASSIGN?
danakj
2017/03/31 20:49:04
Done.
|
| + |
| // This view is used only for the tray. |
| class PowerTrayView : public TrayItemView { |
| public: |
| @@ -100,7 +117,7 @@ class PowerTrayView : public TrayItemView { |
| ~PowerTrayView() override {} |
| - // Overriden from views::View. |
| + // Overridden from views::View. |
| void GetAccessibleNodeData(ui::AXNodeData* node_data) override { |
| node_data->SetName(accessible_name_); |
| node_data->role = ui::AX_ROLE_BUTTON; |
| @@ -118,19 +135,20 @@ class PowerTrayView : public TrayItemView { |
| private: |
| void UpdateImage() { |
| - const PowerStatus::BatteryImageInfo info = |
| + const PowerStatus::BatteryImageInfo& info = |
| PowerStatus::Get()->GetBatteryImageInfo(PowerStatus::ICON_LIGHT); |
| - if (info != previous_image_info_) { |
| - image_view()->SetImage(PowerStatus::Get()->GetBatteryImage(info)); |
| - previous_image_info_ = info; |
| - } |
| + // Only change the image when the info changes. http://crbug.com/589348 |
| + if (image_source_ && image_source_->info() == info) |
| + return; |
| + image_source_ = new PowerTrayImageSource(info); |
| + // gfx::ImageSkia takes ownership of image_source_. It will stay alive until |
|
Peter Kasting
2017/03/25 00:22:30
Blargh, it'd sure be nice if it took a unique_ptr
danakj
2017/03/31 20:49:04
Yeeeeep. I also had a crash until I realized this
|
| + // we call SetImage() again, which only happens here. |
| + image_view()->SetImage( |
| + gfx::ImageSkia(image_source_, PowerStatus::GetBatteryImageSizeInDip())); |
| } |
| base::string16 accessible_name_; |
| - |
| - // Information about the last-used image. Cached to avoid unnecessary updates |
| - // (http://crbug.com/589348). |
| - PowerStatus::BatteryImageInfo previous_image_info_; |
| + PowerTrayImageSource* image_source_ = nullptr; |
| DISALLOW_COPY_AND_ASSIGN(PowerTrayView); |
| }; |