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