| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/native_theme/native_theme_mac.h" | 5 #include "ui/native_theme/native_theme_mac.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include "base/mac/mac_util.h" | 10 #include "base/mac/mac_util.h" |
| 11 #include "base/mac/scoped_cftyperef.h" | 11 #include "base/mac/scoped_cftyperef.h" |
| 12 #include "base/mac/sdk_forward_declarations.h" | 12 #include "base/mac/sdk_forward_declarations.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #import "skia/ext/skia_utils_mac.h" | 14 #import "skia/ext/skia_utils_mac.h" |
| 15 #include "third_party/skia/include/effects/SkGradientShader.h" | 15 #include "third_party/skia/include/effects/SkGradientShader.h" |
| 16 #include "ui/gfx/geometry/rect.h" | 16 #include "ui/gfx/geometry/rect.h" |
| 17 #include "ui/gfx/skia_util.h" | 17 #include "ui/gfx/skia_util.h" |
| 18 #include "ui/native_theme/common_theme.h" | 18 #include "ui/native_theme/common_theme.h" |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 const SkColor kScrollerTrackGradientColors[] = { | |
| 23 SkColorSetRGB(0xEF, 0xEF, 0xEF), | |
| 24 SkColorSetRGB(0xF9, 0xF9, 0xF9), | |
| 25 SkColorSetRGB(0xFD, 0xFD, 0xFD), | |
| 26 SkColorSetRGB(0xF6, 0xF6, 0xF6) }; | |
| 27 const SkColor kScrollerTrackInnerBorderColor = SkColorSetRGB(0xE4, 0xE4, 0xE4); | |
| 28 const SkColor kScrollerTrackOuterBorderColor = SkColorSetRGB(0xEF, 0xEF, 0xEF); | |
| 29 const SkColor kScrollerThumbColor = SkColorSetARGB(0x38, 0, 0, 0); | |
| 30 const SkColor kScrollerThumbHoverColor = SkColorSetARGB(0x80, 0, 0, 0); | |
| 31 const int kScrollerTrackBorderWidth = 1; | |
| 32 | |
| 33 // The amount the thumb is inset from both the ends and the sides of the track. | |
| 34 const int kScrollerThumbInset = 3; | |
| 35 | |
| 36 // Values calculated by reading pixels and solving simultaneous equations | 22 // Values calculated by reading pixels and solving simultaneous equations |
| 37 // derived from "A over B" alpha compositing. Steps: Sample the semi-transparent | 23 // derived from "A over B" alpha compositing. Steps: Sample the semi-transparent |
| 38 // pixel over two backgrounds; P1, P2 over backgrounds B1, B2. Use the color | 24 // pixel over two backgrounds; P1, P2 over backgrounds B1, B2. Use the color |
| 39 // value between 0.0 and 1.0 (i.e. divide by 255.0). Then, | 25 // value between 0.0 and 1.0 (i.e. divide by 255.0). Then, |
| 40 // alpha = (P2 - P1 + B1 - B2) / (B1 - B2) | 26 // alpha = (P2 - P1 + B1 - B2) / (B1 - B2) |
| 41 // color = (P1 - B1 + alpha * B1) / alpha. | 27 // color = (P1 - B1 + alpha * B1) / alpha. |
| 42 const SkColor kMenuPopupBackgroundColor = SkColorSetARGB(255, 255, 255, 255); | 28 const SkColor kMenuPopupBackgroundColor = SkColorSetARGB(255, 255, 255, 255); |
| 43 const SkColor kMenuSeparatorColor = SkColorSetARGB(243, 228, 228, 228); | 29 const SkColor kMenuSeparatorColor = SkColorSetARGB(243, 228, 228, 228); |
| 44 const SkColor kMenuBorderColor = SkColorSetARGB(60, 0, 0, 0); | 30 const SkColor kMenuBorderColor = SkColorSetARGB(60, 0, 0, 0); |
| 45 | 31 |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 case kColorId_TreeArrow: | 219 case kColorId_TreeArrow: |
| 234 case kColorId_TableGroupingIndicatorColor: | 220 case kColorId_TableGroupingIndicatorColor: |
| 235 return SkColorSetRGB(140, 140, 140); | 221 return SkColorSetRGB(140, 140, 140); |
| 236 | 222 |
| 237 default: | 223 default: |
| 238 // TODO(tapted): Handle all values and remove the default case. | 224 // TODO(tapted): Handle all values and remove the default case. |
| 239 return GetAuraColor(color_id, this); | 225 return GetAuraColor(color_id, this); |
| 240 } | 226 } |
| 241 } | 227 } |
| 242 | 228 |
| 243 void NativeThemeMac::PaintScrollbarTrack( | |
| 244 SkCanvas* canvas, | |
| 245 Part part, | |
| 246 State state, | |
| 247 const ScrollbarTrackExtraParams& extra_params, | |
| 248 const gfx::Rect& rect) const { | |
| 249 // Emulate the non-overlay scroller style from OSX 10.7 and later. | |
| 250 SkPoint gradient_bounds[2]; | |
| 251 if (part == kScrollbarVerticalTrack) { | |
| 252 gradient_bounds[0].set(rect.x(), rect.y()); | |
| 253 gradient_bounds[1].set(rect.right(), rect.y()); | |
| 254 } else { | |
| 255 DCHECK_EQ(part, kScrollbarHorizontalTrack); | |
| 256 gradient_bounds[0].set(rect.x(), rect.y()); | |
| 257 gradient_bounds[1].set(rect.x(), rect.bottom()); | |
| 258 } | |
| 259 skia::RefPtr<SkShader> shader = skia::AdoptRef( | |
| 260 SkGradientShader::CreateLinear(gradient_bounds, | |
| 261 kScrollerTrackGradientColors, | |
| 262 NULL, | |
| 263 arraysize(kScrollerTrackGradientColors), | |
| 264 SkShader::kClamp_TileMode)); | |
| 265 SkPaint gradient; | |
| 266 gradient.setShader(shader.get()); | |
| 267 | |
| 268 SkIRect track_rect = gfx::RectToSkIRect(rect); | |
| 269 canvas->drawIRect(track_rect, gradient); | |
| 270 | |
| 271 // Draw inner and outer line borders. | |
| 272 if (part == kScrollbarVerticalTrack) { | |
| 273 SkPaint paint; | |
| 274 paint.setColor(kScrollerTrackInnerBorderColor); | |
| 275 canvas->drawRectCoords(track_rect.left(), | |
| 276 track_rect.top(), | |
| 277 track_rect.left() + kScrollerTrackBorderWidth, | |
| 278 track_rect.bottom(), | |
| 279 paint); | |
| 280 paint.setColor(kScrollerTrackOuterBorderColor); | |
| 281 canvas->drawRectCoords(track_rect.right() - kScrollerTrackBorderWidth, | |
| 282 track_rect.top(), | |
| 283 track_rect.right(), | |
| 284 track_rect.bottom(), | |
| 285 paint); | |
| 286 } else { | |
| 287 SkPaint paint; | |
| 288 paint.setColor(kScrollerTrackInnerBorderColor); | |
| 289 canvas->drawRectCoords(track_rect.left(), | |
| 290 track_rect.top(), | |
| 291 track_rect.right(), | |
| 292 track_rect.top() + kScrollerTrackBorderWidth, | |
| 293 paint); | |
| 294 paint.setColor(kScrollerTrackOuterBorderColor); | |
| 295 canvas->drawRectCoords(track_rect.left(), | |
| 296 track_rect.bottom() - kScrollerTrackBorderWidth, | |
| 297 track_rect.right(), | |
| 298 track_rect.bottom(), | |
| 299 paint); | |
| 300 } | |
| 301 } | |
| 302 | |
| 303 void NativeThemeMac::PaintScrollbarThumb(SkCanvas* canvas, | |
| 304 Part part, | |
| 305 State state, | |
| 306 const gfx::Rect& rect) const { | |
| 307 gfx::Rect thumb_rect(rect); | |
| 308 switch (part) { | |
| 309 case kScrollbarHorizontalThumb: | |
| 310 thumb_rect.Inset(0, kScrollerTrackBorderWidth, 0, 0); | |
| 311 break; | |
| 312 case kScrollbarVerticalThumb: | |
| 313 thumb_rect.Inset(kScrollerTrackBorderWidth, 0, 0, 0); | |
| 314 break; | |
| 315 default: | |
| 316 NOTREACHED(); | |
| 317 break; | |
| 318 } | |
| 319 | |
| 320 thumb_rect.Inset(kScrollerThumbInset, kScrollerThumbInset); | |
| 321 | |
| 322 SkPaint paint; | |
| 323 paint.setAntiAlias(true); | |
| 324 paint.setColor(state == kHovered ? thumb_active_color_ | |
| 325 : thumb_inactive_color_); | |
| 326 const SkScalar radius = std::min(rect.width(), rect.height()); | |
| 327 canvas->drawRoundRect(gfx::RectToSkRect(thumb_rect), radius, radius, paint); | |
| 328 } | |
| 329 | |
| 330 void NativeThemeMac::PaintScrollbarCorner(SkCanvas* canvas, | |
| 331 State state, | |
| 332 const gfx::Rect& rect) const { | |
| 333 DCHECK_GT(rect.width(), 0); | |
| 334 DCHECK_GT(rect.height(), 0); | |
| 335 | |
| 336 // Draw radial gradient from top-left corner. | |
| 337 skia::RefPtr<SkShader> shader = skia::AdoptRef( | |
| 338 SkGradientShader::CreateRadial(SkPoint::Make(rect.x(), rect.y()), | |
| 339 rect.width(), | |
| 340 kScrollerTrackGradientColors, | |
| 341 NULL, | |
| 342 arraysize(kScrollerTrackGradientColors), | |
| 343 SkShader::kClamp_TileMode)); | |
| 344 SkPaint gradient; | |
| 345 gradient.setStyle(SkPaint::kFill_Style); | |
| 346 gradient.setAntiAlias(true); | |
| 347 gradient.setShader(shader.get()); | |
| 348 canvas->drawRect(gfx::RectToSkRect(rect), gradient); | |
| 349 | |
| 350 // Draw inner border corner point. | |
| 351 canvas->drawPoint(rect.x(), rect.y(), kScrollerTrackInnerBorderColor); | |
| 352 | |
| 353 // Draw outer borders. | |
| 354 SkPaint paint; | |
| 355 paint.setColor(kScrollerTrackOuterBorderColor); | |
| 356 canvas->drawRectCoords(rect.right() - kScrollerTrackBorderWidth, | |
| 357 rect.y(), | |
| 358 rect.right(), | |
| 359 rect.bottom(), | |
| 360 paint); | |
| 361 canvas->drawRectCoords(rect.x(), | |
| 362 rect.bottom() - kScrollerTrackBorderWidth, | |
| 363 rect.right(), | |
| 364 rect.bottom(), | |
| 365 paint); | |
| 366 } | |
| 367 | |
| 368 void NativeThemeMac::PaintMenuPopupBackground( | 229 void NativeThemeMac::PaintMenuPopupBackground( |
| 369 SkCanvas* canvas, | 230 SkCanvas* canvas, |
| 370 const gfx::Size& size, | 231 const gfx::Size& size, |
| 371 const MenuBackgroundExtraParams& menu_background) const { | 232 const MenuBackgroundExtraParams& menu_background) const { |
| 372 SkPaint paint; | 233 SkPaint paint; |
| 373 paint.setAntiAlias(true); | 234 paint.setAntiAlias(true); |
| 374 if (base::mac::IsOSYosemiteOrLater()) | 235 if (base::mac::IsOSYosemiteOrLater()) |
| 375 paint.setColor(kMenuPopupBackgroundColorYosemite); | 236 paint.setColor(kMenuPopupBackgroundColorYosemite); |
| 376 else | 237 else |
| 377 paint.setColor(kMenuPopupBackgroundColor); | 238 paint.setColor(kMenuPopupBackgroundColor); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 399 paint.setColor(GetSystemColor(kColorId_HoverMenuItemBackgroundColor)); | 260 paint.setColor(GetSystemColor(kColorId_HoverMenuItemBackgroundColor)); |
| 400 canvas->drawRect(gfx::RectToSkRect(rect), paint); | 261 canvas->drawRect(gfx::RectToSkRect(rect), paint); |
| 401 break; | 262 break; |
| 402 default: | 263 default: |
| 403 NOTREACHED(); | 264 NOTREACHED(); |
| 404 break; | 265 break; |
| 405 } | 266 } |
| 406 } | 267 } |
| 407 | 268 |
| 408 NativeThemeMac::NativeThemeMac() { | 269 NativeThemeMac::NativeThemeMac() { |
| 409 set_scrollbar_button_length(0); | |
| 410 SetScrollbarColors(kScrollerThumbColor, | |
| 411 kScrollerThumbHoverColor, | |
| 412 kScrollerTrackGradientColors[0]); | |
| 413 } | 270 } |
| 414 | 271 |
| 415 NativeThemeMac::~NativeThemeMac() { | 272 NativeThemeMac::~NativeThemeMac() { |
| 416 } | 273 } |
| 417 | 274 |
| 418 } // namespace ui | 275 } // namespace ui |
| OLD | NEW |