OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014 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_aurawin.h" | |
6 | |
7 #include "grit/ui_resources.h" | |
8 #include "third_party/skia/include/core/SkCanvas.h" | |
9 #include "ui/base/nine_image_painter_factory.h" | |
10 #include "ui/base/resource/resource_bundle.h" | |
11 #include "ui/gfx/canvas.h" | |
12 #include "ui/gfx/nine_image_painter.h" | |
13 #include "ui/gfx/skia_util.h" | |
14 #include "ui/gfx/win/dpi.h" | |
15 #include "ui/native_theme/common_theme.h" | |
16 #include "ui/native_theme/native_theme_win.h" | |
17 | |
18 using gfx::NineImagePainter; | |
19 | |
20 #define EMPTY_IMAGE_GRID { 0, 0, 0, 0, 0, 0, 0, 0, 0 } | |
21 | |
22 namespace ui { | |
23 | |
24 namespace { | |
25 | |
26 const int kScrollbarThumbImages[NativeTheme::kMaxState][9] = { | |
27 EMPTY_IMAGE_GRID, | |
28 IMAGE_GRID(IDR_SCROLLBAR_THUMB_BASE_HOVER), | |
29 IMAGE_GRID(IDR_SCROLLBAR_THUMB_BASE_NORMAL), | |
30 IMAGE_GRID(IDR_SCROLLBAR_THUMB_BASE_PRESSED) | |
31 }; | |
32 | |
33 const int kScrollbarArrowButtonImages[NativeTheme::kMaxState][9] = { | |
34 EMPTY_IMAGE_GRID, | |
35 IMAGE_GRID(IDR_SCROLLBAR_ARROW_BUTTON_BASE_HOVER), | |
36 IMAGE_GRID(IDR_SCROLLBAR_ARROW_BUTTON_BASE_NORMAL), | |
37 IMAGE_GRID(IDR_SCROLLBAR_ARROW_BUTTON_BASE_PRESSED) | |
38 }; | |
39 | |
40 const int kScrollbarTrackImages[9] = IMAGE_GRID(IDR_SCROLLBAR_BASE); | |
41 | |
42 } // namespace | |
43 | |
44 // static | |
45 NativeTheme* NativeTheme::instance() { | |
46 return NativeThemeAuraWin::instance(); | |
47 } | |
48 | |
49 // static | |
50 NativeThemeAura* NativeThemeAura::instance() { | |
51 return NativeThemeAuraWin::instance(); | |
52 } | |
53 | |
54 // static | |
55 NativeThemeAuraWin* NativeThemeAuraWin::instance() { | |
56 CR_DEFINE_STATIC_LOCAL(NativeThemeAuraWin, s_native_theme, ()); | |
57 return &s_native_theme; | |
58 } | |
59 | |
60 NativeThemeAuraWin::NativeThemeAuraWin() { | |
61 // Image declarations assume the following order. | |
62 COMPILE_ASSERT(kDisabled == 0, states_unexepctedly_changed); | |
63 COMPILE_ASSERT(kHovered == 1, states_unexepctedly_changed); | |
64 COMPILE_ASSERT(kNormal == 2, states_unexepctedly_changed); | |
65 COMPILE_ASSERT(kPressed == 3, states_unexepctedly_changed); | |
66 COMPILE_ASSERT(kMaxState == 4, states_unexepctedly_changed); | |
67 } | |
68 | |
69 NativeThemeAuraWin::~NativeThemeAuraWin() { | |
70 } | |
71 | |
72 gfx::Size NativeThemeAuraWin::GetPartSize(Part part, | |
73 State state, | |
74 const ExtraParams& extra) const { | |
75 gfx::Size part_size = CommonThemeGetPartSize(part, state, extra); | |
76 if (!part_size.IsEmpty()) | |
77 return part_size; | |
78 | |
79 // We want aura on windows to use the same size for scrollbars as we would in | |
80 // the native theme. | |
81 switch (part) { | |
82 case kScrollbarDownArrow: | |
83 case kScrollbarLeftArrow: | |
84 case kScrollbarRightArrow: | |
85 case kScrollbarUpArrow: | |
86 case kScrollbarHorizontalThumb: | |
87 case kScrollbarVerticalThumb: | |
88 case kScrollbarHorizontalTrack: | |
89 case kScrollbarVerticalTrack: | |
90 return NativeThemeWin::instance()->GetPartSize(part, state, extra); | |
91 default: | |
92 break; | |
93 } | |
94 | |
95 return NativeThemeAura::GetPartSize(part, state, extra); | |
96 } | |
97 | |
98 void NativeThemeAuraWin::PaintArrowButton( | |
99 SkCanvas* gc, | |
100 const gfx::Rect& rect, | |
101 Part direction, | |
102 State state) const { | |
103 if (direction == kInnerSpinButton) { | |
104 NativeThemeAura::PaintArrowButton(gc, rect, direction, state); | |
105 return; | |
106 } | |
107 PaintPainter(GetOrCreatePainter( | |
108 kScrollbarArrowButtonImages, state, | |
109 scrollbar_arrow_button_painters_), | |
110 gc, rect); | |
111 | |
112 // The arrow is slightly offset to better balance with the thumb. | |
113 gfx::Rect arrow_rect(rect); | |
114 if (direction == kScrollbarDownArrow || direction == kScrollbarUpArrow) | |
115 arrow_rect.Offset(1, 0); | |
116 else | |
117 arrow_rect.Offset(0, 1); | |
118 | |
119 // Aura-win uses slightly different arrow colors. | |
120 SkColor arrow_color = GetArrowColor(state); | |
121 switch (state) { | |
122 case kHovered: | |
123 case kNormal: | |
124 arrow_color = SkColorSetRGB(0x50, 0x50, 0x50); | |
125 break; | |
126 case kPressed: | |
127 arrow_color = SK_ColorWHITE; | |
128 default: | |
129 break; | |
130 } | |
131 PaintArrow(gc, arrow_rect, direction, arrow_color); | |
132 } | |
133 | |
134 void NativeThemeAuraWin::PaintScrollbarTrack( | |
135 SkCanvas* sk_canvas, | |
136 Part part, | |
137 State state, | |
138 const ScrollbarTrackExtraParams& extra_params, | |
139 const gfx::Rect& rect) const { | |
140 if (!scrollbar_track_painter_) | |
141 scrollbar_track_painter_ = CreateNineImagePainter(kScrollbarTrackImages); | |
142 PaintPainter(scrollbar_track_painter_.get(), sk_canvas, rect); | |
143 } | |
144 | |
145 void NativeThemeAuraWin::PaintScrollbarThumb(SkCanvas* sk_canvas, | |
146 Part part, | |
147 State state, | |
148 const gfx::Rect& rect) const { | |
149 gfx::Rect thumb_rect(rect); | |
150 if (part == NativeTheme::kScrollbarVerticalThumb) | |
151 thumb_rect.Inset(3, 0, 1, 0); | |
152 else | |
153 thumb_rect.Inset(0, 3, 0, 1); | |
154 PaintPainter(GetOrCreatePainter( | |
155 kScrollbarThumbImages, state, scrollbar_thumb_painters_), | |
156 sk_canvas, thumb_rect); | |
157 } | |
158 | |
159 NineImagePainter* NativeThemeAuraWin::GetOrCreatePainter( | |
160 const int images[kMaxState][9], | |
161 State state, | |
162 scoped_ptr<NineImagePainter> painters[kMaxState]) const { | |
163 if (painters[state]) | |
164 return painters[state].get(); | |
165 if (images[state][0] == 0) { | |
166 // Must always provide normal state images. | |
167 DCHECK_NE(kNormal, state); | |
168 return GetOrCreatePainter(images, kNormal, painters); | |
169 } | |
170 painters[state] = CreateNineImagePainter(images[state]); | |
171 return painters[state].get(); | |
172 } | |
173 | |
174 void NativeThemeAuraWin::PaintPainter(NineImagePainter* painter, | |
175 SkCanvas* sk_canvas, | |
176 const gfx::Rect& rect) const { | |
177 DCHECK(painter); | |
178 scoped_ptr<gfx::Canvas> canvas(CreateCanvas(sk_canvas)); | |
179 painter->Paint(canvas.get(), rect); | |
180 } | |
181 | |
182 } // namespace ui | |
OLD | NEW |