-
-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy patheraser_modal.dart
More file actions
289 lines (268 loc) · 8.67 KB
/
Copy patheraser_modal.dart
File metadata and controls
289 lines (268 loc) · 8.67 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
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:saber/data/extensions/axis_extensions.dart';
import 'package:saber/data/prefs.dart';
import 'package:saber/data/tools/eraser.dart';
import 'package:saber/i18n/strings.g.dart';
class EraserModal extends StatefulWidget {
const EraserModal({super.key, required this.getTool});
final Eraser Function() getTool;
@override
State<EraserModal> createState() => _EraserModalState();
}
class _EraserModalState extends State<EraserModal> {
@override
Widget build(BuildContext context) {
final eraser = widget.getTool();
final axis = stows.editorToolbarAlignment.value.axis.opposite;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
child: Flex(
direction: axis,
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
_EraserSizePicker(
eraser: eraser,
axis: axis,
onDrag: (percent) {
setState(() {
percent = percent.clamp(0.0, 1.0);
final stepsFromMin =
(percent * eraser.sizeStepsBetweenMinAndMax).round();
final newSize = eraser.sizeMin + stepsFromMin * eraser.sizeStep;
eraser.size = newSize;
});
},
),
SizedBox(
width: axis == Axis.horizontal ? 8 : 0,
height: axis == Axis.vertical ? 8 : 0,
),
Flex(
direction: axis,
mainAxisAlignment: MainAxisAlignment.center,
children: [
_EraserModeButton(
selected: !eraser.areaEraser,
tooltip: t.editor.eraserOptions.strokeEraser,
onPressed: () {
setState(() {
eraser.areaEraser = false;
});
},
icon: SvgPicture.asset(
'assets/images/eraser-stroke.svg',
width: 32,
height: 32,
theme: SvgTheme(
currentColor: !eraser.areaEraser
? ColorScheme.of(context).secondary
: ColorScheme.of(context).onSurface,
),
),
),
SizedBox(
width: axis == Axis.horizontal ? 8 : 0,
height: axis == Axis.vertical ? 8 : 0,
),
_EraserModeButton(
selected: eraser.areaEraser,
tooltip: t.editor.eraserOptions.areaEraser,
onPressed: () {
setState(() {
eraser.areaEraser = true;
});
},
icon: SvgPicture.asset(
'assets/images/eraser-area.svg',
width: 32,
height: 32,
theme: SvgTheme(
currentColor: eraser.areaEraser
? ColorScheme.of(context).secondary
: ColorScheme.of(context).onSurface,
),
),
),
],
),
],
),
);
}
}
class _EraserSizePicker extends StatelessWidget {
const _EraserSizePicker({
required this.axis,
required this.eraser,
required this.onDrag,
});
final Axis axis;
final Eraser eraser;
final ValueChanged<double> onDrag;
@override
Widget build(BuildContext context) {
final colorScheme = ColorScheme.of(context);
return Flex(
direction: axis,
mainAxisSize: MainAxisSize.min,
children: [
Column(
children: [
Text(
t.editor.penOptions.size,
style: TextStyle(
color: colorScheme.onSurface.withValues(alpha: 0.8),
fontSize: 10,
height: 1,
),
),
Text(eraser.size.round().toString()),
],
),
const SizedBox(width: 8),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: _EraserSizeSlider(axis: axis, eraser: eraser, onDrag: onDrag),
),
],
);
}
}
class _EraserSizeSlider extends StatelessWidget {
const _EraserSizeSlider({
required this.axis,
required this.eraser,
required this.onDrag,
});
final Axis axis;
final Eraser eraser;
final ValueChanged<double> onDrag;
@override
Widget build(BuildContext context) {
final colorScheme = ColorScheme.of(context);
return GestureDetector(
// Map drag gestures to a normalized 0..1 percent value used by the
// parent. For horizontal layout we use the local dx; for vertical we
// use local dy. The fixed divisor (150) approximates the slider pixel
// range used by the CustomPaint size.
onHorizontalDragStart: axis == Axis.horizontal
? (details) => onDrag(details.localPosition.dx / 150)
: null,
onHorizontalDragUpdate: axis == Axis.horizontal
? (details) => onDrag(details.localPosition.dx / 150)
: null,
onVerticalDragStart: axis == Axis.vertical
? (details) => onDrag(details.localPosition.dy / 150)
: null,
onVerticalDragUpdate: axis == Axis.vertical
? (details) => onDrag(details.localPosition.dy / 150)
: null,
child: RotatedBox(
quarterTurns: axis == Axis.horizontal ? 0 : 1,
child: CustomPaint(
size: const Size(150, 25),
painter: _EraserSizeSliderPainter(
axis: axis,
minSize: eraser.sizeMin,
maxSize: eraser.sizeMax,
currentSize: eraser.size,
trackColor: colorScheme.onSurface.withValues(alpha: 0.2),
thumbColor: colorScheme.primary,
),
),
),
);
}
}
class _EraserModeButton extends StatelessWidget {
const _EraserModeButton({
required this.selected,
required this.icon,
required this.tooltip,
required this.onPressed,
});
final bool selected;
final Widget icon;
final String tooltip;
final VoidCallback onPressed;
@override
Widget build(BuildContext context) {
final colorScheme = ColorScheme.of(context);
final iconColor = selected ? colorScheme.secondary : colorScheme.onSurface;
return IconButton(
onPressed: onPressed,
style: TextButton.styleFrom(
foregroundColor: iconColor,
backgroundColor: selected
? colorScheme.secondary.withAlpha((0.1 * 255).round())
: Colors.transparent,
shape: const CircleBorder(),
),
tooltip: tooltip,
icon: icon,
);
}
}
class _EraserSizeSliderPainter extends CustomPainter {
_EraserSizeSliderPainter({
required this.axis,
required this.minSize,
required this.maxSize,
required this.currentSize,
required this.trackColor,
required this.thumbColor,
});
final Axis axis;
final double minSize;
final double maxSize;
final double currentSize;
final Color trackColor;
final Color thumbColor;
@override
void paint(Canvas canvas, Size size) {
final leftHeight = size.height * minSize / maxSize;
final topLeft = Offset(0, (size.height - leftHeight) / 2);
final bottomLeft = Offset(0, topLeft.dy + leftHeight);
final topRight = Offset(size.width, 0);
final bottomRight = Offset(size.width, size.height);
canvas.drawPath(
Path()
..moveTo(topLeft.dx, topLeft.dy)
..lineTo(bottomLeft.dx, bottomLeft.dy)
..lineTo(bottomRight.dx, bottomRight.dy)
..lineTo(topRight.dx, topRight.dy)
..close(),
Paint()
..color = trackColor
..style = PaintingStyle.fill,
);
final ratio = (currentSize - minSize) / (maxSize - minSize);
final thumbHeight = size.height * ratio;
final thumbRight = size.width * ratio;
final thumbTop = (size.height - thumbHeight) / 2;
canvas.drawPath(
Path()
..moveTo(topLeft.dx, topLeft.dy)
..lineTo(bottomLeft.dx, bottomLeft.dy)
..lineTo(thumbRight, thumbTop + thumbHeight)
..lineTo(thumbRight, thumbTop)
..close(),
Paint()
..color = thumbColor
..style = PaintingStyle.fill,
);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return oldDelegate is! _EraserSizeSliderPainter ||
oldDelegate.axis != axis ||
oldDelegate.minSize != minSize ||
oldDelegate.maxSize != maxSize ||
oldDelegate.currentSize != currentSize ||
oldDelegate.trackColor != trackColor ||
oldDelegate.thumbColor != thumbColor;
}
}