Skip to content

Commit 99dce07

Browse files
committed
Work around broken Actions.handler on Flutter 3.47
See flutter/flutter#191045
1 parent 23c1226 commit 99dce07

3 files changed

Lines changed: 52 additions & 20 deletions

File tree

‎lib/src/actions/scroll.dart‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:material_ui/material_ui.dart';
22
import 'package:orgro/l10n/app_localizations.dart';
3+
import 'package:orgro/src/actions/util.dart';
34

45
class ScrollToDocumentBoundaryAction
56
extends ContextAction<ScrollToDocumentBoundaryIntent> {
@@ -43,7 +44,7 @@ class ScrollTopButton extends StatelessWidget {
4344
return IconButton(
4445
tooltip: AppLocalizations.of(context)!.tooltipScrollTop,
4546
icon: const Icon(Icons.keyboard_arrow_up),
46-
onPressed: Actions.handler(
47+
onPressed: ActionsUtil.handler(
4748
context,
4849
const ScrollToDocumentBoundaryIntent(forward: false),
4950
),
@@ -53,7 +54,7 @@ class ScrollTopButton extends StatelessWidget {
5354

5455
PopupMenuItem<VoidCallback> scrollTopMenuItem(BuildContext context) {
5556
return PopupMenuItem<VoidCallback>(
56-
value: Actions.handler(
57+
value: ActionsUtil.handler(
5758
context,
5859
ScrollToDocumentBoundaryIntent(forward: false),
5960
),
@@ -69,7 +70,7 @@ class ScrollBottomButton extends StatelessWidget {
6970
return IconButton(
7071
tooltip: AppLocalizations.of(context)!.tooltipScrollBottom,
7172
icon: const Icon(Icons.keyboard_arrow_down),
72-
onPressed: Actions.handler(
73+
onPressed: ActionsUtil.handler(
7374
context,
7475
const ScrollToDocumentBoundaryIntent(forward: true),
7576
),
@@ -79,7 +80,7 @@ class ScrollBottomButton extends StatelessWidget {
7980

8081
PopupMenuItem<VoidCallback> scrollBottomMenuItem(BuildContext context) {
8182
return PopupMenuItem<VoidCallback>(
82-
value: Actions.handler(
83+
value: ActionsUtil.handler(
8384
context,
8485
ScrollToDocumentBoundaryIntent(forward: true),
8586
),

‎lib/src/actions/util.dart‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'package:flutter/widgets.dart';
2+
3+
extension ActionsUtil on Actions {
4+
// Work around https://github.com/flutter/flutter/issues/191045
5+
static VoidCallback? handler<T extends Intent>(
6+
BuildContext context,
7+
T intent,
8+
) {
9+
final Action<Intent>? action = Actions.maybeFind<T>(context);
10+
if (action != null && action._isEnabled(intent, context)) {
11+
return () {
12+
// Could be that the action was enabled when the closure was created,
13+
// but is now no longer enabled, so check again.
14+
if (action._isEnabled(intent, context)) {
15+
Actions.of(context).invokeAction(action, intent, context);
16+
}
17+
};
18+
}
19+
return null;
20+
}
21+
}
22+
23+
extension _ActionUtil<T extends Intent> on Action<T> {
24+
// This merely works around the fact that the real _isEnabled method is
25+
// private, and we need to call it from the ActionsUtil extension.
26+
bool _isEnabled(T intent, BuildContext? context) => switch (this) {
27+
final ContextAction<T> action => action.isEnabled(intent, context),
28+
_ => isEnabled(intent),
29+
};
30+
}

‎lib/src/pages/editor/editor.dart‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:material_ui/material_ui.dart';
22
import 'package:flutter/services.dart';
33
import 'package:orgro/l10n/app_localizations.dart';
44
import 'package:orgro/src/actions/common.dart';
5+
import 'package:orgro/src/actions/util.dart';
56
import 'package:orgro/src/components/dialogs.dart';
67
import 'package:orgro/src/components/view_settings.dart';
78
import 'package:orgro/src/debug.dart';
@@ -289,7 +290,7 @@ class _EditorToolbar extends StatelessWidget {
289290
tooltip: AppLocalizations.of(context)!
290291
.tooltipInsertHeadline,
291292
icon: const Icon(Icons.emergency),
292-
onPressed: Actions.handler(
293+
onPressed: ActionsUtil.handler(
293294
context,
294295
const InsertHeadlineIntent(),
295296
),
@@ -298,7 +299,7 @@ class _EditorToolbar extends StatelessWidget {
298299
tooltip: AppLocalizations.of(context)!
299300
.tooltipToggleUnorderedList,
300301
icon: const Icon(Icons.format_list_bulleted),
301-
onPressed: Actions.handler(
302+
onPressed: ActionsUtil.handler(
302303
context,
303304
const ToggleListItemIntent(ordered: false),
304305
),
@@ -307,7 +308,7 @@ class _EditorToolbar extends StatelessWidget {
307308
tooltip: AppLocalizations.of(context)!
308309
.tooltipToggleOrderedList,
309310
icon: const Icon(Icons.format_list_numbered),
310-
onPressed: Actions.handler(
311+
onPressed: ActionsUtil.handler(
311312
context,
312313
const ToggleListItemIntent(ordered: true),
313314
),
@@ -316,7 +317,7 @@ class _EditorToolbar extends StatelessWidget {
316317
tooltip: AppLocalizations.of(context)!
317318
.tooltipDecreaseIndent,
318319
icon: const Icon(Icons.format_indent_decrease),
319-
onPressed: Actions.handler(
320+
onPressed: ActionsUtil.handler(
320321
context,
321322
const ChangeIndentIntent(increase: false),
322323
),
@@ -325,31 +326,31 @@ class _EditorToolbar extends StatelessWidget {
325326
tooltip: AppLocalizations.of(context)!
326327
.tooltipIncreaseIndent,
327328
icon: const Icon(Icons.format_indent_increase),
328-
onPressed: Actions.handler(
329+
onPressed: ActionsUtil.handler(
329330
context,
330331
const ChangeIndentIntent(increase: true),
331332
),
332333
),
333334
IconButton(
334335
tooltip: AppLocalizations.of(context)!.tooltipBold,
335336
icon: const Icon(Icons.format_bold),
336-
onPressed: Actions.handler(
337+
onPressed: ActionsUtil.handler(
337338
context,
338339
const MakeBoldIntent(),
339340
),
340341
),
341342
IconButton(
342343
tooltip: AppLocalizations.of(context)!.tooltipItalic,
343344
icon: const Icon(Icons.format_italic),
344-
onPressed: Actions.handler(
345+
onPressed: ActionsUtil.handler(
345346
context,
346347
const MakeItalicIntent(),
347348
),
348349
),
349350
IconButton(
350351
tooltip: AppLocalizations.of(context)!.tooltipUnderline,
351352
icon: const Icon(Icons.format_underline),
352-
onPressed: Actions.handler(
353+
onPressed: ActionsUtil.handler(
353354
context,
354355
const MakeUnderlineIntent(),
355356
),
@@ -358,51 +359,51 @@ class _EditorToolbar extends StatelessWidget {
358359
tooltip: AppLocalizations.of(context)!
359360
.tooltipStrikethrough,
360361
icon: const Icon(Icons.format_strikethrough),
361-
onPressed: Actions.handler(
362+
onPressed: ActionsUtil.handler(
362363
context,
363364
const MakeStrikethroughIntent(),
364365
),
365366
),
366367
IconButton(
367368
tooltip: AppLocalizations.of(context)!.tooltipCode,
368369
icon: const Icon(Icons.code),
369-
onPressed: Actions.handler(
370+
onPressed: ActionsUtil.handler(
370371
context,
371372
const MakeCodeIntent(),
372373
),
373374
),
374375
IconButton(
375376
tooltip: AppLocalizations.of(context)!.tooltipInsertLink,
376377
icon: const Icon(Icons.link),
377-
onPressed: Actions.handler(
378+
onPressed: ActionsUtil.handler(
378379
context,
379380
const InsertLinkIntent(),
380381
),
381382
),
382383
IconButton(
383384
tooltip: AppLocalizations.of(context)!.tooltipInsertDate,
384385
icon: const Icon(Icons.calendar_today),
385-
onPressed: Actions.handler(
386+
onPressed: ActionsUtil.handler(
386387
context,
387388
const InsertDateIntent(active: false),
388389
),
389-
onLongPress: Actions.handler(
390+
onLongPress: ActionsUtil.handler(
390391
context,
391392
const InsertDateIntent(active: true),
392393
),
393394
),
394395
IconButton(
395396
tooltip: AppLocalizations.of(context)!.tooltipSubscript,
396397
icon: const Icon(Icons.subscript),
397-
onPressed: Actions.handler(
398+
onPressed: ActionsUtil.handler(
398399
context,
399400
const MakeSubscriptIntent(),
400401
),
401402
),
402403
IconButton(
403404
tooltip: AppLocalizations.of(context)!.tooltipSuperscript,
404405
icon: const Icon(Icons.superscript),
405-
onPressed: Actions.handler(
406+
onPressed: ActionsUtil.handler(
406407
context,
407408
const MakeSuperscriptIntent(),
408409
),
@@ -411,7 +412,7 @@ class _EditorToolbar extends StatelessWidget {
411412
tooltip: AppLocalizations.of(context)!
412413
.tooltipEncryptSection,
413414
icon: const Icon(Icons.lock_outline),
414-
onPressed: Actions.handler(
415+
onPressed: ActionsUtil.handler(
415416
context,
416417
const EncryptSectionIntent(),
417418
),

0 commit comments

Comments
 (0)