Skip to content

Commit

Permalink
Add option to change ruler size
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Jan 1, 2025
1 parent e752ebb commit 8a59547
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions api/lib/src/models/tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ sealed class Tool with _$Tool {
@Default('') String name,
@Default('') String displayIcon,
@ColorJsonConverter() SRGBColor? color,
@Default(100) int size,
}) = RulerTool;

factory Tool.grid({
Expand Down
18 changes: 15 additions & 3 deletions api/lib/src/models/tool.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2799,7 +2799,8 @@ abstract class _$$RulerToolImplCopyWith<$Res> implements $ToolCopyWith<$Res> {
$Res call(
{String name,
String displayIcon,
@ColorJsonConverter() SRGBColor? color});
@ColorJsonConverter() SRGBColor? color,
int size});
}

/// @nodoc
Expand All @@ -2818,6 +2819,7 @@ class __$$RulerToolImplCopyWithImpl<$Res>
Object? name = null,
Object? displayIcon = null,
Object? color = freezed,
Object? size = null,
}) {
return _then(_$RulerToolImpl(
name: null == name
Expand All @@ -2832,6 +2834,10 @@ class __$$RulerToolImplCopyWithImpl<$Res>
? _value.color
: color // ignore: cast_nullable_to_non_nullable
as SRGBColor?,
size: null == size
? _value.size
: size // ignore: cast_nullable_to_non_nullable
as int,
));
}
}
Expand All @@ -2843,6 +2849,7 @@ class _$RulerToolImpl extends RulerTool {
{this.name = '',
this.displayIcon = '',
@ColorJsonConverter() this.color,
this.size = 100,
final String? $type})
: $type = $type ?? 'ruler',
super._();
Expand All @@ -2859,13 +2866,16 @@ class _$RulerToolImpl extends RulerTool {
@override
@ColorJsonConverter()
final SRGBColor? color;
@override
@JsonKey()
final int size;

@JsonKey(name: 'type')
final String $type;

@override
String toString() {
return 'Tool.ruler(name: $name, displayIcon: $displayIcon, color: $color)';
return 'Tool.ruler(name: $name, displayIcon: $displayIcon, color: $color, size: $size)';
}

/// Create a copy of Tool
Expand All @@ -2888,7 +2898,8 @@ abstract class RulerTool extends Tool {
factory RulerTool(
{final String name,
final String displayIcon,
@ColorJsonConverter() final SRGBColor? color}) = _$RulerToolImpl;
@ColorJsonConverter() final SRGBColor? color,
final int size}) = _$RulerToolImpl;
RulerTool._() : super._();

factory RulerTool.fromJson(Map<String, dynamic> json) =
Expand All @@ -2900,6 +2911,7 @@ abstract class RulerTool extends Tool {
String get displayIcon;
@ColorJsonConverter()
SRGBColor? get color;
int get size;

/// Create a copy of Tool
/// with the given fields replaced by the non-null parameter values.
Expand Down
2 changes: 2 additions & 0 deletions api/lib/src/models/tool.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions app/lib/handlers/ruler.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
part of 'handler.dart';

Rect _getRulerRect(Size size, Offset position,
Rect _getRulerRect(RulerTool ruler, Size size, Offset position,
[CameraTransform transform = const CameraTransform()]) {
const rulerSize = 100.0;
return Rect.fromLTWH(
transform.position.dx + position.dx / transform.size,
transform.position.dy +
(size.height / 2 + -rulerSize / 2 + position.dy) / transform.size,
(size.height / 2 + -ruler.size / 2 + position.dy) / transform.size,
size.width * 2 / transform.size,
rulerSize / transform.size,
ruler.size / transform.size,
);
}

Expand Down Expand Up @@ -50,7 +49,7 @@ class RulerHandler extends Handler<RulerTool> with PointerManipulationHandler {

Rect getRect(Size size,
[CameraTransform transform = const CameraTransform()]) {
return _getRulerRect(size, _position, transform);
return _getRulerRect(data, size, _position, transform);
}

bool isPointerInside(Offset position, Size viewportSize) {
Expand Down Expand Up @@ -111,7 +110,7 @@ class RulerRenderer extends Renderer<RulerTool> {
canvas.save();
canvas.translate(transform.position.dx, transform.position.dy);
canvas.scale(1 / transform.size, 1 / transform.size);
var rulerRect = _getRulerRect(size, position);
var rulerRect = _getRulerRect(element, size, position);
final rulerCenter = rulerRect.center;
canvas.translate(rulerCenter.dx, rulerCenter.dy);
canvas.rotate(rulerRotation * pi / 180);
Expand Down
2 changes: 1 addition & 1 deletion app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -636,5 +636,5 @@
"confirmPassword": "Confirm password",
"passwordMismatch": "The passwords do not match",
"action": "Action",
"svgText": "SVG text"
"svgText": "SVG Text"
}
12 changes: 12 additions & 0 deletions app/lib/selections/tools/ruler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ class RulerToolSelection extends ToolSelection<RulerTool> {
selected.map((e) => e.copyWith(color: value)).toList(),
),
),
ExactSlider(
header: Text(AppLocalizations.of(context).size),
value: selected.first.size.toDouble(),
min: 1,
max: 500,
defaultValue: 100,
fractionDigits: 0,
onChangeEnd: (value) => update(
context,
selected.map((e) => e.copyWith(size: value.toInt())).toList(),
),
)
];
}

Expand Down

0 comments on commit 8a59547

Please sign in to comment.