| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/chromeos/status_area_view.h" | 5 #include "chrome/browser/chromeos/status_area_view.h" |
| 6 | 6 |
| 7 #include <dlfcn.h> | |
| 8 | |
| 9 #include <algorithm> | 7 #include <algorithm> |
| 10 | 8 |
| 11 #include "app/gfx/canvas.h" | 9 #include "app/gfx/canvas.h" |
| 12 #include "app/gfx/font.h" | |
| 13 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
| 14 #include "app/resource_bundle.h" | |
| 15 #include "app/theme_provider.h" | 11 #include "app/theme_provider.h" |
| 16 #include "base/path_service.h" | |
| 17 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 18 #include "base/time.h" | |
| 19 #include "base/timer.h" | |
| 20 #include "chrome/app/chrome_dll_resource.h" | 13 #include "chrome/app/chrome_dll_resource.h" |
| 21 #include "chrome/browser/browser.h" | 14 #include "chrome/browser/browser.h" |
| 22 #include "chrome/browser/browser_window.h" | 15 #include "chrome/browser/browser_window.h" |
| 16 #include "chrome/browser/chromeos/clock_menu_button.h" |
| 23 #include "chrome/browser/chromeos/network_menu_button.h" | 17 #include "chrome/browser/chromeos/network_menu_button.h" |
| 18 #include "chrome/browser/chromeos/power_menu_button.h" |
| 24 #include "chrome/browser/gtk/browser_window_gtk.h" | 19 #include "chrome/browser/gtk/browser_window_gtk.h" |
| 25 #include "chrome/browser/profile.h" | 20 #include "chrome/browser/profile.h" |
| 26 #include "chrome/common/chrome_paths.h" | |
| 27 #include "grit/chromium_strings.h" | 21 #include "grit/chromium_strings.h" |
| 28 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
| 29 #include "grit/theme_resources.h" | 23 #include "grit/theme_resources.h" |
| 30 #include "views/controls/button/menu_button.h" | 24 #include "views/controls/button/menu_button.h" |
| 31 #include "views/controls/image_view.h" | |
| 32 #include "views/controls/menu/menu.h" | 25 #include "views/controls/menu/menu.h" |
| 33 #include "views/controls/menu/simple_menu_model.h" | 26 #include "views/controls/menu/simple_menu_model.h" |
| 34 | 27 |
| 35 namespace { | 28 namespace { |
| 36 | 29 |
| 37 // The number of images representing the fullness of the battery. | |
| 38 const int kNumBatteryImages = 8; | |
| 39 | |
| 40 // Number of pixels to separate adjacent status items. | 30 // Number of pixels to separate adjacent status items. |
| 41 const int kStatusItemSeparation = 1; | 31 const int kStatusItemSeparation = 1; |
| 42 | 32 |
| 43 // BrowserWindowGtk tiles its image with this offset | 33 // BrowserWindowGtk tiles its image with this offset |
| 44 const int kCustomFrameBackgroundVerticalOffset = 15; | 34 const int kCustomFrameBackgroundVerticalOffset = 15; |
| 45 | 35 |
| 46 class ClockView : public views::View { | |
| 47 public: | |
| 48 ClockView(); | |
| 49 virtual ~ClockView(); | |
| 50 | |
| 51 // views::View* overrides. | |
| 52 virtual gfx::Size GetPreferredSize(); | |
| 53 virtual void Paint(gfx::Canvas* canvas); | |
| 54 | |
| 55 private: | |
| 56 // Schedules the timer to fire at the next minute interval. | |
| 57 void SetNextTimer(); | |
| 58 | |
| 59 // Schedules a paint when the timer goes off. | |
| 60 void OnTimer(); | |
| 61 | |
| 62 gfx::Font font_; | |
| 63 | |
| 64 base::OneShotTimer<ClockView> timer_; | |
| 65 | |
| 66 DISALLOW_COPY_AND_ASSIGN(ClockView); | |
| 67 }; | |
| 68 | |
| 69 // Amount of slop to add into the timer to make sure we're into the next minute | |
| 70 // when the timer goes off. | |
| 71 const int kTimerSlopSeconds = 1; | |
| 72 | |
| 73 ClockView::ClockView() | |
| 74 : font_(ResourceBundle::GetSharedInstance().GetFont( | |
| 75 ResourceBundle::BaseFont).DeriveFont(0, gfx::Font::BOLD)) { | |
| 76 SetNextTimer(); | |
| 77 } | |
| 78 | |
| 79 ClockView::~ClockView() { | |
| 80 } | |
| 81 | |
| 82 gfx::Size ClockView::GetPreferredSize() { | |
| 83 return gfx::Size(40, 12); | |
| 84 } | |
| 85 | |
| 86 void ClockView::Paint(gfx::Canvas* canvas) { | |
| 87 base::Time now = base::Time::Now(); | |
| 88 base::Time::Exploded now_exploded; | |
| 89 now.LocalExplode(&now_exploded); | |
| 90 int hour = now_exploded.hour % 12; | |
| 91 if (hour == 0) | |
| 92 hour = 12; | |
| 93 | |
| 94 std::wstring time_string = StringPrintf(L"%d:%02d%lc", | |
| 95 hour, | |
| 96 now_exploded.minute, | |
| 97 now_exploded.hour < 12 ? L'p' : L'p'); | |
| 98 canvas->DrawStringInt(time_string, font_, SK_ColorWHITE, 0, 0, | |
| 99 width(), height(), gfx::Canvas::TEXT_ALIGN_CENTER); | |
| 100 } | |
| 101 | |
| 102 void ClockView::SetNextTimer() { | |
| 103 // Try to set the timer to go off at the next change of the minute. We don't | |
| 104 // want to have the timer go off more than necessary since that will cause | |
| 105 // the CPU to wake up and consume power. | |
| 106 base::Time now = base::Time::Now(); | |
| 107 base::Time::Exploded exploded; | |
| 108 now.LocalExplode(&exploded); | |
| 109 | |
| 110 // Often this will be called at minute boundaries, and we'll actually want | |
| 111 // 60 seconds from now. | |
| 112 int seconds_left = 60 - exploded.second; | |
| 113 if (seconds_left == 0) | |
| 114 seconds_left = 60; | |
| 115 | |
| 116 // Make sure that the timer fires on the next minute. Without this, if it is | |
| 117 // called just a teeny bit early, then it will skip the next minute. | |
| 118 seconds_left += kTimerSlopSeconds; | |
| 119 | |
| 120 timer_.Start(base::TimeDelta::FromSeconds(seconds_left), | |
| 121 this, &ClockView::OnTimer); | |
| 122 } | |
| 123 | |
| 124 void ClockView::OnTimer() { | |
| 125 SchedulePaint(); | |
| 126 SetNextTimer(); | |
| 127 } | |
| 128 | |
| 129 class OptionsMenuModel : public views::SimpleMenuModel, | 36 class OptionsMenuModel : public views::SimpleMenuModel, |
| 130 public views::SimpleMenuModel::Delegate { | 37 public views::SimpleMenuModel::Delegate { |
| 131 public: | 38 public: |
| 132 // These extra command IDs must be unique when combined with the options, | 39 // These extra command IDs must be unique when combined with the options, |
| 133 // so we just pick up the numbering where that stops. | 40 // so we just pick up the numbering where that stops. |
| 134 enum OtherCommands { | 41 enum OtherCommands { |
| 135 CREATE_NEW_WINDOW = StatusAreaView::OPEN_TABS_ON_RIGHT + 1, | 42 CREATE_NEW_WINDOW = StatusAreaView::OPEN_TABS_ON_RIGHT + 1, |
| 136 }; | 43 }; |
| 137 | 44 |
| 138 explicit OptionsMenuModel(Browser* browser, | 45 explicit OptionsMenuModel(Browser* browser, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 | 100 |
| 194 DISALLOW_COPY_AND_ASSIGN(OptionsMenuModel); | 101 DISALLOW_COPY_AND_ASSIGN(OptionsMenuModel); |
| 195 }; | 102 }; |
| 196 | 103 |
| 197 } // namespace | 104 } // namespace |
| 198 | 105 |
| 199 // Default to opening new tabs on the left. | 106 // Default to opening new tabs on the left. |
| 200 StatusAreaView::OpenTabsMode StatusAreaView::open_tabs_mode_ = | 107 StatusAreaView::OpenTabsMode StatusAreaView::open_tabs_mode_ = |
| 201 StatusAreaView::OPEN_TABS_ON_LEFT; | 108 StatusAreaView::OPEN_TABS_ON_LEFT; |
| 202 | 109 |
| 203 // static | |
| 204 bool StatusAreaView::cros_library_loaded_ = false; | |
| 205 bool StatusAreaView::cros_library_error_ = false; | |
| 206 | |
| 207 StatusAreaView::StatusAreaView(Browser* browser) | 110 StatusAreaView::StatusAreaView(Browser* browser) |
| 208 : browser_(browser), | 111 : browser_(browser), |
| 112 clock_view_(NULL), |
| 209 network_view_(NULL), | 113 network_view_(NULL), |
| 210 battery_view_(NULL), | 114 battery_view_(NULL), |
| 211 menu_view_(NULL), | 115 menu_view_(NULL) { |
| 212 power_status_connection_(NULL) { | |
| 213 } | |
| 214 | |
| 215 StatusAreaView::~StatusAreaView() { | |
| 216 if (power_status_connection_) | |
| 217 chromeos::DisconnectPowerStatus(power_status_connection_); | |
| 218 } | 116 } |
| 219 | 117 |
| 220 void StatusAreaView::Init() { | 118 void StatusAreaView::Init() { |
| 221 LoadCrosLibrary(); | |
| 222 ThemeProvider* theme = browser_->profile()->GetThemeProvider(); | 119 ThemeProvider* theme = browser_->profile()->GetThemeProvider(); |
| 223 | 120 |
| 224 // Clock. | 121 // Clock. |
| 225 AddChildView(new ClockView); | 122 clock_view_ = new ClockMenuButton(); |
| 123 AddChildView(clock_view_); |
| 226 | 124 |
| 227 // Network. | 125 // Network. |
| 228 network_view_ = new NetworkMenuButton(browser_, cros_library_loaded_); | 126 network_view_ = new NetworkMenuButton(browser_); |
| 229 AddChildView(network_view_); | 127 AddChildView(network_view_); |
| 230 | 128 |
| 231 // Battery. | 129 // Battery. |
| 232 battery_view_ = new views::ImageView; | 130 battery_view_ = new PowerMenuButton(); |
| 233 battery_view_->SetImage(theme->GetBitmapNamed(IDR_STATUSBAR_BATTERY_UNKNOWN)); | |
| 234 AddChildView(battery_view_); | 131 AddChildView(battery_view_); |
| 235 | 132 |
| 236 // Menu. | 133 // Menu. |
| 237 menu_view_ = new views::MenuButton(NULL, std::wstring(), this, false); | 134 menu_view_ = new views::MenuButton(NULL, std::wstring(), this, false); |
| 135 menu_view_->SetShowHighlighted(false); |
| 238 menu_view_->SetIcon(*theme->GetBitmapNamed(IDR_STATUSBAR_MENU)); | 136 menu_view_->SetIcon(*theme->GetBitmapNamed(IDR_STATUSBAR_MENU)); |
| 239 AddChildView(menu_view_); | 137 AddChildView(menu_view_); |
| 240 | |
| 241 if (cros_library_loaded_) { | |
| 242 power_status_connection_ = chromeos::MonitorPowerStatus( | |
| 243 StatusAreaView::PowerStatusChangedHandler, | |
| 244 this); | |
| 245 } | |
| 246 } | 138 } |
| 247 | 139 |
| 248 gfx::Size StatusAreaView::GetPreferredSize() { | 140 gfx::Size StatusAreaView::GetPreferredSize() { |
| 249 int result_w = kStatusItemSeparation; // Left border. | 141 int result_w = kStatusItemSeparation; // Left border. |
| 250 int result_h = 0; | 142 int result_h = 0; |
| 251 for (int i = 0; i < GetChildViewCount(); i++) { | 143 for (int i = 0; i < GetChildViewCount(); i++) { |
| 252 gfx::Size cur_size = GetChildViewAt(i)->GetPreferredSize(); | 144 gfx::Size cur_size = GetChildViewAt(i)->GetPreferredSize(); |
| 253 result_w += cur_size.width() + kStatusItemSeparation; | 145 result_w += cur_size.width() + kStatusItemSeparation; |
| 254 result_h = std::max(result_h, cur_size.height()); | 146 result_h = std::max(result_h, cur_size.height()); |
| 255 } | 147 } |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 void StatusAreaView::ExecuteCommand(int command_id) { | 258 void StatusAreaView::ExecuteCommand(int command_id) { |
| 367 browser_->ExecuteCommand(command_id); | 259 browser_->ExecuteCommand(command_id); |
| 368 } | 260 } |
| 369 | 261 |
| 370 void StatusAreaView::RunMenu(views::View* source, const gfx::Point& pt, | 262 void StatusAreaView::RunMenu(views::View* source, const gfx::Point& pt, |
| 371 gfx::NativeView hwnd) { | 263 gfx::NativeView hwnd) { |
| 372 CreateAppMenu(); | 264 CreateAppMenu(); |
| 373 app_menu_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); | 265 app_menu_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); |
| 374 } | 266 } |
| 375 | 267 |
| 376 // static | |
| 377 void StatusAreaView::LoadCrosLibrary() { | |
| 378 if (cros_library_loaded_) { | |
| 379 // Already loaded. | |
| 380 return; | |
| 381 } | |
| 382 | |
| 383 if (cros_library_error_) { | |
| 384 // Error in previous load attempt. | |
| 385 return; | |
| 386 } | |
| 387 | |
| 388 FilePath path; | |
| 389 if (PathService::Get(chrome::FILE_CHROMEOS_API, &path)) { | |
| 390 cros_library_loaded_ = chromeos::LoadCros(path.value().c_str()); | |
| 391 if (!cros_library_loaded_) { | |
| 392 cros_library_error_ = true; | |
| 393 char* error = dlerror(); | |
| 394 if (error) { | |
| 395 LOG(ERROR) << "Problem loading chromeos shared object: " << error; | |
| 396 } | |
| 397 } | |
| 398 } | |
| 399 } | |
| 400 | |
| 401 // static | |
| 402 void StatusAreaView::PowerStatusChangedHandler( | |
| 403 void* object, const chromeos::PowerStatus& status) { | |
| 404 static_cast<StatusAreaView*>(object)->PowerStatusChanged(status); | |
| 405 } | |
| 406 | |
| 407 void StatusAreaView::PowerStatusChanged(const chromeos::PowerStatus& status) { | |
| 408 ThemeProvider* theme = browser_->profile()->GetThemeProvider(); | |
| 409 int image_index; | |
| 410 | |
| 411 if (status.battery_state == chromeos::BATTERY_STATE_FULLY_CHARGED && | |
| 412 status.line_power_on) { | |
| 413 image_index = IDR_STATUSBAR_BATTERY_CHARGED; | |
| 414 } else { | |
| 415 image_index = status.line_power_on ? | |
| 416 IDR_STATUSBAR_BATTERY_CHARGING_1 : | |
| 417 IDR_STATUSBAR_BATTERY_DISCHARGING_1; | |
| 418 double percentage = status.battery_percentage; | |
| 419 int offset = floor(percentage / (100.0 / kNumBatteryImages)); | |
| 420 // This can happen if the battery is 100% full. | |
| 421 if (offset == kNumBatteryImages) | |
| 422 offset--; | |
| 423 image_index += offset; | |
| 424 } | |
| 425 battery_view_->SetImage(theme->GetBitmapNamed(image_index)); | |
| 426 } | |
| OLD | NEW |