Skip to content

Commit 3c7a93a

Browse files
committed
Improve label mode change
1 parent c719673 commit 3c7a93a

3 files changed

Lines changed: 64 additions & 5 deletions

File tree

app/lib/models/label.dart

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,67 @@ sealed class LabelContext with _$LabelContext {
3131
@Default(TextSelection.collapsed(offset: 0)) TextSelection selection,
3232
}) = MarkdownContext;
3333

34+
LabelContext toMode(LabelMode mode) {
35+
if (tool.mode == mode) return this;
36+
final newTool = tool.copyWith(mode: mode);
37+
switch (mode) {
38+
case LabelMode.text:
39+
return TextContext(
40+
tool: newTool,
41+
textPainter: textPainter,
42+
zoom: zoom,
43+
selection: selection,
44+
element: switch (this) {
45+
TextContext e => e.element,
46+
MarkdownContext e =>
47+
e.element == null
48+
? null
49+
: TextElement(
50+
id: e.element!.id,
51+
position: e.element!.position,
52+
scale: e.element!.scale,
53+
foreground: e.element!.foreground,
54+
styleSheet: e.element!.styleSheet,
55+
constraint: e.element!.constraint,
56+
extra: e.element!.extra,
57+
area: txt.TextArea(
58+
paragraph: txt.TextParagraph(
59+
textSpans: [
60+
txt.InlineSpan.text(text: e.element!.text),
61+
],
62+
),
63+
areaProperty: e.element!.areaProperty,
64+
),
65+
),
66+
},
67+
);
68+
case LabelMode.markdown:
69+
return MarkdownContext(
70+
tool: newTool,
71+
textPainter: textPainter,
72+
zoom: zoom,
73+
selection: selection,
74+
element: switch (this) {
75+
MarkdownContext e => e.element,
76+
TextContext e =>
77+
e.element == null
78+
? null
79+
: MarkdownElement(
80+
id: e.element!.id,
81+
position: e.element!.position,
82+
scale: e.element!.scale,
83+
foreground: e.element!.foreground,
84+
styleSheet: e.element!.styleSheet,
85+
constraint: e.element!.constraint,
86+
extra: e.element!.extra,
87+
text: e.element!.area.paragraph.text,
88+
areaProperty: e.element!.area.areaProperty,
89+
),
90+
},
91+
);
92+
}
93+
}
94+
3495
bool get isCreating => true;
3596

3697
String? get text => labelElement?.text;

app/lib/views/toolbar/label.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,8 @@ class _LabelToolbarViewState extends State<LabelToolbarView> {
288288
e.icon(PhosphorIconsStyle.light),
289289
),
290290
child: Text(e.getLocalizedName(context)),
291-
onPressed: () => widget.onChanged(
292-
value.copyWith(
293-
tool: value.tool.copyWith(mode: e),
294-
),
295-
),
291+
onPressed: () =>
292+
widget.onChanged(value.toMode(e)),
296293
),
297294
)
298295
.toList(),

metadata/en-US/changelogs/168.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Add area creation dialog in area navigator if no areas exist
44
* Add area teleportion after entering area
55
* Show other areas if inside an area
6+
* Improve label mode change
67
* Improve bottom bar in area and page navigator
78
* Fix area teleporting has offset on render resolutions other than fast
89
* Fix handwriting, suggestions and emoji input not working for text fields ([#936](https://github.com/LinwoodDev/Butterfly/issues/936))

0 commit comments

Comments
 (0)