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