OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "ui/native_theme/native_theme_aura_overlay.h" |
| 6 |
| 7 #include "base/trace_event/trace_event.h" |
| 8 #include "ui/gfx/skia_util.h" |
| 9 #include "ui/native_theme/native_theme_switches.h" |
| 10 #include "ui/native_theme/overlay_scrollbar_constants_aura.h" |
| 11 |
| 12 namespace ui { |
| 13 |
| 14 namespace { |
| 15 |
| 16 // Constants for painting overlay scrollbars. Other properties needed outside |
| 17 // this painting code are defined in overlay_scrollbar_constants_aura.h. |
| 18 constexpr int kOverlayScrollbarStrokeWidth = 1; |
| 19 constexpr int kOverlayScrollbarMinimumLength = 12; |
| 20 constexpr SkAlpha kOverlayScrollbarAlphaNormal = 0x4D; |
| 21 constexpr SkAlpha kOverlayScrollbarAlphaHovered = 0x80; |
| 22 constexpr SkAlpha kOverlayScrollbarAlphaPressed = 0x80; |
| 23 |
| 24 // Indexed by ScrollbarOverlayColorTheme. |
| 25 constexpr SkColor kOverlayScrollbarThumbColor[] = {SK_ColorBLACK, |
| 26 SK_ColorWHITE}; |
| 27 constexpr SkColor kOverlayScrollbarStrokeColor[] = {SK_ColorWHITE, |
| 28 SK_ColorBLACK}; |
| 29 |
| 30 SkAlpha ThumbAlphaForState(NativeTheme::State state) { |
| 31 switch (state) { |
| 32 case NativeTheme::kDisabled: |
| 33 return 0x00; |
| 34 case NativeTheme::kHovered: |
| 35 return kOverlayScrollbarAlphaHovered; |
| 36 case NativeTheme::kNormal: |
| 37 return kOverlayScrollbarAlphaNormal; |
| 38 case NativeTheme::kPressed: |
| 39 return kOverlayScrollbarAlphaPressed; |
| 40 case NativeTheme::kNumStates: |
| 41 break; |
| 42 } |
| 43 |
| 44 NOTREACHED(); |
| 45 return 0xFF; |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 // static |
| 51 NativeTheme* NativeTheme::GetInstanceForWeb(bool use_overlay_scrollbars) { |
| 52 if (use_overlay_scrollbars) |
| 53 return NativeThemeAuraOverlay::instance(); |
| 54 |
| 55 return NativeThemeAura::instance(); |
| 56 } |
| 57 |
| 58 // static |
| 59 NativeThemeAuraOverlay* NativeThemeAuraOverlay::instance() { |
| 60 CR_DEFINE_STATIC_LOCAL(NativeThemeAuraOverlay, s_native_theme, ()); |
| 61 return &s_native_theme; |
| 62 } |
| 63 |
| 64 NativeThemeAuraOverlay::NativeThemeAuraOverlay() { |
| 65 scrollbar_width_ = |
| 66 kOverlayScrollbarThumbWidthPressed + kOverlayScrollbarStrokeWidth * 2; |
| 67 } |
| 68 |
| 69 NativeThemeAuraOverlay::~NativeThemeAuraOverlay() {} |
| 70 |
| 71 void NativeThemeAuraOverlay::PaintScrollbarTrack( |
| 72 SkCanvas* canvas, |
| 73 Part part, |
| 74 State state, |
| 75 const ScrollbarTrackExtraParams& extra_params, |
| 76 const gfx::Rect& rect) const { |
| 77 NOTREACHED(); |
| 78 } |
| 79 |
| 80 void NativeThemeAuraOverlay::PaintScrollbarThumb( |
| 81 SkCanvas* canvas, |
| 82 Part part, |
| 83 State state, |
| 84 const gfx::Rect& rect, |
| 85 ScrollbarOverlayColorTheme theme) const { |
| 86 // Do not paint if state is disabled. |
| 87 if (state == kDisabled) |
| 88 return; |
| 89 |
| 90 TRACE_EVENT0("blink", "NativeThemeAuraOverlay::PaintScrollbarThumb"); |
| 91 |
| 92 gfx::Rect thumb_rect(rect); |
| 93 SkAlpha thumb_alpha = ThumbAlphaForState(state); |
| 94 |
| 95 // In overlay mode, draw a stroke (border). |
| 96 { |
| 97 constexpr int kStrokeWidth = kOverlayScrollbarStrokeWidth; |
| 98 SkPaint paint; |
| 99 paint.setColor( |
| 100 SkColorSetA(kOverlayScrollbarStrokeColor[theme], thumb_alpha)); |
| 101 paint.setStyle(SkPaint::kStroke_Style); |
| 102 paint.setStrokeWidth(kStrokeWidth); |
| 103 gfx::RectF stroke_rect(thumb_rect); |
| 104 constexpr float kHalfStrokeWidth = kStrokeWidth / 2.f; |
| 105 stroke_rect.Inset(kHalfStrokeWidth, kHalfStrokeWidth); |
| 106 canvas->drawRect(gfx::RectFToSkRect(stroke_rect), paint); |
| 107 |
| 108 // Inset the all the edges edges so we fill-in the stroke below. |
| 109 thumb_rect.Inset(kStrokeWidth, kStrokeWidth); |
| 110 } |
| 111 |
| 112 SkPaint paint; |
| 113 paint.setColor(SkColorSetA(kOverlayScrollbarThumbColor[theme], thumb_alpha)); |
| 114 canvas->drawIRect(gfx::RectToSkIRect(thumb_rect), paint); |
| 115 } |
| 116 |
| 117 void NativeThemeAuraOverlay::PaintScrollbarCorner(SkCanvas* canvas, |
| 118 State state, |
| 119 const gfx::Rect& rect) const { |
| 120 NOTREACHED(); |
| 121 } |
| 122 |
| 123 gfx::Size NativeThemeAuraOverlay::GetPartSize(Part part, |
| 124 State state, |
| 125 const ExtraParams& extra) const { |
| 126 constexpr int minimum_length = |
| 127 kOverlayScrollbarMinimumLength + 2 * kOverlayScrollbarStrokeWidth; |
| 128 |
| 129 // Aura overlay scrollbars need a slight tweak from the base sizes. |
| 130 switch (part) { |
| 131 case kScrollbarHorizontalThumb: |
| 132 return gfx::Size(minimum_length, scrollbar_width_); |
| 133 case kScrollbarVerticalThumb: |
| 134 return gfx::Size(scrollbar_width_, minimum_length); |
| 135 default: |
| 136 // TODO(bokan): We should probably make sure code using overlay |
| 137 // scrollbars isn't asking for part sizes that don't exist. This |
| 138 // currently breaks in Views layout code which indicates they aren't |
| 139 // overlay aware yet. The Views code should be fixed and either this |
| 140 // branch return 0 for parts that don't exist or assert NOTREACHED. |
| 141 // crbug.com/657159. |
| 142 break; |
| 143 } |
| 144 |
| 145 return NativeThemeAura::GetPartSize(part, state, extra); |
| 146 } |
| 147 |
| 148 } // namespace ui |
OLD | NEW |