Skip to content

Commit

Permalink
Virtual keyboard example #141
Browse files Browse the repository at this point in the history
  • Loading branch information
xtyxtyx committed Oct 30, 2022
1 parent 4cc85ca commit 44c9198
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
80 changes: 80 additions & 0 deletions example/lib/src/virtual_keyboard.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import 'package:flutter/material.dart';
import 'package:xterm/xterm.dart';

class VirtualKeyboardView extends StatelessWidget {
const VirtualKeyboardView(this.keyboard, {super.key});

final VirtualKeyboard keyboard;

@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: keyboard,
builder: (context, child) => ToggleButtons(
children: [Text('Ctrl'), Text('Alt'), Text('Shift')],
isSelected: [keyboard.ctrl, keyboard.alt, keyboard.shift],
onPressed: (index) {
switch (index) {
case 0:
keyboard.ctrl = !keyboard.ctrl;
break;
case 1:
keyboard.alt = !keyboard.alt;
break;
case 2:
keyboard.shift = !keyboard.shift;
break;
}
},
),
);
}
}

class VirtualKeyboard extends TerminalInputHandler with ChangeNotifier {
final TerminalInputHandler _inputHandler;

VirtualKeyboard(this._inputHandler);

bool _ctrl = false;

bool get ctrl => _ctrl;

set ctrl(bool value) {
if (_ctrl != value) {
_ctrl = value;
notifyListeners();
}
}

bool _shift = false;

bool get shift => _shift;

set shift(bool value) {
if (_shift != value) {
_shift = value;
notifyListeners();
}
}

bool _alt = false;

bool get alt => _alt;

set alt(bool value) {
if (_alt != value) {
_alt = value;
notifyListeners();
}
}

@override
String? call(TerminalInputEvent event) {
return _inputHandler.call(event.copyWith(
ctrl: event.ctrl || _ctrl,
shift: event.shift || _shift,
alt: event.alt || _alt,
));
}
}
14 changes: 11 additions & 3 deletions example/lib/ssh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';
import 'dart:typed_data';

import 'package:dartssh2/dartssh2.dart';
import 'package:example/src/virtual_keyboard.dart';
import 'package:flutter/cupertino.dart';
import 'package:xterm/xterm.dart';

Expand Down Expand Up @@ -34,7 +35,9 @@ class MyHomePage extends StatefulWidget {
}

class _MyHomePageState extends State<MyHomePage> {
final terminal = Terminal();
late final terminal = Terminal(inputHandler: keyboard);

final keyboard = VirtualKeyboard(defaultInputHandler);

var title = host;

Expand Down Expand Up @@ -96,8 +99,13 @@ class _MyHomePageState extends State<MyHomePage> {
backgroundColor:
CupertinoTheme.of(context).barBackgroundColor.withOpacity(0.5),
),
child: TerminalView(
terminal,
child: Column(
children: [
Expanded(
child: TerminalView(terminal),
),
VirtualKeyboardView(keyboard),
],
),
);
}
Expand Down

0 comments on commit 44c9198

Please sign in to comment.