|
1 | 1 | import "package:flutter/material.dart"; |
| 2 | +import "package:typewriter/utils/color_filter.dart"; |
2 | 3 | import "package:flutter_animate/flutter_animate.dart"; |
3 | 4 | import "package:hooks_riverpod/hooks_riverpod.dart"; |
4 | 5 | import "package:typewriter/models/adapter.dart"; |
5 | 6 | import "package:typewriter/utils/passing_reference.dart"; |
6 | 7 | import "package:typewriter/widgets/inspector/editors.dart"; |
7 | 8 | import "package:typewriter/widgets/inspector/editors/field.dart"; |
| 9 | +import "package:typewriter/widgets/inspector/header.dart"; |
8 | 10 | import "package:typewriter/widgets/inspector/inspector.dart"; |
9 | 11 |
|
10 | 12 | class OptionalEditorFilter extends EditorFilter { |
11 | 13 | @override |
12 | | - bool canEdit(FieldInfo info) => info is CustomField && info.editor == "optional"; |
| 14 | + bool canEdit(FieldInfo info) => |
| 15 | + info is CustomField && info.editor == "optional"; |
13 | 16 |
|
14 | 17 | @override |
15 | | - Widget build(String path, FieldInfo info) => OptionalEditor(path: path, field: info as CustomField); |
| 18 | + Widget build(String path, FieldInfo info) => |
| 19 | + OptionalEditor(path: path, field: info as CustomField); |
16 | 20 | } |
17 | 21 |
|
18 | 22 | class OptionalEditor extends HookConsumerWidget { |
@@ -44,32 +48,100 @@ class OptionalEditor extends HookConsumerWidget { |
44 | 48 | ); |
45 | 49 | } |
46 | 50 |
|
47 | | - return Row( |
48 | | - children: [ |
49 | | - Checkbox( |
50 | | - value: enabled, |
51 | | - onChanged: (value) { |
52 | | - ref.read(inspectingEntryDefinitionProvider)?.updateField(ref.passing, "$path.enabled", value ?? false); |
53 | | - }, |
54 | | - ), |
55 | | - Expanded( |
56 | | - child: AnimatedOpacity( |
57 | | - opacity: enabled ? 1 : 0.6, |
58 | | - duration: 200.ms, |
59 | | - curve: Curves.easeOut, |
60 | | - child: MouseRegion( |
61 | | - cursor: enabled ? MouseCursor.defer : SystemMouseCursors.forbidden, |
62 | | - child: AbsorbPointer( |
63 | | - absorbing: !enabled, |
64 | | - child: FieldEditor( |
65 | | - path: "$path.value", |
66 | | - type: subField, |
| 51 | + final subPath = "$path.value"; |
| 52 | + final headerActionFilters = ref.watch(headerActionFiltersProvider); |
| 53 | + final subFieldActions = headerActionFilters |
| 54 | + .where((filter) => filter.shouldShow(subPath, subField)) |
| 55 | + .toList(); |
| 56 | + |
| 57 | + return FieldHeader( |
| 58 | + field: field, |
| 59 | + path: path, |
| 60 | + leading: _buildActions( |
| 61 | + HeaderActionLocation.leading, |
| 62 | + subFieldActions, |
| 63 | + subPath, |
| 64 | + subField, |
| 65 | + enabled: enabled, |
| 66 | + ), |
| 67 | + trailing: _buildActions( |
| 68 | + HeaderActionLocation.trailing, |
| 69 | + subFieldActions, |
| 70 | + subPath, |
| 71 | + subField, |
| 72 | + enabled: enabled, |
| 73 | + ), |
| 74 | + actions: _buildActions( |
| 75 | + HeaderActionLocation.actions, |
| 76 | + subFieldActions, |
| 77 | + subPath, |
| 78 | + subField, |
| 79 | + enabled: enabled, |
| 80 | + ), |
| 81 | + child: Row( |
| 82 | + children: [ |
| 83 | + Checkbox( |
| 84 | + value: enabled, |
| 85 | + onChanged: (value) { |
| 86 | + ref |
| 87 | + .read(inspectingEntryDefinitionProvider) |
| 88 | + ?.updateField(ref.passing, "$path.enabled", value ?? false); |
| 89 | + }, |
| 90 | + ), |
| 91 | + Expanded( |
| 92 | + child: AnimatedOpacity( |
| 93 | + opacity: enabled ? 1 : 0.6, |
| 94 | + duration: 200.ms, |
| 95 | + curve: Curves.easeOut, |
| 96 | + child: MouseRegion( |
| 97 | + cursor: |
| 98 | + enabled ? MouseCursor.defer : SystemMouseCursors.forbidden, |
| 99 | + child: AbsorbPointer( |
| 100 | + absorbing: !enabled, |
| 101 | + child: FieldEditor( |
| 102 | + path: "$path.value", |
| 103 | + type: subField, |
| 104 | + ), |
67 | 105 | ), |
68 | 106 | ), |
69 | 107 | ), |
70 | 108 | ), |
71 | | - ), |
72 | | - ], |
| 109 | + ], |
| 110 | + ), |
73 | 111 | ); |
74 | 112 | } |
| 113 | + |
| 114 | + List<Widget> _buildActions( |
| 115 | + HeaderActionLocation location, |
| 116 | + List<HeaderActionFilter> actions, |
| 117 | + String path, |
| 118 | + FieldInfo field, { |
| 119 | + bool enabled = true, |
| 120 | + }) { |
| 121 | + final children = actions |
| 122 | + .where((action) => action.location(path, field) == location) |
| 123 | + .map((action) => action.build(path, field)) |
| 124 | + .toList(); |
| 125 | + if (enabled) { |
| 126 | + return children; |
| 127 | + } |
| 128 | + |
| 129 | + return [ |
| 130 | + for (final child in children) |
| 131 | + MouseRegion( |
| 132 | + cursor: SystemMouseCursors.forbidden, |
| 133 | + child: AbsorbPointer( |
| 134 | + child: AnimatedOpacity( |
| 135 | + opacity: 0.6, |
| 136 | + duration: 200.ms, |
| 137 | + curve: Curves.easeOut, |
| 138 | + child: ColorFiltered( |
| 139 | + colorFilter: greyscale, |
| 140 | + child: child, |
| 141 | + ), |
| 142 | + ), |
| 143 | + ), |
| 144 | + ), |
| 145 | + ]; |
| 146 | + } |
75 | 147 | } |
0 commit comments