summaryrefslogtreecommitdiff
path: root/chromium/ash/system/chromeos/settings/tray_settings.cc
blob: 8868b7cf50d816a894ee5a37cb80fc82c2ce79a4 (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
// Copyright (c) 2012 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 "ash/system/chromeos/settings/tray_settings.h"

#include "ash/shell.h"
#include "ash/system/chromeos/power/power_status.h"
#include "ash/system/chromeos/power/power_status_view.h"
#include "ash/system/tray/actionable_view.h"
#include "ash/system/tray/fixed_sized_image_view.h"
#include "ash/system/tray/system_tray_delegate.h"
#include "ash/system/tray/tray_constants.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "grit/ash_resources.h"
#include "grit/ash_strings.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/accessibility/accessible_view_state.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/view.h"

namespace ash {
namespace internal {

namespace tray {

class SettingsDefaultView : public ActionableView,
                            public PowerStatus::Observer {
 public:
  explicit SettingsDefaultView(user::LoginStatus status)
      : login_status_(status),
        label_(NULL),
        power_status_view_(NULL) {
    PowerStatus::Get()->AddObserver(this);
    SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
        ash::kTrayPopupPaddingHorizontal, 0,
        ash::kTrayPopupPaddingBetweenItems));

    bool power_view_right_align = false;
    if (login_status_ != user::LOGGED_IN_NONE &&
        login_status_ != user::LOGGED_IN_LOCKED) {
      ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
      views::ImageView* icon =
          new ash::internal::FixedSizedImageView(0, ash::kTrayPopupItemHeight);
      icon->SetImage(
          rb.GetImageNamed(IDR_AURA_UBER_TRAY_SETTINGS).ToImageSkia());
      AddChildView(icon);

      base::string16 text = rb.GetLocalizedString(IDS_ASH_STATUS_TRAY_SETTINGS);
      label_ = new views::Label(text);
      AddChildView(label_);
      SetAccessibleName(text);

      power_view_right_align = true;
    }

    if (PowerStatus::Get()->IsBatteryPresent()) {
      power_status_view_ = new ash::internal::PowerStatusView(
          ash::internal::PowerStatusView::VIEW_DEFAULT, power_view_right_align);
      AddChildView(power_status_view_);
      OnPowerStatusChanged();
    }
  }

  virtual ~SettingsDefaultView() {
    PowerStatus::Get()->RemoveObserver(this);
  }

  // Overridden from ash::internal::ActionableView.
  virtual bool PerformAction(const ui::Event& event) OVERRIDE {
    if (login_status_ == user::LOGGED_IN_NONE ||
        login_status_ == user::LOGGED_IN_LOCKED)
      return false;

    ash::Shell::GetInstance()->system_tray_delegate()->ShowSettings();
    return true;
  }

  // Overridden from views::View.
  virtual void Layout() OVERRIDE {
    views::View::Layout();

    if (label_ && power_status_view_) {
      // Let the box-layout do the layout first. Then move power_status_view_
      // to right align if it is created.
      gfx::Size size = power_status_view_->GetPreferredSize();
      gfx::Rect bounds(size);
      bounds.set_x(width() - size.width() - ash::kTrayPopupPaddingBetweenItems);
      bounds.set_y((height() - size.height()) / 2);
      power_status_view_->SetBoundsRect(bounds);
    }
  }

  // Overridden from views::View.
  virtual void ChildPreferredSizeChanged(views::View* child) OVERRIDE {
    views::View::ChildPreferredSizeChanged(child);
    Layout();
  }

  // Overridden from PowerStatus::Observer.
  virtual void OnPowerStatusChanged() OVERRIDE {
    if (!PowerStatus::Get()->IsBatteryPresent())
      return;

    base::string16 accessible_name = label_ ?
        label_->text() + ASCIIToUTF16(", ") +
            PowerStatus::Get()->GetAccessibleNameString() :
        PowerStatus::Get()->GetAccessibleNameString();
    SetAccessibleName(accessible_name);
  }

 private:
  user::LoginStatus login_status_;
  views::Label* label_;
  ash::internal::PowerStatusView* power_status_view_;

  DISALLOW_COPY_AND_ASSIGN(SettingsDefaultView);
 };

}  // namespace tray

TraySettings::TraySettings(SystemTray* system_tray)
    : SystemTrayItem(system_tray),
      default_view_(NULL) {
}

TraySettings::~TraySettings() {
}

views::View* TraySettings::CreateTrayView(user::LoginStatus status) {
  return NULL;
}

views::View* TraySettings::CreateDefaultView(user::LoginStatus status) {
  if ((status == user::LOGGED_IN_NONE || status == user::LOGGED_IN_LOCKED) &&
      !PowerStatus::Get()->IsBatteryPresent())
    return NULL;

  if (!ash::Shell::GetInstance()->system_tray_delegate()->ShouldShowSettings())
    return NULL;

  CHECK(default_view_ == NULL);
  default_view_ =  new tray::SettingsDefaultView(status);
  return default_view_;
}

views::View* TraySettings::CreateDetailedView(user::LoginStatus status) {
  NOTIMPLEMENTED();
  return NULL;
}

void TraySettings::DestroyTrayView() {
}

void TraySettings::DestroyDefaultView() {
  default_view_ = NULL;
}

void TraySettings::DestroyDetailedView() {
}

void TraySettings::UpdateAfterLoginStatusChange(user::LoginStatus status) {
}

}  // namespace internal
}  // namespace ash