Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(290)

Side by Side Diff: ui/views/controls/menu/menu_image_util.cc

Issue 10702136: Support high dpi in menu scroll arrow, submenu arrow and drag_utils. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/views/controls/menu/menu_image_util.h" 5 #include "ui/views/controls/menu/menu_image_util.h"
6 6
7 #include "base/i18n/rtl.h" 7 #include "base/i18n/rtl.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "grit/ui_resources.h" 9 #include "grit/ui_resources.h"
10 #include "third_party/skia/include/effects/SkGradientShader.h" 10 #include "third_party/skia/include/effects/SkGradientShader.h"
11 #include "ui/base/resource/resource_bundle.h" 11 #include "ui/base/resource/resource_bundle.h"
12 #include "ui/gfx/canvas.h" 12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/image/canvas_image_source.h"
13 #include "ui/gfx/image/image.h" 14 #include "ui/gfx/image/image.h"
14 #include "ui/gfx/image/image_skia.h" 15 #include "ui/gfx/image/image_skia.h"
15 #include "ui/gfx/image/image_skia_source.h" 16 #include "ui/gfx/image/image_skia_source.h"
16 #include "ui/gfx/point.h" 17 #include "ui/gfx/point.h"
17 #include "ui/gfx/size.h" 18 #include "ui/gfx/size.h"
18 19
19 namespace { 20 namespace {
20 21
21 // Size of the radio button inciator. 22 // Size of the radio button inciator.
22 const int kSelectedIndicatorSize = 5; 23 const int kSelectedIndicatorSize = 5;
23 const int kIndicatorSize = 10; 24 const int kIndicatorSize = 10;
24 25
25 // Used for the radio indicator. See theme_draw for details. 26 // Used for the radio indicator. See theme_draw for details.
26 const double kGradientStop = .5; 27 const double kGradientStop = .5;
27 const SkColor kGradient0 = SkColorSetRGB(255, 255, 255); 28 const SkColor kGradient0 = SkColorSetRGB(255, 255, 255);
28 const SkColor kGradient1 = SkColorSetRGB(255, 255, 255); 29 const SkColor kGradient1 = SkColorSetRGB(255, 255, 255);
29 const SkColor kGradient2 = SkColorSetRGB(0xD8, 0xD8, 0xD8); 30 const SkColor kGradient2 = SkColorSetRGB(0xD8, 0xD8, 0xD8);
30 const SkColor kBaseStroke = SkColorSetRGB(0x8F, 0x8F, 0x8F); 31 const SkColor kBaseStroke = SkColorSetRGB(0x8F, 0x8F, 0x8F);
31 const SkColor kRadioButtonIndicatorGradient0 = SkColorSetRGB(0, 0, 0); 32 const SkColor kRadioButtonIndicatorGradient0 = SkColorSetRGB(0, 0, 0);
32 const SkColor kRadioButtonIndicatorGradient1 = SkColorSetRGB(0x83, 0x83, 0x83); 33 const SkColor kRadioButtonIndicatorGradient1 = SkColorSetRGB(0x83, 0x83, 0x83);
33 const SkColor kIndicatorStroke = SkColorSetRGB(0, 0, 0); 34 const SkColor kIndicatorStroke = SkColorSetRGB(0, 0, 0);
34 35
35 class RadioButtonImageSource : public gfx::ImageSkiaSource { 36 class RadioButtonImageSource : public gfx::CanvasImageSource {
36 public: 37 public:
37 explicit RadioButtonImageSource(bool selected) : selected_(selected) { 38 explicit RadioButtonImageSource(bool selected)
39 : CanvasImageSource(gfx::Size(kIndicatorSize + 2, kIndicatorSize + 2),
40 false),
41 selected_(selected) {
38 } 42 }
39 virtual ~RadioButtonImageSource() {} 43 virtual ~RadioButtonImageSource() {}
40 44
41 virtual gfx::ImageSkiaRep GetImageForScale( 45 virtual void Draw(gfx::Canvas* canvas) OVERRIDE {
42 ui::ScaleFactor scale_factor) OVERRIDE { 46 canvas->Translate(gfx::Point(1, 1));
43 float scale = GetScaleFactorScale(scale_factor);
44 // + 2 (1px on each side) to cover rounding error.
45 gfx::Size size(kIndicatorSize + 2, kIndicatorSize + 2);
46 gfx::Canvas canvas(size.Scale(scale), false);
47 canvas.Scale(scale, scale);
48 canvas.Translate(gfx::Point(1, 1));
49 47
50 SkPoint gradient_points[3]; 48 SkPoint gradient_points[3];
51 gradient_points[0].iset(0, 0); 49 gradient_points[0].iset(0, 0);
52 gradient_points[1].iset(0, 50 gradient_points[1].iset(0,
53 static_cast<int>(kIndicatorSize * kGradientStop)); 51 static_cast<int>(kIndicatorSize * kGradientStop));
54 gradient_points[2].iset(0, kIndicatorSize); 52 gradient_points[2].iset(0, kIndicatorSize);
55 SkColor gradient_colors[3] = { kGradient0, kGradient1, kGradient2 }; 53 SkColor gradient_colors[3] = { kGradient0, kGradient1, kGradient2 };
56 SkShader* shader = SkGradientShader::CreateLinear( 54 SkShader* shader = SkGradientShader::CreateLinear(
57 gradient_points, gradient_colors, NULL, arraysize(gradient_points), 55 gradient_points, gradient_colors, NULL, arraysize(gradient_points),
58 SkShader::kClamp_TileMode, NULL); 56 SkShader::kClamp_TileMode, NULL);
59 SkPaint paint; 57 SkPaint paint;
60 paint.setStyle(SkPaint::kFill_Style); 58 paint.setStyle(SkPaint::kFill_Style);
61 paint.setAntiAlias(true); 59 paint.setAntiAlias(true);
62 paint.setShader(shader); 60 paint.setShader(shader);
63 shader->unref(); 61 shader->unref();
64 int radius = kIndicatorSize / 2; 62 int radius = kIndicatorSize / 2;
65 canvas.sk_canvas()->drawCircle(radius, radius, radius, paint); 63 canvas->sk_canvas()->drawCircle(radius, radius, radius, paint);
66 paint.setStrokeWidth(SkIntToScalar(0)); 64 paint.setStrokeWidth(SkIntToScalar(0));
67 paint.setShader(NULL); 65 paint.setShader(NULL);
68 paint.setStyle(SkPaint::kStroke_Style); 66 paint.setStyle(SkPaint::kStroke_Style);
69 paint.setColor(kBaseStroke); 67 paint.setColor(kBaseStroke);
70 canvas.sk_canvas()->drawCircle(radius, radius, radius, paint); 68 canvas->sk_canvas()->drawCircle(radius, radius, radius, paint);
71 69
72 if (selected_) { 70 if (selected_) {
73 SkPoint selected_gradient_points[2]; 71 SkPoint selected_gradient_points[2];
74 selected_gradient_points[0].iset(0, 0); 72 selected_gradient_points[0].iset(0, 0);
75 selected_gradient_points[1].iset(0, kSelectedIndicatorSize); 73 selected_gradient_points[1].iset(0, kSelectedIndicatorSize);
76 SkColor selected_gradient_colors[2] = { kRadioButtonIndicatorGradient0, 74 SkColor selected_gradient_colors[2] = { kRadioButtonIndicatorGradient0,
77 kRadioButtonIndicatorGradient1 }; 75 kRadioButtonIndicatorGradient1 };
78 shader = SkGradientShader::CreateLinear( 76 shader = SkGradientShader::CreateLinear(
79 selected_gradient_points, selected_gradient_colors, NULL, 77 selected_gradient_points, selected_gradient_colors, NULL,
80 arraysize(selected_gradient_points), SkShader::kClamp_TileMode, NULL); 78 arraysize(selected_gradient_points), SkShader::kClamp_TileMode, NULL);
81 paint.setShader(shader); 79 paint.setShader(shader);
82 shader->unref(); 80 shader->unref();
83 paint.setStyle(SkPaint::kFill_Style); 81 paint.setStyle(SkPaint::kFill_Style);
84 canvas.sk_canvas()->drawCircle(radius, radius, 82 canvas->sk_canvas()->drawCircle(radius, radius,
85 kSelectedIndicatorSize / 2, paint); 83 kSelectedIndicatorSize / 2, paint);
86 84
87 paint.setStrokeWidth(SkIntToScalar(0)); 85 paint.setStrokeWidth(SkIntToScalar(0));
88 paint.setShader(NULL); 86 paint.setShader(NULL);
89 paint.setStyle(SkPaint::kStroke_Style); 87 paint.setStyle(SkPaint::kStroke_Style);
90 paint.setColor(kIndicatorStroke); 88 paint.setColor(kIndicatorStroke);
91 canvas.sk_canvas()->drawCircle(radius, radius, 89 canvas->sk_canvas()->drawCircle(radius, radius,
92 kSelectedIndicatorSize / 2, paint); 90 kSelectedIndicatorSize / 2, paint);
93 } 91 }
94 LOG(ERROR) << "Generating:" << selected_;
95 return gfx::ImageSkiaRep(canvas.ExtractBitmap(), scale_factor);
96 } 92 }
97 93
98 private: 94 private:
99 bool selected_; 95 bool selected_;
100 96
101 DISALLOW_COPY_AND_ASSIGN(RadioButtonImageSource); 97 DISALLOW_COPY_AND_ASSIGN(RadioButtonImageSource);
102 }; 98 };
103 99
104 gfx::ImageSkia* CreateRadioButtonImage(bool selected) { 100 gfx::ImageSkia* CreateRadioButtonImage(bool selected) {
105 return new gfx::ImageSkia(new RadioButtonImageSource(selected), 101 RadioButtonImageSource* source = new RadioButtonImageSource(selected);
106 gfx::Size(kIndicatorSize + 2, kIndicatorSize + 2)); 102 return new gfx::ImageSkia(source, source->size());
107 } 103 }
108 104
105 class SubmenuArrowImageSource : public gfx::CanvasImageSource {
106 public:
107 SubmenuArrowImageSource()
108 : gfx::CanvasImageSource(GetSubmenuArrowSize(), false) {
109 }
110 virtual ~SubmenuArrowImageSource() {}
111
112 virtual void Draw(gfx::Canvas* canvas) OVERRIDE {
113 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
114 const gfx::ImageSkia* r = rb.GetImageNamed(IDR_MENU_ARROW).ToImageSkia();
115 canvas->Scale(-1, 1);
116 canvas->DrawImageInt(*r, - r->width(), 0);
117 }
118
119 private:
120 static gfx::Size GetSubmenuArrowSize() {
121 return ui::ResourceBundle::GetSharedInstance()
122 .GetImageNamed(IDR_MENU_ARROW).ToImageSkia()->size();
123 }
124
125 DISALLOW_COPY_AND_ASSIGN(SubmenuArrowImageSource);
126 };
127
109 gfx::ImageSkia* GetRtlSubmenuArrowImage() { 128 gfx::ImageSkia* GetRtlSubmenuArrowImage() {
110 static gfx::ImageSkia* kRtlArrow = NULL; 129 static gfx::ImageSkia* kRtlArrow = NULL;
111 if (!kRtlArrow) { 130 if (!kRtlArrow) {
112 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 131 SubmenuArrowImageSource* source = new SubmenuArrowImageSource();
113 const gfx::ImageSkia* r = rb.GetImageNamed(IDR_MENU_ARROW).ToImageSkia(); 132 kRtlArrow = new gfx::ImageSkia(source, source->size());
114 gfx::Canvas canvas(gfx::Size(r->width(), r->height()), false);
115 canvas.Scale(-1, 1);
116 canvas.DrawImageInt(*r, - r->width(), 0);
117 kRtlArrow = new gfx::ImageSkia(canvas.ExtractBitmap());
118 } 133 }
119 return kRtlArrow; 134 return kRtlArrow;
120 } 135 }
121 136
122 } // namespace 137 } // namespace
123 138
124 namespace views { 139 namespace views {
125 140
126 const gfx::ImageSkia* GetRadioButtonImage(bool selected) { 141 const gfx::ImageSkia* GetRadioButtonImage(bool selected) {
127 static const gfx::ImageSkia* kRadioOn = CreateRadioButtonImage(true); 142 static const gfx::ImageSkia* kRadioOn = CreateRadioButtonImage(true);
128 static const gfx::ImageSkia* kRadioOff = CreateRadioButtonImage(false); 143 static const gfx::ImageSkia* kRadioOff = CreateRadioButtonImage(false);
129 144
130 return selected ? kRadioOn : kRadioOff; 145 return selected ? kRadioOn : kRadioOff;
131 } 146 }
132 147
133 const gfx::ImageSkia* GetSubmenuArrowImage() { 148 const gfx::ImageSkia* GetSubmenuArrowImage() {
134 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 149 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
135 return base::i18n::IsRTL() ? GetRtlSubmenuArrowImage() 150 return base::i18n::IsRTL() ? GetRtlSubmenuArrowImage()
136 : rb.GetImageNamed(IDR_MENU_ARROW).ToImageSkia(); 151 : rb.GetImageNamed(IDR_MENU_ARROW).ToImageSkia();
137 } 152 }
138 153
139 } // namespace views 154 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698