diff --git a/android/app/src/main/kotlin/com/wirelesstextsyncer/wireless_text_syncer_android/ConnectionService.kt b/android/app/src/main/kotlin/com/wirelesstextsyncer/wireless_text_syncer_android/ConnectionService.kt index 9701776..cead12c 100644 --- a/android/app/src/main/kotlin/com/wirelesstextsyncer/wireless_text_syncer_android/ConnectionService.kt +++ b/android/app/src/main/kotlin/com/wirelesstextsyncer/wireless_text_syncer_android/ConnectionService.kt @@ -291,6 +291,34 @@ class ConnectionService : Service() { return sendAction(socket, "enter", null) } + fun sendMute(muted: Boolean): Boolean { + Log.d("WTS", "ConnectionService.sendMute muted=$muted connected=${state.connected}") + val socket = currentSocket ?: return false + if (!state.connected) { + return false + } + + return sendMuteAction(socket, muted) + } + + private fun sendMuteAction(socket: WebSocket, muted: Boolean): Boolean { + val payload = JSONObject() + .put("action", "setMute") + .put("muted", muted) + .toString() + val sent = socket.send(payload) + if (!sent && currentSocket == socket) { + currentSocket = null + updateState( + connected = false, + connecting = false, + lastError = "连接已断开" + ) + } + + return sent + } + private fun sendAction(socket: WebSocket, action: String, text: String?): Boolean { val payload = JSONObject() .put("action", action) diff --git a/android/app/src/main/kotlin/com/wirelesstextsyncer/wireless_text_syncer_android/MainActivity.kt b/android/app/src/main/kotlin/com/wirelesstextsyncer/wireless_text_syncer_android/MainActivity.kt index bb5362f..940b2c9 100644 --- a/android/app/src/main/kotlin/com/wirelesstextsyncer/wireless_text_syncer_android/MainActivity.kt +++ b/android/app/src/main/kotlin/com/wirelesstextsyncer/wireless_text_syncer_android/MainActivity.kt @@ -44,6 +44,11 @@ class MainActivity : FlutterActivity() { Log.d(tag, "MethodChannel sendEnter") result.success(ConnectionService.sendEnter()) } + "sendMute" -> { + val muted = call.argument("muted") ?: false + Log.d(tag, "MethodChannel sendMute muted=$muted") + result.success(ConnectionService.sendMute(muted)) + } "getState" -> { Log.d(tag, "MethodChannel getState") result.success(ConnectionService.stateMap()) diff --git a/lib/main.dart b/lib/main.dart index 7bc933e..886e12e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -134,6 +134,13 @@ class NativeConnectionApi { return result ?? false; } + Future sendMute(bool muted) async { + final result = await _methodChannel.invokeMethod('sendMute', { + 'muted': muted, + }); + return result ?? false; + } + Future> startDiscovery() async { try { final result = await _methodChannel.invokeListMethod( @@ -184,6 +191,7 @@ class _InputSyncPageState extends State bool headerExpanded = true; bool inputFocused = false; bool scanning = false; + bool desktopMuted = false; @override void initState() { @@ -265,6 +273,7 @@ class _InputSyncPageState extends State } if (!nextState.connected) { sendButtonState = SendButtonState.idle; + desktopMuted = false; } }); @@ -423,6 +432,23 @@ class _InputSyncPageState extends State } } + Future _toggleMute() async { + if (!connection.connected) { + return; + } + final nextMuted = !desktopMuted; + setState(() { + desktopMuted = nextMuted; + }); + final sent = await api.sendMute(nextMuted); + if (!sent && mounted) { + setState(() { + desktopMuted = !nextMuted; + }); + _showToast('发送失败,连接已断开'); + } + } + Future _saveHistory(String text) async { final trimmed = text.trim(); if (trimmed.isEmpty) { @@ -904,6 +930,13 @@ class _InputSyncPageState extends State icon: const Icon(Icons.delete_outline), ), ), + IconButton( + tooltip: desktopMuted ? '取消静音' : '静音桌面', + onPressed: connection.connected ? _toggleMute : null, + icon: Icon( + desktopMuted ? Icons.volume_off : Icons.volume_up, + ), + ), ], ), ),