OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/launcher/launcher_tooltip_manager.h" | 5 #include "ash/launcher/launcher_tooltip_manager.h" |
6 | 6 |
7 #include "ash/launcher/launcher_view.h" | 7 #include "ash/launcher/launcher_view.h" |
8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
9 #include "ash/shell_window_ids.h" | 9 #include "ash/shell_window_ids.h" |
10 #include "ash/wm/window_animations.h" | 10 #include "ash/wm/window_animations.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "base/timer.h" | 14 #include "base/timer.h" |
15 #include "ui/aura/event.h" | 15 #include "ui/aura/event.h" |
16 #include "ui/aura/root_window.h" | 16 #include "ui/aura/root_window.h" |
17 #include "ui/aura/window.h" | 17 #include "ui/aura/window.h" |
18 #include "ui/base/events.h" | 18 #include "ui/base/events.h" |
19 #include "ui/gfx/insets.h" | 19 #include "ui/gfx/insets.h" |
20 #include "ui/views/bubble/bubble_delegate.h" | 20 #include "ui/views/bubble/bubble_delegate.h" |
| 21 #include "ui/views/bubble/bubble_frame_view.h" |
| 22 #include "ui/views/bubble/bubble_border_2.h" |
21 #include "ui/views/controls/label.h" | 23 #include "ui/views/controls/label.h" |
22 #include "ui/views/layout/fill_layout.h" | 24 #include "ui/views/layout/fill_layout.h" |
23 #include "ui/views/widget/widget.h" | 25 #include "ui/views/widget/widget.h" |
24 | 26 |
25 namespace ash { | 27 namespace ash { |
26 namespace internal { | 28 namespace internal { |
27 namespace { | 29 namespace { |
28 const int kTooltipMargin = 3; | 30 const int kTooltipTopBottomMargin = 3; |
| 31 const int kTooltipLeftRightMargin = 10; |
29 const int kTooltipAppearanceDelay = 200; // msec | 32 const int kTooltipAppearanceDelay = 200; // msec |
| 33 const int kTooltipMinHeight = 29 - 2 * kTooltipTopBottomMargin; |
| 34 const SkColor kTooltipTextColor = SkColorSetRGB(127, 127, 127); |
30 | 35 |
31 // The maximum width of the tooltip bubble. Borrowed the value from | 36 // The maximum width of the tooltip bubble. Borrowed the value from |
32 // ash/tooltip/tooltip_controller.cc | 37 // ash/tooltip/tooltip_controller.cc |
33 const int kTooltipMaxWidth = 400; | 38 const int kTooltipMaxWidth = 250; |
| 39 |
| 40 // Bubble border metrics |
| 41 const int kArrowHeight = 7; |
| 42 const int kArrowWidth = 15; |
| 43 const int kShadowWidth = 8; |
| 44 // The distance between the arrow tip and edge of the anchor view. |
| 45 const int kArrowOffset = 10; |
34 | 46 |
35 views::BubbleBorder::ArrowLocation GetArrowLocation(ShelfAlignment alignment) { | 47 views::BubbleBorder::ArrowLocation GetArrowLocation(ShelfAlignment alignment) { |
36 switch (alignment) { | 48 switch (alignment) { |
37 case SHELF_ALIGNMENT_LEFT: | 49 case SHELF_ALIGNMENT_LEFT: |
38 return views::BubbleBorder::LEFT_BOTTOM; | 50 return views::BubbleBorder::LEFT_BOTTOM; |
39 case SHELF_ALIGNMENT_RIGHT: | 51 case SHELF_ALIGNMENT_RIGHT: |
40 return views::BubbleBorder::RIGHT_BOTTOM; | 52 return views::BubbleBorder::RIGHT_BOTTOM; |
41 case SHELF_ALIGNMENT_BOTTOM: | 53 case SHELF_ALIGNMENT_BOTTOM: |
42 return views::BubbleBorder::BOTTOM_RIGHT; | 54 return views::BubbleBorder::BOTTOM_RIGHT; |
43 } | 55 } |
44 | 56 |
45 return views::BubbleBorder::NONE; | 57 return views::BubbleBorder::NONE; |
46 } | 58 } |
47 } // namespace | 59 } // namespace |
48 | 60 |
49 // The implementation of tooltip of the launcher. | 61 // The implementation of tooltip of the launcher. |
50 class LauncherTooltipManager::LauncherTooltipBubble | 62 class LauncherTooltipManager::LauncherTooltipBubble |
51 : public views::BubbleDelegateView { | 63 : public views::BubbleDelegateView { |
52 public: | 64 public: |
53 LauncherTooltipBubble(views::View* anchor, | 65 LauncherTooltipBubble(views::View* anchor, |
54 views::BubbleBorder::ArrowLocation arrow_location, | 66 views::BubbleBorder::ArrowLocation arrow_location, |
55 LauncherTooltipManager* host); | 67 LauncherTooltipManager* host); |
56 | 68 |
57 void SetText(const string16& text); | 69 void SetText(const string16& text); |
58 void Close(); | 70 void Close(); |
59 | 71 |
60 private: | 72 private: |
| 73 // Overridden from views::BubbleDelegateView: |
| 74 virtual gfx::Rect GetBubbleBounds() OVERRIDE; |
| 75 |
61 // views::WidgetDelegate overrides: | 76 // views::WidgetDelegate overrides: |
62 virtual void WindowClosing() OVERRIDE; | 77 virtual void WindowClosing() OVERRIDE; |
63 | 78 |
| 79 // views::View overrides: |
| 80 virtual gfx::Size GetPreferredSize() OVERRIDE; |
| 81 |
64 LauncherTooltipManager* host_; | 82 LauncherTooltipManager* host_; |
65 views::Label* label_; | 83 views::Label* label_; |
| 84 views::BubbleBorder2* bubble_border_; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(LauncherTooltipBubble); |
66 }; | 87 }; |
67 | 88 |
68 LauncherTooltipManager::LauncherTooltipBubble::LauncherTooltipBubble( | 89 LauncherTooltipManager::LauncherTooltipBubble::LauncherTooltipBubble( |
69 views::View* anchor, | 90 views::View* anchor, |
70 views::BubbleBorder::ArrowLocation arrow_location, | 91 views::BubbleBorder::ArrowLocation arrow_location, |
71 LauncherTooltipManager* host) | 92 LauncherTooltipManager* host) |
72 : views::BubbleDelegateView(anchor, arrow_location), | 93 : views::BubbleDelegateView(anchor, arrow_location), |
73 host_(host) { | 94 host_(host), |
| 95 bubble_border_(NULL) { |
74 set_close_on_esc(false); | 96 set_close_on_esc(false); |
75 set_close_on_deactivate(false); | 97 set_close_on_deactivate(false); |
76 set_use_focusless(true); | 98 set_use_focusless(true); |
77 set_margins(gfx::Insets(kTooltipMargin, kTooltipMargin, kTooltipMargin, | 99 set_margins(gfx::Insets(kTooltipTopBottomMargin, kTooltipLeftRightMargin, |
78 kTooltipMargin)); | 100 kTooltipTopBottomMargin, kTooltipLeftRightMargin)); |
79 SetLayoutManager(new views::FillLayout()); | 101 SetLayoutManager(new views::FillLayout()); |
80 // The anchor may not have the widget in tests. | 102 // The anchor may not have the widget in tests. |
81 if (anchor->GetWidget() && anchor->GetWidget()->GetNativeView()) { | 103 if (anchor->GetWidget() && anchor->GetWidget()->GetNativeView()) { |
82 aura::RootWindow* root_window = | 104 aura::RootWindow* root_window = |
83 anchor->GetWidget()->GetNativeView()->GetRootWindow(); | 105 anchor->GetWidget()->GetNativeView()->GetRootWindow(); |
84 set_parent_window(ash::Shell::GetInstance()->GetContainer( | 106 set_parent_window(ash::Shell::GetInstance()->GetContainer( |
85 root_window, ash::internal::kShellWindowId_SettingBubbleContainer)); | 107 root_window, ash::internal::kShellWindowId_SettingBubbleContainer)); |
86 } | 108 } |
87 label_ = new views::Label; | 109 label_ = new views::Label; |
88 label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | 110 label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 111 label_->SetEnabledColor(kTooltipTextColor); |
89 AddChildView(label_); | 112 AddChildView(label_); |
| 113 views::BubbleDelegateView::CreateBubble(this); |
| 114 bubble_border_ = new views::BubbleBorder2(views::BubbleBorder::BOTTOM_RIGHT); |
| 115 bubble_border_->SetShadow(gfx::ShadowValue( |
| 116 gfx::Point(0, 5), kShadowWidth, SkColorSetARGB(0x72, 0, 0, 0))); |
| 117 bubble_border_->set_arrow_width(kArrowWidth); |
| 118 bubble_border_->set_arrow_height(kArrowHeight); |
| 119 set_anchor_insets(gfx::Insets(kArrowOffset, kArrowOffset, kArrowOffset, |
| 120 kArrowOffset)); |
| 121 GetBubbleFrameView()->SetBubbleBorder(bubble_border_); |
90 } | 122 } |
91 | 123 |
92 void LauncherTooltipManager::LauncherTooltipBubble::SetText( | 124 void LauncherTooltipManager::LauncherTooltipBubble::SetText( |
93 const string16& text) { | 125 const string16& text) { |
94 label_->SetText(text); | 126 label_->SetText(text); |
95 label_->SetMultiLine(true); | |
96 label_->SizeToFit(kTooltipMaxWidth); | |
97 SizeToContents(); | 127 SizeToContents(); |
98 } | 128 } |
99 | 129 |
100 void LauncherTooltipManager::LauncherTooltipBubble::Close() { | 130 void LauncherTooltipManager::LauncherTooltipBubble::Close() { |
101 if (GetWidget()) { | 131 if (GetWidget()) { |
102 host_ = NULL; | 132 host_ = NULL; |
103 GetWidget()->Close(); | 133 GetWidget()->Close(); |
104 } | 134 } |
105 } | 135 } |
106 | 136 |
| 137 gfx::Rect LauncherTooltipManager::LauncherTooltipBubble::GetBubbleBounds() { |
| 138 // This happens before replacing the default border. |
| 139 if (!bubble_border_) |
| 140 return views::BubbleDelegateView::GetBubbleBounds(); |
| 141 |
| 142 const gfx::Rect anchor_rect = GetAnchorRect(); |
| 143 gfx::Rect bubble_rect = GetBubbleFrameView()->GetUpdatedWindowBounds( |
| 144 anchor_rect, |
| 145 GetPreferredSize(), |
| 146 false /* try_mirroring_arrow */); |
| 147 |
| 148 const gfx::Point old_offset = bubble_border_->offset(); |
| 149 bubble_rect = bubble_border_->ComputeOffsetAndUpdateBubbleRect(bubble_rect, |
| 150 anchor_rect); |
| 151 |
| 152 // Repaints border if arrow offset is changed. |
| 153 if (bubble_border_->offset() != old_offset) |
| 154 GetBubbleFrameView()->SchedulePaint(); |
| 155 |
| 156 return bubble_rect; |
| 157 } |
| 158 |
107 void LauncherTooltipManager::LauncherTooltipBubble::WindowClosing() { | 159 void LauncherTooltipManager::LauncherTooltipBubble::WindowClosing() { |
108 views::BubbleDelegateView::WindowClosing(); | 160 views::BubbleDelegateView::WindowClosing(); |
109 if (host_) | 161 if (host_) |
110 host_->OnBubbleClosed(this); | 162 host_->OnBubbleClosed(this); |
111 } | 163 } |
112 | 164 |
| 165 gfx::Size LauncherTooltipManager::LauncherTooltipBubble::GetPreferredSize() { |
| 166 gfx::Size pref_size = views::BubbleDelegateView::GetPreferredSize(); |
| 167 if (pref_size.height() < kTooltipMinHeight) |
| 168 pref_size.set_height(kTooltipMinHeight); |
| 169 if (pref_size.width() > kTooltipMaxWidth) |
| 170 pref_size.set_width(kTooltipMaxWidth); |
| 171 return pref_size; |
| 172 } |
| 173 |
113 LauncherTooltipManager::LauncherTooltipManager( | 174 LauncherTooltipManager::LauncherTooltipManager( |
114 ShelfAlignment alignment, | 175 ShelfAlignment alignment, |
115 ShelfLayoutManager* shelf_layout_manager, | 176 ShelfLayoutManager* shelf_layout_manager, |
116 LauncherView* launcher_view) | 177 LauncherView* launcher_view) |
117 : view_(NULL), | 178 : view_(NULL), |
118 widget_(NULL), | 179 widget_(NULL), |
119 anchor_(NULL), | 180 anchor_(NULL), |
120 alignment_(alignment), | 181 alignment_(alignment), |
121 shelf_layout_manager_(shelf_layout_manager), | 182 shelf_layout_manager_(shelf_layout_manager), |
122 launcher_view_(launcher_view) { | 183 launcher_view_(launcher_view) { |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 } | 393 } |
333 | 394 |
334 void LauncherTooltipManager::CreateBubble(views::View* anchor, | 395 void LauncherTooltipManager::CreateBubble(views::View* anchor, |
335 const string16& text) { | 396 const string16& text) { |
336 DCHECK(!view_); | 397 DCHECK(!view_); |
337 | 398 |
338 anchor_ = anchor; | 399 anchor_ = anchor; |
339 text_ = text; | 400 text_ = text; |
340 view_ = new LauncherTooltipBubble( | 401 view_ = new LauncherTooltipBubble( |
341 anchor, GetArrowLocation(alignment_), this); | 402 anchor, GetArrowLocation(alignment_), this); |
342 views::BubbleDelegateView::CreateBubble(view_); | |
343 widget_ = view_->GetWidget(); | 403 widget_ = view_->GetWidget(); |
344 view_->SetText(text_); | 404 view_->SetText(text_); |
345 | 405 |
346 gfx::NativeView native_view = widget_->GetNativeView(); | 406 gfx::NativeView native_view = widget_->GetNativeView(); |
347 SetWindowVisibilityAnimationType( | 407 SetWindowVisibilityAnimationType( |
348 native_view, WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL); | 408 native_view, WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL); |
349 SetWindowVisibilityAnimationTransition(native_view, ANIMATE_HIDE); | 409 SetWindowVisibilityAnimationTransition(native_view, ANIMATE_HIDE); |
350 } | 410 } |
351 | 411 |
352 } // namespace internal | 412 } // namespace internal |
353 } // namespace ash | 413 } // namespace ash |
OLD | NEW |