Skip to content

Commit

Permalink
opt.: more virtual keys
Browse files Browse the repository at this point in the history
Fixes #595
  • Loading branch information
lollipopkit committed Sep 28, 2024
1 parent 02444fc commit a33ee20
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 102 deletions.
214 changes: 114 additions & 100 deletions lib/data/model/ssh/virtual_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:xterm/core.dart';

part 'virtual_key.g.dart';

enum VirtualKeyFunc { toggleIME, backspace, clipboard, snippet, file }

@HiveType(typeId: 4)
enum VirtKey {
@HiveField(0)
Expand Down Expand Up @@ -38,23 +40,80 @@ enum VirtKey {
@HiveField(14)
pgup,
@HiveField(15)
pgdn;
pgdn,
@HiveField(16)
slash,
@HiveField(17)
backSlash,
@HiveField(18)
underscore,
@HiveField(19)
plus,
@HiveField(20)
equal,
@HiveField(21)
minus,
@HiveField(22)
parenLeft,
@HiveField(23)
parenRight,
@HiveField(24)
bracketLeft,
@HiveField(25)
bracketRight,
@HiveField(26)
braceLeft,
@HiveField(27)
braceRight,
@HiveField(28)
chevronLeft,
@HiveField(29)
chevronRight,
@HiveField(30)
colon,
@HiveField(31)
semicolon,
;
}

extension VirtKeyX on VirtKey {
/// Used for input to terminal
String? get inputRaw => switch (this) {
VirtKey.slash => '/',
VirtKey.backSlash => '\\',
VirtKey.underscore => '_',
VirtKey.plus => '+',
VirtKey.equal => '=',
VirtKey.minus => '-',
VirtKey.parenLeft => '(',
VirtKey.parenRight => ')',
VirtKey.bracketLeft => '[',
VirtKey.bracketRight => ']',
VirtKey.braceLeft => '{',
VirtKey.braceRight => '}',
VirtKey.chevronLeft => '<',
VirtKey.chevronRight => '>',
VirtKey.colon => ':',
VirtKey.semicolon => ';',
_ => null,
};

/// Used for displaying on UI
String get text {
switch (this) {
case VirtKey.pgdn:
return 'PgDn';
case VirtKey.pgup:
return 'PgUp';
default:
if (name.length > 1) {
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
return name;
final t = inputRaw;
if (t != null) return t;

if (this == VirtKey.pgdn) return 'PgDn';
if (this == VirtKey.pgup) return 'PgUp';

if (name.length > 1) {
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
return name;
}

static final defaultOrder = [
/// Default order of virtual keys
static const defaultOrder = [
VirtKey.esc,
VirtKey.alt,
VirtKey.home,
Expand All @@ -71,99 +130,56 @@ enum VirtKey {
VirtKey.ime,
];

TerminalKey? get key {
switch (this) {
case VirtKey.esc:
return TerminalKey.escape;
case VirtKey.alt:
return TerminalKey.alt;
case VirtKey.home:
return TerminalKey.home;
case VirtKey.up:
return TerminalKey.arrowUp;
case VirtKey.end:
return TerminalKey.end;
case VirtKey.tab:
return TerminalKey.tab;
case VirtKey.ctrl:
return TerminalKey.control;
case VirtKey.left:
return TerminalKey.arrowLeft;
case VirtKey.down:
return TerminalKey.arrowDown;
case VirtKey.right:
return TerminalKey.arrowRight;
case VirtKey.pgup:
return TerminalKey.pageUp;
case VirtKey.pgdn:
return TerminalKey.pageDown;
default:
return null;
}
}
/// Corresponding [TerminalKey]
TerminalKey? get key => switch (this) {
VirtKey.esc => TerminalKey.escape,
VirtKey.alt => TerminalKey.alt,
VirtKey.home => TerminalKey.home,
VirtKey.up => TerminalKey.arrowUp,
VirtKey.end => TerminalKey.end,
VirtKey.tab => TerminalKey.tab,
VirtKey.ctrl => TerminalKey.control,
VirtKey.left => TerminalKey.arrowLeft,
VirtKey.down => TerminalKey.arrowDown,
VirtKey.right => TerminalKey.arrowRight,
VirtKey.pgup => TerminalKey.pageUp,
VirtKey.pgdn => TerminalKey.pageDown,
_ => null,
};

IconData? get icon {
switch (this) {
case VirtKey.up:
return Icons.arrow_upward;
case VirtKey.left:
return Icons.arrow_back;
case VirtKey.down:
return Icons.arrow_downward;
case VirtKey.right:
return Icons.arrow_forward;
case VirtKey.sftp:
return Icons.file_open;
case VirtKey.snippet:
return Icons.code;
case VirtKey.clipboard:
return Icons.paste;
case VirtKey.ime:
return Icons.keyboard;
default:
return null;
}
}
/// Icons for virtual keys
IconData? get icon => switch (this) {
VirtKey.up => Icons.arrow_upward,
VirtKey.left => Icons.arrow_back,
VirtKey.down => Icons.arrow_downward,
VirtKey.right => Icons.arrow_forward,
VirtKey.sftp => Icons.file_open,
VirtKey.snippet => Icons.code,
VirtKey.clipboard => Icons.paste,
VirtKey.ime => Icons.keyboard,
_ => null,
};

// Use [VirtualKeyFunc] instead of [VirtKey]
// This can help linter to enum all [VirtualKeyFunc]
// and make sure all [VirtualKeyFunc] are handled
VirtualKeyFunc? get func {
switch (this) {
case VirtKey.sftp:
return VirtualKeyFunc.file;
case VirtKey.snippet:
return VirtualKeyFunc.snippet;
case VirtKey.clipboard:
return VirtualKeyFunc.clipboard;
case VirtKey.ime:
return VirtualKeyFunc.toggleIME;
default:
return null;
}
}
VirtualKeyFunc? get func => switch (this) {
VirtKey.sftp => VirtualKeyFunc.file,
VirtKey.snippet => VirtualKeyFunc.snippet,
VirtKey.clipboard => VirtualKeyFunc.clipboard,
VirtKey.ime => VirtualKeyFunc.toggleIME,
_ => null,
};

bool get toggleable {
switch (this) {
case VirtKey.alt:
case VirtKey.ctrl:
return true;
default:
return false;
}
}
bool get toggleable => switch (this) {
VirtKey.alt || VirtKey.ctrl => true,
_ => false,
};

bool get canLongPress {
switch (this) {
case VirtKey.up:
case VirtKey.left:
case VirtKey.down:
case VirtKey.right:
return true;
default:
return false;
}
}
bool get canLongPress => switch (this) {
VirtKey.up || VirtKey.left || VirtKey.down || VirtKey.right => true,
_ => false,
};

String? get help => switch (this) {
VirtKey.sftp => l10n.virtKeyHelpSFTP,
Expand All @@ -172,5 +188,3 @@ enum VirtKey {
_ => null,
};
}

enum VirtualKeyFunc { toggleIME, backspace, clipboard, snippet, file }
80 changes: 80 additions & 0 deletions lib/data/model/ssh/virtual_key.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/data/store/setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class SettingStore extends PersistentStore {

late final sshVirtKeys = listProperty(
'sshVirtKeys',
VirtKey.defaultOrder.map((e) => e.index).toList(),
VirtKeyX.defaultOrder.map((e) => e.index).toList(),
);

late final netViewType = property('netViewType', NetViewType.speed);
Expand Down
7 changes: 6 additions & 1 deletion lib/view/page/ssh/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class SSHPageState extends State<SSHPage>
);
}
final rows = _virtKeysList
.map((e) => Row(children: e.map((f) => _buildVirtKeyItem(f)).toList()))
.map((e) => Row(children: e.map(_buildVirtKeyItem).toList()))
.toList();
return Column(
mainAxisSize: MainAxisSize.min,
Expand Down Expand Up @@ -280,6 +280,11 @@ class SSHPageState extends State<SSHPage>
HapticFeedback.mediumImpact();
_doVirtualKeyInput(item.key!);
}
final inputRaw = item.inputRaw;
if (inputRaw != null) {
HapticFeedback.mediumImpact();
_terminal.textInput(inputRaw);
}
}

void _doVirtualKeyInput(TerminalKey key) {
Expand Down

0 comments on commit a33ee20

Please sign in to comment.