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/non_client_frame_view_mash.h" | |
6 | |
7 #include <algorithm> | |
8 #include <memory> | |
9 #include <vector> | |
10 | |
11 #include "base/macros.h" | |
12 #include "components/mus/public/cpp/window.h" | |
13 #include "components/mus/public/cpp/window_tree_client.h" | |
14 #include "grit/mash_wm_resources.h" | |
15 #include "mash/wm/frame/caption_buttons/frame_caption_button_container_view.h" | |
16 #include "mash/wm/frame/default_header_painter.h" | |
17 #include "mash/wm/frame/frame_border_hit_test_controller.h" | |
18 #include "mash/wm/frame/header_painter.h" | |
19 #include "ui/base/resource/resource_bundle.h" | |
20 #include "ui/compositor/paint_recorder.h" | |
21 #include "ui/events/mojo/input_events_type_converters.h" | |
22 #include "ui/gfx/canvas.h" | |
23 #include "ui/gfx/geometry/rect.h" | |
24 #include "ui/gfx/geometry/rect_conversions.h" | |
25 #include "ui/gfx/geometry/size.h" | |
26 #include "ui/gfx/image/image.h" | |
27 #include "ui/views/view.h" | |
28 #include "ui/views/widget/widget.h" | |
29 #include "ui/views/widget/widget_delegate.h" | |
30 | |
31 namespace mash { | |
32 namespace wm { | |
33 | |
34 /////////////////////////////////////////////////////////////////////////////// | |
35 // NonClientFrameViewMash::HeaderView | |
36 | |
37 // View which paints the header. | |
38 class NonClientFrameViewMash::HeaderView : public views::View { | |
39 public: | |
40 // |frame| is the widget that the caption buttons act on. | |
41 HeaderView(views::Widget* frame, mus::Window* window); | |
42 ~HeaderView() override; | |
43 | |
44 // Schedules a repaint for the entire title. | |
45 void SchedulePaintForTitle(); | |
46 | |
47 // Tells the window controls to reset themselves to the normal state. | |
48 void ResetWindowControls(); | |
49 | |
50 // Returns the view's preferred height. | |
51 int GetPreferredHeight() const; | |
52 | |
53 // Returns the view's minimum width. | |
54 int GetMinimumWidth() const; | |
55 | |
56 void SizeConstraintsChanged(); | |
57 | |
58 void SetFrameColors(SkColor active_frame_color, SkColor inactive_frame_color); | |
59 | |
60 // views::View: | |
61 void Layout() override; | |
62 void OnPaint(gfx::Canvas* canvas) override; | |
63 void ChildPreferredSizeChanged(views::View* child) override; | |
64 | |
65 FrameCaptionButtonContainerView* caption_button_container() { | |
66 return caption_button_container_; | |
67 } | |
68 | |
69 private: | |
70 // The widget that the caption buttons act on. | |
71 views::Widget* frame_; | |
72 | |
73 // Helper for painting the header. | |
74 std::unique_ptr<DefaultHeaderPainter> header_painter_; | |
75 | |
76 // View which contains the window caption buttons. | |
77 FrameCaptionButtonContainerView* caption_button_container_; | |
78 | |
79 mus::Window* window_; | |
80 | |
81 DISALLOW_COPY_AND_ASSIGN(HeaderView); | |
82 }; | |
83 | |
84 NonClientFrameViewMash::HeaderView::HeaderView(views::Widget* frame, | |
85 mus::Window* window) | |
86 : frame_(frame), | |
87 header_painter_(new DefaultHeaderPainter), | |
88 caption_button_container_(nullptr), | |
89 window_(window) { | |
90 caption_button_container_ = new FrameCaptionButtonContainerView(frame_); | |
91 caption_button_container_->UpdateSizeButtonVisibility(); | |
92 AddChildView(caption_button_container_); | |
93 | |
94 header_painter_->Init(frame_, this, caption_button_container_); | |
95 } | |
96 | |
97 NonClientFrameViewMash::HeaderView::~HeaderView() {} | |
98 | |
99 void NonClientFrameViewMash::HeaderView::SchedulePaintForTitle() { | |
100 header_painter_->SchedulePaintForTitle(); | |
101 } | |
102 | |
103 void NonClientFrameViewMash::HeaderView::ResetWindowControls() { | |
104 caption_button_container_->ResetWindowControls(); | |
105 } | |
106 | |
107 int NonClientFrameViewMash::HeaderView::GetPreferredHeight() const { | |
108 return header_painter_->GetHeaderHeightForPainting(); | |
109 } | |
110 | |
111 int NonClientFrameViewMash::HeaderView::GetMinimumWidth() const { | |
112 return header_painter_->GetMinimumHeaderWidth(); | |
113 } | |
114 | |
115 void NonClientFrameViewMash::HeaderView::SizeConstraintsChanged() { | |
116 caption_button_container_->ResetWindowControls(); | |
117 caption_button_container_->UpdateSizeButtonVisibility(); | |
118 Layout(); | |
119 } | |
120 | |
121 void NonClientFrameViewMash::HeaderView::SetFrameColors( | |
122 SkColor active_frame_color, | |
123 SkColor inactive_frame_color) { | |
124 header_painter_->SetFrameColors(active_frame_color, inactive_frame_color); | |
125 } | |
126 | |
127 /////////////////////////////////////////////////////////////////////////////// | |
128 // NonClientFrameViewMash::HeaderView, views::View overrides: | |
129 | |
130 void NonClientFrameViewMash::HeaderView::Layout() { | |
131 header_painter_->LayoutHeader(); | |
132 } | |
133 | |
134 void NonClientFrameViewMash::HeaderView::OnPaint(gfx::Canvas* canvas) { | |
135 const mus::Window* focused_window = | |
136 window_->window_tree()->GetFocusedWindow(); | |
137 const bool paint_as_active = | |
138 focused_window && window_->Contains(focused_window); | |
139 caption_button_container_->SetPaintAsActive(paint_as_active); | |
140 | |
141 HeaderPainter::Mode header_mode = paint_as_active | |
142 ? HeaderPainter::MODE_ACTIVE | |
143 : HeaderPainter::MODE_INACTIVE; | |
144 header_painter_->PaintHeader(canvas, header_mode); | |
145 } | |
146 | |
147 void NonClientFrameViewMash::HeaderView::ChildPreferredSizeChanged( | |
148 views::View* child) { | |
149 // FrameCaptionButtonContainerView animates the visibility changes in | |
150 // UpdateSizeButtonVisibility(false). Due to this a new size is not available | |
151 // until the completion of the animation. Layout in response to the preferred | |
152 // size changes. | |
153 if (child != caption_button_container_) | |
154 return; | |
155 parent()->Layout(); | |
156 } | |
157 | |
158 //////////////////////////////////////////////////////////////////////////////// | |
159 // NonClientFrameViewMash, public: | |
160 | |
161 // static | |
162 const char NonClientFrameViewMash::kViewClassName[] = "NonClientFrameViewMash"; | |
163 | |
164 NonClientFrameViewMash::NonClientFrameViewMash(views::Widget* frame, | |
165 mus::Window* window) | |
166 : frame_(frame), | |
167 window_(window), | |
168 header_view_(new HeaderView(frame, window)) { | |
169 // |header_view_| is set as the non client view's overlay view so that it can | |
170 // overlay the web contents in immersive fullscreen. | |
171 AddChildView(header_view_); | |
172 window_->AddObserver(this); | |
173 window_->window_tree()->AddObserver(this); | |
174 } | |
175 | |
176 NonClientFrameViewMash::~NonClientFrameViewMash() { | |
177 RemoveObservers(); | |
178 } | |
179 | |
180 // static | |
181 gfx::Insets NonClientFrameViewMash::GetPreferredClientAreaInsets() { | |
182 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
183 const int header_height = | |
184 rb.GetImageSkiaNamed(IDR_MASH_WM_WINDOW_CONTROL_BACKGROUND_P) | |
185 ->size() | |
186 .height(); | |
187 return gfx::Insets(header_height, 0, 0, 0); | |
188 } | |
189 | |
190 // static | |
191 int NonClientFrameViewMash::GetMaxTitleBarButtonWidth() { | |
192 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
193 return rb.GetImageSkiaNamed(IDR_MASH_WM_WINDOW_CONTROL_BACKGROUND_P) | |
194 ->size() | |
195 .width() * | |
196 3; | |
197 } | |
198 | |
199 void NonClientFrameViewMash::SetFrameColors(SkColor active_frame_color, | |
200 SkColor inactive_frame_color) { | |
201 header_view_->SetFrameColors(active_frame_color, inactive_frame_color); | |
202 } | |
203 | |
204 //////////////////////////////////////////////////////////////////////////////// | |
205 // NonClientFrameViewMash, views::NonClientFrameView overrides: | |
206 | |
207 gfx::Rect NonClientFrameViewMash::GetBoundsForClientView() const { | |
208 gfx::Rect result(GetLocalBounds()); | |
209 result.Inset(window_->client_area()); | |
210 return result; | |
211 } | |
212 | |
213 gfx::Rect NonClientFrameViewMash::GetWindowBoundsForClientBounds( | |
214 const gfx::Rect& client_bounds) const { | |
215 gfx::Rect window_bounds = client_bounds; | |
216 window_bounds.Inset( | |
217 window_->client_area().left(), window_->client_area().top(), | |
218 window_->client_area().right(), window_->client_area().bottom()); | |
219 return window_bounds; | |
220 } | |
221 | |
222 int NonClientFrameViewMash::NonClientHitTest(const gfx::Point& point) { | |
223 return FrameBorderHitTestController::NonClientHitTest( | |
224 this, header_view_->caption_button_container(), point); | |
225 } | |
226 | |
227 void NonClientFrameViewMash::GetWindowMask(const gfx::Size& size, | |
228 gfx::Path* window_mask) {} | |
229 | |
230 void NonClientFrameViewMash::ResetWindowControls() { | |
231 header_view_->ResetWindowControls(); | |
232 } | |
233 | |
234 void NonClientFrameViewMash::UpdateWindowIcon() {} | |
235 | |
236 void NonClientFrameViewMash::UpdateWindowTitle() { | |
237 header_view_->SchedulePaintForTitle(); | |
238 } | |
239 | |
240 void NonClientFrameViewMash::SizeConstraintsChanged() { | |
241 header_view_->SizeConstraintsChanged(); | |
242 } | |
243 | |
244 //////////////////////////////////////////////////////////////////////////////// | |
245 // NonClientFrameViewMash, views::View overrides: | |
246 | |
247 void NonClientFrameViewMash::Layout() { | |
248 header_view_->SetBounds(0, 0, width(), header_view_->GetPreferredHeight()); | |
249 header_view_->Layout(); | |
250 } | |
251 | |
252 gfx::Size NonClientFrameViewMash::GetPreferredSize() const { | |
253 gfx::Size pref = frame_->client_view()->GetPreferredSize(); | |
254 return frame_->non_client_view() | |
255 ->GetWindowBoundsForClientBounds(gfx::Rect(pref)) | |
256 .size(); | |
257 } | |
258 | |
259 const char* NonClientFrameViewMash::GetClassName() const { | |
260 return kViewClassName; | |
261 } | |
262 | |
263 gfx::Size NonClientFrameViewMash::GetMinimumSize() const { | |
264 // If the client area is empty we assume the client is rendering everything | |
265 // and the window can be resized to anything. | |
266 // TODO(sky): we need a minimum-size property. | |
267 if (window_->client_area().IsEmpty()) | |
268 return gfx::Size(); | |
269 | |
270 gfx::Size min_client_view_size(frame_->client_view()->GetMinimumSize()); | |
271 return gfx::Size( | |
272 std::max(header_view_->GetMinimumWidth(), min_client_view_size.width()), | |
273 NonClientTopBorderHeight() + min_client_view_size.height()); | |
274 } | |
275 | |
276 gfx::Size NonClientFrameViewMash::GetMaximumSize() const { | |
277 gfx::Size max_client_size(frame_->client_view()->GetMaximumSize()); | |
278 int width = 0; | |
279 int height = 0; | |
280 | |
281 if (max_client_size.width() > 0) | |
282 width = std::max(header_view_->GetMinimumWidth(), max_client_size.width()); | |
283 if (max_client_size.height() > 0) | |
284 height = NonClientTopBorderHeight() + max_client_size.height(); | |
285 | |
286 return gfx::Size(width, height); | |
287 } | |
288 | |
289 void NonClientFrameViewMash::OnPaint(gfx::Canvas* canvas) { | |
290 canvas->Save(); | |
291 NonClientFrameView::OnPaint(canvas); | |
292 canvas->Restore(); | |
293 | |
294 // The client app draws the client area. Make ours totally transparent so | |
295 // we only see the client apps client area. | |
296 canvas->FillRect(GetBoundsForClientView(), SK_ColorBLACK, | |
297 SkXfermode::kSrc_Mode); | |
298 } | |
299 | |
300 void NonClientFrameViewMash::PaintChildren(const ui::PaintContext& context) { | |
301 NonClientFrameView::PaintChildren(context); | |
302 | |
303 // The client app draws the client area. Make ours totally transparent so | |
304 // we only see the client apps client area. | |
305 ui::PaintRecorder recorder(context, size(), &paint_cache_); | |
306 recorder.canvas()->FillRect(GetBoundsForClientView(), SK_ColorBLACK, | |
307 SkXfermode::kSrc_Mode); | |
308 } | |
309 | |
310 void NonClientFrameViewMash::OnWindowClientAreaChanged( | |
311 mus::Window* window, | |
312 const gfx::Insets& old_client_area, | |
313 const std::vector<gfx::Rect>& old_additional_client_area) { | |
314 // Only the insets effect the rendering. | |
315 if (old_client_area == window->client_area()) | |
316 return; | |
317 | |
318 Layout(); | |
319 // NonClientView (our parent) positions the client view based on bounds from | |
320 // us. We need to layout from parent to trigger a layout of the client view. | |
321 if (parent()) | |
322 parent()->Layout(); | |
323 SchedulePaint(); | |
324 } | |
325 | |
326 void NonClientFrameViewMash::OnWindowDestroyed(mus::Window* window) { | |
327 RemoveObservers(); | |
328 } | |
329 | |
330 void NonClientFrameViewMash::OnWindowSharedPropertyChanged( | |
331 mus::Window* window, | |
332 const std::string& name, | |
333 const std::vector<uint8_t>* old_data, | |
334 const std::vector<uint8_t>* new_data) { | |
335 if (name == mus::mojom::WindowManager::kResizeBehavior_Property) | |
336 header_view_->SizeConstraintsChanged(); | |
337 else if (name == mus::mojom::WindowManager::kWindowTitle_Property) | |
338 header_view_->SchedulePaintForTitle(); | |
339 } | |
340 | |
341 views::View* NonClientFrameViewMash::GetHeaderView() { | |
342 return header_view_; | |
343 } | |
344 | |
345 //////////////////////////////////////////////////////////////////////////////// | |
346 // NonClientFrameViewMash, private: | |
347 | |
348 int NonClientFrameViewMash::NonClientTopBorderHeight() const { | |
349 return header_view_->GetPreferredHeight(); | |
350 } | |
351 | |
352 void NonClientFrameViewMash::RemoveObservers() { | |
353 if (!window_) | |
354 return; | |
355 | |
356 window_->RemoveObserver(this); | |
357 window_->window_tree()->RemoveObserver(this); | |
358 window_ = nullptr; | |
359 } | |
360 | |
361 void NonClientFrameViewMash::OnWindowTreeFocusChanged(mus::Window* gained_focus, | |
362 mus::Window* lost_focus) { | |
363 const bool had_focus = lost_focus && window_->Contains(lost_focus); | |
364 const bool has_focus = gained_focus && window_->Contains(gained_focus); | |
365 if (had_focus != has_focus) | |
366 SchedulePaint(); | |
367 } | |
368 | |
369 } // namespace wm | |
370 } // namespace mash | |
OLD | NEW |