Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: ash/mus/frame/non_client_frame_view_mash.cc

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

Powered by Google App Engine
This is Rietveld 408576698