OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "mash/wm/frame/default_header_painter.h" | |
6 | |
7 #include "base/debug/leak_annotations.h" | |
8 #include "base/logging.h" | |
9 #include "grit/mash_wm_resources.h" | |
10 #include "mash/wm/frame/caption_buttons/frame_caption_button_container_view.h" | |
11 #include "mash/wm/frame/header_painter_util.h" | |
12 #include "third_party/skia/include/core/SkPaint.h" | |
13 #include "third_party/skia/include/core/SkPath.h" | |
14 #include "ui/base/resource/resource_bundle.h" | |
15 #include "ui/gfx/animation/slide_animation.h" | |
16 #include "ui/gfx/canvas.h" | |
17 #include "ui/gfx/color_utils.h" | |
18 #include "ui/gfx/font_list.h" | |
19 #include "ui/gfx/geometry/rect.h" | |
20 #include "ui/gfx/image/image.h" | |
21 #include "ui/gfx/scoped_canvas.h" | |
22 #include "ui/gfx/skia_util.h" | |
23 #include "ui/views/view.h" | |
24 #include "ui/views/widget/native_widget_aura.h" | |
25 #include "ui/views/widget/widget.h" | |
26 #include "ui/views/widget/widget_delegate.h" | |
27 | |
28 using views::Widget; | |
29 | |
30 namespace { | |
31 | |
32 // Color for the window title text. | |
33 const SkColor kTitleTextColor = SkColorSetRGB(40, 40, 40); | |
34 // Color of the active window header/content separator line. | |
35 const SkColor kHeaderContentSeparatorColor = SkColorSetRGB(150, 150, 152); | |
36 // Color of the inactive window header/content separator line. | |
37 const SkColor kHeaderContentSeparatorInactiveColor = | |
38 SkColorSetRGB(180, 180, 182); | |
39 // The default color of the frame. | |
40 const SkColor kDefaultFrameColor = SkColorSetRGB(242, 242, 242); | |
41 // Duration of crossfade animation for activating and deactivating frame. | |
42 const int kActivationCrossfadeDurationMs = 200; | |
43 | |
44 // Tiles an image into an area, rounding the top corners. | |
45 void TileRoundRect(gfx::Canvas* canvas, | |
46 const SkPaint& paint, | |
47 const gfx::Rect& bounds, | |
48 int corner_radius) { | |
49 SkRect rect = gfx::RectToSkRect(bounds); | |
50 const SkScalar corner_radius_scalar = SkIntToScalar(corner_radius); | |
51 SkScalar radii[8] = {corner_radius_scalar, | |
52 corner_radius_scalar, // top-left | |
53 corner_radius_scalar, | |
54 corner_radius_scalar, // top-right | |
55 0, | |
56 0, // bottom-right | |
57 0, | |
58 0}; // bottom-left | |
59 SkPath path; | |
60 path.addRoundRect(rect, radii, SkPath::kCW_Direction); | |
61 canvas->DrawPath(path, paint); | |
62 } | |
63 | |
64 // Returns the FontList to use for the title. | |
65 const gfx::FontList& GetTitleFontList() { | |
66 static const gfx::FontList* title_font_list = | |
67 new gfx::FontList(views::NativeWidgetAura::GetWindowTitleFontList()); | |
68 ANNOTATE_LEAKING_OBJECT_PTR(title_font_list); | |
69 return *title_font_list; | |
70 } | |
71 | |
72 } // namespace | |
73 | |
74 namespace mash { | |
75 namespace wm { | |
76 | |
77 /////////////////////////////////////////////////////////////////////////////// | |
78 // DefaultHeaderPainter, public: | |
79 | |
80 DefaultHeaderPainter::DefaultHeaderPainter() | |
81 : frame_(NULL), | |
82 view_(NULL), | |
83 left_header_view_(NULL), | |
84 active_frame_color_(kDefaultFrameColor), | |
85 inactive_frame_color_(kDefaultFrameColor), | |
86 caption_button_container_(NULL), | |
87 painted_height_(0), | |
88 mode_(MODE_INACTIVE), | |
89 initial_paint_(true), | |
90 activation_animation_(new gfx::SlideAnimation(this)) {} | |
91 | |
92 DefaultHeaderPainter::~DefaultHeaderPainter() {} | |
93 | |
94 void DefaultHeaderPainter::Init( | |
95 views::Widget* frame, | |
96 views::View* header_view, | |
97 FrameCaptionButtonContainerView* caption_button_container) { | |
98 DCHECK(frame); | |
99 DCHECK(header_view); | |
100 DCHECK(caption_button_container); | |
101 frame_ = frame; | |
102 view_ = header_view; | |
103 caption_button_container_ = caption_button_container; | |
104 UpdateAllButtonImages(); | |
105 } | |
106 | |
107 int DefaultHeaderPainter::GetMinimumHeaderWidth() const { | |
108 // Ensure we have enough space for the window icon and buttons. We allow | |
109 // the title string to collapse to zero width. | |
110 return GetTitleBounds().x() + | |
111 caption_button_container_->GetMinimumSize().width(); | |
112 } | |
113 | |
114 void DefaultHeaderPainter::PaintHeader(gfx::Canvas* canvas, Mode mode) { | |
115 Mode old_mode = mode_; | |
116 mode_ = mode; | |
117 | |
118 if (mode_ != old_mode) { | |
119 UpdateAllButtonImages(); | |
120 if (!initial_paint_ && HeaderPainterUtil::CanAnimateActivation(frame_)) { | |
121 activation_animation_->SetSlideDuration(kActivationCrossfadeDurationMs); | |
122 if (mode_ == MODE_ACTIVE) | |
123 activation_animation_->Show(); | |
124 else | |
125 activation_animation_->Hide(); | |
126 } else { | |
127 if (mode_ == MODE_ACTIVE) | |
128 activation_animation_->Reset(1); | |
129 else | |
130 activation_animation_->Reset(0); | |
131 } | |
132 initial_paint_ = false; | |
133 } | |
134 | |
135 int corner_radius = (frame_->IsMaximized() || frame_->IsFullscreen()) | |
136 ? 0 | |
137 : HeaderPainterUtil::GetTopCornerRadiusWhenRestored(); | |
138 | |
139 SkPaint paint; | |
140 int active_alpha = activation_animation_->CurrentValueBetween(0, 255); | |
141 paint.setColor(color_utils::AlphaBlend(active_frame_color_, | |
142 inactive_frame_color_, active_alpha)); | |
143 | |
144 TileRoundRect(canvas, paint, GetLocalBounds(), corner_radius); | |
145 | |
146 if (!frame_->IsMaximized() && !frame_->IsFullscreen() && | |
147 mode_ == MODE_INACTIVE && !UsesCustomFrameColors()) { | |
148 PaintHighlightForInactiveRestoredWindow(canvas); | |
149 } | |
150 if (frame_->widget_delegate() && | |
151 frame_->widget_delegate()->ShouldShowWindowTitle()) { | |
152 PaintTitleBar(canvas); | |
153 } | |
154 if (!UsesCustomFrameColors()) | |
155 PaintHeaderContentSeparator(canvas); | |
156 } | |
157 | |
158 void DefaultHeaderPainter::LayoutHeader() { | |
159 UpdateSizeButtonImages(ShouldUseLightImages()); | |
160 caption_button_container_->Layout(); | |
161 | |
162 gfx::Size caption_button_container_size = | |
163 caption_button_container_->GetPreferredSize(); | |
164 caption_button_container_->SetBounds( | |
165 view_->width() - caption_button_container_size.width(), 0, | |
166 caption_button_container_size.width(), | |
167 caption_button_container_size.height()); | |
168 | |
169 if (left_header_view_) { | |
170 // Vertically center the left header view with respect to the caption button | |
171 // container. | |
172 // Floor when computing the center of |caption_button_container_|. | |
173 gfx::Size size = left_header_view_->GetPreferredSize(); | |
174 int icon_offset_y = | |
175 caption_button_container_->height() / 2 - size.height() / 2; | |
176 left_header_view_->SetBounds(HeaderPainterUtil::GetLeftViewXInset(), | |
177 icon_offset_y, size.width(), size.height()); | |
178 } | |
179 | |
180 // The header/content separator line overlays the caption buttons. | |
181 SetHeaderHeightForPainting(caption_button_container_->height()); | |
182 } | |
183 | |
184 int DefaultHeaderPainter::GetHeaderHeight() const { | |
185 return caption_button_container_->height(); | |
186 } | |
187 | |
188 int DefaultHeaderPainter::GetHeaderHeightForPainting() const { | |
189 return painted_height_; | |
190 } | |
191 | |
192 void DefaultHeaderPainter::SetHeaderHeightForPainting(int height) { | |
193 painted_height_ = height; | |
194 } | |
195 | |
196 void DefaultHeaderPainter::SchedulePaintForTitle() { | |
197 view_->SchedulePaintInRect(GetTitleBounds()); | |
198 } | |
199 | |
200 void DefaultHeaderPainter::SetFrameColors(SkColor active_frame_color, | |
201 SkColor inactive_frame_color) { | |
202 active_frame_color_ = active_frame_color; | |
203 inactive_frame_color_ = inactive_frame_color; | |
204 UpdateAllButtonImages(); | |
205 } | |
206 | |
207 void DefaultHeaderPainter::UpdateLeftHeaderView(views::View* left_header_view) { | |
208 left_header_view_ = left_header_view; | |
209 } | |
210 | |
211 /////////////////////////////////////////////////////////////////////////////// | |
212 // gfx::AnimationDelegate overrides: | |
213 | |
214 void DefaultHeaderPainter::AnimationProgressed( | |
215 const gfx::Animation* animation) { | |
216 view_->SchedulePaintInRect(GetLocalBounds()); | |
217 } | |
218 | |
219 /////////////////////////////////////////////////////////////////////////////// | |
220 // DefaultHeaderPainter, private: | |
221 | |
222 void DefaultHeaderPainter::PaintHighlightForInactiveRestoredWindow( | |
223 gfx::Canvas* canvas) { | |
224 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
225 gfx::ImageSkia top_edge = | |
226 *rb.GetImageSkiaNamed(IDR_MASH_WM_WINDOW_HEADER_SHADE_INACTIVE_TOP); | |
227 gfx::ImageSkia left_edge = | |
228 *rb.GetImageSkiaNamed(IDR_MASH_WM_WINDOW_HEADER_SHADE_INACTIVE_LEFT); | |
229 gfx::ImageSkia right_edge = | |
230 *rb.GetImageSkiaNamed(IDR_MASH_WM_WINDOW_HEADER_SHADE_INACTIVE_RIGHT); | |
231 gfx::ImageSkia bottom_edge = | |
232 *rb.GetImageSkiaNamed(IDR_MASH_WM_WINDOW_HEADER_SHADE_INACTIVE_BOTTOM); | |
233 | |
234 int left_edge_width = left_edge.width(); | |
235 int right_edge_width = right_edge.width(); | |
236 canvas->DrawImageInt(left_edge, 0, 0); | |
237 canvas->DrawImageInt(right_edge, view_->width() - right_edge_width, 0); | |
238 canvas->TileImageInt(top_edge, left_edge_width, 0, | |
239 view_->width() - left_edge_width - right_edge_width, | |
240 top_edge.height()); | |
241 | |
242 DCHECK_EQ(left_edge.height(), right_edge.height()); | |
243 int bottom = left_edge.height(); | |
244 int bottom_height = bottom_edge.height(); | |
245 canvas->TileImageInt(bottom_edge, left_edge_width, bottom - bottom_height, | |
246 view_->width() - left_edge_width - right_edge_width, | |
247 bottom_height); | |
248 } | |
249 | |
250 void DefaultHeaderPainter::PaintTitleBar(gfx::Canvas* canvas) { | |
251 // The window icon is painted by its own views::View. | |
252 gfx::Rect title_bounds = GetTitleBounds(); | |
253 title_bounds.set_x(view_->GetMirroredXForRect(title_bounds)); | |
254 canvas->DrawStringRectWithFlags( | |
255 frame_->widget_delegate()->GetWindowTitle(), GetTitleFontList(), | |
256 kTitleTextColor, title_bounds, gfx::Canvas::NO_SUBPIXEL_RENDERING); | |
257 } | |
258 | |
259 void DefaultHeaderPainter::PaintHeaderContentSeparator(gfx::Canvas* canvas) { | |
260 gfx::ScopedCanvas scoped_canvas(canvas); | |
261 const float scale = canvas->UndoDeviceScaleFactor(); | |
262 gfx::RectF rect(0, painted_height_ * scale - 1, view_->width() * scale, 1); | |
263 SkPaint paint; | |
264 paint.setColor((mode_ == MODE_ACTIVE) ? kHeaderContentSeparatorColor | |
265 : kHeaderContentSeparatorInactiveColor); | |
266 canvas->sk_canvas()->drawRect(gfx::RectFToSkRect(rect), paint); | |
267 } | |
268 | |
269 bool DefaultHeaderPainter::ShouldUseLightImages() { | |
270 return color_utils::IsDark( | |
271 mode_ == MODE_INACTIVE ? inactive_frame_color_ : active_frame_color_); | |
272 } | |
273 | |
274 void DefaultHeaderPainter::UpdateAllButtonImages() { | |
275 bool use_light_images = ShouldUseLightImages(); | |
276 caption_button_container_->SetButtonImages( | |
277 CAPTION_BUTTON_ICON_MINIMIZE, | |
278 use_light_images ? IDR_MASH_WM_WINDOW_CONTROL_ICON_MINIMIZE_WHITE | |
279 : IDR_MASH_WM_WINDOW_CONTROL_ICON_MINIMIZE, | |
280 IDR_MASH_WM_WINDOW_CONTROL_BACKGROUND_H, | |
281 IDR_MASH_WM_WINDOW_CONTROL_BACKGROUND_P); | |
282 | |
283 UpdateSizeButtonImages(use_light_images); | |
284 | |
285 caption_button_container_->SetButtonImages( | |
286 CAPTION_BUTTON_ICON_CLOSE, | |
287 use_light_images ? IDR_MASH_WM_WINDOW_CONTROL_ICON_CLOSE_WHITE | |
288 : IDR_MASH_WM_WINDOW_CONTROL_ICON_CLOSE, | |
289 IDR_MASH_WM_WINDOW_CONTROL_BACKGROUND_H, | |
290 IDR_MASH_WM_WINDOW_CONTROL_BACKGROUND_P); | |
291 | |
292 caption_button_container_->SetButtonImages( | |
293 CAPTION_BUTTON_ICON_LEFT_SNAPPED, | |
294 use_light_images ? IDR_MASH_WM_WINDOW_CONTROL_ICON_LEFT_SNAPPED_WHITE | |
295 : IDR_MASH_WM_WINDOW_CONTROL_ICON_LEFT_SNAPPED, | |
296 IDR_MASH_WM_WINDOW_CONTROL_BACKGROUND_H, | |
297 IDR_MASH_WM_WINDOW_CONTROL_BACKGROUND_P); | |
298 | |
299 caption_button_container_->SetButtonImages( | |
300 CAPTION_BUTTON_ICON_RIGHT_SNAPPED, | |
301 use_light_images ? IDR_MASH_WM_WINDOW_CONTROL_ICON_RIGHT_SNAPPED_WHITE | |
302 : IDR_MASH_WM_WINDOW_CONTROL_ICON_RIGHT_SNAPPED, | |
303 IDR_MASH_WM_WINDOW_CONTROL_BACKGROUND_H, | |
304 IDR_MASH_WM_WINDOW_CONTROL_BACKGROUND_P); | |
305 } | |
306 | |
307 void DefaultHeaderPainter::UpdateSizeButtonImages(bool use_light_images) { | |
308 int icon_id = 0; | |
309 if (frame_->IsMaximized() || frame_->IsFullscreen()) { | |
310 icon_id = use_light_images ? IDR_MASH_WM_WINDOW_CONTROL_ICON_RESTORE_WHITE | |
311 : IDR_MASH_WM_WINDOW_CONTROL_ICON_RESTORE; | |
312 } else { | |
313 icon_id = use_light_images ? IDR_MASH_WM_WINDOW_CONTROL_ICON_MAXIMIZE_WHITE | |
314 : IDR_MASH_WM_WINDOW_CONTROL_ICON_MAXIMIZE; | |
315 } | |
316 caption_button_container_->SetButtonImages( | |
317 CAPTION_BUTTON_ICON_MAXIMIZE_RESTORE, icon_id, | |
318 IDR_MASH_WM_WINDOW_CONTROL_BACKGROUND_H, | |
319 IDR_MASH_WM_WINDOW_CONTROL_BACKGROUND_P); | |
320 } | |
321 | |
322 gfx::Rect DefaultHeaderPainter::GetLocalBounds() const { | |
323 return gfx::Rect(view_->width(), painted_height_); | |
324 } | |
325 | |
326 gfx::Rect DefaultHeaderPainter::GetTitleBounds() const { | |
327 return HeaderPainterUtil::GetTitleBounds( | |
328 left_header_view_, caption_button_container_, GetTitleFontList()); | |
329 } | |
330 | |
331 bool DefaultHeaderPainter::UsesCustomFrameColors() const { | |
332 return active_frame_color_ != kDefaultFrameColor || | |
333 inactive_frame_color_ != kDefaultFrameColor; | |
334 } | |
335 | |
336 } // namespace wm | |
337 } // namespace mash | |
OLD | NEW |