summaryrefslogtreecommitdiffstats
path: root/athena/system/system_ui_impl.cc
blob: 353a45bb395490e59579f38f4ef17dbea35815cc (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
// Copyright 2014 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 "athena/system/public/system_ui.h"

#include "athena/screen/public/screen_manager.h"
#include "athena/system/background_controller.h"
#include "athena/system/orientation_controller.h"
#include "athena/system/shutdown_dialog.h"
#include "athena/system/status_icon_container_view.h"
#include "athena/system/time_view.h"
#include "athena/util/container_priorities.h"
#include "athena/util/fill_layout_manager.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "ui/aura/window.h"
#include "ui/views/view.h"

namespace athena {
namespace {

SystemUI* instance = NULL;

// View which positions the TimeView on the left and the StatusIconView on the
// right.
class SystemInfoView : public views::View {
 public:
  SystemInfoView(SystemUI::ColorScheme color_scheme,
                 aura::Window* system_modal_container)
      : time_view_(new TimeView(color_scheme)),
        status_icon_view_(
            new StatusIconContainerView(color_scheme, system_modal_container)) {
    AddChildView(time_view_);
    AddChildView(status_icon_view_);
  }

  virtual ~SystemInfoView() {
  }

  // views::View:
  virtual gfx::Size GetPreferredSize() const override {
    // The view should be as wide as its parent view.
    return gfx::Size(0,
                     std::max(time_view_->GetPreferredSize().height(),
                              status_icon_view_->GetPreferredSize().height()));
  }

  virtual void Layout() override {
    time_view_->SetBoundsRect(gfx::Rect(time_view_->GetPreferredSize()));
    gfx::Size status_icon_preferred_size =
        status_icon_view_->GetPreferredSize();
    status_icon_view_->SetBoundsRect(
        gfx::Rect(width() - status_icon_preferred_size.width(),
                  0,
                  status_icon_preferred_size.width(),
                  status_icon_preferred_size.height()));
  }

  virtual void ChildPreferredSizeChanged(views::View* child) override {
    // Relayout to take into account changes in |status_icon_view_|'s width.
    // Assume that |time_view_|'s and |status_icon_view_|'s preferred height
    // does not change.
    Layout();
  }

 private:
  views::View* time_view_;
  views::View* status_icon_view_;

  DISALLOW_COPY_AND_ASSIGN(SystemInfoView);
};

class SystemUIImpl : public SystemUI {
 public:
  SystemUIImpl(scoped_refptr<base::TaskRunner> blocking_task_runner)
      : orientation_controller_(new OrientationController()),
        background_container_(NULL),
        system_modal_container_(NULL) {
    orientation_controller_->InitWith(blocking_task_runner);
  }

  virtual ~SystemUIImpl() {
    // Stops file watching now if exists. Waiting until message loop shutdon
    // leads to FilePathWatcher crash.
    orientation_controller_->Shutdown();
  }

  void Init() {
    ScreenManager* screen_manager = ScreenManager::Get();
    background_container_ = screen_manager->CreateContainer(
        ScreenManager::ContainerParams("AthenaBackground", CP_BACKGROUND));
    background_container_->SetLayoutManager(
        new FillLayoutManager(background_container_));
    ScreenManager::ContainerParams system_modal_params(
        "AthenaSystemModalContainer", CP_SYSTEM_MODAL);
    system_modal_params.can_activate_children = true;
    system_modal_container_ =
        screen_manager->CreateContainer(system_modal_params);
    login_screen_system_modal_container_ = screen_manager->CreateContainer(
        ScreenManager::ContainerParams("AthenaLoginScreenSystemModalContainer",
                                       CP_LOGIN_SCREEN_SYSTEM_MODAL));

    // Use |login_screen_system_modal_container_| for the power button's dialog
    // because it needs to show over the login screen.
    // TODO(pkotwicz): Pick the most appropriate container based on whether the
    // user has logged in.
    shutdown_dialog_.reset(
        new ShutdownDialog(login_screen_system_modal_container_));
    background_controller_.reset(
        new BackgroundController(background_container_));
  }

  virtual void SetBackgroundImage(const gfx::ImageSkia& image) override {
    background_controller_->SetImage(image);
  }

  virtual views::View* CreateSystemInfoView(ColorScheme color_scheme) override {
    return new SystemInfoView(color_scheme, system_modal_container_);
  }

 private:
  scoped_ptr<OrientationController> orientation_controller_;
  scoped_ptr<ShutdownDialog> shutdown_dialog_;
  scoped_ptr<BackgroundController> background_controller_;

  // The parent container for the background.
  aura::Window* background_container_;

  // The parent container used by system modal dialogs.
  aura::Window* system_modal_container_;

  // The parent container used by system modal dialogs when the login screen is
  // visible.
  aura::Window* login_screen_system_modal_container_;

  DISALLOW_COPY_AND_ASSIGN(SystemUIImpl);
};

}  // namespace

// static
SystemUI* SystemUI::Create(
    scoped_refptr<base::TaskRunner> blocking_task_runner) {
  SystemUIImpl* system_ui = new SystemUIImpl(blocking_task_runner);
  instance = system_ui;
  system_ui->Init();
  return instance;
}

// static
SystemUI* SystemUI::Get() {
  DCHECK(instance);
  return instance;
}

// static
void SystemUI::Shutdown() {
  CHECK(instance);
  delete instance;
  instance = NULL;
}

}  // namespace athena