forked from flutter/devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicons.dart
More file actions
292 lines (254 loc) · 7.52 KB
/
icons.dart
File metadata and controls
292 lines (254 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright 2017 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd.
/// Platform independent definition of icons.
///
/// If you add an Icon class you also need to add a renderer class to handle the
/// actual platform specific icon rendering.
/// The benefit of this approach is that icons can be const objects and tests
/// of code that uses icons can run on the Dart VM.
library;
import 'package:devtools_app_shared/ui.dart';
import 'package:flutter/material.dart';
import '../../screens/inspector/layout_explorer/ui/widgets_theme.dart';
import 'colors.dart';
class CustomIcon extends StatelessWidget {
const CustomIcon({
super.key,
required this.kind,
required this.text,
this.isAbstract = false,
});
final IconKind kind;
final String text;
final bool isAbstract;
AssetImageIcon get baseIcon => kind.icon;
@override
Widget build(BuildContext context) {
return SizedBox(
width: baseIcon.width,
height: baseIcon.height,
child: Stack(
alignment: AlignmentDirectional.center,
children: <Widget>[
baseIcon,
Text(
text,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: scaleByFontFactor(9.0),
color: const Color(0xFF231F20),
),
),
],
),
);
}
}
/// An icon with one character
class CircleIcon extends StatelessWidget {
const CircleIcon({
super.key,
required this.text,
required this.color,
this.textColor = const Color(0xFF231F20),
});
/// Text to display. Should be one character.
final String text;
/// Background circle color.
final Color color;
/// Background circle color.
final Color textColor;
@override
Widget build(BuildContext context) {
return Container(
width: defaultIconSize,
height: defaultIconSize,
decoration: BoxDecoration(shape: BoxShape.circle, color: color),
alignment: Alignment.center,
child: Text(
text,
textAlign: TextAlign.center,
style: TextStyle(fontSize: scaleByFontFactor(9.0), color: textColor),
),
);
}
}
class CustomIconMaker {
final iconCache = <String, Widget>{};
Widget? getCustomIcon(
String fromText, {
IconKind? kind,
bool isAbstract = false,
}) {
final theKind = kind ?? IconKind.classIcon;
if (fromText.isEmpty) {
return null;
}
final text = fromText[0].toUpperCase();
final mapKey = '${text}_${theKind.name}_$isAbstract';
return iconCache.putIfAbsent(mapKey, () {
return CustomIcon(kind: theKind, text: text, isAbstract: isAbstract);
});
}
Widget? fromWidgetName(String? name) {
if (name == null) {
return null;
}
while (name!.isNotEmpty && !isAlphabetic(name.codeUnitAt(0))) {
name = name.substring(1);
}
if (name.isEmpty) {
return null;
}
final widgetTheme = WidgetTheme.fromName(name);
final icon = widgetTheme.iconAsset;
if (icon != null) {
return iconCache.putIfAbsent(name, () {
return AssetImageIcon(asset: icon);
});
}
final text = name[0].toUpperCase();
return iconCache.putIfAbsent(name, () {
return CircleIcon(text: text, color: widgetTheme.color);
});
}
CustomIcon fromInfo(String name) {
return getCustomIcon(name, kind: IconKind.info) as CustomIcon;
}
bool isAlphabetic(int char) {
return (char < '0'.codeUnitAt(0) || char > '9'.codeUnitAt(0)) &&
char != '_'.codeUnitAt(0) &&
char != r'$'.codeUnitAt(0);
}
}
class IconKind {
const IconKind(this.name, this.icon, [AssetImageIcon? abstractIcon])
: abstractIcon = abstractIcon ?? icon;
static IconKind classIcon = const IconKind(
'class',
AssetImageIcon(asset: 'icons/custom/class.png'),
AssetImageIcon(asset: 'icons/custom/class_abstract.png'),
);
static IconKind field = const IconKind(
'fields',
AssetImageIcon(asset: 'icons/custom/fields.png'),
);
static IconKind interface = const IconKind(
'interface',
AssetImageIcon(asset: 'icons/custom/interface.png'),
);
static IconKind method = const IconKind(
'method',
AssetImageIcon(asset: 'icons/custom/method.png'),
AssetImageIcon(asset: 'icons/custom/method_abstract.png'),
);
static IconKind property = const IconKind(
'property',
AssetImageIcon(asset: 'icons/custom/property.png'),
);
static IconKind info = const IconKind(
'info',
AssetImageIcon(asset: 'icons/custom/info.png'),
);
final String name;
final AssetImageIcon icon;
final AssetImageIcon abstractIcon;
}
class ColorIcon extends StatelessWidget {
const ColorIcon(this.color, {super.key});
final Color color;
@override
Widget build(BuildContext context) {
final colorScheme = Theme.of(context).colorScheme;
return CustomPaint(
painter: _ColorIconPainter(color, colorScheme),
size: Size(defaultIconSize, defaultIconSize),
);
}
}
class ColorIconMaker {
final iconCache = <Color, ColorIcon>{};
ColorIcon getCustomIcon(Color color) {
return iconCache.putIfAbsent(color, () => ColorIcon(color));
}
}
class _ColorIconPainter extends CustomPainter {
const _ColorIconPainter(this.color, this.colorScheme);
final Color color;
final ColorScheme colorScheme;
static const iconMargin = 1.0;
@override
void paint(Canvas canvas, Size size) {
// draw a black and gray grid to use as the background to disambiguate
// opaque colors from translucent colors.
final greyPaint = Paint()..color = colorScheme.grey;
final iconRect = Rect.fromLTRB(
iconMargin,
iconMargin,
size.width - iconMargin,
size.height - iconMargin,
);
canvas
..drawRect(
Rect.fromLTRB(
iconMargin,
iconMargin,
size.width - iconMargin,
size.height - iconMargin,
),
Paint()..color = colorScheme.surface,
)
..drawRect(
Rect.fromLTRB(
iconMargin,
iconMargin,
size.width * 0.5,
size.height * 0.5,
),
greyPaint,
)
..drawRect(
Rect.fromLTRB(
size.width * 0.5,
size.height * 0.5,
size.width - iconMargin,
size.height - iconMargin,
),
greyPaint,
)
..drawRect(iconRect, Paint()..color = color)
..drawRect(
iconRect,
Paint()
..style = PaintingStyle.stroke
..color = colorScheme.onPrimary,
);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
if (oldDelegate is _ColorIconPainter) {
return oldDelegate.colorScheme.isLight != colorScheme.isLight;
}
return true;
}
}
class FlutterMaterialIcons {
FlutterMaterialIcons._();
static Icon getIconForCodePoint(int charCode, ColorScheme colorScheme) {
return Icon(
IconData(charCode, fontFamily: 'MaterialIcons'),
color: colorScheme.onSurface,
);
}
}
class Octicons {
static const bug = IconData(61714, fontFamily: 'Octicons');
static const info = IconData(61778, fontFamily: 'Octicons');
static const deviceMobile = IconData(61739, fontFamily: 'Octicons');
static const fileZip = IconData(61757, fontFamily: 'Octicons');
static const clippy = IconData(61724, fontFamily: 'Octicons');
static const package = IconData(61812, fontFamily: 'Octicons');
static const dashboard = IconData(61733, fontFamily: 'Octicons');
static const pulse = IconData(61823, fontFamily: 'Octicons');
}