Skip to content

Commit

Permalink
ref: reformat everything with dart format .
Browse files Browse the repository at this point in the history
  • Loading branch information
adil192 committed Dec 11, 2023
1 parent 6d23bf7 commit 7338b40
Show file tree
Hide file tree
Showing 134 changed files with 3,613 additions and 2,738 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"files.exclude": {
"submodules/": true
},
"editor.formatOnSave": false
"editor.formatOnSave": true
}
2 changes: 2 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,7 @@ linter:

use_string_buffers: true

curly_braces_in_flow_control_structures: false

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
10 changes: 6 additions & 4 deletions lib/components/canvas/_asset_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@ import 'package:flutter/painting.dart';
import 'package:saber/components/canvas/image/editor_image.dart';

/// A cache for assets that are loaded from disk.
///
///
/// This is the analogue to Flutter's image cache,
/// but for non-image assets.
///
///
/// There should be one instance of this class per
/// [EditorCoreInfo] instance.
class AssetCache {
AssetCache();

/// Maps a file to its value.
final Map<File, Object> _cache = {};

/// Maps a file to the visible images that use it.
final Map<File, Set<EditorImage>> _images = {};

/// Marks [image] as currently visible.
///
/// It's safe to call this method multiple times.
///
///
/// [file] is allowed to be null for convenience,
/// in which case this function does nothing.
void addImage<T extends Object>(EditorImage image, File? file, T value) {
Expand Down Expand Up @@ -98,7 +99,8 @@ class OrderedAssetCache {
} else if (item is FileImage) {
return item.file.readAsBytes();
} else {
throw Exception('OrderedAssetCache.getBytes: unknown type ${item.runtimeType}');
throw Exception(
'OrderedAssetCache.getBytes: unknown type ${item.runtimeType}');
}
}
}
37 changes: 24 additions & 13 deletions lib/components/canvas/_canvas_background_painter.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:saber/data/extensions/color_extensions.dart';
Expand All @@ -17,11 +16,14 @@ class CanvasBackgroundPainter extends CustomPainter {

final bool invert;
final Color backgroundColor;

/// The pattern to use for the background. See [CanvasBackgroundPatterns].
final CanvasBackgroundPattern backgroundPattern;

/// The height between each line in the background pattern
final int lineHeight;
final Color primaryColor, secondaryColor;

/// Whether to draw the background pattern in a preview mode (more opaque).
final bool preview;

Expand Down Expand Up @@ -54,13 +56,14 @@ class CanvasBackgroundPainter extends CustomPainter {
}

@override
bool shouldRepaint(CanvasBackgroundPainter oldDelegate) => kDebugMode
|| oldDelegate.invert != invert
|| oldDelegate.backgroundColor != backgroundColor
|| oldDelegate.backgroundPattern != backgroundPattern
|| oldDelegate.lineHeight != lineHeight
|| oldDelegate.primaryColor != primaryColor
|| oldDelegate.secondaryColor != secondaryColor;
bool shouldRepaint(CanvasBackgroundPainter oldDelegate) =>
kDebugMode ||
oldDelegate.invert != invert ||
oldDelegate.backgroundColor != backgroundColor ||
oldDelegate.backgroundPattern != backgroundPattern ||
oldDelegate.lineHeight != lineHeight ||
oldDelegate.primaryColor != primaryColor ||
oldDelegate.secondaryColor != secondaryColor;

static Iterable<PatternElement> getPatternElements({
required CanvasBackgroundPattern pattern,
Expand Down Expand Up @@ -130,8 +133,8 @@ class CanvasBackgroundPainter extends CustomPainter {
final staffSpacing = lineHeight * 3;

for (double topOfStaff = staffSpacing.toDouble() - lineHeight;
topOfStaff + staffHeight < size.height;
topOfStaff += staffHeight + staffSpacing) {
topOfStaff + staffHeight < size.height;
topOfStaff += staffHeight + staffSpacing) {
// horizontal lines
for (int line = 0; line < staffSpaces + 1; line++) {
yield PatternElement(
Expand Down Expand Up @@ -185,17 +188,23 @@ class CanvasBackgroundPainter extends CustomPainter {
}
}
}

}

class PatternElement {
final Offset start, end;

/// Whether this is a line or a dot
final bool isLine;

/// Whether this should use a secondary color
final bool secondaryColor;

PatternElement(this.start, this.end, {this.isLine = true, this.secondaryColor = false});
PatternElement(
this.start,
this.end, {
this.isLine = true,
this.secondaryColor = false,
});
}

enum CanvasBackgroundPattern {
Expand All @@ -205,6 +214,7 @@ enum CanvasBackgroundPattern {
/// College ruled paper (ltr): horizontal lines with one
/// vertical line along the left margin
collegeLtr('college'),

/// College ruled paper (rtl): horizontal lines with one
/// vertical line along the right margin
collegeRtl('college-rtl'),
Expand All @@ -222,8 +232,9 @@ enum CanvasBackgroundPattern {

/// Music staffs
staffs('staffs'),

/// Music tablature
///
///
/// Like staffs but with 6 lines instead of 5 (and 5 spaces instead of 4).
tablature('tablature'),

Expand Down
53 changes: 29 additions & 24 deletions lib/components/canvas/_canvas_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ import 'package:saber/data/tools/shape_pen.dart';
class CanvasPainter extends CustomPainter {
const CanvasPainter({
super.repaint,

this.invert = false,
required this.strokes,
required this.laserStrokes,
required this.currentStroke,
required this.currentSelection,
required this.primaryColor,

required this.page,
required this.showPageIndicator,
required this.pageIndex,
Expand Down Expand Up @@ -57,9 +55,9 @@ class CanvasPainter extends CustomPainter {

@override
bool shouldRepaint(CanvasPainter oldDelegate) {
return currentStroke != null
|| oldDelegate.currentStroke != null
|| strokes.length != oldDelegate.strokes.length;
return currentStroke != null ||
oldDelegate.currentStroke != null ||
strokes.length != oldDelegate.strokes.length;
}

void _drawHighlighterStrokes(Canvas canvas, Rect canvasRect) {
Expand All @@ -72,9 +70,11 @@ class CanvasPainter extends CustomPainter {
for (Stroke stroke in strokes) {
if (stroke.penType != (Highlighter).toString()) continue;

final color = stroke.strokeProperties.color.withOpacity(1).withInversion(invert);
final color =
stroke.strokeProperties.color.withOpacity(1).withInversion(invert);

if (color != lastColor) { // new layer for each color
if (color != lastColor) {
// new layer for each color
if (needToRestoreCanvasLayer) canvas.restore();
canvas.saveLayer(canvasRect, layerPaint);

Expand Down Expand Up @@ -102,9 +102,9 @@ class CanvasPainter extends CustomPainter {
if (stroke.penType == (Pencil).toString()) {
paint.color = Colors.white;
paint.shader = page.pencilShader
..setFloat(0, color.red / 255)
..setFloat(1, color.green / 255)
..setFloat(2, color.blue / 255);
..setFloat(0, color.red / 255)
..setFloat(1, color.green / 255)
..setFloat(2, color.blue / 255);
} else {
paint.color = color;
paint.shader = null;
Expand All @@ -130,7 +130,8 @@ class CanvasPainter extends CustomPainter {
),
shapePaint,
);
} else if (stroke.length <= 2) { // a dot
} else if (stroke.length <= 2) {
// a dot
final bounds = stroke.path.getBounds();
final radius = max(bounds.size.width, stroke.strokeProperties.size) / 2;
canvas.drawCircle(bounds.center, radius, paint);
Expand All @@ -149,17 +150,21 @@ class CanvasPainter extends CustomPainter {
if (currentStroke!.penType == (Pencil).toString()) {
paint.color = Colors.white;
paint.shader = page.pencilShader
..setFloat(0, color.red / 255)
..setFloat(1, color.green / 255)
..setFloat(2, color.blue / 255);
..setFloat(0, color.red / 255)
..setFloat(1, color.green / 255)
..setFloat(2, color.blue / 255);
} else {
paint.color = color;
paint.shader = null;
}

if (currentStroke!.length <= 2) { // a dot
if (currentStroke!.length <= 2) {
// a dot
final bounds = currentStroke!.path.getBounds();
final radius = max(bounds.size.width, currentStroke!.strokeProperties.size * 0.5) / 2;
final radius = max(
bounds.size.width * 0.5,
currentStroke!.strokeProperties.size * 0.25,
);
canvas.drawCircle(bounds.center, radius, paint);
} else {
canvas.drawPath(currentStroke!.path, paint);
Expand All @@ -170,12 +175,12 @@ class CanvasPainter extends CustomPainter {
final shape = ShapePen.detectedShape;
if (shape == null) return;

final color = currentStroke?.strokeProperties.color.withInversion(invert)
?? Colors.black;
final color = currentStroke?.strokeProperties.color.withInversion(invert) ??
Colors.black;
final shapePaint = Paint()
..color = Color.lerp(color, primaryColor, 0.5)!.withOpacity(0.7)
..style = PaintingStyle.stroke
..strokeWidth = currentStroke?.strokeProperties.size ?? 3;
..color = Color.lerp(color, primaryColor, 0.5)!.withOpacity(0.7)
..style = PaintingStyle.stroke
..strokeWidth = currentStroke?.strokeProperties.size ?? 3;

switch (shape.name) {
case null:
Expand Down Expand Up @@ -215,9 +220,9 @@ class CanvasPainter extends CustomPainter {
dashArray: CircularIntervalList([10, 10]),
),
Paint()
..color = primaryColor
..strokeWidth = 3
..style = PaintingStyle.stroke,
..color = primaryColor
..strokeWidth = 3
..style = PaintingStyle.stroke,
);
}

Expand Down
15 changes: 9 additions & 6 deletions lib/components/canvas/_circle_stroke.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,21 @@ class CircleStroke extends Stroke {
bool _polygonNeedsUpdating = true;
late List<Offset> _polygon = const [];
late Path _path = Path();

/// A list of 25 points that form a circle
/// with [center] and [radius].
@override
List<Offset> get polygon {
if (_polygonNeedsUpdating) _updatePolygon();
return _polygon;
}

@override
Path get path {
if (_polygonNeedsUpdating) _updatePolygon();
return _path;
}

void _updatePolygon() {
_polygon = List.generate(25, (i) => i / 25 * 2 * pi)
.map((radians) => Offset(cos(radians), sin(radians)))
Expand Down Expand Up @@ -119,10 +122,10 @@ class CircleStroke extends Stroke {

@override
CircleStroke copy() => CircleStroke(
strokeProperties: strokeProperties.copy(),
pageIndex: pageIndex,
penType: penType,
center: center,
radius: radius,
);
strokeProperties: strokeProperties.copy(),
pageIndex: pageIndex,
penType: penType,
center: center,
radius: radius,
);
}
26 changes: 17 additions & 9 deletions lib/components/canvas/_rectangle_stroke.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class RectangleStroke extends Stroke {
bool _polygonNeedsUpdating = true;
late List<Offset> _polygon = const [];
late Path _path = Path();

/// A list of points that form the
/// rectangle's perimeter.
/// Each side has 25 points.
Expand All @@ -58,28 +59,35 @@ class RectangleStroke extends Stroke {
if (_polygonNeedsUpdating) _updatePolygon();
return _polygon;
}

@override
Path get path {
if (_polygonNeedsUpdating) _updatePolygon();
return _path;
}

void _updatePolygon() {
_polygon = _getPolygon();
_path = Path()..addPolygon(_polygon, true);
_polygonNeedsUpdating = false;
}

List<Offset> _getPolygon() {
final polygon = <Offset>[];
for (int i = 0; i < 25; ++i) { // left side
for (int i = 0; i < 25; ++i) {
// left side
polygon.add(Offset(rect.left, rect.top + rect.height * i / 25));
}
for (int i = 0; i < 25; ++i) { // bottom side
for (int i = 0; i < 25; ++i) {
// bottom side
polygon.add(Offset(rect.left + rect.width * i / 25, rect.bottom));
}
for (int i = 0; i < 25; ++i) { // right side
for (int i = 0; i < 25; ++i) {
// right side
polygon.add(Offset(rect.right, rect.bottom - rect.height * i / 25));
}
for (int i = 0; i < 25; ++i) { // top side
for (int i = 0; i < 25; ++i) {
// top side
polygon.add(Offset(rect.right - rect.width * i / 25, rect.top));
}
return polygon;
Expand Down Expand Up @@ -135,9 +143,9 @@ class RectangleStroke extends Stroke {

@override
RectangleStroke copy() => RectangleStroke(
strokeProperties: strokeProperties.copy(),
pageIndex: pageIndex,
penType: penType,
rect: rect,
);
strokeProperties: strokeProperties.copy(),
pageIndex: pageIndex,
penType: penType,
rect: rect,
);
}
Loading

0 comments on commit 7338b40

Please sign in to comment.