OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "ui/views/corewm/tooltip_aura.h" | 5 #include "ui/views/corewm/tooltip_aura.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "ui/aura/window.h" | 10 #include "ui/aura/window.h" |
11 #include "ui/aura/window_tree_host.h" | 11 #include "ui/aura/window_tree_host.h" |
12 #include "ui/base/material_design/material_design_controller.h" | |
13 #include "ui/display/display.h" | 12 #include "ui/display/display.h" |
14 #include "ui/display/screen.h" | 13 #include "ui/display/screen.h" |
15 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
16 #include "ui/gfx/render_text.h" | 15 #include "ui/gfx/render_text.h" |
17 #include "ui/gfx/text_elider.h" | 16 #include "ui/gfx/text_elider.h" |
18 #include "ui/gfx/text_utils.h" | 17 #include "ui/gfx/text_utils.h" |
19 #include "ui/native_theme/native_theme.h" | 18 #include "ui/native_theme/native_theme.h" |
20 #include "ui/views/background.h" | 19 #include "ui/views/background.h" |
21 #include "ui/views/border.h" | 20 #include "ui/views/border.h" |
22 #include "ui/views/painter.h" | 21 #include "ui/views/painter.h" |
23 #include "ui/views/view.h" | 22 #include "ui/views/view.h" |
24 #include "ui/views/widget/widget.h" | 23 #include "ui/views/widget/widget.h" |
25 | 24 |
26 namespace { | 25 namespace { |
27 | 26 |
28 // Max visual tooltip width. If a tooltip is greater than this width, it will | 27 // Max visual tooltip width. If a tooltip is greater than this width, it will |
29 // be wrapped. | 28 // be wrapped. |
30 const int kTooltipMaxWidthPixels = 400; | 29 const int kTooltipMaxWidthPixels = 400; |
31 | 30 |
32 // FIXME: get cursor offset from actual cursor size. | 31 // FIXME: get cursor offset from actual cursor size. |
33 const int kCursorOffsetX = 10; | 32 const int kCursorOffsetX = 10; |
34 const int kCursorOffsetY = 15; | 33 const int kCursorOffsetY = 15; |
35 | 34 |
36 // TODO(varkha): Update if native widget can be transparent on Linux. | 35 // TODO(varkha): Update if native widget can be transparent on Linux. |
37 bool CanUseTranslucentTooltipWidget() { | 36 bool CanUseTranslucentTooltipWidget() { |
38 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 37 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
39 return false; | 38 return false; |
40 #else | 39 #else |
41 return ui::MaterialDesignController::IsModeMaterial(); | 40 return true; |
42 #endif | 41 #endif |
43 } | 42 } |
44 | 43 |
45 // Creates a widget of type TYPE_TOOLTIP | 44 // Creates a widget of type TYPE_TOOLTIP |
46 views::Widget* CreateTooltipWidget(aura::Window* tooltip_window) { | 45 views::Widget* CreateTooltipWidget(aura::Window* tooltip_window) { |
47 views::Widget* widget = new views::Widget; | 46 views::Widget* widget = new views::Widget; |
48 views::Widget::InitParams params; | 47 views::Widget::InitParams params; |
49 // For aura, since we set the type to TYPE_TOOLTIP, the widget will get | 48 // For aura, since we set the type to TYPE_TOOLTIP, the widget will get |
50 // auto-parented to the right container. | 49 // auto-parented to the right container. |
51 params.type = views::Widget::InitParams::TYPE_TOOLTIP; | 50 params.type = views::Widget::InitParams::TYPE_TOOLTIP; |
52 params.context = tooltip_window; | 51 params.context = tooltip_window; |
53 DCHECK(params.context); | 52 DCHECK(params.context); |
54 params.keep_on_top = true; | 53 params.keep_on_top = true; |
55 params.accept_events = false; | 54 params.accept_events = false; |
56 if (CanUseTranslucentTooltipWidget()) | 55 if (CanUseTranslucentTooltipWidget()) |
57 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | 56 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
58 if (ui::MaterialDesignController::IsModeMaterial()) | 57 params.shadow_type = views::Widget::InitParams::SHADOW_TYPE_NONE; |
59 params.shadow_type = views::Widget::InitParams::SHADOW_TYPE_NONE; | |
60 widget->Init(params); | 58 widget->Init(params); |
61 return widget; | 59 return widget; |
62 } | 60 } |
63 | 61 |
64 } // namespace | 62 } // namespace |
65 | 63 |
66 namespace views { | 64 namespace views { |
67 namespace corewm { | 65 namespace corewm { |
68 | 66 |
69 // TODO(oshima): Consider to use views::Label. | 67 // TODO(oshima): Consider to use views::Label. |
70 class TooltipAura::TooltipView : public views::View { | 68 class TooltipAura::TooltipView : public views::View { |
71 public: | 69 public: |
72 TooltipView() | 70 TooltipView() |
73 : render_text_(gfx::RenderText::CreateInstance()), | 71 : render_text_(gfx::RenderText::CreateInstance()), |
74 max_width_(0) { | 72 max_width_(0) { |
75 const bool material = ui::MaterialDesignController::IsModeMaterial(); | 73 const int kHorizontalPadding = 8; |
76 const int kHorizontalPadding = material ? 8 : 3; | 74 const int kVerticalPaddingTop = 4; |
77 const int kVerticalPaddingTop = material ? 4 : 2; | 75 const int kVerticalPaddingBottom = 5; |
78 const int kVerticalPaddingBottom = material ? 5 : kVerticalPaddingTop; | |
79 SetBorder(Border::CreateEmptyBorder(kVerticalPaddingTop, kHorizontalPadding, | 76 SetBorder(Border::CreateEmptyBorder(kVerticalPaddingTop, kHorizontalPadding, |
80 kVerticalPaddingBottom, | 77 kVerticalPaddingBottom, |
81 kHorizontalPadding)); | 78 kHorizontalPadding)); |
82 | 79 |
83 set_owned_by_client(); | 80 set_owned_by_client(); |
84 render_text_->SetWordWrapBehavior(gfx::WRAP_LONG_WORDS); | 81 render_text_->SetWordWrapBehavior(gfx::WRAP_LONG_WORDS); |
85 render_text_->SetMultiline(true); | 82 render_text_->SetMultiline(true); |
86 | 83 |
87 ResetDisplayRect(); | 84 ResetDisplayRect(); |
88 } | 85 } |
(...skipping 29 matching lines...) Expand all Loading... |
118 render_text_->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD); | 115 render_text_->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD); |
119 render_text_->SetText(text); | 116 render_text_->SetText(text); |
120 SchedulePaint(); | 117 SchedulePaint(); |
121 } | 118 } |
122 | 119 |
123 void SetForegroundColor(SkColor color) { | 120 void SetForegroundColor(SkColor color) { |
124 render_text_->SetColor(color); | 121 render_text_->SetColor(color); |
125 } | 122 } |
126 | 123 |
127 void SetBackgroundColor(SkColor background_color) { | 124 void SetBackgroundColor(SkColor background_color) { |
128 // Corner radius of tooltip background used with Material Design. | 125 // Corner radius of tooltip background. |
129 const float kTooltipCornerRadius = 2.f; | 126 const float kTooltipCornerRadius = 2.f; |
130 views::Background* background = | 127 views::Background* background = |
131 CanUseTranslucentTooltipWidget() | 128 CanUseTranslucentTooltipWidget() |
132 ? views::Background::CreateBackgroundPainter( | 129 ? views::Background::CreateBackgroundPainter( |
133 true, views::Painter::CreateSolidRoundRectPainter( | 130 true, views::Painter::CreateSolidRoundRectPainter( |
134 background_color, kTooltipCornerRadius)) | 131 background_color, kTooltipCornerRadius)) |
135 : views::Background::CreateSolidBackground(background_color); | 132 : views::Background::CreateSolidBackground(background_color); |
136 set_background(background); | 133 set_background(background); |
137 // Force the text color to be readable when |background_color| is not | 134 // Force the text color to be readable when |background_color| is not |
138 // opaque. | 135 // opaque. |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 } | 242 } |
246 | 243 |
247 void TooltipAura::OnWidgetDestroying(views::Widget* widget) { | 244 void TooltipAura::OnWidgetDestroying(views::Widget* widget) { |
248 DCHECK_EQ(widget_, widget); | 245 DCHECK_EQ(widget_, widget); |
249 widget_ = NULL; | 246 widget_ = NULL; |
250 tooltip_window_ = NULL; | 247 tooltip_window_ = NULL; |
251 } | 248 } |
252 | 249 |
253 } // namespace corewm | 250 } // namespace corewm |
254 } // namespace views | 251 } // namespace views |
OLD | NEW |