| 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 "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "ui/base/layout.h" | 13 #include "ui/base/layout.h" |
| 14 #include "ui/base/material_design/material_design_controller.h" | 14 #include "ui/base/material_design/material_design_controller.h" |
| 15 #include "ui/gfx/animation/tween.h" | 15 #include "ui/gfx/animation/tween.h" |
| 16 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
| 17 #include "ui/gfx/color_palette.h" | 17 #include "ui/gfx/color_palette.h" |
| 18 #include "ui/gfx/geometry/rect.h" | 18 #include "ui/gfx/geometry/rect.h" |
| 19 #include "ui/gfx/geometry/size.h" | 19 #include "ui/gfx/geometry/size.h" |
| 20 #include "ui/gfx/image/image_skia.h" | 20 #include "ui/gfx/image/image_skia.h" |
| 21 #include "ui/gfx/path.h" | 21 #include "ui/gfx/path.h" |
| 22 #include "ui/gfx/skia_util.h" | 22 #include "ui/gfx/skia_util.h" |
| 23 #include "ui/native_theme/common_theme.h" | 23 #include "ui/native_theme/common_theme.h" |
| 24 #include "ui/native_theme/native_theme_switches.h" | |
| 25 #include "ui/native_theme/overlay_scrollbar_constants_aura.h" | |
| 26 | 24 |
| 27 namespace ui { | 25 namespace ui { |
| 28 | 26 |
| 29 namespace { | 27 namespace { |
| 30 | 28 |
| 31 // Constants for painting overlay scrollbars. Other properties needed outside | |
| 32 // this painting code are defined in overlay_scrollbar_constants_aura.h. | |
| 33 constexpr int kOverlayScrollbarStrokeWidth = 1; | |
| 34 constexpr int kOverlayScrollbarMinimumLength = 12; | |
| 35 constexpr SkAlpha kOverlayScrollbarAlphaNormal = 0x4D; | |
| 36 constexpr SkAlpha kOverlayScrollbarAlphaHovered = 0x80; | |
| 37 constexpr SkAlpha kOverlayScrollbarAlphaPressed = 0x80; | |
| 38 | |
| 39 // Indexed by ScrollbarOverlayColorTheme. | |
| 40 constexpr SkColor kOverlayScrollbarThumbColor[] = {SK_ColorBLACK, | |
| 41 SK_ColorWHITE}; | |
| 42 constexpr SkColor kOverlayScrollbarStrokeColor[] = {SK_ColorWHITE, | |
| 43 SK_ColorBLACK}; | |
| 44 | |
| 45 SkAlpha ThumbAlphaForState(NativeTheme::State state) { | 29 SkAlpha ThumbAlphaForState(NativeTheme::State state) { |
| 46 bool overlay = IsOverlayScrollbarEnabled(); | |
| 47 switch (state) { | 30 switch (state) { |
| 48 case NativeTheme::kDisabled: | 31 case NativeTheme::kDisabled: |
| 49 return 0x00; | 32 return 0x00; |
| 50 case NativeTheme::kHovered: | 33 case NativeTheme::kHovered: |
| 51 return overlay ? kOverlayScrollbarAlphaHovered : 0x4D; | 34 return 0x4D; |
| 52 case NativeTheme::kNormal: | 35 case NativeTheme::kNormal: |
| 53 return overlay ? kOverlayScrollbarAlphaNormal : 0x33; | 36 return 0x33; |
| 54 case NativeTheme::kPressed: | 37 case NativeTheme::kPressed: |
| 55 return overlay ? kOverlayScrollbarAlphaPressed : 0x80; | 38 return 0x80; |
| 56 case NativeTheme::kNumStates: | 39 case NativeTheme::kNumStates: |
| 57 break; | 40 break; |
| 58 } | 41 } |
| 59 | 42 |
| 60 NOTREACHED(); | 43 NOTREACHED(); |
| 61 return 0xFF; | 44 return 0xFF; |
| 62 } | 45 } |
| 63 | 46 |
| 64 const SkColor kTrackColor = SkColorSetRGB(0xF1, 0xF1, 0xF1); | 47 const SkColor kTrackColor = SkColorSetRGB(0xF1, 0xF1, 0xF1); |
| 65 | 48 |
| 66 } // namespace | 49 } // namespace |
| 67 | 50 |
| 68 // static | 51 // static |
| 69 NativeTheme* NativeTheme::GetInstanceForWeb() { | |
| 70 return NativeThemeAura::instance(); | |
| 71 } | |
| 72 | |
| 73 // static | |
| 74 NativeThemeAura* NativeThemeAura::instance() { | 52 NativeThemeAura* NativeThemeAura::instance() { |
| 75 CR_DEFINE_STATIC_LOCAL(NativeThemeAura, s_native_theme, ()); | 53 CR_DEFINE_STATIC_LOCAL(NativeThemeAura, s_native_theme, ()); |
| 76 return &s_native_theme; | 54 return &s_native_theme; |
| 77 } | 55 } |
| 78 | 56 |
| 79 NativeThemeAura::NativeThemeAura() { | 57 NativeThemeAura::NativeThemeAura() { |
| 80 // We don't draw scrollbar buttons. | 58 // We don't draw scrollbar buttons. |
| 81 #if defined(OS_CHROMEOS) | 59 #if defined(OS_CHROMEOS) |
| 82 set_scrollbar_button_length(0); | 60 set_scrollbar_button_length(0); |
| 83 #endif | 61 #endif |
| 84 | 62 |
| 85 if (IsOverlayScrollbarEnabled()) { | |
| 86 scrollbar_width_ = | |
| 87 kOverlayScrollbarThumbWidthPressed + kOverlayScrollbarStrokeWidth * 2; | |
| 88 } | |
| 89 | |
| 90 // Images and alphas declarations assume the following order. | 63 // Images and alphas declarations assume the following order. |
| 91 static_assert(kDisabled == 0, "states unexpectedly changed"); | 64 static_assert(kDisabled == 0, "states unexpectedly changed"); |
| 92 static_assert(kHovered == 1, "states unexpectedly changed"); | 65 static_assert(kHovered == 1, "states unexpectedly changed"); |
| 93 static_assert(kNormal == 2, "states unexpectedly changed"); | 66 static_assert(kNormal == 2, "states unexpectedly changed"); |
| 94 static_assert(kPressed == 3, "states unexpectedly changed"); | 67 static_assert(kPressed == 3, "states unexpectedly changed"); |
| 95 } | 68 } |
| 96 | 69 |
| 97 NativeThemeAura::~NativeThemeAura() {} | 70 NativeThemeAura::~NativeThemeAura() {} |
| 98 | 71 |
| 99 // This implementation returns hardcoded colors. | 72 // This implementation returns hardcoded colors. |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 | 139 |
| 167 PaintArrow(canvas, rect, direction, arrow_color); | 140 PaintArrow(canvas, rect, direction, arrow_color); |
| 168 } | 141 } |
| 169 | 142 |
| 170 void NativeThemeAura::PaintScrollbarTrack( | 143 void NativeThemeAura::PaintScrollbarTrack( |
| 171 SkCanvas* canvas, | 144 SkCanvas* canvas, |
| 172 Part part, | 145 Part part, |
| 173 State state, | 146 State state, |
| 174 const ScrollbarTrackExtraParams& extra_params, | 147 const ScrollbarTrackExtraParams& extra_params, |
| 175 const gfx::Rect& rect) const { | 148 const gfx::Rect& rect) const { |
| 176 // Overlay Scrollbar should never paint a scrollbar track. | |
| 177 DCHECK(!IsOverlayScrollbarEnabled()); | |
| 178 SkPaint paint; | 149 SkPaint paint; |
| 179 paint.setColor(kTrackColor); | 150 paint.setColor(kTrackColor); |
| 180 canvas->drawIRect(gfx::RectToSkIRect(rect), paint); | 151 canvas->drawIRect(gfx::RectToSkIRect(rect), paint); |
| 181 } | 152 } |
| 182 | 153 |
| 183 void NativeThemeAura::PaintScrollbarThumb( | 154 void NativeThemeAura::PaintScrollbarThumb( |
| 184 SkCanvas* canvas, | 155 SkCanvas* canvas, |
| 185 Part part, | 156 Part part, |
| 186 State state, | 157 State state, |
| 187 const gfx::Rect& rect, | 158 const gfx::Rect& rect, |
| 188 ScrollbarOverlayColorTheme theme) const { | 159 ScrollbarOverlayColorTheme) const { |
| 189 // Do not paint if state is disabled. | 160 // Do not paint if state is disabled. |
| 190 if (state == kDisabled) | 161 if (state == kDisabled) |
| 191 return; | 162 return; |
| 192 | 163 |
| 193 TRACE_EVENT0("blink", "NativeThemeAura::PaintScrollbarThumb"); | 164 TRACE_EVENT0("blink", "NativeThemeAura::PaintScrollbarThumb"); |
| 194 | 165 |
| 195 gfx::Rect thumb_rect(rect); | 166 gfx::Rect thumb_rect(rect); |
| 196 SkColor thumb_color; | 167 SkColor thumb_color; |
| 197 SkAlpha thumb_alpha = ThumbAlphaForState(state); | 168 SkAlpha thumb_alpha = ThumbAlphaForState(state); |
| 198 | 169 |
| 199 if (IsOverlayScrollbarEnabled()) { | 170 // If there are no scrollbuttons then provide some padding so that the thumb |
| 200 thumb_color = kOverlayScrollbarThumbColor[theme]; | 171 // doesn't touch the top of the track. |
| 172 const int kThumbPadding = 2; |
| 173 const int extra_padding = |
| 174 (scrollbar_button_length() == 0) ? kThumbPadding : 0; |
| 175 if (part == NativeTheme::kScrollbarVerticalThumb) |
| 176 thumb_rect.Inset(kThumbPadding, extra_padding); |
| 177 else |
| 178 thumb_rect.Inset(extra_padding, kThumbPadding); |
| 201 | 179 |
| 202 // In overlay mode, draw a stroke (border). | 180 thumb_color = SK_ColorBLACK; |
| 203 constexpr int kStrokeWidth = kOverlayScrollbarStrokeWidth; | |
| 204 SkPaint paint; | |
| 205 paint.setColor( | |
| 206 SkColorSetA(kOverlayScrollbarStrokeColor[theme], thumb_alpha)); | |
| 207 paint.setStyle(SkPaint::kStroke_Style); | |
| 208 paint.setStrokeWidth(kStrokeWidth); | |
| 209 | |
| 210 gfx::RectF stroke_rect(thumb_rect); | |
| 211 constexpr float kHalfStrokeWidth = kStrokeWidth / 2.f; | |
| 212 stroke_rect.Inset(kHalfStrokeWidth, kHalfStrokeWidth); | |
| 213 canvas->drawRect(gfx::RectFToSkRect(stroke_rect), paint); | |
| 214 | |
| 215 // Inset the all the edges edges so we fill-in the stroke below. | |
| 216 thumb_rect.Inset(kStrokeWidth, kStrokeWidth); | |
| 217 } else { | |
| 218 // If there are no scrollbuttons then provide some padding so that the thumb | |
| 219 // doesn't touch the top of the track. | |
| 220 const int kThumbPadding = 2; | |
| 221 const int extra_padding = | |
| 222 (scrollbar_button_length() == 0) ? kThumbPadding : 0; | |
| 223 if (part == NativeTheme::kScrollbarVerticalThumb) | |
| 224 thumb_rect.Inset(kThumbPadding, extra_padding); | |
| 225 else | |
| 226 thumb_rect.Inset(extra_padding, kThumbPadding); | |
| 227 | |
| 228 thumb_color = SK_ColorBLACK; | |
| 229 } | |
| 230 | 181 |
| 231 SkPaint paint; | 182 SkPaint paint; |
| 232 paint.setColor(SkColorSetA(thumb_color, thumb_alpha)); | 183 paint.setColor(SkColorSetA(thumb_color, thumb_alpha)); |
| 233 canvas->drawIRect(gfx::RectToSkIRect(thumb_rect), paint); | 184 canvas->drawIRect(gfx::RectToSkIRect(thumb_rect), paint); |
| 234 } | 185 } |
| 235 | 186 |
| 236 void NativeThemeAura::PaintScrollbarCorner(SkCanvas* canvas, | 187 void NativeThemeAura::PaintScrollbarCorner(SkCanvas* canvas, |
| 237 State state, | 188 State state, |
| 238 const gfx::Rect& rect) const { | 189 const gfx::Rect& rect) const { |
| 239 // Overlay Scrollbar should never paint a scrollbar corner. | |
| 240 DCHECK(!IsOverlayScrollbarEnabled()); | |
| 241 SkPaint paint; | 190 SkPaint paint; |
| 242 paint.setColor(SkColorSetRGB(0xDC, 0xDC, 0xDC)); | 191 paint.setColor(SkColorSetRGB(0xDC, 0xDC, 0xDC)); |
| 243 canvas->drawIRect(RectToSkIRect(rect), paint); | 192 canvas->drawIRect(RectToSkIRect(rect), paint); |
| 244 } | 193 } |
| 245 | 194 |
| 246 gfx::Size NativeThemeAura::GetPartSize(Part part, | |
| 247 State state, | |
| 248 const ExtraParams& extra) const { | |
| 249 if (IsOverlayScrollbarEnabled()) { | |
| 250 constexpr int minimum_length = | |
| 251 kOverlayScrollbarMinimumLength + 2 * kOverlayScrollbarStrokeWidth; | |
| 252 | |
| 253 // Aura overlay scrollbars need a slight tweak from the base sizes. | |
| 254 switch (part) { | |
| 255 case kScrollbarHorizontalThumb: | |
| 256 return gfx::Size(minimum_length, scrollbar_width_); | |
| 257 case kScrollbarVerticalThumb: | |
| 258 return gfx::Size(scrollbar_width_, minimum_length); | |
| 259 default: | |
| 260 // TODO(bokan): We should probably make sure code using overlay | |
| 261 // scrollbars isn't asking for part sizes that don't exist. This | |
| 262 // currently breaks in Views layout code which indicates they aren't | |
| 263 // overlay aware yet. The Views code should be fixed and either this | |
| 264 // branch return 0 for parts that don't exist or assert NOTREACHED. | |
| 265 // crbug.com/657159. | |
| 266 break; | |
| 267 } | |
| 268 } | |
| 269 | |
| 270 return NativeThemeBase::GetPartSize(part, state, extra); | |
| 271 } | |
| 272 | |
| 273 } // namespace ui | 195 } // namespace ui |
| OLD | NEW |