Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ash / ime / mode_indicator_view.cc
1 // Copyright 2014 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/ime/mode_indicator_view.h"
6
7 #include "base/logging.h"
8 #include "ui/gfx/display.h"
9 #include "ui/gfx/screen.h"
10 #include "ui/views/bubble/bubble_delegate.h"
11 #include "ui/views/bubble/bubble_frame_view.h"
12 #include "ui/views/controls/label.h"
13 #include "ui/views/corewm/window_animations.h"
14 #include "ui/views/layout/fill_layout.h"
15
16 namespace ash {
17 namespace ime {
18
19 namespace {
20 // Minimum size of inner contents in pixel.
21 // 43 is the designed size including the default margin (6 * 2).
22 const int kMinSize = 31;
23
24 // After this duration in msec, the mode inicator will be fading out.
25 const int kShowingDuration = 500;
26
27 class ModeIndicatorFrameView : public views::BubbleFrameView {
28  public:
29   explicit ModeIndicatorFrameView(const gfx::Insets& content_margins)
30       : views::BubbleFrameView(content_margins) {}
31   virtual ~ModeIndicatorFrameView() {}
32
33  private:
34   // views::BubbleFrameView overrides:
35   virtual gfx::Rect GetAvailableScreenBounds(const gfx::Rect& rect) OVERRIDE {
36     return gfx::Screen::GetNativeScreen()->GetDisplayNearestPoint(
37         rect.CenterPoint()).bounds();
38   }
39
40   DISALLOW_COPY_AND_ASSIGN(ModeIndicatorFrameView);
41 };
42
43 }  // namespace
44
45
46 ModeIndicatorView::ModeIndicatorView(gfx::NativeView parent,
47                                      const gfx::Rect& cursor_bounds,
48                                      const base::string16& label)
49     : cursor_bounds_(cursor_bounds),
50       label_view_(new views::Label(label)) {
51   set_use_focusless(true);
52   set_accept_events(false);
53   set_parent_window(parent);
54   set_shadow(views::BubbleBorder::NO_SHADOW);
55   set_arrow(views::BubbleBorder::TOP_CENTER);
56 }
57
58 ModeIndicatorView::~ModeIndicatorView() {}
59
60 void ModeIndicatorView::FadeOut() {
61   StartFade(false);
62 }
63
64 void ModeIndicatorView::ShowAndFadeOut() {
65   views::corewm::SetWindowVisibilityAnimationTransition(
66       GetWidget()->GetNativeView(),
67       views::corewm::ANIMATE_HIDE);
68   GetWidget()->Show();
69   timer_.Start(FROM_HERE,
70                base::TimeDelta::FromMilliseconds(kShowingDuration),
71                this,
72                &ModeIndicatorView::FadeOut);
73 }
74
75 gfx::Size ModeIndicatorView::GetPreferredSize() {
76   gfx::Size size = label_view_->GetPreferredSize();
77   size.SetToMax(gfx::Size(kMinSize, kMinSize));
78   return size;
79 }
80
81 void ModeIndicatorView::Init() {
82   SetLayoutManager(new views::FillLayout());
83   AddChildView(label_view_);
84
85   SetAnchorRect(cursor_bounds_);
86 }
87
88 views::NonClientFrameView* ModeIndicatorView::CreateNonClientFrameView(
89     views::Widget* widget) {
90   views::BubbleFrameView* frame = new ModeIndicatorFrameView(margins());
91   // arrow adjustment in BubbleDelegateView is unnecessary because arrow
92   // of this bubble is always center.
93   frame->SetBubbleBorder(scoped_ptr<views::BubbleBorder>(
94       new views::BubbleBorder(arrow(), shadow(), color())));
95   return frame;
96 }
97
98 }  // namespace ime
99 }  // namespace ash