summaryrefslogtreecommitdiff
path: root/chromium/ash/system/chromeos/screen_security/screen_tray_item.cc
blob: 4b10a270723f70519a0a7dae7a4d218e4613c998 (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
// Copyright 2013 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/screen_security/screen_tray_item.h"

#include "ash/system/tray/fixed_sized_image_view.h"
#include "ash/system/tray/tray_constants.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/message_center/message_center.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"

namespace {
const int kStopButtonRightPadding = 18;
}  // namespace

namespace ash {
namespace internal {

namespace tray {

// ScreenTrayView implementations.
ScreenTrayView::ScreenTrayView(ScreenTrayItem* screen_tray_item, int icon_id)
    : TrayItemView(screen_tray_item),
      screen_tray_item_(screen_tray_item) {
  CreateImageView();
  image_view()->SetImage(ui::ResourceBundle::GetSharedInstance()
      .GetImageNamed(icon_id).ToImageSkia());

  Update();
}

ScreenTrayView::~ScreenTrayView() {
}

void ScreenTrayView::Update() {
  SetVisible(screen_tray_item_->is_started());
}


// ScreenStatusView implementations.
ScreenStatusView::ScreenStatusView(ScreenTrayItem* screen_tray_item,
                                   int icon_id,
                                   const base::string16& label_text,
                                   const base::string16& stop_button_text)
    : screen_tray_item_(screen_tray_item),
      icon_(NULL),
      label_(NULL),
      stop_button_(NULL),
      icon_id_(icon_id),
      label_text_(label_text),
      stop_button_text_(stop_button_text) {
  CreateItems();
  Update();
}

ScreenStatusView::~ScreenStatusView() {
}

void ScreenStatusView::Layout() {
  views::View::Layout();

  // Give the stop button the space it requests.
  gfx::Size stop_size = stop_button_->GetPreferredSize();
  gfx::Rect stop_bounds(stop_size);
  stop_bounds.set_x(width() - stop_size.width() - kStopButtonRightPadding);
  stop_bounds.set_y((height() - stop_size.height()) / 2);
  stop_button_->SetBoundsRect(stop_bounds);

  // Adjust the label's bounds in case it got cut off by |stop_button_|.
  if (label_->bounds().Intersects(stop_button_->bounds())) {
    gfx::Rect label_bounds = label_->bounds();
    label_bounds.set_width(
        stop_button_->x() - kTrayPopupPaddingBetweenItems - label_->x());
    label_->SetBoundsRect(label_bounds);
  }
}

void ScreenStatusView::ButtonPressed(
    views::Button* sender,
    const ui::Event& event) {
  DCHECK(sender == stop_button_);
  screen_tray_item_->Stop();
}

void ScreenStatusView::CreateItems() {
  set_background(views::Background::CreateSolidBackground(kBackgroundColor));
  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
  SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
                                        kTrayPopupPaddingHorizontal,
                                        0,
                                        kTrayPopupPaddingBetweenItems));
  icon_ = new FixedSizedImageView(0, kTrayPopupItemHeight);
  icon_->SetImage(bundle.GetImageNamed(icon_id_).ToImageSkia());
  AddChildView(icon_);
  label_ = new views::Label;
  label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  label_->SetMultiLine(true);
  label_->SetText(label_text_);
  AddChildView(label_);

  stop_button_ = new TrayPopupLabelButton(this, stop_button_text_);
  AddChildView(stop_button_);
}

void ScreenStatusView::Update() {
  // Hide the notification bubble when the ash tray bubble opens.
  screen_tray_item_->HideNotificationView();
  SetVisible(screen_tray_item_->is_started());
}

ScreenNotificationDelegate::ScreenNotificationDelegate(
    ScreenTrayItem* screen_tray)
  : screen_tray_(screen_tray) {
}

ScreenNotificationDelegate::~ScreenNotificationDelegate() {
}

void ScreenNotificationDelegate::Display() {
}

void ScreenNotificationDelegate::Error() {
}

void ScreenNotificationDelegate::Close(bool by_user) {
}

void ScreenNotificationDelegate::Click() {
}

void ScreenNotificationDelegate::ButtonClick(int button_index) {
  DCHECK_EQ(0, button_index);
  screen_tray_->Stop();
}

}  // namespace tray

ScreenTrayItem::ScreenTrayItem(SystemTray* system_tray)
    : SystemTrayItem(system_tray),
      tray_view_(NULL),
      default_view_(NULL),
      is_started_(false),
      stop_callback_(base::Bind(&base::DoNothing)) {
}

ScreenTrayItem::~ScreenTrayItem() {}

void ScreenTrayItem::Update() {
  if (tray_view_)
    tray_view_->Update();
  if (default_view_)
    default_view_->Update();
  if (is_started_) {
    CreateOrUpdateNotification();
  } else {
    message_center::MessageCenter::Get()->RemoveNotification(
        GetNotificationId(), false /* by_user */);
  }
}

void ScreenTrayItem::Start(const base::Closure& stop_callback) {
  stop_callback_ = stop_callback;
  is_started_ = true;

  if (tray_view_)
    tray_view_->Update();

  if (default_view_)
    default_view_->Update();

  if (!system_tray()->HasSystemBubbleType(
      SystemTrayBubble::BUBBLE_TYPE_DEFAULT)) {
    CreateOrUpdateNotification();
  }
}

void ScreenTrayItem::Stop() {
  is_started_ = false;
  Update();

  if (stop_callback_.is_null())
    return;

  base::Closure callback = stop_callback_;
  stop_callback_.Reset();
  callback.Run();
}

void ScreenTrayItem::DestroyTrayView() {
  tray_view_ = NULL;
}

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

}  // namespace internal
}  // namespace ash