diff --git a/lib/main.dart b/lib/main.dart index 4319585..c1fb664 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,3 @@ -import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'; @@ -43,7 +42,6 @@ class _InputSyncPageState extends State { final inputFocusNode = FocusNode(); WebSocketChannel? channel; - Timer? syncDebounce; String lastSyncedText = ''; bool connected = false; @@ -51,12 +49,10 @@ class _InputSyncPageState extends State { void initState() { super.initState(); _loadSavedConnection(); - inputController.addListener(_syncTextChange); } @override void dispose() { - syncDebounce?.cancel(); channel?.sink.close(); hostController.dispose(); portController.dispose(); @@ -88,12 +84,10 @@ class _InputSyncPageState extends State { setState(() { connected = true; }); - _sendCurrentText(force: true); inputFocusNode.requestFocus(); } void _disconnect() { - syncDebounce?.cancel(); channel?.sink.close(); channel = null; setState(() { @@ -101,41 +95,14 @@ class _InputSyncPageState extends State { }); } - void _syncTextChange() { + void _sendCurrentText() { if (!connected || channel == null) { return; } - final value = inputController.value; - if (value.composing.isValid) { - return; - } - - final currentText = value.text; - if (currentText == lastSyncedText) { - return; - } - - syncDebounce?.cancel(); - syncDebounce = Timer(const Duration(milliseconds: 800), _sendCurrentText); - } - - void _sendCurrentText({bool force = false}) { - if (!connected || channel == null) { - return; - } - - final latestValue = inputController.value; - if (latestValue.composing.isValid) { - return; - } - - if (!force && latestValue.text == lastSyncedText) { - return; - } - - lastSyncedText = latestValue.text; - _send('replaceAll', text: latestValue.text); + lastSyncedText = inputController.text; + _send('replaceAll', text: inputController.text); + inputFocusNode.requestFocus(); } void _send(String action, {String? text}) { @@ -182,6 +149,12 @@ class _InputSyncPageState extends State { onPressed: connected ? _disconnect : _connect, icon: Icon(connected ? Icons.link_off : Icons.link), ), + const SizedBox(width: 8), + IconButton.filled( + tooltip: 'Send', + onPressed: connected ? _sendCurrentText : null, + icon: const Icon(Icons.send), + ), ], ), ),