summaryrefslogtreecommitdiffstats
path: root/ash/system/tray/system_tray.cc
blob: 9ff924e40efadb3234bf8b02aff22a8c9c939544 (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
// 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/tray/system_tray.h"

#include "ash/shell/panel_window.h"
#include "ash/system/tray/system_tray_item.h"
#include "base/logging.h"
#include "base/utf_string_conversions.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/views/border.h"
#include "ui/views/bubble/bubble_delegate.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view.h"

namespace {

const int kTrayIconHeight = 50;
const int kPadding = 5;

class SystemTrayBubble : public views::BubbleDelegateView {
 public:
  explicit SystemTrayBubble(ash::SystemTray* tray)
      : views::BubbleDelegateView(tray, views::BubbleBorder::BOTTOM_RIGHT),
        tray_(tray) {
    set_margin(1);
  }

  virtual ~SystemTrayBubble() {
    std::vector<ash::SystemTrayItem*> items = tray_->items();
    for (std::vector<ash::SystemTrayItem*>::iterator it = items.begin();
        it != items.end();
        ++it) {
      (*it)->DestroyDefaultView();
    }
  }

 private:
  // Overridden from views::BubbleDelegateView.
  virtual void Init() OVERRIDE {
    SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical,
          0, 0, 1));

    std::vector<ash::SystemTrayItem*> items = tray_->items();
    for (std::vector<ash::SystemTrayItem*>::iterator it = items.begin();
        it != items.end();
        ++it) {
      views::View* view = (*it)->CreateDefaultView();
      if (it != items.begin())
        view->set_border(views::Border::CreateSolidSidedBorder(
              1, 0, 0, 0, SkColorSetARGB(25, 0, 0, 0)));
      AddChildView(view);
    }
  }

  ash::SystemTray* tray_;

  DISALLOW_COPY_AND_ASSIGN(SystemTrayBubble);
};

}  // namespace

namespace ash {

SystemTray::SystemTray()
    : items_(),
      popup_(NULL) {
  SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
        5, 10, 3));
}

SystemTray::~SystemTray() {
  for (std::vector<ash::SystemTrayItem*>::iterator it = items_.begin();
      it != items_.end();
      ++it) {
    (*it)->DestroyTrayView();
  }
}

void SystemTray::AddTrayItem(SystemTrayItem* item) {
  items_.push_back(item);

  views::View* tray_item = item->CreateTrayView();
  if (tray_item) {
    AddChildView(tray_item);
    PreferredSizeChanged();
  }
}

void SystemTray::RemoveTrayItem(SystemTrayItem* item) {
  NOTIMPLEMENTED();
}

bool SystemTray::OnMousePressed(const views::MouseEvent& event) {
  if (popup_) {
    popup_->Show();
  } else {
    SystemTrayBubble* bubble = new SystemTrayBubble(this);
    popup_ = views::BubbleDelegateView::CreateBubble(bubble);
    popup_->AddObserver(this);
    bubble->Show();
  }
  return true;
}

void SystemTray::OnWidgetClosing(views::Widget* widget) {
  CHECK_EQ(popup_, widget);
  popup_ = NULL;
}

}  // namespace ash