Add Giant PTT: a full-screen talk button on Android and a resizable window on desktop
Holding it forces transmission in any input mode, past the voice-activation gate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -384,6 +384,9 @@ class SessionController(private val context: Context) {
|
||||
/** Opens the microphone of the server holding it while push-to-talk is held. */
|
||||
fun setPushToTalk(pressed: Boolean) = sessions.setPushToTalk(pressed)
|
||||
|
||||
/** Transmits on the server holding the microphone while held, past the voice-activation gate too. */
|
||||
fun setForceTransmit(pressed: Boolean) = sessions.setForceTransmit(pressed)
|
||||
|
||||
private fun microphoneMoved() {
|
||||
val owner = sessions.microphoneOwner()
|
||||
for (server in _servers.value) {
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.ts3client.android.ui
|
||||
|
||||
import androidx.compose.foundation.gestures.detectTapGestures
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.Shape
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.ts3client.android.ServerController
|
||||
import com.ts3client.android.ServerTree
|
||||
import com.ts3client.android.SessionController
|
||||
import com.ts3client.android.SessionState
|
||||
import com.ts3client.net.ClientStatus
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
|
||||
/** What push-to-talk would talk on: the server holding the microphone, which need not be the one on screen. */
|
||||
internal class PttTarget(val owner: ServerController?, val state: SessionState, val talking: Boolean)
|
||||
|
||||
@Composable
|
||||
internal fun rememberPttTarget(vm: SessionController): PttTarget {
|
||||
val owner by vm.micOwner.collectAsStateWithLifecycle()
|
||||
val state by remember(owner) { owner?.state ?: MutableStateFlow(SessionState()) }.collectAsStateWithLifecycle()
|
||||
val tree by remember(owner) { owner?.tree ?: MutableStateFlow(ServerTree()) }.collectAsStateWithLifecycle()
|
||||
val self = tree.client(state.selfClientId)
|
||||
val talking = self?.status == ClientStatus.TALKING || self?.status == ClientStatus.COMMANDER_TALKING
|
||||
return PttTarget(owner, state, talking)
|
||||
}
|
||||
|
||||
/** Talks while held. */
|
||||
@Composable
|
||||
internal fun PushToTalk(
|
||||
talking: Boolean,
|
||||
onPressed: (Boolean) -> Unit,
|
||||
modifier: Modifier,
|
||||
shape: Shape = RoundedCornerShape(24.dp),
|
||||
textStyle: TextStyle = MaterialTheme.typography.titleMedium,
|
||||
) {
|
||||
val colors = MaterialTheme.colorScheme
|
||||
Surface(
|
||||
modifier.pointerInput(Unit) {
|
||||
detectTapGestures(onPress = {
|
||||
onPressed(true)
|
||||
try {
|
||||
awaitRelease()
|
||||
} finally {
|
||||
onPressed(false)
|
||||
}
|
||||
})
|
||||
},
|
||||
shape = shape,
|
||||
color = if (talking) StatusColors.talking else colors.primaryContainer,
|
||||
contentColor = if (talking) Color.White else colors.onPrimaryContainer,
|
||||
) {
|
||||
Box(contentAlignment = Alignment.Center) {
|
||||
Text(if (talking) "Talking…" else "Hold to talk", style = textStyle)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Nothing but a push-to-talk button, as big as the screen; it talks in any input mode. */
|
||||
@Composable
|
||||
fun GiantPttScreen(vm: SessionController, onBack: () -> Unit) {
|
||||
val target = rememberPttTarget(vm)
|
||||
SubScreen("Giant PTT", onBack) { padding ->
|
||||
val fill = Modifier.padding(padding).fillMaxSize().padding(16.dp)
|
||||
if (target.owner != null && target.state.connected) {
|
||||
PushToTalk(target.talking, vm::setForceTransmit, fill, RoundedCornerShape(32.dp),
|
||||
MaterialTheme.typography.displaySmall)
|
||||
} else {
|
||||
Box(fill, contentAlignment = Alignment.Center) {
|
||||
Text("Not connected",
|
||||
style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import androidx.activity.compose.BackHandler
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.combinedClickable
|
||||
import androidx.compose.foundation.gestures.detectTapGestures
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
@@ -20,7 +19,6 @@ import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.ArrowDropDown
|
||||
import androidx.compose.material3.Badge
|
||||
@@ -56,14 +54,12 @@ import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.input.key.Key
|
||||
import androidx.compose.ui.input.key.KeyEventType
|
||||
import androidx.compose.ui.input.key.isCtrlPressed
|
||||
import androidx.compose.ui.input.key.key
|
||||
import androidx.compose.ui.input.key.onPreviewKeyEvent
|
||||
import androidx.compose.ui.input.key.type
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalUriHandler
|
||||
import androidx.compose.ui.semantics.Role
|
||||
@@ -74,15 +70,12 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.ts3client.android.ChatKey
|
||||
import com.ts3client.android.ChatMatch
|
||||
import com.ts3client.android.ServerController
|
||||
import com.ts3client.android.ServerTree
|
||||
import com.ts3client.android.SessionController
|
||||
import com.ts3client.android.SessionState
|
||||
import com.ts3client.android.TreeFind
|
||||
import com.ts3client.android.findIn
|
||||
import com.ts3client.config.Settings
|
||||
import com.ts3client.net.ClientStatus
|
||||
import com.ts3client.text.TsLink
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
|
||||
private val ChatKeySaver = Saver<ChatKey, String>(
|
||||
save = { key ->
|
||||
@@ -245,15 +238,8 @@ fun ServerScreen(
|
||||
}
|
||||
},
|
||||
bottomBar = {
|
||||
// Push-to-talk talks on whichever server holds the microphone, which need not be this one.
|
||||
val owner by vm.micOwner.collectAsStateWithLifecycle()
|
||||
val ownerState by remember(owner) { owner?.state ?: MutableStateFlow(SessionState()) }
|
||||
.collectAsStateWithLifecycle()
|
||||
val ownerTree by remember(owner) { owner?.tree ?: MutableStateFlow(ServerTree()) }
|
||||
.collectAsStateWithLifecycle()
|
||||
val self = ownerTree.client(ownerState.selfClientId)
|
||||
val talking = self?.status == ClientStatus.TALKING || self?.status == ClientStatus.COMMANDER_TALKING
|
||||
VoiceBar(state, owner != null, talking, vm, server, onEnableMicrophone, showChat, chats.anyUnread) {
|
||||
val ptt = rememberPttTarget(vm)
|
||||
VoiceBar(state, ptt.owner != null, ptt.talking, vm, server, onEnableMicrophone, showChat, chats.anyUnread) {
|
||||
showChat = !showChat
|
||||
}
|
||||
},
|
||||
@@ -444,7 +430,7 @@ private fun VoiceBar(
|
||||
}
|
||||
val middle = @Composable {
|
||||
when {
|
||||
ptt -> PushToTalk(talking, vm::setPushToTalk, Modifier.fillMaxWidth())
|
||||
ptt -> PushToTalk(talking, vm::setPushToTalk, Modifier.fillMaxWidth().height(48.dp))
|
||||
!state.micPermission -> FilledTonalButton(onClick = onEnableMicrophone) {
|
||||
PackIcon("CAPTURE", null)
|
||||
Text("Use microphone", Modifier.padding(start = 8.dp), maxLines = 1)
|
||||
@@ -503,32 +489,6 @@ private fun BarButton(onClick: () -> Unit, onLongClick: () -> Unit, enabled: Boo
|
||||
) { content() }
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun PushToTalk(talking: Boolean, onPressed: (Boolean) -> Unit, modifier: Modifier) {
|
||||
val colors = MaterialTheme.colorScheme
|
||||
Surface(
|
||||
modifier
|
||||
.height(48.dp)
|
||||
.pointerInput(Unit) {
|
||||
detectTapGestures(onPress = {
|
||||
onPressed(true)
|
||||
try {
|
||||
awaitRelease()
|
||||
} finally {
|
||||
onPressed(false)
|
||||
}
|
||||
})
|
||||
},
|
||||
shape = RoundedCornerShape(24.dp),
|
||||
color = if (talking) StatusColors.talking else colors.primaryContainer,
|
||||
contentColor = if (talking) Color.White else colors.onPrimaryContainer,
|
||||
) {
|
||||
Box(contentAlignment = Alignment.Center) {
|
||||
Text(if (talking) "Talking…" else "Hold to talk", style = MaterialTheme.typography.titleMedium)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The server's own icon, else the pack's server icon. */
|
||||
@Composable
|
||||
private fun ServerLogo(server: ServerController, modifier: Modifier = Modifier) {
|
||||
|
||||
@@ -21,7 +21,7 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
private enum class SettingsPage { SOUND, NETWORK, DESIGN, IDENTITIES, MY_TEAMSPEAK }
|
||||
private enum class SettingsPage { SOUND, NETWORK, GIANT_PTT, DESIGN, IDENTITIES, MY_TEAMSPEAK }
|
||||
|
||||
/**
|
||||
* The settings, by category. Every change is saved and put into effect at once,
|
||||
@@ -33,6 +33,7 @@ fun SettingsScreen(vm: SessionController, onContacts: () -> Unit, onBack: () ->
|
||||
when (page) {
|
||||
SettingsPage.SOUND -> return SoundSettingsScreen(vm, onBack = { page = null })
|
||||
SettingsPage.NETWORK -> return NetworkSettingsScreen(vm, onBack = { page = null })
|
||||
SettingsPage.GIANT_PTT -> return GiantPttScreen(vm, onBack = { page = null })
|
||||
SettingsPage.DESIGN -> return DesignScreen(vm, onBack = { page = null })
|
||||
SettingsPage.IDENTITIES -> return IdentitiesScreen(vm.identities, onBack = { page = null })
|
||||
SettingsPage.MY_TEAMSPEAK -> return MyTeamSpeakScreen(vm, onBack = { page = null })
|
||||
@@ -48,6 +49,9 @@ fun SettingsScreen(vm: SessionController, onContacts: () -> Unit, onBack: () ->
|
||||
LinkRow("Network", "Bitrate, error correction, redundancy, buffering", "PING_4") {
|
||||
page = SettingsPage.NETWORK
|
||||
}
|
||||
LinkRow("Giant PTT", "A push-to-talk button filling the screen", "CAPTURE") {
|
||||
page = SettingsPage.GIANT_PTT
|
||||
}
|
||||
LinkRow("Design", "Icon pack, country flags, description pictures", "GUISETUP") {
|
||||
page = SettingsPage.DESIGN
|
||||
}
|
||||
|
||||
@@ -54,6 +54,7 @@ public final class CaptureVoiceInput implements VoiceInput {
|
||||
private final AtomicBoolean localMuted = new AtomicBoolean(false);
|
||||
private final AtomicBoolean transmitting = new AtomicBoolean(false);
|
||||
private final AtomicBoolean pttDown = new AtomicBoolean(false);
|
||||
private final AtomicBoolean forceDown = new AtomicBoolean(false);
|
||||
private final AtomicBoolean running = new AtomicBoolean(false);
|
||||
|
||||
private volatile Settings.InputMode mode;
|
||||
@@ -270,6 +271,11 @@ public final class CaptureVoiceInput implements VoiceInput {
|
||||
this.pttDown.set(down);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setForceTransmit(boolean down) {
|
||||
this.forceDown.set(down);
|
||||
}
|
||||
|
||||
public void setMuted(boolean m) {
|
||||
this.muted.set(m);
|
||||
}
|
||||
@@ -478,6 +484,7 @@ public final class CaptureVoiceInput implements VoiceInput {
|
||||
return false;
|
||||
}
|
||||
mutedTalking = false;
|
||||
if (forceDown.get()) return true;
|
||||
switch (mode) {
|
||||
case CONTINUOUS:
|
||||
return true;
|
||||
|
||||
@@ -71,6 +71,9 @@ public interface VoiceInput extends Microphone {
|
||||
|
||||
void setPushToTalk(boolean pressed);
|
||||
|
||||
/** Transmits while held, whatever the input mode or the gate says; a mute still wins. */
|
||||
void setForceTransmit(boolean pressed);
|
||||
|
||||
/** Receives the live input level on {@link InputLevel}'s scale, once per captured frame. */
|
||||
void setLevelListener(Consumer<Double> listener);
|
||||
|
||||
|
||||
@@ -107,6 +107,12 @@ public final class Sessions {
|
||||
if (mic != null) mic.setPushToTalk(talking);
|
||||
}
|
||||
|
||||
/** Transmits on the server holding the microphone while held, in any input mode. */
|
||||
public void setForceTransmit(boolean talking) {
|
||||
VoiceInput mic = microphoneOf(microphoneOwner);
|
||||
if (mic != null) mic.setForceTransmit(talking);
|
||||
}
|
||||
|
||||
/** Tells every connected microphone about a key press, as TS3 tells all its connections. */
|
||||
public void keyPressed() {
|
||||
for (ServerSession s : sessions) {
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
package com.ts3client.ui;
|
||||
|
||||
import com.formdev.flatlaf.util.UIScale;
|
||||
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JFrame;
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Font;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.util.function.BooleanSupplier;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* A resizable window that is one big push-to-talk button. Pressing it transmits in any
|
||||
* input mode, past the voice-activation gate.
|
||||
*/
|
||||
final class GiantPttWindow extends JDialog {
|
||||
|
||||
private static final Color IDLE = new Color(0x1C, 0x6F, 0xB8);
|
||||
private static final Color TALKING = new Color(0x2E, 0x9B, 0xF0);
|
||||
|
||||
private final Consumer<Boolean> transmit;
|
||||
private final BooleanSupplier talking;
|
||||
private final Pad pad = new Pad();
|
||||
private boolean pressed;
|
||||
|
||||
/**
|
||||
* @param transmit forces transmission on the server holding the microphone while given {@code true}
|
||||
* @param talking whether we are talking on that server, as it reports
|
||||
*/
|
||||
GiantPttWindow(JFrame owner, Consumer<Boolean> transmit, BooleanSupplier talking) {
|
||||
super(owner, "Giant PTT", false);
|
||||
this.transmit = transmit;
|
||||
this.talking = talking;
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
setContentPane(pad);
|
||||
pad.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
press(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
press(false);
|
||||
}
|
||||
});
|
||||
addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowDeactivated(WindowEvent e) {
|
||||
press(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void windowClosed(WindowEvent e) {
|
||||
press(false);
|
||||
}
|
||||
});
|
||||
pad.setPreferredSize(new Dimension(UIScale.scale(360), UIScale.scale(360)));
|
||||
pack();
|
||||
setLocationRelativeTo(owner);
|
||||
}
|
||||
|
||||
/** Our talking state changed on some server. */
|
||||
void refresh() {
|
||||
pad.repaint();
|
||||
}
|
||||
|
||||
private void press(boolean down) {
|
||||
if (pressed == down) return;
|
||||
pressed = down;
|
||||
transmit.accept(down);
|
||||
pad.repaint();
|
||||
}
|
||||
|
||||
private final class Pad extends JComponent {
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
Graphics2D g2 = (Graphics2D) g.create();
|
||||
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
g2.setColor(Theme.windowBg());
|
||||
g2.fillRect(0, 0, getWidth(), getHeight());
|
||||
|
||||
boolean lit = pressed || talking.getAsBoolean();
|
||||
int margin = UIScale.scale(12);
|
||||
int arc = UIScale.scale(32);
|
||||
g2.setColor(lit ? TALKING : IDLE);
|
||||
g2.fillRoundRect(margin, margin, getWidth() - 2 * margin, getHeight() - 2 * margin, arc, arc);
|
||||
|
||||
String text = lit ? "Talking…" : "Hold to talk";
|
||||
float size = Math.max(UIScale.scale(12f), Math.min(getWidth() / 8f, getHeight() / 4f));
|
||||
g2.setFont(Theme.uiFont().deriveFont(Font.BOLD, size));
|
||||
FontMetrics fm = g2.getFontMetrics();
|
||||
g2.setColor(Color.WHITE);
|
||||
g2.drawString(text, (getWidth() - fm.stringWidth(text)) / 2,
|
||||
(getHeight() - fm.getHeight()) / 2 + fm.getAscent());
|
||||
g2.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,6 +91,7 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
|
||||
|
||||
/** Mirrors the active server's own client state next to the clock; set once the window is up. */
|
||||
private volatile TrayController tray;
|
||||
private GiantPttWindow giantPtt;
|
||||
private final DesktopNotifier notifier;
|
||||
|
||||
private MainToolbar toolbar;
|
||||
@@ -292,6 +293,11 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
|
||||
if (selected != null) selected.avatar().delete();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGiantPtt() {
|
||||
showGiantPtt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onShowIdentities() {
|
||||
showIdentities();
|
||||
@@ -554,7 +560,9 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
|
||||
|
||||
/** The local client on {@code tab} started or stopped talking, muted itself, … */
|
||||
void selfStateChanged(ServerTab tab) {
|
||||
if (tabs.contains(tab)) updateTray();
|
||||
if (!tabs.contains(tab)) return;
|
||||
updateTray();
|
||||
if (giantPtt != null) giantPtt.refresh();
|
||||
}
|
||||
|
||||
void tabConnected(ServerTab tab) {
|
||||
@@ -624,6 +632,18 @@ public final class MainFrame extends JFrame implements ServerTabPane.Listener {
|
||||
sessions.setPushToTalk(talking);
|
||||
}
|
||||
|
||||
private void showGiantPtt() {
|
||||
if (giantPtt == null || !giantPtt.isDisplayable()) {
|
||||
giantPtt = new GiantPttWindow(this, sessions::setForceTransmit, () -> {
|
||||
ServerSession owner = sessions.microphoneOwner();
|
||||
SelfState state = owner == null ? null : owner.selfState();
|
||||
return state == SelfState.TALKING || state == SelfState.COMMANDER_TALKING;
|
||||
});
|
||||
}
|
||||
giantPtt.setVisible(true);
|
||||
giantPtt.toFront();
|
||||
}
|
||||
|
||||
void moveMicrophoneToSelectedTab() {
|
||||
if (selected != null && selected.session().isConnected()) sessions.giveMicrophoneTo(selected.session());
|
||||
updateToolbar();
|
||||
|
||||
@@ -72,6 +72,8 @@ final class MainMenuBar extends JMenuBar {
|
||||
|
||||
void onDeleteAvatar();
|
||||
|
||||
void onGiantPtt();
|
||||
|
||||
void onShowIdentities();
|
||||
|
||||
void onShowContacts();
|
||||
@@ -223,10 +225,13 @@ final class MainMenuBar extends JMenuBar {
|
||||
avatarItem.addActionListener(e -> listener.onSetAvatar());
|
||||
deleteAvatarItem = new JMenuItem("Delete avatar", Icons.of("DELETE_AVATAR"));
|
||||
deleteAvatarItem.addActionListener(e -> listener.onDeleteAvatar());
|
||||
JMenuItem giantPtt = new JMenuItem("Giant PTT", Icons.of("CAPTURE"));
|
||||
giantPtt.addActionListener(e -> listener.onGiantPtt());
|
||||
self.add(awayMenu);
|
||||
self.add(activateItem);
|
||||
self.add(muteItem);
|
||||
self.add(deafItem);
|
||||
self.add(giantPtt);
|
||||
self.addSeparator();
|
||||
self.add(commanderItem);
|
||||
self.addSeparator();
|
||||
|
||||
Reference in New Issue
Block a user