TermRemoteCtl/apps/mobile_app/lib/features/terminal/terminal_interaction_controller.dart

90 lines
2.1 KiB
Dart

import 'package:flutter/foundation.dart';
import 'history_window.dart';
enum TerminalConnectionState {
connecting,
connected,
reconnecting,
disconnected,
}
class TerminalInteractionController extends ChangeNotifier {
TerminalInteractionController({
HistoryWindow historyWindow = const HistoryWindow(
lines: <String>[],
hasMoreAbove: false,
),
}) : _historyWindow = historyWindow;
TerminalConnectionState _connectionState = TerminalConnectionState.connecting;
bool _isFollowingLiveOutput = true;
bool _hasPendingLiveOutput = false;
HistoryWindow _historyWindow;
final List<String> _liveLines = <String>[];
TerminalConnectionState get connectionState => _connectionState;
bool get isFollowingLiveOutput => _isFollowingLiveOutput;
bool get hasPendingLiveOutput => _hasPendingLiveOutput;
bool get canSendInput => _connectionState == TerminalConnectionState.connected;
HistoryWindow get historyWindow => _historyWindow;
List<String> get liveLines => List.unmodifiable(_liveLines);
void markConnecting() {
_connectionState = TerminalConnectionState.connecting;
notifyListeners();
}
void markConnected() {
_connectionState = TerminalConnectionState.connected;
notifyListeners();
}
void markReconnecting() {
_connectionState = TerminalConnectionState.reconnecting;
notifyListeners();
}
void markDisconnected() {
_connectionState = TerminalConnectionState.disconnected;
notifyListeners();
}
void enterScrollback() {
_isFollowingLiveOutput = false;
notifyListeners();
}
void jumpToLive() {
_isFollowingLiveOutput = true;
_hasPendingLiveOutput = false;
notifyListeners();
}
void registerIncomingFrame() {
if (!_isFollowingLiveOutput) {
_hasPendingLiveOutput = true;
notifyListeners();
}
}
void applyFrame(String chunk) {
_liveLines.add(chunk);
notifyListeners();
}
void loadHistory(HistoryWindow historyWindow) {
_historyWindow = historyWindow;
_liveLines
..clear()
..addAll(historyWindow.lines);
_hasPendingLiveOutput = false;
notifyListeners();
}
}