Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_aura.h" | 5 #include "ui/native_theme/native_theme_aura.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "ui/base/layout.h" | 12 #include "ui/base/layout.h" |
| 13 #include "ui/base/material_design/material_design_controller.h" | 13 #include "ui/base/material_design/material_design_controller.h" |
| 14 #include "ui/gfx/animation/tween.h" | 14 #include "ui/gfx/animation/tween.h" |
| 15 #include "ui/gfx/canvas.h" | 15 #include "ui/gfx/canvas.h" |
| 16 #include "ui/gfx/color_palette.h" | 16 #include "ui/gfx/color_palette.h" |
| 17 #include "ui/gfx/geometry/rect.h" | 17 #include "ui/gfx/geometry/rect.h" |
| 18 #include "ui/gfx/geometry/size.h" | 18 #include "ui/gfx/geometry/size.h" |
| 19 #include "ui/gfx/image/image_skia.h" | 19 #include "ui/gfx/image/image_skia.h" |
| 20 #include "ui/gfx/path.h" | 20 #include "ui/gfx/path.h" |
| 21 #include "ui/gfx/skia_util.h" | 21 #include "ui/gfx/skia_util.h" |
| 22 #include "ui/native_theme/common_theme.h" | 22 #include "ui/native_theme/common_theme.h" |
| 23 #include "ui/native_theme/material_design_constants.h" | |
| 23 #include "ui/native_theme/native_theme_switches.h" | 24 #include "ui/native_theme/native_theme_switches.h" |
| 24 | 25 |
| 25 namespace ui { | 26 namespace ui { |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 SkAlpha ThumbAlphaForState(NativeTheme::State state) { | 30 SkAlpha ThumbAlphaForState(NativeTheme::State state) { |
| 30 bool overlay = IsOverlayScrollbarEnabled(); | 31 bool overlay = IsOverlayScrollbarEnabled(); |
| 31 switch (state) { | 32 switch (state) { |
| 32 case NativeTheme::kDisabled: | 33 case NativeTheme::kDisabled: |
| 33 return 0x00; | 34 return 0x00; |
| 34 case NativeTheme::kHovered: | 35 case NativeTheme::kHovered: |
| 35 return overlay ? 0xB2 : 0x4D; | 36 return overlay ? kMDScrollbarAlphaHovered : 0x4D; |
| 36 case NativeTheme::kNormal: | 37 case NativeTheme::kNormal: |
| 37 return overlay ? 0x8C : 0x33; | 38 return overlay ? kMDScrollbarAlphaNormal : 0x33; |
| 38 case NativeTheme::kPressed: | 39 case NativeTheme::kPressed: |
| 39 return overlay ? 0xB2 : 0x80; | 40 return overlay ? kMDScrollbarAlphaPressed : 0x80; |
| 40 case NativeTheme::kNumStates: | 41 case NativeTheme::kNumStates: |
| 41 break; | 42 break; |
| 42 } | 43 } |
| 43 | 44 |
| 44 NOTREACHED(); | 45 NOTREACHED(); |
| 45 return 0xFF; | 46 return 0xFF; |
| 46 } | 47 } |
| 47 | 48 |
| 48 SkAlpha ThumbStrokeAlphaForState(NativeTheme::State state) { | 49 SkAlpha ThumbStrokeAlphaForState(NativeTheme::State state) { |
|
Peter Kasting
2016/10/17 19:10:25
Do we even need this function anymore?
It looks l
bokan
2016/10/17 23:21:03
This function is now gone.
| |
| 49 DCHECK(IsOverlayScrollbarEnabled()); | 50 DCHECK(IsOverlayScrollbarEnabled()); |
| 50 switch (state) { | 51 switch (state) { |
| 51 case NativeTheme::kDisabled: | 52 case NativeTheme::kDisabled: |
| 52 return 0x00; | 53 return 0x00; |
| 53 case NativeTheme::kHovered: | 54 case NativeTheme::kHovered: |
| 55 return kMDScrollbarAlphaHovered; | |
| 54 case NativeTheme::kPressed: | 56 case NativeTheme::kPressed: |
| 55 return 0x33; | 57 return kMDScrollbarAlphaPressed; |
| 56 case NativeTheme::kNormal: | 58 case NativeTheme::kNormal: |
| 57 return 0x26; | 59 return kMDScrollbarAlphaNormal; |
| 58 case NativeTheme::kNumStates: | 60 case NativeTheme::kNumStates: |
| 59 break; | 61 break; |
| 60 } | 62 } |
| 61 | 63 |
| 62 NOTREACHED(); | 64 NOTREACHED(); |
| 63 return 0xFF; | 65 return 0xFF; |
| 64 } | 66 } |
| 65 | 67 |
| 66 const SkColor kTrackColor = SkColorSetRGB(0xF1, 0xF1, 0xF1); | 68 const SkColor kTrackColor = SkColorSetRGB(0xF1, 0xF1, 0xF1); |
| 67 | 69 |
| 68 } // namespace | 70 } // namespace |
| 69 | 71 |
| 70 // static | 72 // static |
| 71 NativeTheme* NativeTheme::GetInstanceForWeb() { | 73 NativeTheme* NativeTheme::GetInstanceForWeb() { |
| 72 return NativeThemeAura::instance(); | 74 return NativeThemeAura::instance(); |
| 73 } | 75 } |
| 74 | 76 |
| 75 // static | 77 // static |
| 76 NativeThemeAura* NativeThemeAura::instance() { | 78 NativeThemeAura* NativeThemeAura::instance() { |
| 77 CR_DEFINE_STATIC_LOCAL(NativeThemeAura, s_native_theme, ()); | 79 CR_DEFINE_STATIC_LOCAL(NativeThemeAura, s_native_theme, ()); |
| 78 return &s_native_theme; | 80 return &s_native_theme; |
| 79 } | 81 } |
| 80 | 82 |
| 81 NativeThemeAura::NativeThemeAura() { | 83 NativeThemeAura::NativeThemeAura() { |
| 82 // We don't draw scrollbar buttons. | 84 // We don't draw scrollbar buttons. |
| 83 #if defined(OS_CHROMEOS) | 85 #if defined(OS_CHROMEOS) |
| 84 set_scrollbar_button_length(0); | 86 set_scrollbar_button_length(0); |
| 85 #endif | 87 #endif |
| 86 | 88 |
| 89 if (IsOverlayScrollbarEnabled()) { | |
| 90 scrollbar_width_ = | |
| 91 kMDScrollbarThumbWidthPressed + kMDScrollbarStrokeWidth * 2; | |
| 92 } | |
| 93 | |
| 87 // Images and alphas declarations assume the following order. | 94 // Images and alphas declarations assume the following order. |
| 88 static_assert(kDisabled == 0, "states unexpectedly changed"); | 95 static_assert(kDisabled == 0, "states unexpectedly changed"); |
| 89 static_assert(kHovered == 1, "states unexpectedly changed"); | 96 static_assert(kHovered == 1, "states unexpectedly changed"); |
| 90 static_assert(kNormal == 2, "states unexpectedly changed"); | 97 static_assert(kNormal == 2, "states unexpectedly changed"); |
| 91 static_assert(kPressed == 3, "states unexpectedly changed"); | 98 static_assert(kPressed == 3, "states unexpectedly changed"); |
| 92 } | 99 } |
| 93 | 100 |
| 94 NativeThemeAura::~NativeThemeAura() { | 101 NativeThemeAura::~NativeThemeAura() { |
| 95 } | 102 } |
| 96 | 103 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 190 } | 197 } |
| 191 | 198 |
| 192 void NativeThemeAura::PaintScrollbarThumbStateTransition( | 199 void NativeThemeAura::PaintScrollbarThumbStateTransition( |
| 193 SkCanvas* canvas, | 200 SkCanvas* canvas, |
| 194 Part part, | 201 Part part, |
| 195 State start_state, | 202 State start_state, |
| 196 State end_state, | 203 State end_state, |
| 197 double progress, | 204 double progress, |
| 198 const gfx::Rect& rect) const { | 205 const gfx::Rect& rect) const { |
| 199 gfx::Rect thumb_rect(rect); | 206 gfx::Rect thumb_rect(rect); |
| 207 SkColor thumb_color; | |
| 200 if (IsOverlayScrollbarEnabled()) { | 208 if (IsOverlayScrollbarEnabled()) { |
| 201 // In overlay mode, draw a stroke (border). | 209 // In overlay mode, draw a stroke (border). |
| 202 const int kStrokeWidth = 1; | 210 const int kStrokeWidth = kMDScrollbarStrokeWidth; |
|
Peter Kasting
2016/10/17 19:10:25
Nit: Can be constexpr
bokan
2016/10/17 23:21:03
Done.
| |
| 203 SkAlpha stroke_alpha = gfx::Tween::IntValueBetween( | 211 SkAlpha stroke_alpha = gfx::Tween::IntValueBetween( |
| 204 progress, ThumbStrokeAlphaForState(start_state), | 212 progress, ThumbStrokeAlphaForState(start_state), |
| 205 ThumbStrokeAlphaForState(end_state)); | 213 ThumbStrokeAlphaForState(end_state)); |
| 206 SkPaint paint; | 214 SkPaint paint; |
| 207 paint.setColor(SkColorSetA(SK_ColorWHITE, stroke_alpha)); | 215 paint.setColor(SkColorSetA(kMDScrollbarStrokeColor, stroke_alpha)); |
| 208 paint.setStyle(SkPaint::kStroke_Style); | 216 paint.setStyle(SkPaint::kStroke_Style); |
| 209 paint.setStrokeWidth(kStrokeWidth); | 217 paint.setStrokeWidth(kStrokeWidth); |
| 218 | |
| 219 // When stroking in skia, there's some surprising behavior so we need to | |
| 220 // inset the right and bottom edges first. See the discussion here: | |
| 221 // https://groups.google.com/forum/#!topic/skia-discuss/HGZcc3Z7w1Q | |
|
Peter Kasting
2016/10/17 19:10:25
I'm concerned about this code.
Looking at the ref
bokan
2016/10/17 23:21:03
You're right, I misunderstood the discussion in th
| |
| 222 thumb_rect.Inset(0, 0, kStrokeWidth, kStrokeWidth); | |
| 210 canvas->drawIRect(gfx::RectToSkIRect(thumb_rect), paint); | 223 canvas->drawIRect(gfx::RectToSkIRect(thumb_rect), paint); |
| 211 | 224 |
| 212 thumb_rect.Inset(kStrokeWidth, kStrokeWidth, kStrokeWidth, kStrokeWidth); | 225 // Inset the top and left edges for filling in the stroke below. |
| 226 thumb_rect.Inset(kStrokeWidth, kStrokeWidth, 0, 0); | |
| 227 | |
| 228 thumb_color = kMDScrollbarThumbColor; | |
| 213 } else { | 229 } else { |
| 214 // If there are no scrollbuttons then provide some padding so that the thumb | 230 // If there are no scrollbuttons then provide some padding so that the thumb |
| 215 // doesn't touch the top of the track. | 231 // doesn't touch the top of the track. |
| 216 const int kThumbPadding = 2; | 232 const int kThumbPadding = 2; |
| 217 const int extra_padding = | 233 const int extra_padding = |
| 218 (scrollbar_button_length() == 0) ? kThumbPadding : 0; | 234 (scrollbar_button_length() == 0) ? kThumbPadding : 0; |
| 219 if (part == NativeTheme::kScrollbarVerticalThumb) | 235 if (part == NativeTheme::kScrollbarVerticalThumb) |
| 220 thumb_rect.Inset(kThumbPadding, extra_padding); | 236 thumb_rect.Inset(kThumbPadding, extra_padding); |
| 221 else | 237 else |
| 222 thumb_rect.Inset(extra_padding, kThumbPadding); | 238 thumb_rect.Inset(extra_padding, kThumbPadding); |
| 239 | |
| 240 thumb_color = SK_ColorBLACK; | |
| 223 } | 241 } |
| 224 | 242 |
| 225 SkPaint paint; | 243 SkPaint paint; |
| 226 SkAlpha alpha = gfx::Tween::IntValueBetween( | 244 SkAlpha alpha = gfx::Tween::IntValueBetween( |
| 227 progress, ThumbAlphaForState(start_state), ThumbAlphaForState(end_state)); | 245 progress, ThumbAlphaForState(start_state), ThumbAlphaForState(end_state)); |
| 228 paint.setColor(SkColorSetA(SK_ColorBLACK, alpha)); | 246 paint.setColor(SkColorSetA(thumb_color, alpha)); |
| 229 canvas->drawIRect(gfx::RectToSkIRect(thumb_rect), paint); | 247 canvas->drawIRect(gfx::RectToSkIRect(thumb_rect), paint); |
| 230 } | 248 } |
| 231 | 249 |
| 232 void NativeThemeAura::PaintScrollbarCorner(SkCanvas* canvas, | 250 void NativeThemeAura::PaintScrollbarCorner(SkCanvas* canvas, |
| 233 State state, | 251 State state, |
| 234 const gfx::Rect& rect) const { | 252 const gfx::Rect& rect) const { |
| 235 // Overlay Scrollbar should never paint a scrollbar corner. | 253 // Overlay Scrollbar should never paint a scrollbar corner. |
| 236 DCHECK(!IsOverlayScrollbarEnabled()); | 254 DCHECK(!IsOverlayScrollbarEnabled()); |
| 237 SkPaint paint; | 255 SkPaint paint; |
| 238 paint.setColor(SkColorSetRGB(0xDC, 0xDC, 0xDC)); | 256 paint.setColor(SkColorSetRGB(0xDC, 0xDC, 0xDC)); |
| 239 canvas->drawIRect(RectToSkIRect(rect), paint); | 257 canvas->drawIRect(RectToSkIRect(rect), paint); |
| 240 } | 258 } |
| 241 | 259 |
| 260 gfx::Size NativeThemeAura::GetPartSize(Part part, | |
| 261 State state, | |
| 262 const ExtraParams& extra) const { | |
| 263 if (!IsOverlayScrollbarEnabled()) | |
|
Peter Kasting
2016/10/17 19:10:24
Nit: This conditional effectively results in dupli
bokan
2016/10/17 23:21:04
Done.
| |
| 264 return NativeThemeBase::GetPartSize(part, state, extra); | |
|
Peter Kasting
2016/10/17 19:10:24
Nit: 2 spaces
bokan
2016/10/17 23:21:03
Done.
| |
| 265 | |
| 266 // Aura overlay scrollbars need a slight tweak from the base sizes. | |
| 267 switch (part) { | |
| 268 case kScrollbarDownArrow: | |
| 269 case kScrollbarUpArrow: | |
| 270 case kScrollbarLeftArrow: | |
| 271 case kScrollbarRightArrow: | |
| 272 case kScrollbarHorizontalTrack: | |
| 273 case kScrollbarVerticalTrack: | |
| 274 case kScrollbarHorizontalGripper: | |
| 275 case kScrollbarVerticalGripper: | |
| 276 // Aura overlay scrollbars only have a thumb. | |
| 277 NOTREACHED(); | |
|
Peter Kasting
2016/10/17 19:10:25
Is NOTREACHED really correct here, or should we ju
bokan
2016/10/17 23:21:04
I leaned towards forcing a runtime failure since I
bokan
2016/10/18 21:39:48
Ok, I found a case where this did get tripped. Whe
| |
| 278 break; | |
| 279 case kScrollbarHorizontalThumb: | |
| 280 return gfx::Size(kMDScrollbarMinimumLength + 2 * kMDScrollbarStrokeWidth, | |
|
Peter Kasting
2016/10/17 19:10:24
Nit: You can factor this out to a constant above t
bokan
2016/10/17 23:21:04
Done.
| |
| 281 scrollbar_width_); | |
| 282 case kScrollbarVerticalThumb: | |
| 283 return gfx::Size(scrollbar_width_, | |
| 284 kMDScrollbarMinimumLength + 2 * kMDScrollbarStrokeWidth); | |
| 285 default: | |
| 286 break; | |
| 287 } | |
| 288 | |
| 289 return NativeThemeBase::GetPartSize(part, state, extra); | |
| 290 } | |
| 291 | |
| 242 } // namespace ui | 292 } // namespace ui |
| OLD | NEW |