From 4eb7bc256ceb70b9639ea770ab782d811a5387ff Mon Sep 17 00:00:00 2001 From: debuggerx Date: Tue, 9 Nov 2021 18:38:44 +0800 Subject: [PATCH] wip: shortcut listener widget. --- app/lib/constants/constants.dart | 15 ++ app/lib/pages/gesture_editor.dart | 160 +++++++++++++++++----- app/lib/widgets/table_cell_shortcut_listener.dart | 105 ++++++++++++++ app/lib/widgets/table_cell_text_field.dart | 4 +- 4 files changed, 251 insertions(+), 33 deletions(-) create mode 100644 app/lib/widgets/table_cell_shortcut_listener.dart diff --git a/app/lib/constants/constants.dart b/app/lib/constants/constants.dart index 848ac13..dfee4a3 100644 --- a/app/lib/constants/constants.dart +++ b/app/lib/constants/constants.dart @@ -20,6 +20,21 @@ const double defaultBorderRadius = 8; const double defaultButtonHeight = 36; +const List builtInCommands = [ + 'ShowWorkspace', + 'ToggleMaximize', + 'Minimize', + 'ShowWindow', + 'ShowAllWindow', + 'SwitchApplication', + 'ReverseSwitchApplication', + 'SwitchWorkspace', + 'ReverseSwitchWorkspace', + 'SplitWindowLeft', + 'SplitWindowRight', + 'MoveWindow', +]; + enum PanelType { local_manager, market, diff --git a/app/lib/pages/gesture_editor.dart b/app/lib/pages/gesture_editor.dart index 2d73620..e5d00ff 100644 --- a/app/lib/pages/gesture_editor.dart +++ b/app/lib/pages/gesture_editor.dart @@ -8,6 +8,7 @@ import 'package:dde_gesture_manager/models/settings.provider.dart'; import 'package:dde_gesture_manager/utils/helper.dart'; import 'package:dde_gesture_manager/widgets/dde_button.dart'; import 'package:dde_gesture_manager/widgets/dde_data_table.dart'; +import 'package:dde_gesture_manager/widgets/table_cell_shortcut_listener.dart'; import 'package:dde_gesture_manager/widgets/table_cell_text_field.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; @@ -46,7 +47,8 @@ class GestureEditor extends StatelessWidget { visible: layoutProvider.localManagerOpened == false, child: DButton( width: defaultButtonHeight, - onTap: () => H.openPanel(context, PanelType.local_manager), + onTap: () => + H.openPanel(context, PanelType.local_manager), child: Icon( CupertinoIcons.square_list, ), @@ -75,11 +77,14 @@ class GestureEditor extends StatelessWidget { Expanded( child: GestureDetector( onTap: () { - context.read().setProps(editMode: false); + context + .read() + .setProps(editMode: false); }, child: Container( decoration: BoxDecoration( - borderRadius: BorderRadius.circular(defaultBorderRadius), + borderRadius: + BorderRadius.circular(defaultBorderRadius), border: Border.all( width: .2, color: context.t.dividerColor, @@ -87,16 +92,19 @@ class GestureEditor extends StatelessWidget { ), width: double.infinity, clipBehavior: Clip.antiAlias, - child: LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) { + child: LayoutBuilder(builder: + (BuildContext context, BoxConstraints constraints) { return AdaptiveScrollbar( controller: verticalCtrl, underColor: Colors.transparent, sliderDecoration: BoxDecoration( - borderRadius: BorderRadius.circular(_scrollBarWidth / 2), + borderRadius: + BorderRadius.circular(_scrollBarWidth / 2), color: Colors.grey.withOpacity(.4), ), sliderActiveDecoration: BoxDecoration( - borderRadius: BorderRadius.circular(_scrollBarWidth / 2), + borderRadius: + BorderRadius.circular(_scrollBarWidth / 2), color: Colors.grey.withOpacity(.6), ), position: ScrollbarPosition.right, @@ -106,11 +114,13 @@ class GestureEditor extends StatelessWidget { width: _scrollBarWidth, underColor: Colors.transparent, sliderDecoration: BoxDecoration( - borderRadius: BorderRadius.circular(_scrollBarWidth / 2), + borderRadius: + BorderRadius.circular(_scrollBarWidth / 2), color: Colors.grey.withOpacity(.4), ), sliderActiveDecoration: BoxDecoration( - borderRadius: BorderRadius.circular(_scrollBarWidth / 2), + borderRadius: + BorderRadius.circular(_scrollBarWidth / 2), color: Colors.grey.withOpacity(.6), ), controller: horizontalCtrl, @@ -119,28 +129,58 @@ class GestureEditor extends StatelessWidget { scrollDirection: Axis.horizontal, controller: horizontalCtrl, child: ConstrainedBox( - constraints: BoxConstraints(minWidth: constraints.maxWidth), + constraints: BoxConstraints( + minWidth: constraints.maxWidth), child: DDataTable( showBottomBorder: true, headingRowHeight: _headingRowHeight, showCheckboxColumn: true, - headerBackgroundColor: context.t.dialogBackgroundColor, + headerBackgroundColor: + context.t.dialogBackgroundColor, verticalScrollController: verticalCtrl, - dataRowColor: MaterialStateProperty.resolveWith((Set states) { - if (states.contains(MaterialState.hovered)) return context.t.dialogBackgroundColor; + dataRowColor: + MaterialStateProperty.resolveWith( + (Set states) { + if (states.contains(MaterialState.hovered)) + return context.t.dialogBackgroundColor; if (states.contains(MaterialState.selected)) - return context.read().currentActiveColor; + return context + .read() + .currentActiveColor; return null; }), columns: [ - DDataColumn(label: Text(LocaleKeys.gesture_editor_fingers.tr()), center: true), - DDataColumn(label: Text(LocaleKeys.gesture_editor_gesture.tr()), center: true), - DDataColumn(label: Text(LocaleKeys.gesture_editor_direction.tr()), center: true), - DDataColumn(label: Text(LocaleKeys.gesture_editor_type.tr()), center: true), - DDataColumn(label: Text(LocaleKeys.gesture_editor_command.tr())), - DDataColumn(label: Text(LocaleKeys.gesture_editor_remark.tr())), + DDataColumn( + label: Text(LocaleKeys + .gesture_editor_fingers + .tr()), + center: true), + DDataColumn( + label: Text(LocaleKeys + .gesture_editor_gesture + .tr()), + center: true), + DDataColumn( + label: Text(LocaleKeys + .gesture_editor_direction + .tr()), + center: true), + DDataColumn( + label: Text(LocaleKeys + .gesture_editor_type + .tr()), + center: true), + DDataColumn( + label: Text(LocaleKeys + .gesture_editor_command + .tr())), + DDataColumn( + label: Text(LocaleKeys + .gesture_editor_remark + .tr())), ], - rows: _buildDataRows(schemeProvider.gestures, context), + rows: _buildDataRows( + schemeProvider.gestures, context), ), ), ), @@ -170,9 +210,12 @@ class GestureEditor extends StatelessWidget { } } -List _buildDataRows(List? gestures, BuildContext context) => (gestures ?? []).map((gesture) { +List _buildDataRows( + List? gestures, BuildContext context) => + (gestures ?? []).map((gesture) { var gesturePropProvider = context.watch(); - bool editing = gesturePropProvider == gesture && gesturePropProvider.editMode == true; + bool editing = gesturePropProvider == gesture && + gesturePropProvider.editMode == true; bool selected = gesturePropProvider == gesture && !editing; return DDataRow( onSelectChanged: (selected) { @@ -200,11 +243,15 @@ List _buildDataRows(List? gestures, BuildContext context) } }, selected: selected, - cells: editing ? _buildRowCellsEditing(context, gesture) : _buildRowCellsNormal(context, selected, gesture), + cells: editing + ? _buildRowCellsEditing(context, gesture) + : _buildRowCellsNormal(context, selected, gesture), ); }).toList(); -List _buildRowCellsEditing(BuildContext context, GestureProp gesture) => [ +List _buildRowCellsEditing( + BuildContext context, GestureProp gesture) => + [ DButton.dropdown( enabled: true, child: DropdownButton( @@ -297,25 +344,74 @@ List _buildRowCellsEditing(BuildContext context, GestureProp gesture) isExpanded: true, ), ), + _buildCommandCellsEditing(context), TableCellTextField( - initText: gesture.command, + initText: gesture.remark, hint: 'pls input cmd', onComplete: (value) => context.read().setProps( - command: value, + remark: value, editMode: true, ), ), - TableCellTextField( - initText: gesture.remark, + ].map((e) => DDataCell(e)).toList(); + +Widget _buildCommandCellsEditing(BuildContext context) { + 'build cmd cell'.sout(); + var gesture = context.read(); + switch (gesture.type) { + case GestureType.commandline: + return TableCellTextField( + initText: gesture.command, hint: 'pls input cmd', onComplete: (value) => context.read().setProps( - remark: value, + command: value, editMode: true, ), - ), - ].map((e) => DDataCell(e)).toList(); + ); + case GestureType.built_in: + return DButton.dropdown( + enabled: true, + width: 250.0, + child: DropdownButton( + icon: Icon(Icons.keyboard_arrow_down_rounded), + items: builtInCommands + .map( + (e) => DropdownMenuItem( + child: Text( + e, + textScaleFactor: .8, + ), + value: e, + ), + ) + .toList(), + value: builtInCommands.contains(gesture.command) + ? gesture.command + : builtInCommands.first, + onChanged: (value) => context.read().setProps( + command: value, + editMode: true, + ), + isExpanded: true, + ), + ); + case GestureType.shortcut: + return TableCellShortcutListener( + width: 150.0, + initShortcut: gesture.command ?? '', + onComplete: (value) => context.read().setProps( + command: value, + editMode: true, + ), + ); + default: + throw Exception('Unknown gesture command type.'); + } +} -List _buildRowCellsNormal(BuildContext context, bool selected, GestureProp gesture) => [ +List _buildRowCellsNormal( + BuildContext context, bool selected, GestureProp gesture) => + [ Center( child: Text( '${gesture.fingers}', diff --git a/app/lib/widgets/table_cell_shortcut_listener.dart b/app/lib/widgets/table_cell_shortcut_listener.dart new file mode 100644 index 0000000..fc8a963 --- /dev/null +++ b/app/lib/widgets/table_cell_shortcut_listener.dart @@ -0,0 +1,105 @@ +import 'package:dde_gesture_manager/constants/constants.dart'; +import 'package:dde_gesture_manager/models/settings.provider.dart'; +import 'package:flutter/material.dart'; +import 'package:dde_gesture_manager/extensions.dart'; + +class TableCellShortcutListener extends StatefulWidget { + final double width; + final String initShortcut; + final Function(String shortcut) onComplete; + + const TableCellShortcutListener({ + Key? key, + this.width = 150.0, + required this.initShortcut, + required this.onComplete, + }) : super(key: key); + + @override + _TableCellShortcutListenerState createState() => + _TableCellShortcutListenerState(); +} + +class _TableCellShortcutListenerState extends State { + List _shortcut = []; + + @override + void initState() { + _shortcut = widget.initShortcut.split('+'); + super.initState(); + } + + @override + Widget build(BuildContext context) { + return RawKeyboardListener( + focusNode: FocusNode(), + onKey: (evt) { + evt.sout(); + }, + child: GestureDetector( + onTap: () {}, + child: Focus( + autofocus: true, + onKeyEvent: (_, __) => KeyEventResult.skipRemainingHandlers, + child: Container( + height: kMinInteractiveDimension * .86, + width: widget.width, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(defaultBorderRadius), + color: Colors.grey.withOpacity(.3), + border: Border.all( + width: 2, + color: Focus.of(context).hasFocus + ? context.watch().activeColor ?? + Color(0xff565656) + : Color(0xff565656)), + ), + child: Align( + alignment: Alignment.centerLeft, + child: Padding( + padding: EdgeInsets.only(left: 5), + child: Row( + mainAxisAlignment: MainAxisAlignment.start, + children: _shortcut + .map( + (e) => Padding( + padding: const EdgeInsets.symmetric(horizontal: 2.0), + child: Container( + decoration: BoxDecoration( + borderRadius: BorderRadius.circular( + defaultBorderRadius / 2), + color: context.t.dialogBackgroundColor, + border: Border.all( + width: 1, + color: Focus.of(context).hasFocus + ? context + .watch() + .activeColor ?? + Color(0xff565656) + : Color(0xff565656)), + ), + child: Padding( + padding: + const EdgeInsets.symmetric(horizontal: 5.0), + child: Text( + e, + style: TextStyle( + color: context + .watch() + .activeColor, + ), + ), + ), + ), + ), + ) + .toList(), + ), + ), + ), + ), + ), + ), + ); + } +} diff --git a/app/lib/widgets/table_cell_text_field.dart b/app/lib/widgets/table_cell_text_field.dart index 0e32bbd..3404173 100644 --- a/app/lib/widgets/table_cell_text_field.dart +++ b/app/lib/widgets/table_cell_text_field.dart @@ -20,7 +20,9 @@ class TableCellTextField extends StatefulWidget { } class _TableCellTextFieldState extends State { - final FocusNode _focusNode = FocusNode(); + final FocusNode _focusNode = FocusNode( + onKeyEvent: (_, __) => KeyEventResult.skipRemainingHandlers, + ); final TextEditingController _controller = TextEditingController(); @override