Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / views / window / non_client_view.cc
blob7670a2eb5a2421de7086d78f1bbe50800f15854e
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
3 // found in the LICENSE file.
5 #include "ui/views/window/non_client_view.h"
7 #include "ui/accessibility/ax_view_state.h"
8 #include "ui/base/hit_test.h"
9 #include "ui/gfx/geometry/rect_conversions.h"
10 #include "ui/views/rect_based_targeting_utils.h"
11 #include "ui/views/view_targeter.h"
12 #include "ui/views/widget/root_view.h"
13 #include "ui/views/widget/widget.h"
14 #include "ui/views/window/client_view.h"
16 namespace views {
18 // static
19 const char NonClientFrameView::kViewClassName[] =
20 "ui/views/window/NonClientFrameView";
22 const char NonClientView::kViewClassName[] =
23 "ui/views/window/NonClientView";
25 // The frame view and the client view are always at these specific indices,
26 // because the RootView message dispatch sends messages to items higher in the
27 // z-order first and we always want the client view to have first crack at
28 // handling mouse messages.
29 static const int kFrameViewIndex = 0;
30 static const int kClientViewIndex = 1;
31 // The overlay view is always on top (index == child_count() - 1).
33 ////////////////////////////////////////////////////////////////////////////////
34 // NonClientView, public:
36 NonClientView::NonClientView()
37 : client_view_(NULL),
38 overlay_view_(NULL) {
39 SetEventTargeter(
40 scoped_ptr<views::ViewTargeter>(new views::ViewTargeter(this)));
43 NonClientView::~NonClientView() {
44 // This value may have been reset before the window hierarchy shuts down,
45 // so we need to manually remove it.
46 RemoveChildView(frame_view_.get());
49 void NonClientView::SetFrameView(NonClientFrameView* frame_view) {
50 // See comment in header about ownership.
51 frame_view->set_owned_by_client();
52 if (frame_view_.get())
53 RemoveChildView(frame_view_.get());
54 frame_view_.reset(frame_view);
55 if (parent())
56 AddChildViewAt(frame_view_.get(), kFrameViewIndex);
59 void NonClientView::SetOverlayView(View* view) {
60 if (overlay_view_)
61 RemoveChildView(overlay_view_);
63 if (!view)
64 return;
66 overlay_view_ = view;
67 if (parent())
68 AddChildView(overlay_view_);
71 bool NonClientView::CanClose() {
72 return client_view_->CanClose();
75 void NonClientView::WindowClosing() {
76 client_view_->WidgetClosing();
79 void NonClientView::UpdateFrame() {
80 Widget* widget = GetWidget();
81 SetFrameView(widget->CreateNonClientFrameView());
82 widget->ThemeChanged();
83 Layout();
84 SchedulePaint();
87 void NonClientView::SetInactiveRenderingDisabled(bool disable) {
88 frame_view_->SetInactiveRenderingDisabled(disable);
91 gfx::Rect NonClientView::GetWindowBoundsForClientBounds(
92 const gfx::Rect client_bounds) const {
93 return frame_view_->GetWindowBoundsForClientBounds(client_bounds);
96 int NonClientView::NonClientHitTest(const gfx::Point& point) {
97 // The NonClientFrameView is responsible for also asking the ClientView.
98 return frame_view_->NonClientHitTest(point);
101 void NonClientView::GetWindowMask(const gfx::Size& size,
102 gfx::Path* window_mask) {
103 frame_view_->GetWindowMask(size, window_mask);
106 void NonClientView::ResetWindowControls() {
107 frame_view_->ResetWindowControls();
110 void NonClientView::UpdateWindowIcon() {
111 frame_view_->UpdateWindowIcon();
114 void NonClientView::UpdateWindowTitle() {
115 frame_view_->UpdateWindowTitle();
118 void NonClientView::SizeConstraintsChanged() {
119 frame_view_->SizeConstraintsChanged();
122 void NonClientView::LayoutFrameView() {
123 // First layout the NonClientFrameView, which determines the size of the
124 // ClientView...
125 frame_view_->SetBounds(0, 0, width(), height());
127 // We need to manually call Layout here because layout for the frame view can
128 // change independently of the bounds changing - e.g. after the initial
129 // display of the window the metrics of the native window controls can change,
130 // which does not change the bounds of the window but requires a re-layout to
131 // trigger a repaint. We override OnBoundsChanged() for the NonClientFrameView
132 // to do nothing so that SetBounds above doesn't cause Layout to be called
133 // twice.
134 frame_view_->Layout();
137 void NonClientView::SetAccessibleName(const base::string16& name) {
138 accessible_name_ = name;
141 ////////////////////////////////////////////////////////////////////////////////
142 // NonClientView, View overrides:
144 gfx::Size NonClientView::GetPreferredSize() const {
145 // TODO(pkasting): This should probably be made to look similar to
146 // GetMinimumSize() below. This will require implementing GetPreferredSize()
147 // better in the various frame views.
148 gfx::Rect client_bounds(gfx::Point(), client_view_->GetPreferredSize());
149 return GetWindowBoundsForClientBounds(client_bounds).size();
152 gfx::Size NonClientView::GetMinimumSize() const {
153 return frame_view_->GetMinimumSize();
156 gfx::Size NonClientView::GetMaximumSize() const {
157 return frame_view_->GetMaximumSize();
160 void NonClientView::Layout() {
161 LayoutFrameView();
163 // Then layout the ClientView, using those bounds.
164 client_view_->SetBoundsRect(frame_view_->GetBoundsForClientView());
166 // We need to manually call Layout on the ClientView as well for the same
167 // reason as above.
168 client_view_->Layout();
170 if (overlay_view_ && overlay_view_->visible())
171 overlay_view_->SetBoundsRect(GetLocalBounds());
174 void NonClientView::ViewHierarchyChanged(
175 const ViewHierarchyChangedDetails& details) {
176 // Add our two child views here as we are added to the Widget so that if we
177 // are subsequently resized all the parent-child relationships are
178 // established.
179 if (details.is_add && GetWidget() && details.child == this) {
180 AddChildViewAt(frame_view_.get(), kFrameViewIndex);
181 AddChildViewAt(client_view_, kClientViewIndex);
182 if (overlay_view_)
183 AddChildView(overlay_view_);
187 void NonClientView::GetAccessibleState(ui::AXViewState* state) {
188 state->role = ui::AX_ROLE_CLIENT;
189 state->name = accessible_name_;
192 const char* NonClientView::GetClassName() const {
193 return kViewClassName;
196 View* NonClientView::GetTooltipHandlerForPoint(const gfx::Point& point) {
197 // The same logic as for |TargetForRect()| applies here.
198 if (frame_view_->parent() == this) {
199 // During the reset of the frame_view_ it's possible to be in this code
200 // after it's been removed from the view hierarchy but before it's been
201 // removed from the NonClientView.
202 gfx::Point point_in_child_coords(point);
203 View::ConvertPointToTarget(this, frame_view_.get(), &point_in_child_coords);
204 View* handler =
205 frame_view_->GetTooltipHandlerForPoint(point_in_child_coords);
206 if (handler)
207 return handler;
210 return View::GetTooltipHandlerForPoint(point);
213 View* NonClientView::TargetForRect(View* root, const gfx::Rect& rect) {
214 CHECK_EQ(root, this);
216 if (!UsePointBasedTargeting(rect))
217 return ViewTargeterDelegate::TargetForRect(root, rect);
219 // Because of the z-ordering of our child views (the client view is positioned
220 // over the non-client frame view, if the client view ever overlaps the frame
221 // view visually (as it does for the browser window), then it will eat
222 // events for the window controls. We override this method here so that we can
223 // detect this condition and re-route the events to the non-client frame view.
224 // The assumption is that the frame view's implementation of HitTest will only
225 // return true for area not occupied by the client view.
226 if (frame_view_->parent() == this) {
227 // During the reset of the frame_view_ it's possible to be in this code
228 // after it's been removed from the view hierarchy but before it's been
229 // removed from the NonClientView.
230 gfx::RectF rect_in_child_coords_f(rect);
231 View::ConvertRectToTarget(this, frame_view_.get(), &rect_in_child_coords_f);
232 gfx::Rect rect_in_child_coords = gfx::ToEnclosingRect(
233 rect_in_child_coords_f);
234 if (frame_view_->HitTestRect(rect_in_child_coords))
235 return frame_view_->GetEventHandlerForRect(rect_in_child_coords);
238 return ViewTargeterDelegate::TargetForRect(root, rect);
241 ////////////////////////////////////////////////////////////////////////////////
242 // NonClientFrameView, public:
244 NonClientFrameView::~NonClientFrameView() {
247 void NonClientFrameView::SetInactiveRenderingDisabled(bool disable) {
248 if (inactive_rendering_disabled_ == disable)
249 return;
251 bool should_paint_as_active_old = ShouldPaintAsActive();
252 inactive_rendering_disabled_ = disable;
254 // The widget schedules a paint when the activation changes.
255 if (should_paint_as_active_old != ShouldPaintAsActive())
256 SchedulePaint();
259 bool NonClientFrameView::ShouldPaintAsActive() const {
260 return inactive_rendering_disabled_ || GetWidget()->IsActive();
263 int NonClientFrameView::GetHTComponentForFrame(const gfx::Point& point,
264 int top_resize_border_height,
265 int resize_border_thickness,
266 int top_resize_corner_height,
267 int resize_corner_width,
268 bool can_resize) {
269 // Tricky: In XP, native behavior is to return HTTOPLEFT and HTTOPRIGHT for
270 // a |resize_corner_size|-length strip of both the side and top borders, but
271 // only to return HTBOTTOMLEFT/HTBOTTOMRIGHT along the bottom border + corner
272 // (not the side border). Vista goes further and doesn't return these on any
273 // of the side borders. We allow callers to match either behavior.
274 int component;
275 if (point.x() < resize_border_thickness) {
276 if (point.y() < top_resize_corner_height)
277 component = HTTOPLEFT;
278 else if (point.y() >= (height() - resize_border_thickness))
279 component = HTBOTTOMLEFT;
280 else
281 component = HTLEFT;
282 } else if (point.x() >= (width() - resize_border_thickness)) {
283 if (point.y() < top_resize_corner_height)
284 component = HTTOPRIGHT;
285 else if (point.y() >= (height() - resize_border_thickness))
286 component = HTBOTTOMRIGHT;
287 else
288 component = HTRIGHT;
289 } else if (point.y() < top_resize_border_height) {
290 if (point.x() < resize_corner_width)
291 component = HTTOPLEFT;
292 else if (point.x() >= (width() - resize_corner_width))
293 component = HTTOPRIGHT;
294 else
295 component = HTTOP;
296 } else if (point.y() >= (height() - resize_border_thickness)) {
297 if (point.x() < resize_corner_width)
298 component = HTBOTTOMLEFT;
299 else if (point.x() >= (width() - resize_corner_width))
300 component = HTBOTTOMRIGHT;
301 else
302 component = HTBOTTOM;
303 } else {
304 return HTNOWHERE;
307 // If the window can't be resized, there are no resize boundaries, just
308 // window borders.
309 return can_resize ? component : HTBORDER;
312 void NonClientFrameView::GetAccessibleState(ui::AXViewState* state) {
313 state->role = ui::AX_ROLE_CLIENT;
316 const char* NonClientFrameView::GetClassName() const {
317 return kViewClassName;
320 ////////////////////////////////////////////////////////////////////////////////
321 // NonClientFrameView, protected:
323 NonClientFrameView::NonClientFrameView() : inactive_rendering_disabled_(false) {
324 SetEventTargeter(
325 scoped_ptr<views::ViewTargeter>(new views::ViewTargeter(this)));
328 // ViewTargeterDelegate:
329 bool NonClientFrameView::DoesIntersectRect(const View* target,
330 const gfx::Rect& rect) const {
331 CHECK_EQ(target, this);
333 // For the default case, we assume the non-client frame view never overlaps
334 // the client view.
335 return !GetWidget()->client_view()->bounds().Intersects(rect);
338 void NonClientFrameView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
339 // Overridden to do nothing. The NonClientView manually calls Layout on the
340 // FrameView when it is itself laid out, see comment in NonClientView::Layout.
343 } // namespace views