OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/ui/views/extensions/shell_window_frame_view.h" | |
6 | |
7 #include "base/strings/utf_string_conversions.h" | |
8 #include "chrome/browser/ui/views/apps/native_app_window_views.h" | |
9 #include "extensions/common/draggable_region.h" | |
10 #include "grit/theme_resources.h" | |
11 #include "grit/ui_strings.h" // Accessibility names | |
12 #include "third_party/skia/include/core/SkPaint.h" | |
13 #include "ui/base/hit_test.h" | |
14 #include "ui/base/l10n/l10n_util.h" | |
15 #include "ui/base/resource/resource_bundle.h" | |
16 #include "ui/gfx/canvas.h" | |
17 #include "ui/gfx/image/image.h" | |
18 #include "ui/gfx/path.h" | |
19 #include "ui/views/controls/button/image_button.h" | |
20 #include "ui/views/layout/grid_layout.h" | |
21 #include "ui/views/views_delegate.h" | |
22 #include "ui/views/widget/widget.h" | |
23 | |
24 #if defined(OS_WIN) && !defined(USE_AURA) | |
25 #include "chrome/browser/shell_integration.h" | |
26 #include "chrome/browser/web_applications/web_app.h" | |
27 #include "ui/base/win/shell.h" | |
28 #endif | |
29 | |
30 #if defined(USE_ASH) | |
31 #include "ash/ash_constants.h" | |
32 #include "chrome/browser/ui/ash/ash_util.h" | |
33 #include "ui/aura/env.h" | |
34 #endif | |
35 | |
36 #if defined(USE_AURA) | |
37 #include "ui/aura/window.h" | |
38 #endif | |
39 | |
40 namespace { | |
41 const int kResizeInsideBoundsSize = 5; | |
42 const int kResizeAreaCornerSize = 16; | |
43 | |
44 // Height of the chrome-style caption, in pixels. | |
45 const int kCaptionHeight = 25; | |
46 } // namespace | |
47 | |
48 | |
49 const char ShellWindowFrameView::kViewClassName[] = | |
50 "browser/ui/views/extensions/ShellWindowFrameView"; | |
51 | |
52 ShellWindowFrameView::ShellWindowFrameView(NativeAppWindowViews* window) | |
53 : window_(window), | |
54 frame_(NULL), | |
55 close_button_(NULL) { | |
56 } | |
57 | |
58 ShellWindowFrameView::~ShellWindowFrameView() { | |
59 } | |
60 | |
61 void ShellWindowFrameView::Init(views::Widget* frame) { | |
62 frame_ = frame; | |
63 | |
64 if (!window_->frameless()) { | |
65 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
66 close_button_ = new views::ImageButton(this); | |
67 close_button_->SetImage(views::CustomButton::STATE_NORMAL, | |
68 rb.GetNativeImageNamed(IDR_APP_WINDOW_CLOSE).ToImageSkia()); | |
69 close_button_->SetImage(views::CustomButton::STATE_HOVERED, | |
70 rb.GetNativeImageNamed(IDR_APP_WINDOW_CLOSE_H).ToImageSkia()); | |
71 close_button_->SetImage(views::CustomButton::STATE_PRESSED, | |
72 rb.GetNativeImageNamed(IDR_APP_WINDOW_CLOSE_P).ToImageSkia()); | |
73 close_button_->SetAccessibleName( | |
74 l10n_util::GetStringUTF16(IDS_APP_ACCNAME_CLOSE)); | |
75 AddChildView(close_button_); | |
76 maximize_button_ = new views::ImageButton(this); | |
77 maximize_button_->SetImage(views::CustomButton::STATE_NORMAL, | |
78 rb.GetNativeImageNamed(IDR_APP_WINDOW_MAXIMIZE).ToImageSkia()); | |
79 maximize_button_->SetImage(views::CustomButton::STATE_HOVERED, | |
80 rb.GetNativeImageNamed(IDR_APP_WINDOW_MAXIMIZE_H).ToImageSkia()); | |
81 maximize_button_->SetImage(views::CustomButton::STATE_PRESSED, | |
82 rb.GetNativeImageNamed(IDR_APP_WINDOW_MAXIMIZE_P).ToImageSkia()); | |
83 maximize_button_->SetImage(views::CustomButton::STATE_DISABLED, | |
84 rb.GetNativeImageNamed(IDR_APP_WINDOW_MAXIMIZE_D).ToImageSkia()); | |
85 maximize_button_->SetAccessibleName( | |
86 l10n_util::GetStringUTF16(IDS_APP_ACCNAME_MAXIMIZE)); | |
87 AddChildView(maximize_button_); | |
88 restore_button_ = new views::ImageButton(this); | |
89 restore_button_->SetImage(views::CustomButton::STATE_NORMAL, | |
90 rb.GetNativeImageNamed(IDR_APP_WINDOW_RESTORE).ToImageSkia()); | |
91 restore_button_->SetImage(views::CustomButton::STATE_HOVERED, | |
92 rb.GetNativeImageNamed(IDR_APP_WINDOW_RESTORE_H).ToImageSkia()); | |
93 restore_button_->SetImage(views::CustomButton::STATE_PRESSED, | |
94 rb.GetNativeImageNamed(IDR_APP_WINDOW_RESTORE_P).ToImageSkia()); | |
95 restore_button_->SetAccessibleName( | |
96 l10n_util::GetStringUTF16(IDS_APP_ACCNAME_RESTORE)); | |
97 AddChildView(restore_button_); | |
98 minimize_button_ = new views::ImageButton(this); | |
99 minimize_button_->SetImage(views::CustomButton::STATE_NORMAL, | |
100 rb.GetNativeImageNamed(IDR_APP_WINDOW_MINIMIZE).ToImageSkia()); | |
101 minimize_button_->SetImage(views::CustomButton::STATE_HOVERED, | |
102 rb.GetNativeImageNamed(IDR_APP_WINDOW_MINIMIZE_H).ToImageSkia()); | |
103 minimize_button_->SetImage(views::CustomButton::STATE_PRESSED, | |
104 rb.GetNativeImageNamed(IDR_APP_WINDOW_MINIMIZE_P).ToImageSkia()); | |
105 minimize_button_->SetAccessibleName( | |
106 l10n_util::GetStringUTF16(IDS_APP_ACCNAME_MINIMIZE)); | |
107 AddChildView(minimize_button_); | |
108 } | |
109 | |
110 #if defined(USE_AURA) | |
111 int resize_inside_bounds_size = kResizeInsideBoundsSize; | |
112 aura::Window* window = frame->GetNativeWindow(); | |
113 #if defined(USE_ASH) | |
114 if (chrome::IsNativeWindowInAsh(window)) { | |
115 gfx::Insets mouse_insets = gfx::Insets(-ash::kResizeOutsideBoundsSize, | |
116 -ash::kResizeOutsideBoundsSize, | |
117 -ash::kResizeOutsideBoundsSize, | |
118 -ash::kResizeOutsideBoundsSize); | |
119 gfx::Insets touch_insets = mouse_insets.Scale( | |
120 ash::kResizeOutsideBoundsScaleForTouch); | |
121 // Ensure we get resize cursors for a few pixels outside our bounds. | |
122 window->SetHitTestBoundsOverrideOuter(mouse_insets, touch_insets); | |
123 | |
124 // If the window is in ash, the inside area used for resizing will be | |
125 // smaller due to the fact that outside area is also used for resizing. | |
126 resize_inside_bounds_size = ash::kResizeInsideBoundsSize; | |
127 } | |
128 #endif | |
129 // Ensure we get resize cursors just inside our bounds as well. | |
130 // TODO(jeremya): do we need to update these when in fullscreen/maximized? | |
131 window->set_hit_test_bounds_override_inner( | |
132 gfx::Insets(resize_inside_bounds_size, resize_inside_bounds_size, | |
133 resize_inside_bounds_size, resize_inside_bounds_size)); | |
134 #endif | |
135 } | |
136 | |
137 // views::NonClientFrameView implementation. | |
138 | |
139 gfx::Rect ShellWindowFrameView::GetBoundsForClientView() const { | |
140 if (window_->frameless() || frame_->IsFullscreen()) | |
141 return bounds(); | |
142 return gfx::Rect(0, kCaptionHeight, width(), | |
143 std::max(0, height() - kCaptionHeight)); | |
144 } | |
145 | |
146 gfx::Rect ShellWindowFrameView::GetWindowBoundsForClientBounds( | |
147 const gfx::Rect& client_bounds) const { | |
148 if (window_->frameless()) { | |
149 gfx::Rect window_bounds = client_bounds; | |
150 // Enforce minimum size (1, 1) in case that client_bounds is passed with | |
151 // empty size. This could occur when the frameless window is being | |
152 // initialized. | |
153 if (window_bounds.IsEmpty()) { | |
154 window_bounds.set_width(1); | |
155 window_bounds.set_height(1); | |
156 } | |
157 return window_bounds; | |
158 } | |
159 | |
160 int closeButtonOffsetX = | |
161 (kCaptionHeight - close_button_->height()) / 2; | |
162 int header_width = close_button_->width() + closeButtonOffsetX * 2; | |
163 return gfx::Rect(client_bounds.x(), | |
164 std::max(0, client_bounds.y() - kCaptionHeight), | |
165 std::max(header_width, client_bounds.width()), | |
166 client_bounds.height() + kCaptionHeight); | |
167 } | |
168 | |
169 int ShellWindowFrameView::NonClientHitTest(const gfx::Point& point) { | |
170 if (frame_->IsFullscreen()) | |
171 return HTCLIENT; | |
172 | |
173 int resize_inside_bounds_size = kResizeInsideBoundsSize; | |
174 int resize_area_corner_size = kResizeAreaCornerSize; | |
175 | |
176 #if defined(USE_ASH) | |
177 aura::Window* window = frame_->GetNativeWindow(); | |
178 if (chrome::IsNativeWindowInAsh(window)) { | |
179 gfx::Rect expanded_bounds = bounds(); | |
180 int outside_bounds = ash::kResizeOutsideBoundsSize; | |
181 if (aura::Env::GetInstance()->is_touch_down()) | |
182 outside_bounds *= ash::kResizeOutsideBoundsScaleForTouch; | |
183 expanded_bounds.Inset(-outside_bounds, -outside_bounds); | |
184 if (!expanded_bounds.Contains(point)) | |
185 return HTNOWHERE; | |
186 | |
187 resize_inside_bounds_size = ash::kResizeInsideBoundsSize; | |
188 resize_area_corner_size = ash::kResizeAreaCornerSize; | |
189 } | |
190 #endif | |
191 | |
192 // Check the frame first, as we allow a small area overlapping the contents | |
193 // to be used for resize handles. | |
194 bool can_ever_resize = frame_->widget_delegate() ? | |
195 frame_->widget_delegate()->CanResize() : | |
196 false; | |
197 // Don't allow overlapping resize handles when the window is maximized or | |
198 // fullscreen, as it can't be resized in those states. | |
199 int resize_border = | |
200 (frame_->IsMaximized() || frame_->IsFullscreen()) ? 0 : | |
201 resize_inside_bounds_size; | |
202 int frame_component = GetHTComponentForFrame(point, | |
203 resize_border, | |
204 resize_border, | |
205 resize_area_corner_size, | |
206 resize_area_corner_size, | |
207 can_ever_resize); | |
208 if (frame_component != HTNOWHERE) | |
209 return frame_component; | |
210 | |
211 // Check for possible draggable region in the client area for the frameless | |
212 // window. | |
213 if (window_->frameless() && | |
214 window_->draggable_region() && | |
215 window_->draggable_region()->contains(point.x(), point.y())) | |
216 return HTCAPTION; | |
217 | |
218 int client_component = frame_->client_view()->NonClientHitTest(point); | |
219 if (client_component != HTNOWHERE) | |
220 return client_component; | |
221 | |
222 // Then see if the point is within any of the window controls. | |
223 if (close_button_ && close_button_->visible() && | |
224 close_button_->GetMirroredBounds().Contains(point)) | |
225 return HTCLOSE; | |
226 | |
227 // Caption is a safe default. | |
228 return HTCAPTION; | |
229 } | |
230 | |
231 void ShellWindowFrameView::GetWindowMask(const gfx::Size& size, | |
232 gfx::Path* window_mask) { | |
233 // We got nothing to say about no window mask. | |
234 } | |
235 | |
236 // views::View implementation. | |
237 | |
238 gfx::Size ShellWindowFrameView::GetPreferredSize() { | |
239 gfx::Size pref = frame_->client_view()->GetPreferredSize(); | |
240 gfx::Rect bounds(0, 0, pref.width(), pref.height()); | |
241 return frame_->non_client_view()->GetWindowBoundsForClientBounds( | |
242 bounds).size(); | |
243 } | |
244 | |
245 void ShellWindowFrameView::Layout() { | |
246 if (window_->frameless()) | |
247 return; | |
248 gfx::Size close_size = close_button_->GetPreferredSize(); | |
249 const int kButtonOffsetY = 0; | |
250 const int kButtonSpacing = 1; | |
251 const int kRightMargin = 3; | |
252 | |
253 close_button_->SetBounds( | |
254 width() - kRightMargin - close_size.width(), | |
255 kButtonOffsetY, | |
256 close_size.width(), | |
257 close_size.height()); | |
258 | |
259 bool can_ever_resize = frame_->widget_delegate() ? | |
260 frame_->widget_delegate()->CanResize() : | |
261 false; | |
262 maximize_button_->SetEnabled(can_ever_resize); | |
263 gfx::Size maximize_size = maximize_button_->GetPreferredSize(); | |
264 maximize_button_->SetBounds( | |
265 close_button_->x() - kButtonSpacing - maximize_size.width(), | |
266 kButtonOffsetY, | |
267 maximize_size.width(), | |
268 maximize_size.height()); | |
269 gfx::Size restore_size = restore_button_->GetPreferredSize(); | |
270 restore_button_->SetBounds( | |
271 close_button_->x() - kButtonSpacing - restore_size.width(), | |
272 kButtonOffsetY, | |
273 restore_size.width(), | |
274 restore_size.height()); | |
275 | |
276 bool maximized = frame_->IsMaximized(); | |
277 maximize_button_->SetVisible(!maximized); | |
278 restore_button_->SetVisible(maximized); | |
279 if (maximized) | |
280 maximize_button_->SetState(views::CustomButton::STATE_NORMAL); | |
281 else | |
282 restore_button_->SetState(views::CustomButton::STATE_NORMAL); | |
283 | |
284 gfx::Size minimize_size = minimize_button_->GetPreferredSize(); | |
285 minimize_button_->SetBounds( | |
286 maximize_button_->x() - kButtonSpacing - minimize_size.width(), | |
287 kButtonOffsetY, | |
288 minimize_size.width(), | |
289 minimize_size.height()); | |
290 } | |
291 | |
292 void ShellWindowFrameView::OnPaint(gfx::Canvas* canvas) { | |
293 if (window_->frameless()) | |
294 return; | |
295 | |
296 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
297 if (ShouldPaintAsActive()) { | |
298 close_button_->SetImage(views::CustomButton::STATE_NORMAL, | |
299 rb.GetNativeImageNamed(IDR_APP_WINDOW_CLOSE).ToImageSkia()); | |
300 } else { | |
301 close_button_->SetImage(views::CustomButton::STATE_NORMAL, | |
302 rb.GetNativeImageNamed(IDR_APP_WINDOW_CLOSE_U).ToImageSkia()); | |
303 } | |
304 | |
305 // TODO(jeremya): different look for inactive? | |
306 SkPaint paint; | |
307 paint.setAntiAlias(false); | |
308 paint.setStyle(SkPaint::kFill_Style); | |
309 paint.setColor(SK_ColorWHITE); | |
310 gfx::Path path; | |
311 const int radius = frame_->IsMaximized() ? 0 : 1; | |
312 path.moveTo(0, radius); | |
313 path.lineTo(radius, 0); | |
314 path.lineTo(width() - radius - 1, 0); | |
315 path.lineTo(width(), radius + 1); | |
316 path.lineTo(width(), kCaptionHeight); | |
317 path.lineTo(0, kCaptionHeight); | |
318 path.close(); | |
319 canvas->DrawPath(path, paint); | |
320 } | |
321 | |
322 const char* ShellWindowFrameView::GetClassName() const { | |
323 return kViewClassName; | |
324 } | |
325 | |
326 gfx::Size ShellWindowFrameView::GetMinimumSize() { | |
327 gfx::Size min_size = frame_->client_view()->GetMinimumSize(); | |
328 if (window_->frameless()) | |
329 return min_size; | |
330 | |
331 // Ensure we can display the top of the caption area. | |
332 gfx::Rect client_bounds = GetBoundsForClientView(); | |
333 min_size.Enlarge(0, client_bounds.y()); | |
334 // Ensure we have enough space for the window icon and buttons. We allow | |
335 // the title string to collapse to zero width. | |
336 int closeButtonOffsetX = | |
337 (kCaptionHeight - close_button_->height()) / 2; | |
338 int header_width = close_button_->width() + closeButtonOffsetX * 2; | |
339 if (header_width > min_size.width()) | |
340 min_size.set_width(header_width); | |
341 return min_size; | |
342 } | |
343 | |
344 gfx::Size ShellWindowFrameView::GetMaximumSize() { | |
345 gfx::Size max_size = frame_->client_view()->GetMaximumSize(); | |
346 | |
347 // Add to the client maximum size the height of any title bar and borders. | |
348 gfx::Size client_size = GetBoundsForClientView().size(); | |
349 if (max_size.width()) | |
350 max_size.Enlarge(width() - client_size.width(), 0); | |
351 if (max_size.height()) | |
352 max_size.Enlarge(0, height() - client_size.height()); | |
353 | |
354 return max_size; | |
355 } | |
356 | |
357 // views::ButtonListener implementation. | |
358 | |
359 void ShellWindowFrameView::ButtonPressed(views::Button* sender, | |
360 const ui::Event& event) { | |
361 DCHECK(!window_->frameless()); | |
362 if (sender == close_button_) | |
363 frame_->Close(); | |
364 else if (sender == maximize_button_) | |
365 frame_->Maximize(); | |
366 else if (sender == restore_button_) | |
367 frame_->Restore(); | |
368 else if (sender == minimize_button_) | |
369 frame_->Minimize(); | |
370 } | |
OLD | NEW |