status_area_view.cc revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2010 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 5#include "chrome/browser/chromeos/status/status_area_view.h" 6 7#include <algorithm> 8 9#include "chrome/browser/chromeos/status/clock_menu_button.h" 10#include "chrome/browser/chromeos/status/input_method_menu_button.h" 11#include "chrome/browser/chromeos/status/network_menu_button.h" 12#include "chrome/browser/chromeos/status/power_menu_button.h" 13#include "chrome/browser/chromeos/status/status_area_host.h" 14#include "chrome/browser/chromeos/status/window_switcher_button.h" 15#include "ui/gfx/canvas.h" 16 17namespace chromeos { 18 19// Number of pixels to separate each icon. 20const int kSeparation = 1; 21 22StatusAreaView::StatusAreaView(StatusAreaHost* host) 23 : host_(host), 24 clock_view_(NULL), 25 input_method_view_(NULL), 26 network_view_(NULL), 27 power_view_(NULL), 28 window_switcher_view_(NULL) { 29} 30 31void StatusAreaView::Init() { 32 // Clock. 33 clock_view_ = new ClockMenuButton(host_); 34 AddChildView(clock_view_); 35 36 // InputMethod. 37 input_method_view_ = new InputMethodMenuButton(host_); 38 AddChildView(input_method_view_); 39 40 // Network. 41 network_view_ = new NetworkMenuButton(host_); 42 AddChildView(network_view_); 43 44 // Power. 45 power_view_ = new PowerMenuButton(); 46 AddChildView(power_view_); 47 48 // Window Switcher. 49 window_switcher_view_ = new WindowSwitcherButton(host_); 50 AddChildView(window_switcher_view_); 51} 52 53gfx::Size StatusAreaView::GetPreferredSize() { 54 int result_w = kSeparation; 55 int result_h = 0; 56 for (int i = 0; i < child_count(); i++) { 57 views::View* cur = GetChildViewAt(i); 58 if (cur->IsVisible()) { 59 gfx::Size cur_size = cur->GetPreferredSize(); 60 // Add each width. 61 result_w += cur_size.width() + kSeparation; 62 // Use max height. 63 result_h = std::max(result_h, cur_size.height()); 64 } 65 } 66 return gfx::Size(result_w, result_h); 67} 68 69void StatusAreaView::Layout() { 70 int cur_x = kSeparation; 71 for (int i = 0; i < child_count(); i++) { 72 views::View* cur = GetChildViewAt(i); 73 if (cur->IsVisible()) { 74 gfx::Size cur_size = cur->GetPreferredSize(); 75 int cur_y = (height() - cur_size.height()) / 2; 76 77 // Handle odd number of pixels. 78 cur_y += (height() - cur_size.height()) % 2; 79 80 // Put next in row horizontally, and center vertically. 81 cur->SetBounds(cur_x, cur_y, cur_size.width(), cur_size.height()); 82 83 if (cur_size.width() > 0) 84 cur_x += cur_size.width() + kSeparation; 85 } 86 } 87} 88 89void StatusAreaView::ChildPreferredSizeChanged(View* child) { 90 // When something like the clock menu button's size changes, we need to 91 // relayout. Also mark that this view's size has changed. This will let 92 // BrowserView know to relayout, which will reset the bounds of this view. 93 Layout(); 94 PreferredSizeChanged(); 95} 96 97void StatusAreaView::MakeButtonsActive(bool active) { 98 clock_view()->set_active(active); 99 input_method_view()->set_active(active); 100 network_view()->set_active(active); 101 power_view()->set_active(active); 102} 103 104} // namespace chromeos 105