Skip to content

Commit

Permalink
Add editing of label elements
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Jun 19, 2023
1 parent 6a18d9b commit aeb9592
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 42 deletions.
84 changes: 54 additions & 30 deletions app/lib/handlers/label.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,50 @@ class LabelHandler extends Handler<LabelPainter>

LabelHandler(super.data);

LabelContext _createContext([Point<double>? position, double zoom = 1]) {
LabelContext _createContext(
{Point<double>? position, double zoom = 1, LabelElement? element}) {
final scale = data.zoomDependent ? 1 / zoom : 1.0;
switch (data.mode) {
final mode = element != null
? (element is TextElement ? LabelMode.text : LabelMode.markdown)
: data.mode;
switch (mode) {
case LabelMode.text:
final forced = _context?.mapOrNull(text: (e) => e.forcedProperty);
return TextContext(
painter: data,
isCreating: true,
element: position == null
? null
: TextElement(
position: position,
area: text.TextArea(
paragraph: text.TextParagraph.text(
property:
forced ?? const text.ParagraphProperty.undefined(),
),
),
styleSheet: data.styleSheet,
scale: scale,
foreground: data.foreground,
),
element: (element as TextElement?) ??
(position == null
? null
: TextElement(
position: position,
area: text.TextArea(
paragraph: text.TextParagraph.text(
property: forced ??
const text.ParagraphProperty.undefined(),
),
),
styleSheet: data.styleSheet,
scale: scale,
foreground: data.foreground,
)),
textPainter: TextPainter(),
forcedProperty: forced,
);
case LabelMode.markdown:
return MarkdownContext(
painter: data,
isCreating: true,
element: position == null
? null
: MarkdownElement(
position: position,
text: '',
styleSheet: data.styleSheet,
scale: scale,
foreground: data.foreground,
),
element: (element as MarkdownElement?) ??
(position == null
? null
: MarkdownElement(
position: position,
text: '',
styleSheet: data.styleSheet,
scale: scale,
foreground: data.foreground,
)),
textPainter: TextPainter(),
);
}
Expand Down Expand Up @@ -113,11 +119,18 @@ class LabelHandler extends Handler<LabelPainter>
}

@override
Future<void> onTapUp(TapUpDetails details, EventContext context) async {
void onTapUp(TapUpDetails details, EventContext context) =>
create(context, details.localPosition, context.isShiftPressed);

@override
void onLongPressEnd(LongPressEndDetails details, EventContext context) =>
create(context, details.localPosition, true);

Future<void> create(EventContext context, Offset localPosition,
[bool forceCreate = false]) async {
final pixelRatio = context.devicePixelRatio;
final focusNode = Focus.of(context.buildContext);
final globalPos =
context.getCameraTransform().localToGlobal(details.localPosition);
final globalPos = context.getCameraTransform().localToGlobal(localPosition);
final hitRect = _context?.getRect();
final hit = hitRect?.contains(globalPos) ?? false;
final hadFocus = focusNode.hasFocus && !hit;
Expand Down Expand Up @@ -147,8 +160,19 @@ class LabelHandler extends Handler<LabelPainter>
_connection!.show();
if (hadFocus || _context?.element == null) {
if (_context?.element != null) _submit(context.getDocumentBloc());
_context = _createContext(
globalPos.toPoint(), context.getCameraTransform().size);
final hit = await rayCast(globalPos, context.getDocumentBloc(),
context.getCameraTransform(), 0.0);
final labelRenderer = hit.whereType<Renderer<LabelElement>>().firstOrNull;
if (labelRenderer == null) {
_context = _createContext(
position: globalPos.toPoint(),
zoom: context.getCameraTransform().size);
} else {
context
.getDocumentBloc()
.add(ElementsRemoved([labelRenderer.element as PadElement]));
_context = _createContext(element: labelRenderer.element);
}
}
if (hit) {
final position = _context!.textPainter.getPositionForOffset(globalPos -
Expand Down
8 changes: 4 additions & 4 deletions app/lib/selections/elements/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ class ImageElementSelection extends ElementSelection<ImageElement> {
onChanged: (constraints) => updateElements(context,
elements.map((e) => e.copyWith(constraints: constraints)).toList()),
),
ListTile(
title: Text(AppLocalizations.of(context).export),
leading: const PhosphorIcon(PhosphorIconsLight.export),
onTap: () async {
MenuItemButton(
leadingIcon: const PhosphorIcon(PhosphorIconsLight.export),
onPressed: () async {
final localization = AppLocalizations.of(context);
final state = context.read<DocumentBloc>().state;
if (state is! DocumentLoaded) {
Expand Down Expand Up @@ -45,6 +44,7 @@ class ImageElementSelection extends ElementSelection<ImageElement> {
openImage(data);
}
},
child: Text(AppLocalizations.of(context).export),
),
];
}
Expand Down
8 changes: 4 additions & 4 deletions app/lib/selections/elements/svg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ class SvgElementSelection extends ElementSelection<SvgElement> {
onChanged: (constraints) => updateElements(context,
elements.map((e) => e.copyWith(constraints: constraints)).toList()),
),
ListTile(
title: Text(AppLocalizations.of(context).export),
leading: const PhosphorIcon(PhosphorIconsLight.export),
onTap: () async {
MenuItemButton(
leadingIcon: const PhosphorIcon(PhosphorIconsLight.export),
onPressed: () async {
final localization = AppLocalizations.of(context);
final state = context.read<DocumentBloc>().state;
if (state is! DocumentLoaded) {
Expand Down Expand Up @@ -46,6 +45,7 @@ class SvgElementSelection extends ElementSelection<SvgElement> {
openSvg(data);
}
},
child: Text(AppLocalizations.of(context).export),
),
];
}
Expand Down
8 changes: 4 additions & 4 deletions app/lib/selections/tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@ class _ToolViewState extends State<_ToolView> with TickerProviderStateMixin {
],
),
Column(children: [
CheckboxListTile(
title: Text(AppLocalizations.of(context).showGrid),
CheckboxMenuButton(
value: widget.state.gridEnabled,
onChanged: (value) => widget.onStateChanged(
widget.state.copyWith(gridEnabled: value ?? false),
),
child: Text(AppLocalizations.of(context).showGrid),
),
const SizedBox(height: 8),
OffsetPropertyView(
Expand Down Expand Up @@ -236,11 +236,11 @@ class _ToolViewState extends State<_ToolView> with TickerProviderStateMixin {
),
]),
Column(children: [
CheckboxListTile(
title: Text(AppLocalizations.of(context).ruler),
CheckboxMenuButton(
value: widget.state.rulerEnabled,
onChanged: (value) => widget.onStateChanged(
widget.state.copyWith(rulerEnabled: value ?? false)),
child: Text(AppLocalizations.of(context).ruler),
),
const SizedBox(height: 8),
OffsetPropertyView(
Expand Down
2 changes: 2 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/65.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
* Add grid view
* Add editing of label elements
* Add move elements in the z axis ([#396](https://github.com/LinwoodDev/Butterfly/issues/396))
* Add saving indicator ([#402](https://github.com/LinwoodDev/Butterfly/issues/402))
* Add pack remote directory ([#389](https://github.com/LinwoodDev/Butterfly/issues/389))
* Use delay on reorder item for painters
* Use delay on files in home page
* Migrate selection context menu to new menu button widget
* Fix layer view not updating when layer visibility changes
* Fix page won't be saved if autosave is disabled on page change
* Fix importing of assets
Expand Down

0 comments on commit aeb9592

Please sign in to comment.