1// Copyright (c) 2012 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 "ash/session/session_state_delegate.h"
6#include "ash/shell.h"
7#include "ash/shell/example_factory.h"
8#include "ash/shell_window_ids.h"
9#include "base/strings/utf_string_conversions.h"
10#include "ui/aura/window.h"
11#include "ui/aura/window_event_dispatcher.h"
12#include "ui/gfx/canvas.h"
13#include "ui/gfx/font_list.h"
14#include "ui/gfx/geometry/rect.h"
15#include "ui/gfx/text_utils.h"
16#include "ui/views/controls/button/label_button.h"
17#include "ui/views/corewm/tooltip_controller.h"
18#include "ui/views/widget/widget.h"
19#include "ui/views/widget/widget_delegate.h"
20
21using ash::Shell;
22
23namespace ash {
24namespace shell {
25
26class LockView : public views::WidgetDelegateView,
27                 public views::ButtonListener {
28 public:
29  LockView() : unlock_button_(new views::LabelButton(
30                                  this, base::ASCIIToUTF16("Unlock"))) {
31    unlock_button_->SetStyle(views::Button::STYLE_BUTTON);
32    AddChildView(unlock_button_);
33    unlock_button_->SetFocusable(true);
34  }
35  virtual ~LockView() {}
36
37  // Overridden from views::View:
38  virtual gfx::Size GetPreferredSize() const OVERRIDE {
39    return gfx::Size(500, 400);
40  }
41
42 private:
43  // Overridden from views::View:
44  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
45    canvas->FillRect(GetLocalBounds(), SK_ColorYELLOW);
46    base::string16 text = base::ASCIIToUTF16("LOCKED!");
47    int string_width = gfx::GetStringWidth(text, font_list_);
48    canvas->DrawStringRect(text, font_list_, SK_ColorRED,
49                           gfx::Rect((width() - string_width)/ 2,
50                                     (height() - font_list_.GetHeight()) / 2,
51                                     string_width, font_list_.GetHeight()));
52  }
53  virtual void Layout() OVERRIDE {
54    gfx::Rect bounds = GetLocalBounds();
55    gfx::Size ps = unlock_button_->GetPreferredSize();
56    bounds.set_y(bounds.bottom() - ps.height() - 5);
57    bounds.set_x((bounds.width() - ps.width()) / 2);
58    bounds.set_size(ps);
59    unlock_button_->SetBoundsRect(bounds);
60  }
61  virtual void ViewHierarchyChanged(
62      const ViewHierarchyChangedDetails& details) OVERRIDE {
63    if (details.is_add && details.child == this)
64      unlock_button_->RequestFocus();
65  }
66
67  // Overridden from views::WidgetDelegateView:
68  virtual void WindowClosing() OVERRIDE {
69    Shell::GetInstance()->session_state_delegate()->UnlockScreen();
70  }
71
72  // Overridden from views::ButtonListener:
73  virtual void ButtonPressed(views::Button* sender,
74                             const ui::Event& event) OVERRIDE {
75    DCHECK(sender == unlock_button_);
76    GetWidget()->Close();
77  }
78
79  gfx::FontList font_list_;
80  views::LabelButton* unlock_button_;
81
82  DISALLOW_COPY_AND_ASSIGN(LockView);
83};
84
85void CreateLockScreen() {
86  LockView* lock_view = new LockView;
87  views::Widget* widget = new views::Widget;
88  views::Widget::InitParams params(
89      views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
90  gfx::Size ps = lock_view->GetPreferredSize();
91
92  gfx::Size root_window_size = Shell::GetPrimaryRootWindow()->bounds().size();
93  params.bounds = gfx::Rect((root_window_size.width() - ps.width()) / 2,
94                            (root_window_size.height() - ps.height()) / 2,
95                            ps.width(), ps.height());
96  params.delegate = lock_view;
97  params.parent = Shell::GetContainer(Shell::GetPrimaryRootWindow(),
98                                      kShellWindowId_LockScreenContainer);
99  widget->Init(params);
100  widget->SetContentsView(lock_view);
101  widget->Show();
102  widget->GetNativeView()->SetName("LockView");
103  widget->GetNativeView()->Focus();
104
105  // TODO: it shouldn't be necessary to invoke UpdateTooltip() here.
106  Shell::GetInstance()->tooltip_controller()->UpdateTooltip(
107      widget->GetNativeView());
108}
109
110}  // namespace shell
111}  // namespace ash
112