From 13752b345126e6d788f7d09cd2a5f21e4cd03ab9 Mon Sep 17 00:00:00 2001 From: debuggerx Date: Thu, 18 Nov 2021 18:29:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20automatically=20calculate=20the=20avail?= =?UTF-8?q?able=20drop-down=20menu=20options=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/lib/pages/gesture_editor.dart | 243 ++++++++++++++++++++++---------------- app/lib/pages/home.dart | 6 +- 2 files changed, 141 insertions(+), 108 deletions(-) diff --git a/app/lib/pages/gesture_editor.dart b/app/lib/pages/gesture_editor.dart index 80abcaa..aa74efb 100644 --- a/app/lib/pages/gesture_editor.dart +++ b/app/lib/pages/gesture_editor.dart @@ -165,10 +165,11 @@ class GestureEditor extends StatelessWidget { enabled: !gesturePropProvider.editMode! && !schemeTree.fullFiled, onTap: () { var schemeProvider = context.read(); - context.read().setProps(gestures: [ - ...?schemeProvider.gestures, - H.getNextAvailableGestureProp(schemeProvider.buildSchemeTree())!, - ]); + context.read().setProps( + gestures: [ + ...?schemeProvider.gestures, + H.getNextAvailableGestureProp(schemeProvider.buildSchemeTree())!, + ]..sort()); }, ), DButton.delete( @@ -181,8 +182,9 @@ class GestureEditor extends StatelessWidget { ]; context.read().setProps(gestures: newGestures); if (newGestures.length > 0) - gesturePropProvider.copyFrom(newGestures[ - (index ?? 0) > newGestures.length - 1 ? newGestures.length - 1 : index ?? 0]); + gesturePropProvider.copyFrom( + newGestures[(index ?? 0) > newGestures.length - 1 ? newGestures.length - 1 : index ?? 0] + ..editMode = false); }, ), DButton.duplicate( @@ -217,10 +219,11 @@ class GestureEditor extends StatelessWidget { newGestureProp.remark = copiedGesturePropProvider.remark; } newGestureProp.id = Uuid().v1(); - context.read().setProps(gestures: [ - ...?schemeProvider.gestures, - newGestureProp, - ]); + context.read().setProps( + gestures: [ + ...?schemeProvider.gestures, + newGestureProp, + ]..sort()); }, ), ] @@ -280,114 +283,144 @@ List _buildDataRows(List? gestures, BuildContext context) } }, selected: selected, - cells: editing ? _buildRowCellsEditing(context, gesture) : _buildRowCellsNormal(context, selected, gesture), + cells: editing ? _buildRowCellsEditing(context) : _buildRowCellsNormal(context, selected, gesture), ); }).toList(); -List _buildRowCellsEditing(BuildContext context, GestureProp gesture) => [ - DButton.dropdown( - enabled: true, - child: DropdownButton( - icon: Icon(Icons.keyboard_arrow_down_rounded), - items: [3, 4, 5] - .map( - (e) => DropdownMenuItem( - child: Text('$e'), - value: e, - ), - ) - .toList(), - value: context.watch().fingers, - onChanged: (value) => context.read().setProps( - fingers: value, - editMode: true, +List _buildRowCellsEditing(BuildContext context) { + var gesture = context.read(); + var schemeTree = context.read().buildSchemeTree(); + var availableFingers = schemeTree.nodes.where((node) => !node.fullFiled).map((e) => e.fingers); + if (!availableFingers.contains(gesture.fingers)) { + availableFingers = [...availableFingers, gesture.fingers!]..sort(); + } + + var availableGestures = schemeTree.nodes + .firstWhere((node) => node.fingers == gesture.fingers) + .nodes + .where((node) => !node.fullFiled) + .map((e) => e.type); + if (!availableGestures.any((type) => type == gesture.gesture)) { + availableGestures = [...availableGestures, gesture.gesture!]..sort(); + } + + 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( + icon: Icon(Icons.keyboard_arrow_down_rounded), + items: availableFingers + .map( + (e) => DropdownMenuItem( + child: Text('$e'), + value: e, ), - isExpanded: true, - ), + ) + .toList(), + value: gesture.fingers, + onChanged: (value) => context.read().setProps( + fingers: value, + editMode: true, + ), + isExpanded: true, ), - DButton.dropdown( - enabled: true, - width: 60.0, - child: DropdownButton( - icon: Icon(Icons.keyboard_arrow_down_rounded), - items: Gesture.values - .map( - (e) => DropdownMenuItem( - child: Text( - '${LocaleKeys.gesture_editor_gestures}.${H.getGestureName(e)}', - textScaleFactor: .8, - ).tr(), - value: e, - ), - ) - .toList(), - value: context.watch().gesture, - onChanged: (value) => context.read().setProps( - gesture: value, - editMode: true, + ), + DButton.dropdown( + enabled: true, + width: 100.0, + child: DropdownButton( + icon: Icon(Icons.keyboard_arrow_down_rounded), + items: availableGestures + .map( + (e) => DropdownMenuItem( + child: Text( + '${LocaleKeys.gesture_editor_gestures}.${H.getGestureName(e)}', + textScaleFactor: .8, + ).tr(), + value: e, ), - isExpanded: true, - ), + ) + .toList(), + value: gesture.gesture, + onChanged: (value) => context.read().setProps( + gesture: value, + editMode: true, + ), + isExpanded: true, ), - DButton.dropdown( - enabled: true, - width: 100.0, - child: DropdownButton( - icon: Icon(Icons.keyboard_arrow_down_rounded), - items: GestureDirection.values - .map( - (e) => DropdownMenuItem( - child: Text( - '${LocaleKeys.gesture_editor_directions}.${H.getGestureDirectionName(e)}', - textScaleFactor: .8, - ).tr(), - value: e, - ), - ) - .toList(), - value: context.watch().direction, - onChanged: (value) => context.read().setProps( - direction: value, - editMode: true, + ), + DButton.dropdown( + enabled: true, + width: 100.0, + child: DropdownButton( + icon: Icon(Icons.keyboard_arrow_down_rounded), + items: availableDirection + .map( + (e) => DropdownMenuItem( + child: Text( + '${LocaleKeys.gesture_editor_directions}.${H.getGestureDirectionName(e)}', + textScaleFactor: .8, + ).tr(), + value: e, ), - isExpanded: true, - ), + ) + .toList(), + value: gesture.direction, + onChanged: (value) => context.read().setProps( + direction: value, + editMode: true, + ), + isExpanded: true, ), - DButton.dropdown( - enabled: true, - width: 100.0, - child: DropdownButton( - icon: Icon(Icons.keyboard_arrow_down_rounded), - items: GestureType.values - .map( - (e) => DropdownMenuItem( - child: Text( - '${LocaleKeys.gesture_editor_types}.${H.getGestureTypeName(e)}', - textScaleFactor: .8, - ).tr(), - value: e, - ), - ) - .toList(), - value: context.watch().type, - onChanged: (value) => context.read().setProps( - type: value, - command: '', - editMode: true, + ), + DButton.dropdown( + enabled: true, + width: 100.0, + child: DropdownButton( + icon: Icon(Icons.keyboard_arrow_down_rounded), + items: GestureType.values + .map( + (e) => DropdownMenuItem( + child: Text( + '${LocaleKeys.gesture_editor_types}.${H.getGestureTypeName(e)}', + textScaleFactor: .8, + ).tr(), + value: e, ), - isExpanded: true, - ), - ), - _buildCommandCellsEditing(context), - TableCellTextField( - initText: gesture.remark, - hint: 'pls input cmd', - onComplete: (value) => context.read().setProps( - remark: value, + ) + .toList(), + value: gesture.type, + onChanged: (value) => context.read().setProps( + type: value, + command: '', editMode: true, ), + isExpanded: true, ), - ].map((e) => DDataCell(e)).toList(); + ), + _buildCommandCellsEditing(context), + TableCellTextField( + initText: gesture.remark, + hint: 'pls input cmd', + onComplete: (value) => context.read().setProps( + remark: value, + editMode: true, + ), + ), + ].map((e) => DDataCell(e)).toList(); +} Widget _buildCommandCellsEditing(BuildContext context) { var gesture = context.read(); diff --git a/app/lib/pages/home.dart b/app/lib/pages/home.dart index 50d1f8c..f425962 100644 --- a/app/lib/pages/home.dart +++ b/app/lib/pages/home.dart @@ -28,7 +28,7 @@ class _HomePageState extends State { "direction": "down", "fingers": 3, "type": "shortcut", - "command": "ctrl+w", + "command": "Control_L+w", "remark": "close current page." }, { @@ -36,7 +36,7 @@ class _HomePageState extends State { "direction": "up", "fingers": 3, "type": "shortcut", - "command": "ctrl+alt+t", + "command": "Control_L+Alt_L+t", "remark": "reopen last closed page." }, { @@ -44,7 +44,7 @@ class _HomePageState extends State { "direction": "in", "fingers": 4, "type": "shortcut", - "command": "ctrl+alt+f", + "command": "Control_L+Alt_L+f", "remark": "search files." }, {