• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/layout/fill_layout.h"
14 #include "ui/wm/core/window_animations.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:
ModeIndicatorFrameView(const gfx::Insets & content_margins)29   explicit ModeIndicatorFrameView(const gfx::Insets& content_margins)
30       : views::BubbleFrameView(content_margins) {}
~ModeIndicatorFrameView()31   virtual ~ModeIndicatorFrameView() {}
32 
33  private:
34   // views::BubbleFrameView overrides:
GetAvailableScreenBounds(const gfx::Rect & rect)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 
ModeIndicatorView(gfx::NativeView parent,const gfx::Rect & cursor_bounds,const base::string16 & label)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_can_activate(false);
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 
~ModeIndicatorView()58 ModeIndicatorView::~ModeIndicatorView() {}
59 
ShowAndFadeOut()60 void ModeIndicatorView::ShowAndFadeOut() {
61   wm::SetWindowVisibilityAnimationTransition(
62       GetWidget()->GetNativeView(),
63       wm::ANIMATE_HIDE);
64   GetWidget()->Show();
65   timer_.Start(FROM_HERE,
66                base::TimeDelta::FromMilliseconds(kShowingDuration),
67                GetWidget(),
68                &views::Widget::Close);
69 }
70 
GetPreferredSize() const71 gfx::Size ModeIndicatorView::GetPreferredSize() const {
72   gfx::Size size = label_view_->GetPreferredSize();
73   size.SetToMax(gfx::Size(kMinSize, kMinSize));
74   return size;
75 }
76 
Init()77 void ModeIndicatorView::Init() {
78   SetLayoutManager(new views::FillLayout());
79   AddChildView(label_view_);
80 
81   SetAnchorRect(cursor_bounds_);
82 }
83 
CreateNonClientFrameView(views::Widget * widget)84 views::NonClientFrameView* ModeIndicatorView::CreateNonClientFrameView(
85     views::Widget* widget) {
86   views::BubbleFrameView* frame = new ModeIndicatorFrameView(margins());
87   // arrow adjustment in BubbleDelegateView is unnecessary because arrow
88   // of this bubble is always center.
89   frame->SetBubbleBorder(scoped_ptr<views::BubbleBorder>(
90       new views::BubbleBorder(arrow(), shadow(), color())));
91   return frame;
92 }
93 
94 }  // namespace ime
95 }  // namespace ash
96