summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/status/power_menu_button.cc
blob: 05578636ed0d1f53ac4fa58aa97b20b91021ce35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/status/power_menu_button.h"

#include <algorithm>

#include "base/auto_reset.h"
#include "base/string_number_conversions.h"
#include "base/stringprintf.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/canvas_skia.h"
#include "ui/gfx/font.h"
#include "views/controls/menu/menu_item_view.h"
#include "views/controls/menu/menu_runner.h"
#include "views/controls/menu/submenu_view.h"
#include "views/widget/widget.h"

namespace {

// Menu item ids.
enum {
  POWER_BATTERY_PERCENTAGE_ITEM = 1000,
  POWER_BATTERY_IS_CHARGED_ITEM,
  POWER_NO_BATTERY,
};

enum ImageType {
  DISCHARGING,
  CHARGING,
  BOLT
};

enum ImageSize {
  SMALL,
  LARGE
};

// Initialize time deltas to large values so they will be replaced when set
// to small values.
const int64 kInitialMS = 0x7fffffff;
// Width and height of small images.
const int kSmallImageWidth = 26, kSmallImageHeight = 24;
// Width and height of large images.
const int kLargeImageWidth = 57, kLargeImageHeight = 35;
// Number of different power states.
const int kNumPowerImages = 20;

// Constants for status displayed when user clicks button.
// Padding around status.
const int kPadLeftX = 10, kPadRightX = 10, kPadY = 5;
// Padding between battery and text.
const int kBatteryPadX = 10;
// Spacing between lines of text.
const int kEstimateSpacing = 3;
// Color of text embedded within battery.
const SkColor kPercentageColor = 0xFF333333;
// Used for embossing text.
const SkColor kPercentageShadowColor = 0x80ffffff;
// Status text/
const SkColor kEstimateColor = SK_ColorBLACK;
// Size of percentage w/in battery.
const int kBatteryFontSizeDelta = 3;

// Battery images come from two collections (small and large). In each there
// are |kNumPowerImages| battery states for both on and off charge, followed
// by the missing battery image and the unknown image.
// They are layed out like this:
// Discharging Charging Bolt
// |           |        +
// ||          ||        +
// ...
// |||||       |||||        +
// ||X||       ||?||
SkBitmap GetImage(ImageSize size, ImageType type, int index) {
  int image_width, image_height, image_index;

  if (size == SMALL) {
    image_index = IDR_STATUSBAR_BATTERY_SMALL_ALL;
    image_width = kSmallImageWidth;
    image_height = kSmallImageHeight;
  } else {
    image_index = IDR_STATUSBAR_BATTERY_LARGE_ALL;
    image_width = kLargeImageWidth;
    image_height = kLargeImageHeight;
  }
  SkBitmap* all_images =
      ResourceBundle::GetSharedInstance().GetBitmapNamed(image_index);
  SkIRect subset =
      SkIRect::MakeXYWH(
          static_cast<int>(type) * image_width,
          index * image_height,
          image_width,
          image_height);

  SkBitmap image;
  all_images->extractSubset(&image, subset);
  return image;
}

SkBitmap GetMissingImage(ImageSize size) {
  return GetImage(size, DISCHARGING, kNumPowerImages);
}

SkBitmap GetUnknownImage(ImageSize size) {
  return GetImage(size, CHARGING, kNumPowerImages);
}

}  // namespace

namespace chromeos {

using base::TimeDelta;

class PowerMenuButton::StatusView : public View {
 public:
  explicit StatusView(PowerMenuButton* menu_button)
      : menu_button_(menu_button) {
    estimate_font_ =
        ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont);
    percentage_font_ =
        estimate_font_.DeriveFont(kBatteryFontSizeDelta, gfx::Font::BOLD);
  }

  gfx::Size GetPreferredSize() {
    int estimate_w, estimate_h, charging_w, charging_h;
    string16 estimate_text = menu_button_->GetBatteryIsChargedText();
    string16 charging_text = l10n_util::GetStringUTF16(
        menu_button_->line_power_on_ ?
            IDS_STATUSBAR_BATTERY_CHARGING :
            IDS_STATUSBAR_BATTERY_DISCHARGING);
    gfx::CanvasSkia::SizeStringInt(
        estimate_text, estimate_font_, &estimate_w, &estimate_h, 0);
    gfx::CanvasSkia::SizeStringInt(
        charging_text, estimate_font_, &charging_w, &charging_h, 0);
    gfx::Size size = gfx::Size(
        kPadLeftX + kLargeImageWidth + kBatteryPadX + kPadRightX +
            std::max(charging_w, estimate_w),
        (2 * kPadY) +
            std::max(kLargeImageHeight,
                     kEstimateSpacing + (2 * estimate_font_.GetHeight())));
    return size;
  }

  void Update() {
    PreferredSizeChanged();
    // Force a paint even if the size didn't change.
    SchedulePaint();
  }

 protected:
  void OnPaint(gfx::Canvas* canvas) {
    SkBitmap image;

    bool draw_percentage_text = false;
    if (!CrosLibrary::Get()->EnsureLoaded()) {
      image = GetUnknownImage(LARGE);
    } else if (!menu_button_->battery_is_present_) {
      image = GetMissingImage(LARGE);
    } else {
      image = GetImage(
          LARGE,
          menu_button_->line_power_on_ ? CHARGING : DISCHARGING,
          menu_button_->battery_index_);
      if (menu_button_->battery_percentage_ < 100 ||
          !menu_button_->line_power_on_) {
        draw_percentage_text = true;
      }
    }
    int image_x = kPadLeftX, image_y = (height() - image.height()) / 2;
    canvas->DrawBitmapInt(image, image_x, image_y);

    if (draw_percentage_text) {
      string16 text = UTF8ToUTF16(base::StringPrintf(
          "%d%%",
          static_cast<int>(menu_button_->battery_percentage_)));
      int text_h = percentage_font_.GetHeight();
      int text_y = ((height() - text_h) / 2);
      canvas->DrawStringInt(
          text, percentage_font_, kPercentageShadowColor,
          image_x, text_y + 1, image.width(), text_h,
          gfx::Canvas::TEXT_ALIGN_CENTER | gfx::Canvas::NO_ELLIPSIS);
      canvas->DrawStringInt(
          text, percentage_font_, kPercentageColor,
          image_x, text_y, image.width(), text_h,
          gfx::Canvas::TEXT_ALIGN_CENTER | gfx::Canvas::NO_ELLIPSIS);
      if (menu_button_->line_power_on_) {
        image = GetImage(LARGE, BOLT, menu_button_->battery_index_);
        canvas->DrawBitmapInt(image, image_x, image_y);
      }
    }
    string16 charging_text = l10n_util::GetStringUTF16(
        menu_button_->line_power_on_ ?
            IDS_STATUSBAR_BATTERY_CHARGING :
            IDS_STATUSBAR_BATTERY_DISCHARGING);
    string16 estimate_text = menu_button_->GetBatteryIsChargedText();
    int text_h = estimate_font_.GetHeight();
    int text_x = image_x + kLargeImageWidth + kBatteryPadX;
    int charging_y = (height() - (kEstimateSpacing + (2 * text_h))) / 2;
    int estimate_y = charging_y + text_h + kEstimateSpacing;
    canvas->DrawStringInt(
        charging_text, estimate_font_, kEstimateColor,
        text_x, charging_y, width() - text_x, text_h,
        gfx::Canvas::TEXT_ALIGN_LEFT);
    canvas->DrawStringInt(
        estimate_text, estimate_font_, kEstimateColor,
        text_x, estimate_y, width() - text_x, text_h,
        gfx::Canvas::TEXT_ALIGN_LEFT);
  }

  bool OnMousePressed(const views::MouseEvent& event) {
    return true;
  }

  void OnMouseReleased(const views::MouseEvent& event) {
    if (event.IsLeftMouseButton()) {
      DCHECK(menu_button_->menu_runner_);
      menu_button_->menu_runner_->Cancel();
    }
  }

 private:
  PowerMenuButton* menu_button_;
  gfx::Font percentage_font_;
  gfx::Font estimate_font_;
};

////////////////////////////////////////////////////////////////////////////////
// PowerMenuButton

PowerMenuButton::PowerMenuButton(StatusAreaHost* host)
    : StatusAreaButton(host, this),
      battery_is_present_(false),
      line_power_on_(false),
      battery_percentage_(0.0),
      battery_index_(-1),
      battery_time_to_full_(TimeDelta::FromMicroseconds(kInitialMS)),
      battery_time_to_empty_(TimeDelta::FromMicroseconds(kInitialMS)),
      status_(NULL),
      menu_runner_(NULL) {
  UpdateIconAndLabelInfo();
  CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this);
}

PowerMenuButton::~PowerMenuButton() {
  CrosLibrary::Get()->GetPowerLibrary()->RemoveObserver(this);
}

// PowerMenuButton, views::MenuDelegate implementation:

std::wstring PowerMenuButton::GetLabel(int id) const {
  return std::wstring();
}

bool PowerMenuButton::IsCommandEnabled(int id) const {
  return false;
}

string16 PowerMenuButton::GetBatteryPercentageText() const {
  return l10n_util::GetStringFUTF16(
      IDS_STATUSBAR_BATTERY_PERCENTAGE,
      base::IntToString16(static_cast<int>(battery_percentage_)));
}

string16 PowerMenuButton::GetBatteryIsChargedText() const {
  // The second item shows the battery is charged if it is.
  if (battery_percentage_ >= 100 && line_power_on_)
    return l10n_util::GetStringUTF16(IDS_STATUSBAR_BATTERY_IS_CHARGED);

  // If battery is in an intermediate charge state, show how much time left.
  TimeDelta time = line_power_on_ ? battery_time_to_full_ :
      battery_time_to_empty_;
  if (time.InSeconds() == 0) {
    // If time is 0, then that means we are still calculating how much time.
    // Depending if line power is on, we either show a message saying that we
    // are calculating time until full or calculating remaining time.
    int msg = line_power_on_ ?
        IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_FULL :
        IDS_STATUSBAR_BATTERY_CALCULATING_TIME_UNTIL_EMPTY;
    return l10n_util::GetStringUTF16(msg);
  } else {
    // Depending if line power is on, we either show a message saying XX:YY
    // until full or XX:YY remaining where XX is number of hours and YY is
    // number of minutes.
    int msg = line_power_on_ ? IDS_STATUSBAR_BATTERY_TIME_UNTIL_FULL :
        IDS_STATUSBAR_BATTERY_TIME_UNTIL_EMPTY;
    int hour = time.InHours();
    int min = (time - TimeDelta::FromHours(hour)).InMinutes();
    string16 hour_str = base::IntToString16(hour);
    string16 min_str = base::IntToString16(min);
    // Append a "0" before the minute if it's only a single digit.
    if (min < 10)
      min_str = ASCIIToUTF16("0") + min_str;
    return l10n_util::GetStringFUTF16(msg, hour_str, min_str);
  }
}

int PowerMenuButton::icon_width() {
  return 26;
}

////////////////////////////////////////////////////////////////////////////////
// PowerMenuButton, views::View implementation:
void PowerMenuButton::OnLocaleChanged() {
  UpdateIconAndLabelInfo();
}

////////////////////////////////////////////////////////////////////////////////
// PowerMenuButton, views::ViewMenuDelegate implementation:

void PowerMenuButton::RunMenu(views::View* source, const gfx::Point& pt) {
  views::MenuItemView* menu = new views::MenuItemView(this);
  // MenuRunner takes ownership of |menu|.
  views::MenuRunner menu_runner(menu);
  views::MenuItemView* submenu = menu->AppendMenuItem(
          POWER_BATTERY_PERCENTAGE_ITEM,
          std::wstring(),
          views::MenuItemView::NORMAL);
  status_ = new StatusView(this);
  submenu->AddChildView(status_);
  menu->CreateSubmenu()->set_resize_open_menu(true);
  menu->SetMargins(0, 0);
  submenu->SetMargins(0, 0);
  menu->ChildrenChanged();

  gfx::Point screen_location;
  views::View::ConvertPointToScreen(source, &screen_location);
  gfx::Rect bounds(screen_location, source->size());
  AutoReset<views::MenuRunner*> menu_runner_reseter(&menu_runner_,
                                                    &menu_runner);
  if (menu_runner.RunMenuAt(
          source->GetWidget()->GetTopLevelWidget(), this, bounds,
          views::MenuItemView::TOPRIGHT, views::MenuRunner::HAS_MNEMONICS) ==
      views::MenuRunner::MENU_DELETED)
    return;
  status_ = NULL;
}

////////////////////////////////////////////////////////////////////////////////
// PowerMenuButton, PowerLibrary::Observer implementation:

void PowerMenuButton::PowerChanged(PowerLibrary* obj) {
  UpdateIconAndLabelInfo();
}

////////////////////////////////////////////////////////////////////////////////
// PowerMenuButton, StatusAreaButton implementation:

void PowerMenuButton::UpdateIconAndLabelInfo() {
  PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary();
  if (!cros)
    return;

  bool cros_loaded = CrosLibrary::Get()->EnsureLoaded();
  if (cros_loaded) {
    battery_is_present_ = cros->battery_is_present();
    line_power_on_ = cros->line_power_on();

    // If fully charged, always show 100% even if internal number is a bit less.
    if (cros->battery_fully_charged()) {
      // We always call cros->battery_percentage() for test predictability.
      cros->battery_percentage();
      battery_percentage_ = 100.0;
    } else {
      battery_percentage_ = cros->battery_percentage();
    }

    UpdateBatteryTime(&battery_time_to_full_, cros->battery_time_to_full());
    UpdateBatteryTime(&battery_time_to_empty_, cros->battery_time_to_empty());
  }

  if (!cros_loaded) {
    battery_index_ = -1;
    SetIcon(GetUnknownImage(SMALL));
  } else if (!battery_is_present_) {
    battery_index_ = -1;
    SetIcon(GetMissingImage(SMALL));
  } else {
    // Preserve the fully charged icon for 100% only.
    if (battery_percentage_ >= 100) {
      battery_index_ = kNumPowerImages - 1;
    } else {
      battery_index_ =
          static_cast<int>(battery_percentage_ / 100.0 *
              nextafter(static_cast<float>(kNumPowerImages - 1), 0));
      battery_index_ =
          std::max(std::min(battery_index_, kNumPowerImages - 2), 0);
    }
    SetIcon(GetImage(
        SMALL, line_power_on_ ? CHARGING : DISCHARGING, battery_index_));
  }

  percentage_text_ = GetBatteryPercentageText();
  SetTooltipText(UTF16ToWide(percentage_text_));
  SetAccessibleName(percentage_text_);
  SchedulePaint();
  if (status_)
    status_->Update();
}

void PowerMenuButton::UpdateBatteryTime(TimeDelta* previous,
                                        const TimeDelta& current) {
  static const TimeDelta kMaxDiff(TimeDelta::FromMinutes(10));
  static const TimeDelta kMinDiff(TimeDelta::FromMinutes(0));
  const TimeDelta diff = current - *previous;
  // If the diff is small and positive, ignore it in favor of
  // keeping time monotonically decreasing.
  // If previous is 0, then it either was never set (initial condition)
  // or got down to 0.
  if (*previous == TimeDelta::FromMicroseconds(kInitialMS) ||
      diff < kMinDiff ||
      diff > kMaxDiff) {
    *previous = current;
  }
}

}  // namespace chromeos