wip: shortcut listener widget.

pull/3/head
DebuggerX 4 years ago
parent 4eb7bc256c
commit 930fd5ddf7

@ -22,6 +22,8 @@ const double defaultButtonHeight = 36;
const List<String> builtInCommands = [ const List<String> builtInCommands = [
'ShowWorkspace', 'ShowWorkspace',
'Handle4Or5FingersSwipeUp',
'Handle4Or5FingersSwipeDown',
'ToggleMaximize', 'ToggleMaximize',
'Minimize', 'Minimize',
'ShowWindow', 'ShowWindow',

@ -0,0 +1,11 @@
import 'package:flutter/services.dart';
/// https://github.com/linuxdeepin/dde-daemon/blob/76be73fbf019cee73983292e1edf47611ed9a219/gesture/manager.go#L386
final Map<LogicalKeyboardKey, String> keyMapper = {
LogicalKeyboardKey.control: 'ctrl',
LogicalKeyboardKey.controlLeft: 'ctrl',
LogicalKeyboardKey.controlRight: 'ctrl',
LogicalKeyboardKey.shift: 'shift',
LogicalKeyboardKey.shiftLeft: 'shift',
LogicalKeyboardKey.shiftRight: 'shift',
};

@ -47,8 +47,7 @@ class GestureEditor extends StatelessWidget {
visible: layoutProvider.localManagerOpened == false, visible: layoutProvider.localManagerOpened == false,
child: DButton( child: DButton(
width: defaultButtonHeight, width: defaultButtonHeight,
onTap: () => onTap: () => H.openPanel(context, PanelType.local_manager),
H.openPanel(context, PanelType.local_manager),
child: Icon( child: Icon(
CupertinoIcons.square_list, CupertinoIcons.square_list,
), ),
@ -77,14 +76,11 @@ class GestureEditor extends StatelessWidget {
Expanded( Expanded(
child: GestureDetector( child: GestureDetector(
onTap: () { onTap: () {
context context.read<GesturePropProvider>().setProps(editMode: false);
.read<GesturePropProvider>()
.setProps(editMode: false);
}, },
child: Container( child: Container(
decoration: BoxDecoration( decoration: BoxDecoration(
borderRadius: borderRadius: BorderRadius.circular(defaultBorderRadius),
BorderRadius.circular(defaultBorderRadius),
border: Border.all( border: Border.all(
width: .2, width: .2,
color: context.t.dividerColor, color: context.t.dividerColor,
@ -92,19 +88,16 @@ class GestureEditor extends StatelessWidget {
), ),
width: double.infinity, width: double.infinity,
clipBehavior: Clip.antiAlias, clipBehavior: Clip.antiAlias,
child: LayoutBuilder(builder: child: LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
(BuildContext context, BoxConstraints constraints) {
return AdaptiveScrollbar( return AdaptiveScrollbar(
controller: verticalCtrl, controller: verticalCtrl,
underColor: Colors.transparent, underColor: Colors.transparent,
sliderDecoration: BoxDecoration( sliderDecoration: BoxDecoration(
borderRadius: borderRadius: BorderRadius.circular(_scrollBarWidth / 2),
BorderRadius.circular(_scrollBarWidth / 2),
color: Colors.grey.withOpacity(.4), color: Colors.grey.withOpacity(.4),
), ),
sliderActiveDecoration: BoxDecoration( sliderActiveDecoration: BoxDecoration(
borderRadius: borderRadius: BorderRadius.circular(_scrollBarWidth / 2),
BorderRadius.circular(_scrollBarWidth / 2),
color: Colors.grey.withOpacity(.6), color: Colors.grey.withOpacity(.6),
), ),
position: ScrollbarPosition.right, position: ScrollbarPosition.right,
@ -114,13 +107,11 @@ class GestureEditor extends StatelessWidget {
width: _scrollBarWidth, width: _scrollBarWidth,
underColor: Colors.transparent, underColor: Colors.transparent,
sliderDecoration: BoxDecoration( sliderDecoration: BoxDecoration(
borderRadius: borderRadius: BorderRadius.circular(_scrollBarWidth / 2),
BorderRadius.circular(_scrollBarWidth / 2),
color: Colors.grey.withOpacity(.4), color: Colors.grey.withOpacity(.4),
), ),
sliderActiveDecoration: BoxDecoration( sliderActiveDecoration: BoxDecoration(
borderRadius: borderRadius: BorderRadius.circular(_scrollBarWidth / 2),
BorderRadius.circular(_scrollBarWidth / 2),
color: Colors.grey.withOpacity(.6), color: Colors.grey.withOpacity(.6),
), ),
controller: horizontalCtrl, controller: horizontalCtrl,
@ -129,58 +120,28 @@ class GestureEditor extends StatelessWidget {
scrollDirection: Axis.horizontal, scrollDirection: Axis.horizontal,
controller: horizontalCtrl, controller: horizontalCtrl,
child: ConstrainedBox( child: ConstrainedBox(
constraints: BoxConstraints( constraints: BoxConstraints(minWidth: constraints.maxWidth),
minWidth: constraints.maxWidth),
child: DDataTable( child: DDataTable(
showBottomBorder: true, showBottomBorder: true,
headingRowHeight: _headingRowHeight, headingRowHeight: _headingRowHeight,
showCheckboxColumn: true, showCheckboxColumn: true,
headerBackgroundColor: headerBackgroundColor: context.t.dialogBackgroundColor,
context.t.dialogBackgroundColor,
verticalScrollController: verticalCtrl, verticalScrollController: verticalCtrl,
dataRowColor: dataRowColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
MaterialStateProperty.resolveWith<Color?>( if (states.contains(MaterialState.hovered)) return context.t.dialogBackgroundColor;
(Set<MaterialState> states) {
if (states.contains(MaterialState.hovered))
return context.t.dialogBackgroundColor;
if (states.contains(MaterialState.selected)) if (states.contains(MaterialState.selected))
return context return context.read<SettingsProvider>().currentActiveColor;
.read<SettingsProvider>()
.currentActiveColor;
return null; return null;
}), }),
columns: [ columns: [
DDataColumn( DDataColumn(label: Text(LocaleKeys.gesture_editor_fingers.tr()), center: true),
label: Text(LocaleKeys DDataColumn(label: Text(LocaleKeys.gesture_editor_gesture.tr()), center: true),
.gesture_editor_fingers DDataColumn(label: Text(LocaleKeys.gesture_editor_direction.tr()), center: true),
.tr()), DDataColumn(label: Text(LocaleKeys.gesture_editor_type.tr()), center: true),
center: true), DDataColumn(label: Text(LocaleKeys.gesture_editor_command.tr())),
DDataColumn( DDataColumn(label: Text(LocaleKeys.gesture_editor_remark.tr())),
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( rows: _buildDataRows(schemeProvider.gestures, context),
schemeProvider.gestures, context),
), ),
), ),
), ),
@ -210,12 +171,9 @@ class GestureEditor extends StatelessWidget {
} }
} }
List<DDataRow> _buildDataRows( List<DDataRow> _buildDataRows(List<GestureProp>? gestures, BuildContext context) => (gestures ?? []).map((gesture) {
List<GestureProp>? gestures, BuildContext context) =>
(gestures ?? []).map((gesture) {
var gesturePropProvider = context.watch<GesturePropProvider>(); var gesturePropProvider = context.watch<GesturePropProvider>();
bool editing = gesturePropProvider == gesture && bool editing = gesturePropProvider == gesture && gesturePropProvider.editMode == true;
gesturePropProvider.editMode == true;
bool selected = gesturePropProvider == gesture && !editing; bool selected = gesturePropProvider == gesture && !editing;
return DDataRow( return DDataRow(
onSelectChanged: (selected) { onSelectChanged: (selected) {
@ -243,15 +201,11 @@ List<DDataRow> _buildDataRows(
} }
}, },
selected: selected, selected: selected,
cells: editing cells: editing ? _buildRowCellsEditing(context, gesture) : _buildRowCellsNormal(context, selected, gesture),
? _buildRowCellsEditing(context, gesture)
: _buildRowCellsNormal(context, selected, gesture),
); );
}).toList(); }).toList();
List<DDataCell> _buildRowCellsEditing( List<DDataCell> _buildRowCellsEditing(BuildContext context, GestureProp gesture) => [
BuildContext context, GestureProp gesture) =>
[
DButton.dropdown( DButton.dropdown(
enabled: true, enabled: true,
child: DropdownButton<int>( child: DropdownButton<int>(
@ -339,6 +293,7 @@ List<DDataCell> _buildRowCellsEditing(
value: context.watch<GesturePropProvider>().type, value: context.watch<GesturePropProvider>().type,
onChanged: (value) => context.read<GesturePropProvider>().setProps( onChanged: (value) => context.read<GesturePropProvider>().setProps(
type: value, type: value,
command: '',
editMode: true, editMode: true,
), ),
isExpanded: true, isExpanded: true,
@ -356,7 +311,6 @@ List<DDataCell> _buildRowCellsEditing(
].map((e) => DDataCell(e)).toList(); ].map((e) => DDataCell(e)).toList();
Widget _buildCommandCellsEditing(BuildContext context) { Widget _buildCommandCellsEditing(BuildContext context) {
'build cmd cell'.sout();
var gesture = context.read<GesturePropProvider>(); var gesture = context.read<GesturePropProvider>();
switch (gesture.type) { switch (gesture.type) {
case GestureType.commandline: case GestureType.commandline:
@ -385,9 +339,7 @@ Widget _buildCommandCellsEditing(BuildContext context) {
), ),
) )
.toList(), .toList(),
value: builtInCommands.contains(gesture.command) value: builtInCommands.contains(gesture.command) ? gesture.command : builtInCommands.first,
? gesture.command
: builtInCommands.first,
onChanged: (value) => context.read<GesturePropProvider>().setProps( onChanged: (value) => context.read<GesturePropProvider>().setProps(
command: value, command: value,
editMode: true, editMode: true,
@ -397,7 +349,7 @@ Widget _buildCommandCellsEditing(BuildContext context) {
); );
case GestureType.shortcut: case GestureType.shortcut:
return TableCellShortcutListener( return TableCellShortcutListener(
width: 150.0, width: 250.0,
initShortcut: gesture.command ?? '', initShortcut: gesture.command ?? '',
onComplete: (value) => context.read<GesturePropProvider>().setProps( onComplete: (value) => context.read<GesturePropProvider>().setProps(
command: value, command: value,
@ -409,9 +361,7 @@ Widget _buildCommandCellsEditing(BuildContext context) {
} }
} }
List<DDataCell> _buildRowCellsNormal( List<DDataCell> _buildRowCellsNormal(BuildContext context, bool selected, GestureProp gesture) => [
BuildContext context, bool selected, GestureProp gesture) =>
[
Center( Center(
child: Text( child: Text(
'${gesture.fingers}', '${gesture.fingers}',
@ -430,7 +380,38 @@ List<DDataCell> _buildRowCellsNormal(
child: Text( child: Text(
'${LocaleKeys.gesture_editor_types}.${H.getGestureTypeName(gesture.type)}', '${LocaleKeys.gesture_editor_types}.${H.getGestureTypeName(gesture.type)}',
).tr()), ).tr()),
Text( gesture.type == GestureType.shortcut
? Row(
mainAxisAlignment: MainAxisAlignment.start,
children: (gesture.command ?? '')
.split('+')
.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: Color(0xff565656),
),
),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 5.0),
child: Text(
e.notNull ? e : LocaleKeys.str_null.tr(),
style: TextStyle(
color: context.watch<SettingsProvider>().currentActiveColor,
),
),
),
),
),
)
.toList(),
)
: Text(
gesture.command ?? '', gesture.command ?? '',
), ),
Text( Text(

@ -126,8 +126,9 @@ class _DButtonState extends State<DButton> {
begin: Alignment.topCenter, begin: Alignment.topCenter,
end: Alignment.bottomCenter, end: Alignment.bottomCenter,
), ),
borderColor: borderColor: _hovering
_hovering ? widget.activeBorderColor ?? context.watch<SettingsProvider>().activeColor : Color(0xff565656), ? (widget.activeBorderColor ?? context.watch<SettingsProvider>().currentActiveColor)
: Color(0xff565656),
borderWidth: 2, borderWidth: 2,
borderRadius: BorderRadius.circular(defaultBorderRadius), borderRadius: BorderRadius.circular(defaultBorderRadius),
child: MouseRegion( child: MouseRegion(

@ -1,7 +1,9 @@
import 'package:dde_gesture_manager/constants/constants.dart'; import 'package:dde_gesture_manager/constants/constants.dart';
import 'package:dde_gesture_manager/constants/keyboard_mapper.dart';
import 'package:dde_gesture_manager/models/settings.provider.dart'; import 'package:dde_gesture_manager/models/settings.provider.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:dde_gesture_manager/extensions.dart'; import 'package:dde_gesture_manager/extensions.dart';
import 'package:flutter/services.dart';
class TableCellShortcutListener extends StatefulWidget { class TableCellShortcutListener extends StatefulWidget {
final double width; final double width;
@ -16,28 +18,62 @@ class TableCellShortcutListener extends StatefulWidget {
}) : super(key: key); }) : super(key: key);
@override @override
_TableCellShortcutListenerState createState() => _TableCellShortcutListenerState createState() => _TableCellShortcutListenerState();
_TableCellShortcutListenerState();
} }
class _TableCellShortcutListenerState extends State<TableCellShortcutListener> { class _TableCellShortcutListenerState extends State<TableCellShortcutListener> {
List<String> _shortcut = []; List<String> _shortcut = [];
bool inputMode = false;
FocusNode _focusNode = FocusNode();
_handleFocusChange() {
if (!_focusNode.hasFocus) {
widget.onComplete(_shortcut.join('+'));
}
}
_tryToAddKey(LogicalKeyboardKey key) {
if (key.keyLabel.length == 1) key.keyLabel.sout();
late String _key;
if (keyMapper.containsKey(key))
_key = keyMapper[key]!;
else if (key.keyLabel.length == 1) _key = key.keyLabel.toLowerCase();
if (!_shortcut.contains(_key)) {
setState(() {
_shortcut.add(_key);
});
}
}
@override @override
void initState() { void initState() {
_shortcut = widget.initShortcut.split('+'); _shortcut = widget.initShortcut.split('+');
_focusNode.addListener(_handleFocusChange);
super.initState(); super.initState();
} }
@override @override
void dispose() {
_focusNode.removeListener(_handleFocusChange);
super.dispose();
}
@override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return RawKeyboardListener( return RawKeyboardListener(
focusNode: FocusNode(), focusNode: _focusNode,
onKey: (evt) { onKey: (evt) {
evt.sout(); RawKeyboard.instance.keysPressed.forEach(_tryToAddKey);
RawKeyboard.instance.keysPressed.sout();
}, },
child: GestureDetector( child: GestureDetector(
onTap: () {}, onTap: () {
setState(() {
_shortcut = [];
inputMode = true;
});
},
child: Focus( child: Focus(
autofocus: true, autofocus: true,
onKeyEvent: (_, __) => KeyEventResult.skipRemainingHandlers, onKeyEvent: (_, __) => KeyEventResult.skipRemainingHandlers,
@ -50,8 +86,7 @@ class _TableCellShortcutListenerState extends State<TableCellShortcutListener> {
border: Border.all( border: Border.all(
width: 2, width: 2,
color: Focus.of(context).hasFocus color: Focus.of(context).hasFocus
? context.watch<SettingsProvider>().activeColor ?? ? context.watch<SettingsProvider>().activeColor ?? Color(0xff565656)
Color(0xff565656)
: Color(0xff565656)), : Color(0xff565656)),
), ),
child: Align( child: Align(
@ -66,27 +101,16 @@ class _TableCellShortcutListenerState extends State<TableCellShortcutListener> {
padding: const EdgeInsets.symmetric(horizontal: 2.0), padding: const EdgeInsets.symmetric(horizontal: 2.0),
child: Container( child: Container(
decoration: BoxDecoration( decoration: BoxDecoration(
borderRadius: BorderRadius.circular( borderRadius: BorderRadius.circular(defaultBorderRadius / 2),
defaultBorderRadius / 2),
color: context.t.dialogBackgroundColor, color: context.t.dialogBackgroundColor,
border: Border.all( border: Border.all(width: 1, color: Color(0xff565656)),
width: 1,
color: Focus.of(context).hasFocus
? context
.watch<SettingsProvider>()
.activeColor ??
Color(0xff565656)
: Color(0xff565656)),
), ),
child: Padding( child: Padding(
padding: padding: const EdgeInsets.symmetric(horizontal: 5.0),
const EdgeInsets.symmetric(horizontal: 5.0),
child: Text( child: Text(
e, e.notNull ? e : LocaleKeys.str_null.tr(),
style: TextStyle( style: TextStyle(
color: context color: context.watch<SettingsProvider>().activeColor,
.watch<SettingsProvider>()
.activeColor,
), ),
), ),
), ),

@ -62,5 +62,8 @@
"delete": "delete", "delete": "delete",
"duplicate": "duplicate", "duplicate": "duplicate",
"apply": "apply" "apply": "apply"
},
"str": {
"null": "Null"
} }
} }

@ -62,5 +62,8 @@
"delete": "删除", "delete": "删除",
"duplicate": "复制", "duplicate": "复制",
"apply": "应用" "apply": "应用"
},
"str": {
"null": "无"
} }
} }
Loading…
Cancel
Save