lock_view.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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/strings/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(
59      const ViewHierarchyChangedDetails& details) OVERRIDE {
60    if (details.is_add && details.child == this)
61      unlock_button_->RequestFocus();
62  }
63
64  // Overridden from views::WidgetDelegateView:
65  virtual void WindowClosing() OVERRIDE {
66    Shell::GetInstance()->session_state_delegate()->UnlockScreen();
67  }
68
69  // Overridden from views::ButtonListener:
70  virtual void ButtonPressed(views::Button* sender,
71                             const ui::Event& event) OVERRIDE {
72    DCHECK(sender == unlock_button_);
73    GetWidget()->Close();
74  }
75
76  gfx::Font font_;
77  views::LabelButton* unlock_button_;
78
79  DISALLOW_COPY_AND_ASSIGN(LockView);
80};
81
82void CreateLockScreen() {
83  LockView* lock_view = new LockView;
84  views::Widget* widget = new views::Widget;
85  views::Widget::InitParams params(
86      views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
87  gfx::Size ps = lock_view->GetPreferredSize();
88
89  gfx::Size root_window_size = Shell::GetPrimaryRootWindow()->bounds().size();
90  params.bounds = gfx::Rect((root_window_size.width() - ps.width()) / 2,
91                            (root_window_size.height() - ps.height()) / 2,
92                            ps.width(), ps.height());
93  params.delegate = lock_view;
94  params.parent = Shell::GetContainer(
95      Shell::GetPrimaryRootWindow(),
96      internal::kShellWindowId_LockScreenContainer);
97  widget->Init(params);
98  widget->SetContentsView(lock_view);
99  widget->Show();
100  widget->GetNativeView()->SetName("LockView");
101  widget->GetNativeView()->Focus();
102
103  // TODO: it shouldn't be necessary to invoke UpdateTooltip() here.
104  Shell::GetInstance()->tooltip_controller()->UpdateTooltip(
105      widget->GetNativeView());
106}
107
108}  // namespace shell
109}  // namespace ash
110