Skip to content

Commit

Permalink
wip: Use HotKeyModifier to replace ModifierKey & Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lijy91 committed Mar 2, 2024
1 parent cc6e93d commit 8c5a2d2
Show file tree
Hide file tree
Showing 17 changed files with 172 additions and 120 deletions.
4 changes: 2 additions & 2 deletions packages/hotkey_manager/example/lib/pages/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class _HomePageState extends State<HomePage> {
List<HotKey> _registeredHotKeyList = [];

void _keyDownHandler(HotKey hotKey) {
String log = 'keyDown ${hotKey.physicalKey.debugName} (${hotKey.scope})';
String log = 'keyDown ${hotKey.debugName} (${hotKey.scope})';
BotToast.showText(text: log);
if (kDebugMode) {
print(log);
}
}

void _keyUpHandler(HotKey hotKey) {
String log = 'keyUp ${hotKey.physicalKey.debugName} (${hotKey.scope})';
String log = 'keyUp ${hotKey.debugName} (${hotKey.scope})';
BotToast.showText(text: log);
if (kDebugMode) {
print(log);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 1430;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
331C80D4294CF70F00263BE5 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
13 changes: 5 additions & 8 deletions packages/hotkey_manager/lib/src/hotkey_manager.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:async';
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:hotkey_manager_platform_interface/hotkey_manager_platform_interface.dart';

Expand Down Expand Up @@ -65,17 +64,15 @@ class HotKeyManager {
}

if (keyEvent is KeyDownEvent) {
final physicalKeysPressed = HardwareKeyboard.instance.physicalKeysPressed;
HotKey? hotKey = _hotKeyList.firstWhereOrNull(
(e) {
List<ModifierKey>? pressedModifierKeys =
ModifierKey.values // pressed modifier keys
.where((e) => e.isModifierPressed)
.toList();
List<HotKeyModifier>? modifiers = HotKeyModifier.values
.where((e) => e.physicalKeys.any(physicalKeysPressed.contains))
.toList();
return e.scope == HotKeyScope.inapp &&
keyEvent.logicalKey == e.logicalKey &&
pressedModifierKeys.every(
(modifierKey) => (e.modifiers ?? []).contains(modifierKey),
);
modifiers.every((e.modifiers ?? []).contains);
},
);
if (hotKey != null) {
Expand Down
13 changes: 7 additions & 6 deletions packages/hotkey_manager/lib/src/widgets/hotkey_recorder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,21 @@ class _HotKeyRecorderState extends State<HotKeyRecorder> {

bool _handleKeyEvent(KeyEvent keyEvent) {
if (keyEvent is KeyUpEvent) return false;
final physicalKeysPressed = HardwareKeyboard.instance.physicalKeysPressed;
PhysicalKeyboardKey? key = keyEvent.physicalKey;
List<ModifierKey>? pressedModifierKeys = ModifierKey.values
.where((e) => e.isModifierPressed) // pressed modifier keys
List<HotKeyModifier>? modifiers = HotKeyModifier.values
.where((e) => e.physicalKeys.any(physicalKeysPressed.contains))
.toList();
if (pressedModifierKeys.isNotEmpty) {
// Remove the modifier keys from the list of pressed keys
pressedModifierKeys = pressedModifierKeys
if (modifiers.isNotEmpty) {
// Remove the key from the modifiers list if it is a modifier
modifiers = modifiers
.where((e) => !e.physicalKeys.contains(key)) // linewrap
.toList();
}
_hotKey = HotKey(
identifier: widget.initalHotKey?.identifier,
key: key,
modifiers: pressedModifierKeys,
modifiers: modifiers,
scope: widget.initalHotKey?.scope ?? HotKeyScope.system,
);
widget.onHotKeyRecorded(_hotKey!);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hotkey_manager_platform_interface/hotkey_manager_platform_interface.dart';

class _VirtualKeyView extends StatelessWidget {
Expand Down Expand Up @@ -51,9 +50,9 @@ class HotKeyVirtualView extends StatelessWidget {
return Wrap(
spacing: 8,
children: [
for (ModifierKey modifierKey in hotKey.modifiers ?? [])
for (HotKeyModifier modifier in hotKey.modifiers ?? [])
_VirtualKeyView(
keyLabel: modifierKey.keyLabel,
keyLabel: modifier.physicalKeys.first.keyLabel,
),
_VirtualKeyView(
keyLabel: hotKey.physicalKey.keyLabel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ guint get_mods(const std::vector<std::string>& modifiers) {
guint mods = 0;
for (int i = 0; i < modifiers.size(); i++) {
guint mod = 0;
if (modifiers[i] == "shiftModifier")
mod = GDK_SHIFT_MASK;
else if (modifiers[i] == "controlModifier")
mod = GDK_CONTROL_MASK;
else if (modifiers[i] == "altModifier")
if (modifiers[i] == "alt")
mod = GDK_MOD1_MASK;
else if (modifiers[i] == "metaModifier")
else if (modifiers[i] == "capsLock")
mod = GDK_LOCK_MASK;
else if (modifiers[i] == "control")
mod = GDK_CONTROL_MASK;
else if (modifiers[i] == "meta")
mod = GDK_META_MASK;
else if (modifiers[i] == "shift")
mod = GDK_SHIFT_MASK;
mods = mods | mod;
}
return mods;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
extension NSEvent.ModifierFlags {
public init(pluginModifiers: Array<String>) {
self.init()
if (pluginModifiers.contains("capsLockModifier")) {
insert(.capsLock)
if (pluginModifiers.contains("alt")) {
insert(.option)
}
if (pluginModifiers.contains("shiftModifier")) {
insert(.shift)
if (pluginModifiers.contains("capsLock")) {
insert(.capsLock)
}
if (pluginModifiers.contains("controlModifier")) {
if (pluginModifiers.contains("control")) {
insert(.control)
}
if (pluginModifiers.contains("altModifier")) {
insert(.option)
if (pluginModifiers.contains("fn")) {
insert(.function)
}
if (pluginModifiers.contains("metaModifier")) {
if (pluginModifiers.contains("meta")) {
insert(.command)
}
if (pluginModifiers.contains("functionModifier")) {
insert(.function)
if (pluginModifiers.contains("shift")) {
insert(.shift)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ library hotkey_manager_platform_interface;

export 'src/enums/key_code.dart';
export 'src/enums/key_modifier.dart';
export 'src/extensions/modifier_key.dart';
export 'src/extensions/physical_keyboard_key.dart';
export 'src/extensions/keyboard_key.dart';
export 'src/hotkey.dart';
export 'src/hotkey_manager_method_channel.dart';
export 'src/hotkey_manager_platform_interface.dart';
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'dart:io';

import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -283,6 +285,9 @@ const Map<KeyCode, LogicalKeyboardKey> _knownLogicalKeys =
KeyCode.control: LogicalKeyboardKey.control,
};

@Deprecated(
'No longer supported, Use `PhysicalKeyboardKey` instead. ',
)
enum KeyCode {
// none,
hyper,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use, deprecated_member_use_from_same_package

import 'dart:io';

import 'package:flutter/foundation.dart' show kIsWeb;
Expand Down Expand Up @@ -52,6 +54,9 @@ final Map<KeyModifier, String> _knownKeyLabels = <KeyModifier, String>{
KeyModifier.fn: 'fn',
};

@Deprecated(
'No longer supported, Use `HotKeyModifier` instead. ',
)
enum KeyModifier {
capsLock,
shift,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:uni_platform/uni_platform.dart';

final Map<PhysicalKeyboardKey, String> _knownKeyLabels =
<PhysicalKeyboardKey, String>{
Expand Down Expand Up @@ -90,8 +91,14 @@ final Map<PhysicalKeyboardKey, String> _knownKeyLabels =
PhysicalKeyboardKey.fn: 'fn',
};

extension PhysicalKeyboardKeyExt on PhysicalKeyboardKey {
extension KeyboardKeyExt on KeyboardKey {
String get keyLabel {
return _knownKeyLabels[this] ?? debugName ?? 'Unknown';
PhysicalKeyboardKey? physicalKey;
if (this is LogicalKeyboardKey) {
physicalKey = (this as LogicalKeyboardKey).physicalKey;
} else if (this is PhysicalKeyboardKey) {
physicalKey = this as PhysicalKeyboardKey;
}
return _knownKeyLabels[physicalKey] ?? physicalKey?.debugName ?? 'Unknown';
}
}

This file was deleted.

73 changes: 66 additions & 7 deletions packages/hotkey_manager_platform_interface/lib/src/hotkey.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/services.dart';
import 'package:hotkey_manager_platform_interface/hotkey_manager_platform_interface.dart';
import 'package:json_annotation/json_annotation.dart';
// ignore: implementation_imports
import 'package:uni_platform/src/extensions/keyboard_key.dart';
import 'package:uni_platform/uni_platform.dart';
import 'package:uuid/uuid.dart';

part 'hotkey.g.dart';
Expand All @@ -10,6 +10,40 @@ const _uuid = Uuid();

typedef HotKeyHandler = void Function(HotKey hotKey);

enum HotKeyModifier {
alt([
PhysicalKeyboardKey.altLeft,
PhysicalKeyboardKey.altRight,
]),
capsLock([
PhysicalKeyboardKey.capsLock,
]),
control([
PhysicalKeyboardKey.controlLeft,
PhysicalKeyboardKey.controlRight,
]),
fn([
PhysicalKeyboardKey.fn,
]),
meta([
PhysicalKeyboardKey.metaLeft,
PhysicalKeyboardKey.metaRight,
]),
shift([
PhysicalKeyboardKey.shiftLeft,
PhysicalKeyboardKey.shiftRight,
]);

const HotKeyModifier(this.physicalKeys);

final List<PhysicalKeyboardKey> physicalKeys;

bool get isModifierPressed {
final physicalKeysPressed = HardwareKeyboard.instance.physicalKeysPressed;
return physicalKeys.any(physicalKeysPressed.contains);
}
}

enum HotKeyScope {
system,
inapp,
Expand All @@ -26,11 +60,14 @@ class HotKey {
this.scope = HotKeyScope.system,
}) : identifier = identifier ?? _uuid.v4();

factory HotKey.fromJson(Map<String, dynamic> json) => _$HotKeyFromJson(json);
factory HotKey.fromJson(Map<String, dynamic> json) {
if (json['keyCode'] is String) return _$HotKeyFromOldJson(json);
return _$HotKeyFromJson(json);
}

final String identifier;
final KeyboardKey key;
final List<ModifierKey>? modifiers;
final List<HotKeyModifier>? modifiers;
final HotKeyScope scope;

LogicalKeyboardKey get logicalKey {
Expand All @@ -57,9 +94,14 @@ class HotKey {
);
}

@override
String toString() {
return '${modifiers?.map((e) => e.name).join('')}${key.hashCode}';
String get debugName {
return [
...(modifiers ?? []).map((e) {
final firstPhysicalKey = e.physicalKeys.first;
return firstPhysicalKey.debugName;
}),
physicalKey.debugName,
].join(' + ');
}

Map<String, dynamic> toJson() => _$HotKeyToJson(this);
Expand Down Expand Up @@ -103,3 +145,20 @@ class _KeyboardKeyConverter
}..removeWhere((key, value) => value == null);
}
}

// Convert HotKey from old JSON format
HotKey _$HotKeyFromOldJson(Map<String, dynamic> json) {
LogicalKeyboardKey logicalKey =
KeyCodeParser.parse(json['keyCode']).logicalKey;
return HotKey(
identifier: json['identifier'] as String,
key: logicalKey.physicalKey!,
modifiers: ((json['modifiers'] as List<dynamic>?) ?? []).map((modifier) {
return HotKeyModifier.values.firstWhere((e) => e.name == modifier);
}).toList(),
scope: HotKeyScope.values.firstWhere(
(e) => e.name == json['scope'] as String,
orElse: () => HotKeyScope.system,
),
);
}
Loading

0 comments on commit 8c5a2d2

Please sign in to comment.