feat: automatically calculate the available drop-down menu options。

This commit is contained in:
2021-11-18 18:29:06 +08:00
parent 2fef238bf2
commit 13752b3451
2 changed files with 147 additions and 114 deletions
+144 -111
View File
@@ -165,10 +165,11 @@ class GestureEditor extends StatelessWidget {
enabled: !gesturePropProvider.editMode! && !schemeTree.fullFiled, enabled: !gesturePropProvider.editMode! && !schemeTree.fullFiled,
onTap: () { onTap: () {
var schemeProvider = context.read<SchemeProvider>(); var schemeProvider = context.read<SchemeProvider>();
context.read<SchemeProvider>().setProps(gestures: [ context.read<SchemeProvider>().setProps(
...?schemeProvider.gestures, gestures: [
H.getNextAvailableGestureProp(schemeProvider.buildSchemeTree())!, ...?schemeProvider.gestures,
]); H.getNextAvailableGestureProp(schemeProvider.buildSchemeTree())!,
]..sort());
}, },
), ),
DButton.delete( DButton.delete(
@@ -181,8 +182,9 @@ class GestureEditor extends StatelessWidget {
]; ];
context.read<SchemeProvider>().setProps(gestures: newGestures); context.read<SchemeProvider>().setProps(gestures: newGestures);
if (newGestures.length > 0) if (newGestures.length > 0)
gesturePropProvider.copyFrom(newGestures[ gesturePropProvider.copyFrom(
(index ?? 0) > newGestures.length - 1 ? newGestures.length - 1 : index ?? 0]); newGestures[(index ?? 0) > newGestures.length - 1 ? newGestures.length - 1 : index ?? 0]
..editMode = false);
}, },
), ),
DButton.duplicate( DButton.duplicate(
@@ -217,10 +219,11 @@ class GestureEditor extends StatelessWidget {
newGestureProp.remark = copiedGesturePropProvider.remark; newGestureProp.remark = copiedGesturePropProvider.remark;
} }
newGestureProp.id = Uuid().v1(); newGestureProp.id = Uuid().v1();
context.read<SchemeProvider>().setProps(gestures: [ context.read<SchemeProvider>().setProps(
...?schemeProvider.gestures, gestures: [
newGestureProp, ...?schemeProvider.gestures,
]); newGestureProp,
]..sort());
}, },
), ),
] ]
@@ -280,114 +283,144 @@ List<DDataRow> _buildDataRows(List<GestureProp>? gestures, BuildContext context)
} }
}, },
selected: selected, selected: selected,
cells: editing ? _buildRowCellsEditing(context, gesture) : _buildRowCellsNormal(context, selected, gesture), cells: editing ? _buildRowCellsEditing(context) : _buildRowCellsNormal(context, selected, gesture),
); );
}).toList(); }).toList();
List<DDataCell> _buildRowCellsEditing(BuildContext context, GestureProp gesture) => [ List<DDataCell> _buildRowCellsEditing(BuildContext context) {
DButton.dropdown( var gesture = context.read<GesturePropProvider>();
enabled: true, var schemeTree = context.read<SchemeProvider>().buildSchemeTree();
child: DropdownButton<int>( var availableFingers = schemeTree.nodes.where((node) => !node.fullFiled).map((e) => e.fingers);
icon: Icon(Icons.keyboard_arrow_down_rounded), if (!availableFingers.contains(gesture.fingers)) {
items: [3, 4, 5] availableFingers = [...availableFingers, gesture.fingers!]..sort();
.map( }
(e) => DropdownMenuItem<int>(
child: Text('$e'), var availableGestures = schemeTree.nodes
value: e, .firstWhere((node) => node.fingers == gesture.fingers)
), .nodes
) .where((node) => !node.fullFiled)
.toList(), .map((e) => e.type);
value: context.watch<GesturePropProvider>().fingers, if (!availableGestures.any((type) => type == gesture.gesture)) {
onChanged: (value) => context.read<GesturePropProvider>().setProps( availableGestures = [...availableGestures, gesture.gesture!]..sort();
fingers: value, }
editMode: true,
var availableDirection = schemeTree.nodes
.firstWhere((node) => node.fingers == gesture.fingers)
.nodes
.firstWhere((node) => node.type == gesture.gesture)
.nodes
.where((node) => !node.fullFiled)
.map((e) => e.direction);
if (!availableDirection.any((direction) => direction == gesture.direction)) {
availableDirection = [...availableDirection, gesture.direction!]..sort((a, b) => a.index - b.index);
}
return [
DButton.dropdown(
enabled: true,
child: DropdownButton<int>(
icon: Icon(Icons.keyboard_arrow_down_rounded),
items: availableFingers
.map(
(e) => DropdownMenuItem<int>(
child: Text('$e'),
value: e,
), ),
isExpanded: true, )
), .toList(),
), value: gesture.fingers,
DButton.dropdown( onChanged: (value) => context.read<GesturePropProvider>().setProps(
enabled: true, fingers: value,
width: 60.0,
child: DropdownButton<Gesture>(
icon: Icon(Icons.keyboard_arrow_down_rounded),
items: Gesture.values
.map(
(e) => DropdownMenuItem<Gesture>(
child: Text(
'${LocaleKeys.gesture_editor_gestures}.${H.getGestureName(e)}',
textScaleFactor: .8,
).tr(),
value: e,
),
)
.toList(),
value: context.watch<GesturePropProvider>().gesture,
onChanged: (value) => context.read<GesturePropProvider>().setProps(
gesture: value,
editMode: true,
),
isExpanded: true,
),
),
DButton.dropdown(
enabled: true,
width: 100.0,
child: DropdownButton<GestureDirection>(
icon: Icon(Icons.keyboard_arrow_down_rounded),
items: GestureDirection.values
.map(
(e) => DropdownMenuItem<GestureDirection>(
child: Text(
'${LocaleKeys.gesture_editor_directions}.${H.getGestureDirectionName(e)}',
textScaleFactor: .8,
).tr(),
value: e,
),
)
.toList(),
value: context.watch<GesturePropProvider>().direction,
onChanged: (value) => context.read<GesturePropProvider>().setProps(
direction: value,
editMode: true,
),
isExpanded: true,
),
),
DButton.dropdown(
enabled: true,
width: 100.0,
child: DropdownButton<GestureType>(
icon: Icon(Icons.keyboard_arrow_down_rounded),
items: GestureType.values
.map(
(e) => DropdownMenuItem<GestureType>(
child: Text(
'${LocaleKeys.gesture_editor_types}.${H.getGestureTypeName(e)}',
textScaleFactor: .8,
).tr(),
value: e,
),
)
.toList(),
value: context.watch<GesturePropProvider>().type,
onChanged: (value) => context.read<GesturePropProvider>().setProps(
type: value,
command: '',
editMode: true,
),
isExpanded: true,
),
),
_buildCommandCellsEditing(context),
TableCellTextField(
initText: gesture.remark,
hint: 'pls input cmd',
onComplete: (value) => context.read<GesturePropProvider>().setProps(
remark: value,
editMode: true, editMode: true,
), ),
isExpanded: true,
), ),
].map((e) => DDataCell(e)).toList(); ),
DButton.dropdown(
enabled: true,
width: 100.0,
child: DropdownButton<Gesture>(
icon: Icon(Icons.keyboard_arrow_down_rounded),
items: availableGestures
.map(
(e) => DropdownMenuItem<Gesture>(
child: Text(
'${LocaleKeys.gesture_editor_gestures}.${H.getGestureName(e)}',
textScaleFactor: .8,
).tr(),
value: e,
),
)
.toList(),
value: gesture.gesture,
onChanged: (value) => context.read<GesturePropProvider>().setProps(
gesture: value,
editMode: true,
),
isExpanded: true,
),
),
DButton.dropdown(
enabled: true,
width: 100.0,
child: DropdownButton<GestureDirection>(
icon: Icon(Icons.keyboard_arrow_down_rounded),
items: availableDirection
.map(
(e) => DropdownMenuItem<GestureDirection>(
child: Text(
'${LocaleKeys.gesture_editor_directions}.${H.getGestureDirectionName(e)}',
textScaleFactor: .8,
).tr(),
value: e,
),
)
.toList(),
value: gesture.direction,
onChanged: (value) => context.read<GesturePropProvider>().setProps(
direction: value,
editMode: true,
),
isExpanded: true,
),
),
DButton.dropdown(
enabled: true,
width: 100.0,
child: DropdownButton<GestureType>(
icon: Icon(Icons.keyboard_arrow_down_rounded),
items: GestureType.values
.map(
(e) => DropdownMenuItem<GestureType>(
child: Text(
'${LocaleKeys.gesture_editor_types}.${H.getGestureTypeName(e)}',
textScaleFactor: .8,
).tr(),
value: e,
),
)
.toList(),
value: gesture.type,
onChanged: (value) => context.read<GesturePropProvider>().setProps(
type: value,
command: '',
editMode: true,
),
isExpanded: true,
),
),
_buildCommandCellsEditing(context),
TableCellTextField(
initText: gesture.remark,
hint: 'pls input cmd',
onComplete: (value) => context.read<GesturePropProvider>().setProps(
remark: value,
editMode: true,
),
),
].map((e) => DDataCell(e)).toList();
}
Widget _buildCommandCellsEditing(BuildContext context) { Widget _buildCommandCellsEditing(BuildContext context) {
var gesture = context.read<GesturePropProvider>(); var gesture = context.read<GesturePropProvider>();
+3 -3
View File
@@ -28,7 +28,7 @@ class _HomePageState extends State<HomePage> {
"direction": "down", "direction": "down",
"fingers": 3, "fingers": 3,
"type": "shortcut", "type": "shortcut",
"command": "ctrl+w", "command": "Control_L+w",
"remark": "close current page." "remark": "close current page."
}, },
{ {
@@ -36,7 +36,7 @@ class _HomePageState extends State<HomePage> {
"direction": "up", "direction": "up",
"fingers": 3, "fingers": 3,
"type": "shortcut", "type": "shortcut",
"command": "ctrl+alt+t", "command": "Control_L+Alt_L+t",
"remark": "reopen last closed page." "remark": "reopen last closed page."
}, },
{ {
@@ -44,7 +44,7 @@ class _HomePageState extends State<HomePage> {
"direction": "in", "direction": "in",
"fingers": 4, "fingers": 4,
"type": "shortcut", "type": "shortcut",
"command": "ctrl+alt+f", "command": "Control_L+Alt_L+f",
"remark": "search files." "remark": "search files."
}, },
{ {