From b2733a7b0af7732e63c79d50a871e6bc050fdece Mon Sep 17 00:00:00 2001 From: yourmoln Date: Sun, 25 Oct 2026 22:39:22 +0800 Subject: [PATCH] =?UTF-8?q?feat(subtitles):=20=E7=8B=AC=E7=AB=8B=E5=AD=97?= =?UTF-8?q?=E5=B9=95=E8=AE=BE=E7=BD=AE=E7=AA=97=E5=8F=A3=E5=B9=B6=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=20SteamVR=20=E5=AD=97=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 设置改为独立无标题栏窗口,修复圆角白底并在修改时即时保存。新增 SteamVR 字幕字号配置、渲染和四语言界面文案。 --- README.en.md | 2 +- README.ja.md | 2 +- README.md | 2 +- README.zh-TW.md | 2 +- .../app/audio/SystemAudioLanguageConfig.kt | 6 + .../vrcmc/app/localization/LocaleStrings.kt | 5 +- .../vrcmc/app/localization/LocaleStringsEn.kt | 5 +- .../vrcmc/app/localization/LocaleStringsJa.kt | 5 +- .../app/localization/LocaleStringsZhHans.kt | 5 +- .../app/localization/LocaleStringsZhHant.kt | 5 +- .../app/translation/TranslationSettings.kt | 3 + .../audio/SystemAudioLanguageConfigTest.kt | 32 +- .../vrcmc/app/audio/OpenVrSubtitleOverlay.kt | 12 +- .../vrcmc/app/audio/SteamVrSubtitleOverlay.kt | 9 +- .../app/audio/SteamVrSubtitleRenderer.kt | 36 ++- .../app/audio/SystemAudioListener.desktop.kt | 281 ++++++++++++------ .../app/audio/SteamVrSubtitleOverlayTest.kt | 26 ++ .../audio/SystemAudioSubtitleWindowTest.kt | 261 +++++++++------- 18 files changed, 469 insertions(+), 230 deletions(-) diff --git a/README.en.md b/README.en.md index e1097b1..42911bb 100644 --- a/README.en.md +++ b/README.en.md @@ -30,7 +30,7 @@ VRCMC is a VRChat Chatbox companion built with Kotlin Multiplatform and Compose - **Voice input**: Use Qwen Fun-ASR cloud recognition. On Windows x64, download SenseVoice Small INT8 or Fun-ASR-Nano INT8 for offline recognition. Desktop builds let you select a microphone; Android uses cloud recognition and requires microphone permission. - **Interpretation**: Use system voice input or app recognition. Simultaneous interpretation follows the VRChat microphone state and sends after mute; Always interpretation sends after input pauses and can merge quick successive messages. - **Listen to others (Windows)**: Click the ear button on the Chat page to capture the Windows default playback device. Speech is detected locally and shown sentence by sentence with the original and one translation in an always-on-top subtitle window. Choose listening and target languages, resize or minimize the window, and adjust opacity. -- **SteamVR overlay**: Enable it in subtitle settings and choose the left hand, right hand, or screen center. Hand overlays follow controllers and the center overlay follows the headset. Choose 50%–200% size; opacity is shared with desktop subtitles and ranges from 30%–100%. SteamVR and a connected headset are required. +- **SteamVR overlay**: Enable it in subtitle settings and choose the left hand, right hand, or screen center. Hand overlays follow controllers and the center overlay follows the headset. Choose 50%–200% size and an independent 75%–200% font size; opacity is shared with desktop subtitles and ranges from 30%–100%. SteamVR and a connected headset are required. - **Text-to-speech**: Enable Edge TTS, choose original, translation, or outgoing text, preview voices, and select a desktop playback device. Text is sent to Microsoft for synthesis. - **Hotword dictionary**: Replace keywords or block complete messages before sending; import and export JSON. - **Local settings**: Use light, dark, or system themes; the UI supports Simplified Chinese, Traditional Chinese, English, and Japanese. Japanese romaji display, local chat history, and error logs are also available. diff --git a/README.ja.md b/README.ja.md index 1d7fff8..d212244 100644 --- a/README.ja.md +++ b/README.ja.md @@ -30,7 +30,7 @@ VRCMC は、Kotlin Multiplatform と Compose Multiplatform で開発された VR - **音声入力**:Qwen Fun-ASR のクラウド認識を利用できます。Windows x64 では SenseVoice Small INT8 または Fun-ASR-Nano INT8 をダウンロードし、オフライン認識できます。デスクトップ版ではマイクを選択でき、Android ではクラウド認識とマイク権限を使用します。 - **同時通訳**:システムの音声入力またはアプリ内認識と連携できます。有効にすると VRChat のマイク状態に応じて録音し、マイクを切った後に翻訳して送信します。「常時通訳」では入力が止まった後に自動送信し、短時間に続けて入力したメッセージを結合できます。 - **相手の声を聞く(Windows)**:チャットページ右上の耳ボタンで Windows の既定の再生デバイスを聞き取り、音声をローカルで検出して、常に手前に表示される字幕ウィンドウに原文と 1 つの訳文を文単位で表示します。聞き取り言語と翻訳先言語、ウィンドウのサイズ・最小化・不透明度を設定できます。 -- **SteamVR オーバーレイ**:字幕設定で有効にし、左手・右手・画面中央から位置を選べます。手元のパネルはコントローラーに、画面中央のパネルはヘッドセットに追従します。サイズは 50%–200%、不透明度はデスクトップ字幕と共通の 30%–100% です。SteamVR と接続済みのヘッドセットが必要です。 +- **SteamVR オーバーレイ**:字幕設定で有効にし、左手・右手・画面中央から位置を選べます。手元のパネルはコントローラーに、画面中央のパネルはヘッドセットに追従します。サイズは 50%–200%、字幕の文字サイズは 75%–200% から個別に選べ、不透明度はデスクトップ字幕と共通の 30%–100% です。SteamVR と接続済みのヘッドセットが必要です。 - **メッセージ読み上げ**:Edge TTS を有効にすると、原文・訳文・最終送信テキストのどれを読み上げるか選べます。音声の試聴にも対応し、デスクトップ版では再生デバイスを選択できます。読み上げるテキストは Microsoft に送信されます。 - **ホットワード辞書**:送信前にキーワードを置換したり、文全体をブロックしたりできます。JSON のインポートとエクスポートに対応します。 - **ローカル設定**:ライト・ダーク・システムテーマを選べます。UI は簡体字中国語、繁体字中国語、英語、日本語に対応し、日本語のローマ字表示、チャット履歴、エラーログも利用できます。 diff --git a/README.md b/README.md index 1b9b45c..cd28ebb 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ VRCMC 是一款基于 Kotlin Multiplatform 与 Compose Multiplatform 开发的 V - **语音输入**:可使用 Qwen Fun-ASR 云端识别;Windows x64 设备可下载 SenseVoice Small INT8 或 Fun-ASR-Nano INT8,在本地离线识别。桌面版可选择麦克风,Android 使用云端识别并需要麦克风权限。 - **同声传译**:可配合系统语音输入或应用内识别。开启后会根据 VRChat 麦克风状态采集语音,闭麦后翻译并发送;“始终传译”会在输入暂停后自动发送,并可合并快速连续消息。 - **听别人说话(Windows)**:点击聊天页右上角的耳朵按钮,监听 Windows 默认播放设备并在本地检测人声,在置顶字幕窗口中按句显示原文和一种译文。窗口可调整大小、最小化和不透明度,设置中可选择听的语言和目标语言。 -- **SteamVR 叠加界面**:可在字幕设置中开启,位置支持左手、右手或屏幕中间;手部面板跟随控制器,屏幕中间面板跟随头显。大小可选 50%–200%,与桌面字幕共用 30%–100% 的不透明度,需要 SteamVR 和已连接的头显。 +- **SteamVR 叠加界面**:可在字幕设置中开启,位置支持左手、右手或屏幕中间;手部面板跟随控制器,屏幕中间面板跟随头显。大小可选 50%–200%,字幕字号可独立选择 75%–200%,与桌面字幕共用 30%–100% 的不透明度,需要 SteamVR 和已连接的头显。 - **消息朗读**:启用 Edge TTS 后,可选择朗读原文、译文或最终发送文本,支持音色试听;桌面版可选择播放设备。朗读文本会发送到微软进行语音合成。 - **热词字典**:在发送前替换关键词或屏蔽整句,支持 JSON 导入导出。 - **本地设置**:支持浅色、深色和跟随系统主题,界面提供简体中文、繁体中文、English 和日本語;可选显示日文罗马字,聊天记录和错误日志保存在本机。 diff --git a/README.zh-TW.md b/README.zh-TW.md index d9e00b2..bfa7a9a 100644 --- a/README.zh-TW.md +++ b/README.zh-TW.md @@ -30,7 +30,7 @@ VRCMC 是一款使用 Kotlin Multiplatform 與 Compose Multiplatform 開發的 V - **語音輸入**:可使用 Qwen Fun-ASR 雲端辨識;Windows x64 裝置可下載 SenseVoice Small INT8 或 Fun-ASR-Nano INT8,在本機離線辨識。桌面版可選擇麥克風,Android 使用雲端辨識並需要麥克風權限。 - **同聲口譯**:可搭配系統語音輸入或應用程式內辨識。開啟後會依 VRChat 麥克風狀態收音,閉麥後翻譯並傳送;「始終口譯」會在輸入暫停後自動傳送,並可合併快速連續訊息。 - **聽別人說話(Windows)**:點擊聊天頁右上角的耳朵按鈕,監聽 Windows 預設播放裝置並在本機偵測人聲,在置頂字幕視窗中按句顯示原文與一種譯文。視窗可調整大小、最小化與不透明度,設定中可選擇聆聽語言和目標語言。 -- **SteamVR 疊加介面**:可在字幕設定中開啟,位置支援左手、右手或螢幕中央;手部面板跟隨控制器,螢幕中央面板跟隨頭顯。大小可選 50%–200%,與桌面字幕共用 30%–100% 的不透明度,需要 SteamVR 和已連接的頭顯。 +- **SteamVR 疊加介面**:可在字幕設定中開啟,位置支援左手、右手或螢幕中央;手部面板跟隨控制器,螢幕中央面板跟隨頭顯。大小可選 50%–200%,字幕字級可獨立選擇 75%–200%,與桌面字幕共用 30%–100% 的不透明度,需要 SteamVR 和已連接的頭顯。 - **訊息朗讀**:啟用 Edge TTS 後,可選擇朗讀原文、譯文或最終傳送文字,支援音色試聽;桌面版可選擇播放裝置。朗讀文字會傳送至 Microsoft 進行語音合成。 - **熱詞字典**:在傳送前替換關鍵字或封鎖整句,支援 JSON 匯入與匯出。 - **本機設定**:支援淺色、深色與跟隨系統主題,介面提供簡體中文、繁體中文、English 和日本語;可選顯示日文羅馬字,聊天記錄和錯誤日誌儲存在本機。 diff --git a/composeApp/src/commonMain/kotlin/com/vrcmc/app/audio/SystemAudioLanguageConfig.kt b/composeApp/src/commonMain/kotlin/com/vrcmc/app/audio/SystemAudioLanguageConfig.kt index 1379ba1..4b47c64 100644 --- a/composeApp/src/commonMain/kotlin/com/vrcmc/app/audio/SystemAudioLanguageConfig.kt +++ b/composeApp/src/commonMain/kotlin/com/vrcmc/app/audio/SystemAudioLanguageConfig.kt @@ -7,15 +7,20 @@ data class SystemAudioLanguageConfig( val steamVrOverlayEnabled: Boolean = false, val steamVrOverlayPosition: SteamVrOverlayPosition = SteamVrOverlayPosition.LEFT_HAND, val steamVrOverlayScalePercent: Int = 100, + val steamVrOverlayFontSizePercent: Int = 100, ) enum class SteamVrOverlayPosition { LEFT_HAND, RIGHT_HAND, SCREEN_CENTER } internal val steamVrOverlayScalePercents = listOf(50, 75, 100, 125, 150, 175, 200) +internal val steamVrOverlayFontSizePercents = listOf(75, 100, 125, 150, 175, 200) internal fun normalizedSteamVrOverlayScalePercent(value: Int): Int = value.takeIf { it in steamVrOverlayScalePercents } ?: 100 +internal fun normalizedSteamVrOverlayFontSizePercent(value: Int): Int = + value.takeIf { it in steamVrOverlayFontSizePercents } ?: 100 + internal val systemAudioSourceLanguages = linkedMapOf( "zh" to "中文", "ja" to "日本語", "en" to "English", "ko" to "한국어", "de" to "Deutsch", "fr" to "Français", "ru" to "Русский", @@ -26,4 +31,5 @@ internal fun SystemAudioLanguageConfig.normalized(): SystemAudioLanguageConfig = targetLanguage = targetLanguage.takeIf { it in availableTargetLanguages } ?: "简体中文", opacityPercent = opacityPercent.coerceIn(30, 100), steamVrOverlayScalePercent = normalizedSteamVrOverlayScalePercent(steamVrOverlayScalePercent), + steamVrOverlayFontSizePercent = normalizedSteamVrOverlayFontSizePercent(steamVrOverlayFontSizePercent), ) diff --git a/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStrings.kt b/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStrings.kt index 19ec119..f7762f3 100644 --- a/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStrings.kt +++ b/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStrings.kt @@ -7,7 +7,10 @@ interface LocaleStrings { val steamVrOverlayPosition: String get() = "Overlay position" val steamVrOverlaySize: String get() = "Overlay size" val steamVrOverlayDefaultSize: String get() = "100% (Default)" - val steamVrOverlaySizeHint: String get() = "Resizing keeps the text size and the bottom-center hand alignment." + val steamVrOverlayFontSize: String get() = "Font size" + val steamVrOverlayDefaultFontSize: String get() = "100% (Default)" + val steamVrOverlaySizeHint: String get() = "Resizing keeps the text size and bottom-center hand alignment; use font size to adjust the text independently." + val steamVrOverlayFontSizeHint: String get() = "Adjust the subtitle text size without changing the overlay size or anchor." val steamVrOverlayLeftHand: String get() = "Left hand" val steamVrOverlayRightHand: String get() = "Right hand" val steamVrOverlayScreenCenter: String get() = "Screen center" diff --git a/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStringsEn.kt b/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStringsEn.kt index c353d33..4680f58 100644 --- a/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStringsEn.kt +++ b/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStringsEn.kt @@ -7,7 +7,10 @@ object LocaleStringsEn : LocaleStrings { override val steamVrOverlayPosition = "Overlay position" override val steamVrOverlaySize = "Overlay size" override val steamVrOverlayDefaultSize = "100% (Default)" - override val steamVrOverlaySizeHint = "Resizing keeps the text size and the bottom-center hand alignment." + override val steamVrOverlayFontSize = "Font size" + override val steamVrOverlayDefaultFontSize = "100% (Default)" + override val steamVrOverlaySizeHint = "Resizing keeps the text size and bottom-center hand alignment; use font size to adjust the text independently." + override val steamVrOverlayFontSizeHint = "Adjust the subtitle text size without changing the overlay size or anchor." override val steamVrOverlayLeftHand = "Left hand" override val steamVrOverlayRightHand = "Right hand" override val steamVrOverlayScreenCenter = "Screen center" diff --git a/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStringsJa.kt b/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStringsJa.kt index 38b4574..0aebcbd 100644 --- a/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStringsJa.kt +++ b/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStringsJa.kt @@ -7,7 +7,10 @@ object LocaleStringsJa : LocaleStrings { override val steamVrOverlayPosition = "表示位置" override val steamVrOverlaySize = "表示サイズ" override val steamVrOverlayDefaultSize = "100%(デフォルト)" - override val steamVrOverlaySizeHint = "サイズを変更しても文字の大きさは変わりません。手元のパネルは下辺の中央をコントローラーに合わせます。" + override val steamVrOverlayFontSize = "文字サイズ" + override val steamVrOverlayDefaultFontSize = "100%(デフォルト)" + override val steamVrOverlaySizeHint = "サイズを変更しても文字サイズは変わりません。文字サイズは個別に調整でき、手元のパネルは下辺の中央をコントローラーに合わせます。" + override val steamVrOverlayFontSizeHint = "オーバーレイのサイズや位置を変えずに、字幕の文字サイズだけを調整します。" override val steamVrOverlayLeftHand = "左手" override val steamVrOverlayRightHand = "右手" override val steamVrOverlayScreenCenter = "画面中央" diff --git a/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStringsZhHans.kt b/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStringsZhHans.kt index 0c4d448..9cc257c 100644 --- a/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStringsZhHans.kt +++ b/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStringsZhHans.kt @@ -7,7 +7,10 @@ object LocaleStringsZhHans : LocaleStrings { override val steamVrOverlayPosition = "叠加位置" override val steamVrOverlaySize = "叠加大小" override val steamVrOverlayDefaultSize = "100%(默认)" - override val steamVrOverlaySizeHint = "调整大小时字号不变,手部面板仍以底边中心对齐手柄。" + override val steamVrOverlayFontSize = "字号大小" + override val steamVrOverlayDefaultFontSize = "100%(默认)" + override val steamVrOverlaySizeHint = "调整叠加大小时字号不变,可单独调整字号;手部面板仍以底边中心对齐手柄。" + override val steamVrOverlayFontSizeHint = "单独调整字幕字号,不改变叠加界面大小和锚点。" override val steamVrOverlayLeftHand = "左手" override val steamVrOverlayRightHand = "右手" override val steamVrOverlayScreenCenter = "屏幕中间" diff --git a/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStringsZhHant.kt b/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStringsZhHant.kt index a31ea31..4ed61bc 100644 --- a/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStringsZhHant.kt +++ b/composeApp/src/commonMain/kotlin/com/vrcmc/app/localization/LocaleStringsZhHant.kt @@ -7,7 +7,10 @@ object LocaleStringsZhHant : LocaleStrings { override val steamVrOverlayPosition = "疊加位置" override val steamVrOverlaySize = "疊加大小" override val steamVrOverlayDefaultSize = "100%(預設)" - override val steamVrOverlaySizeHint = "調整大小時字級不變,手部面板仍以底邊中央對齊控制器。" + override val steamVrOverlayFontSize = "字級大小" + override val steamVrOverlayDefaultFontSize = "100%(預設)" + override val steamVrOverlaySizeHint = "調整疊加大小時字級不變,可單獨調整字級;手部面板仍以底邊中央對齊控制器。" + override val steamVrOverlayFontSizeHint = "單獨調整字幕字級,不改變疊加介面大小和錨點。" override val steamVrOverlayLeftHand = "左手" override val steamVrOverlayRightHand = "右手" override val steamVrOverlayScreenCenter = "畫面中央" diff --git a/composeApp/src/commonMain/kotlin/com/vrcmc/app/translation/TranslationSettings.kt b/composeApp/src/commonMain/kotlin/com/vrcmc/app/translation/TranslationSettings.kt index d1ad15f..9e35034 100644 --- a/composeApp/src/commonMain/kotlin/com/vrcmc/app/translation/TranslationSettings.kt +++ b/composeApp/src/commonMain/kotlin/com/vrcmc/app/translation/TranslationSettings.kt @@ -104,6 +104,7 @@ fun StoredTranslationSettings.toJson(): String = put("steamVrOverlayEnabled", systemAudioLanguages.steamVrOverlayEnabled) put("steamVrOverlayPosition", systemAudioLanguages.steamVrOverlayPosition.name) put("steamVrOverlayScalePercent", systemAudioLanguages.normalized().steamVrOverlayScalePercent) + put("steamVrOverlayFontSizePercent", systemAudioLanguages.normalized().steamVrOverlayFontSizePercent) } putJsonObject("readAloud") { put("enabled", readAloud.enabled) @@ -220,6 +221,8 @@ fun storedTranslationSettingsFromJson(value: String): StoredTranslationSettings it.name == (obj["steamVrOverlayPosition"] as? JsonPrimitive)?.contentOrNull } ?: SteamVrOverlayPosition.LEFT_HAND, steamVrOverlayScalePercent = (obj["steamVrOverlayScalePercent"] as? JsonPrimitive)?.intOrNull ?: 100, + steamVrOverlayFontSizePercent = + (obj["steamVrOverlayFontSizePercent"] as? JsonPrimitive)?.intOrNull ?: 100, ).normalized() } ?: SystemAudioLanguageConfig(), readAloud = (root["readAloud"] as? JsonObject)?.let { obj -> diff --git a/composeApp/src/commonTest/kotlin/com/vrcmc/app/audio/SystemAudioLanguageConfigTest.kt b/composeApp/src/commonTest/kotlin/com/vrcmc/app/audio/SystemAudioLanguageConfigTest.kt index 54dd607..f849c24 100644 --- a/composeApp/src/commonTest/kotlin/com/vrcmc/app/audio/SystemAudioLanguageConfigTest.kt +++ b/composeApp/src/commonTest/kotlin/com/vrcmc/app/audio/SystemAudioLanguageConfigTest.kt @@ -6,13 +6,17 @@ import kotlin.test.assertEquals class SystemAudioLanguageConfigTest { @Test fun steamVrSettingsRoundTripWithSharedOpacityAndSafeDefaults() { - for (position in SteamVrOverlayPosition.entries) for (scale in steamVrOverlayScalePercents) { - val config = SystemAudioLanguageConfig( - "en", "日本語", 65, steamVrOverlayEnabled = true, steamVrOverlayPosition = position, - steamVrOverlayScalePercent = scale, - ) - val restored = storedTranslationSettingsFromJson(StoredTranslationSettings(systemAudioLanguages = config).toJson()) - assertEquals(config, restored.systemAudioLanguages) + for (position in SteamVrOverlayPosition.entries) { + for (scale in steamVrOverlayScalePercents) for (fontSize in steamVrOverlayFontSizePercents) { + val config = SystemAudioLanguageConfig( + "en", "日本語", 65, steamVrOverlayEnabled = true, steamVrOverlayPosition = position, + steamVrOverlayScalePercent = scale, steamVrOverlayFontSizePercent = fontSize, + ) + val restored = storedTranslationSettingsFromJson( + StoredTranslationSettings(systemAudioLanguages = config).toJson(), + ) + assertEquals(config, restored.systemAudioLanguages) + } } val old = storedTranslationSettingsFromJson("""{"systemAudioLanguages":{"opacityPercent":45}}""") assertEquals(SystemAudioLanguageConfig(opacityPercent = 45), old.systemAudioLanguages) @@ -30,6 +34,20 @@ class SystemAudioLanguageConfigTest { } val old = storedTranslationSettingsFromJson("""{"systemAudioLanguages":{"steamVrOverlayEnabled":true}}""") assertEquals(100, old.systemAudioLanguages.steamVrOverlayScalePercent) + assertEquals(100, old.systemAudioLanguages.steamVrOverlayFontSizePercent) + } + + @Test + fun missingOrInvalidOverlayFontSizesKeepTheCurrentDefaultAndOtherSettings() { + for (value in listOf("null", "{}", "\"bad\"", "0", "65", "201", "-2147483648")) { + val restored = storedTranslationSettingsFromJson( + """{"systemAudioLanguages":{"sourceLanguage":"en","steamVrOverlayEnabled":true,"steamVrOverlayFontSizePercent":$value}}""", + ) + assertEquals( + SystemAudioLanguageConfig(sourceLanguage = "en", steamVrOverlayEnabled = true), + restored.systemAudioLanguages, + ) + } } @Test diff --git a/composeApp/src/desktopMain/kotlin/com/vrcmc/app/audio/OpenVrSubtitleOverlay.kt b/composeApp/src/desktopMain/kotlin/com/vrcmc/app/audio/OpenVrSubtitleOverlay.kt index c24e8ee..8b02bf1 100644 --- a/composeApp/src/desktopMain/kotlin/com/vrcmc/app/audio/OpenVrSubtitleOverlay.kt +++ b/composeApp/src/desktopMain/kotlin/com/vrcmc/app/audio/OpenVrSubtitleOverlay.kt @@ -24,6 +24,7 @@ internal class OpenVrSubtitleOverlay : SteamVrOverlayBackend { private var lastContent: SteamVrOverlayContent? = null private var lastPosition: SteamVrOverlayPosition? = null private var lastScalePercent = -1 + private var lastFontSizePercent = -1 private var lastDevice = k_unTrackedDeviceIndexInvalid private var lastOpacity = -1 private var visible = false @@ -79,6 +80,7 @@ internal class OpenVrSubtitleOverlay : SteamVrOverlayBackend { return SteamVrOverlayStatus.WAITING_FOR_CONTROLLER } val sizeChanged = frame.scalePercent != lastScalePercent + val fontSizeChanged = frame.fontSizePercent != lastFontSizePercent if (frame.position != lastPosition || device != lastDevice || sizeChanged) { val preset = steamVrOverlayPlacement(frame.position, frame.scalePercent) val transform = HmdMatrix34.calloc(stack) @@ -93,9 +95,14 @@ internal class OpenVrSubtitleOverlay : SteamVrOverlayBackend { checkOverlay(VROverlay_SetOverlayAlpha(handle, frame.opacityPercent.coerceIn(30, 100) / 100f)) lastOpacity = frame.opacityPercent } - if (frame.content != lastContent || sizeChanged) { + if (frame.content != lastContent || sizeChanged || fontSizeChanged) { val buffer = checkNotNull(pixels) - SteamVrSubtitleRenderer.renderRgba(frame.content, buffer, frame.scalePercent) + SteamVrSubtitleRenderer.renderRgba( + frame.content, + buffer, + frame.scalePercent, + frame.fontSizePercent, + ) // SetOverlayRaw replaces SteamVR's image on every subtitle/status change. // Keep a single GPU texture alive, finish its upload, then submit it again. val textureId = checkNotNull(texture).upload(buffer) @@ -105,6 +112,7 @@ internal class OpenVrSubtitleOverlay : SteamVrOverlayBackend { .eColorSpace(EColorSpace_ColorSpace_Gamma) checkOverlay(VROverlay_SetOverlayTexture(handle, submission)) lastContent = frame.content + lastFontSizePercent = frame.fontSizePercent } if (!visible) { checkOverlay(VROverlay_ShowOverlay(handle)) diff --git a/composeApp/src/desktopMain/kotlin/com/vrcmc/app/audio/SteamVrSubtitleOverlay.kt b/composeApp/src/desktopMain/kotlin/com/vrcmc/app/audio/SteamVrSubtitleOverlay.kt index 1fd2fb9..f7a954e 100644 --- a/composeApp/src/desktopMain/kotlin/com/vrcmc/app/audio/SteamVrSubtitleOverlay.kt +++ b/composeApp/src/desktopMain/kotlin/com/vrcmc/app/audio/SteamVrSubtitleOverlay.kt @@ -27,6 +27,7 @@ internal data class SteamVrOverlayFrame( val opacityPercent: Int, val content: SteamVrOverlayContent, val scalePercent: Int = 100, + val fontSizePercent: Int = 100, ) internal interface SteamVrOverlayBackend : AutoCloseable { @@ -43,7 +44,13 @@ internal fun rememberSteamVrSubtitleOverlay( content: SteamVrOverlayContent, onError: (String) -> Unit, ): SteamVrOverlayStatus? { - val frame = SteamVrOverlayFrame(config.steamVrOverlayPosition, config.opacityPercent, content, config.steamVrOverlayScalePercent) + val frame = SteamVrOverlayFrame( + config.steamVrOverlayPosition, + config.opacityPercent, + content, + config.steamVrOverlayScalePercent, + config.steamVrOverlayFontSizePercent, + ) val frames = remember { MutableStateFlow(frame) } val currentOnError by rememberUpdatedState(onError) var status by remember { mutableStateOf(null) } diff --git a/composeApp/src/desktopMain/kotlin/com/vrcmc/app/audio/SteamVrSubtitleRenderer.kt b/composeApp/src/desktopMain/kotlin/com/vrcmc/app/audio/SteamVrSubtitleRenderer.kt index c986829..585e31e 100644 --- a/composeApp/src/desktopMain/kotlin/com/vrcmc/app/audio/SteamVrSubtitleRenderer.kt +++ b/composeApp/src/desktopMain/kotlin/com/vrcmc/app/audio/SteamVrSubtitleRenderer.kt @@ -9,6 +9,7 @@ import java.awt.font.TextLayout import java.awt.image.BufferedImage import java.nio.ByteBuffer import java.text.AttributedString +import kotlin.math.roundToInt /** Renders caption data directly, so minimization and desktop occlusion do not affect VR. */ internal object SteamVrSubtitleRenderer { @@ -16,17 +17,23 @@ internal object SteamVrSubtitleRenderer { const val HEIGHT = 960 private const val PADDING = 28 - private data class Line(val layout: TextLayout?, val color: Color) { - val height: Float get() = layout?.let { it.ascent + it.descent + it.leading + 8f } ?: 18f + private data class Line(val layout: TextLayout?, val color: Color, val blankHeight: Float = 18f) { + val height: Float get() = layout?.let { it.ascent + it.descent + it.leading + 8f } ?: blankHeight } - fun render(content: SteamVrOverlayContent, scalePercent: Int = 100): BufferedImage { + fun render( + content: SteamVrOverlayContent, + scalePercent: Int = 100, + fontSizePercent: Int = 100, + ): BufferedImage { // Keep the same GPU allocation at every size. Change the amount of content // laid out, compensating pixel density so the physical text size stays fixed. val scale = normalizedSteamVrOverlayScalePercent(scalePercent) + val fontScale = normalizedSteamVrOverlayFontSizePercent(fontSizePercent) / 100f val layoutWidth = WIDTH / 2 * scale / 100 val layoutHeight = HEIGHT / 2 * scale / 100 val contentScale = WIDTH.toDouble() / layoutWidth + fun fontSize(size: Int): Int = (size * fontScale).roundToInt().coerceAtLeast(1) val image = BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB) val graphics = image.createGraphics() try { @@ -42,7 +49,7 @@ internal object SteamVrSubtitleRenderer { fun lines(text: String, size: Int, color: Color, bold: Boolean = false): List = buildList { for (paragraph in text.lineSequence()) { if (paragraph.isBlank()) { - add(Line(null, color)) + add(Line(null, color, 18f * fontScale)) continue } val attributed = AttributedString(paragraph).apply { @@ -55,7 +62,7 @@ internal object SteamVrSubtitleRenderer { } } var headerY = PADDING.toFloat() - for (line in lines("VRCMC · ${content.status}", 24, primary).take(3)) { + for (line in lines("VRCMC · ${content.status}", fontSize(24), primary).take(3)) { line.layout?.draw(graphics.apply { this.color = line.color }, PADDING.toFloat(), headerY + line.layout.ascent) headerY += line.height } @@ -66,10 +73,12 @@ internal object SteamVrSubtitleRenderer { // Only recent captions can fit on the panel. Keeping their tail matches // the desktop window's automatic scroll to the most recent subtitle. for (caption in content.captions.takeLast(5)) { - if (caption.original.isNotBlank()) addAll(lines(caption.original, 28, foreground)) - for ((_, translation) in caption.translations) addAll(lines(translation, 30, primary, bold = true)) - caption.error?.let { addAll(lines(it, 26, error)) } - add(Line(null, foreground)) + if (caption.original.isNotBlank()) addAll(lines(caption.original, fontSize(28), foreground)) + for ((_, translation) in caption.translations) { + addAll(lines(translation, fontSize(30), primary, bold = true)) + } + caption.error?.let { addAll(lines(it, fontSize(26), error)) } + add(Line(null, foreground, 18f * fontScale)) } } val availableHeight = layoutHeight - PADDING - headerY - 20f @@ -89,8 +98,13 @@ internal object SteamVrSubtitleRenderer { return image } - fun renderRgba(content: SteamVrOverlayContent, target: ByteBuffer, scalePercent: Int = 100) { - val image = render(content, scalePercent) + fun renderRgba( + content: SteamVrOverlayContent, + target: ByteBuffer, + scalePercent: Int = 100, + fontSizePercent: Int = 100, + ) { + val image = render(content, scalePercent, fontSizePercent) val pixels = image.getRGB(0, 0, WIDTH, HEIGHT, null, 0, WIDTH) target.clear() for (argb in pixels) { diff --git a/composeApp/src/desktopMain/kotlin/com/vrcmc/app/audio/SystemAudioListener.desktop.kt b/composeApp/src/desktopMain/kotlin/com/vrcmc/app/audio/SystemAudioListener.desktop.kt index e61873c..2265ae3 100644 --- a/composeApp/src/desktopMain/kotlin/com/vrcmc/app/audio/SystemAudioListener.desktop.kt +++ b/composeApp/src/desktopMain/kotlin/com/vrcmc/app/audio/SystemAudioListener.desktop.kt @@ -27,7 +27,6 @@ import androidx.compose.ui.semantics.stateDescription import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.compose.ui.window.Window -import androidx.compose.ui.window.Dialog import androidx.compose.ui.window.WindowPosition import androidx.compose.ui.window.rememberWindowState import com.sun.jna.Platform @@ -248,110 +247,191 @@ internal fun SystemAudioSubtitleWindow( } } } - if (showSettings) { - SystemAudioLanguageSettingsDialog( - config = config, - strings = strings, - onSave = { onLanguageConfigChange(it); showSettings = false }, - onDismiss = { showSettings = false }, - ) - } } } + if (showSettings) { + SystemAudioLanguageSettingsWindow( + config = config, + strings = strings, + dark = dark, + position = windowState.position, + onConfigChange = onLanguageConfigChange, + onClose = { showSettings = false }, + ) + } } @Composable -internal fun SystemAudioLanguageSettingsDialog( +internal fun SystemAudioLanguageSettingsWindow( config: SystemAudioLanguageConfig, strings: LocaleStrings, - onSave: (SystemAudioLanguageConfig) -> Unit, - onDismiss: () -> Unit, + dark: Boolean, + position: WindowPosition = WindowPosition(24.dp, 24.dp), + onConfigChange: (SystemAudioLanguageConfig) -> Unit, + onClose: () -> Unit, ) { - var source by remember { mutableStateOf(config.sourceLanguage) } - var target by remember { mutableStateOf(config.targetLanguage) } - var opacity by remember { mutableIntStateOf(config.normalized().opacityPercent) } - var overlayEnabled by remember { mutableStateOf(config.steamVrOverlayEnabled) } - var overlayPosition by remember { mutableStateOf(config.steamVrOverlayPosition) } - var overlayScale by remember { mutableIntStateOf(config.normalized().steamVrOverlayScalePercent) } - Dialog(onDismissRequest = onDismiss) { - Surface( - modifier = Modifier.fillMaxWidth().testTag("systemAudioLanguageSettings"), - shape = MaterialTheme.shapes.extraLarge, - tonalElevation = 6.dp, - ) { - Column( - Modifier.testTag("systemAudioSettingsScroll").verticalScroll(rememberScrollState()).padding(16.dp), - verticalArrangement = Arrangement.spacedBy(8.dp), + var current by remember { mutableStateOf(config.normalized()) } + LaunchedEffect(config) { current = config.normalized() } + fun apply(updated: SystemAudioLanguageConfig) { + val normalized = updated.normalized() + current = normalized + onConfigChange(normalized) + } + val windowState = rememberWindowState(width = 360.dp, height = 480.dp, position = position) + Window( + onCloseRequest = onClose, + title = "VRCMC · ${strings.settings}", + icon = painterResource(Res.drawable.logo), + state = windowState, + undecorated = true, + transparent = true, + alwaysOnTop = true, + ) { + SideEffect { + // Keep the rounded corners transparent while the settings content stays fully opaque. + window.background = java.awt.Color(0, 0, 0, 0) + window.opacity = 1f + window.isResizable = false + } + LaunchedEffect(window) { + if (!setWindowsRoundedCorners(window)) { + window.shape = RoundRectangle2D.Double( + 0.0, + 0.0, + window.width.toDouble(), + window.height.toDouble(), + 16.0, + 16.0, + ) + } + } + MaterialTheme(if (dark) darkColorScheme() else lightColorScheme()) { + Surface( + modifier = Modifier.fillMaxSize().testTag("systemAudioLanguageSettings"), + shape = RoundedCornerShape(16.dp), + color = MaterialTheme.colorScheme.surface, ) { - Text(strings.settings, style = MaterialTheme.typography.titleLarge) - Column { - SystemAudioLanguagePicker( - label = strings.systemAudioSourceLanguage, - selected = source, - options = listOf("auto" to strings.systemAudioAutomaticLanguage) + systemAudioSourceLanguages.toList(), - onSelect = { source = it }, - ) - SystemAudioLanguagePicker( - label = strings.targetLanguage, - selected = target, - options = availableTargetLanguages.map { it to it }, - onSelect = { target = it }, - ) - Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { - Text(strings.steamVrOverlay, Modifier.weight(1f), style = MaterialTheme.typography.labelLarge) - Switch( - checked = overlayEnabled, - onCheckedChange = { overlayEnabled = it }, - modifier = Modifier.semantics { contentDescription = strings.steamVrOverlay }, - ) - } - if (overlayEnabled) { - SystemAudioLanguagePicker( - label = strings.steamVrOverlayPosition, - selected = overlayPosition.name, - options = listOf( - SteamVrOverlayPosition.LEFT_HAND.name to strings.steamVrOverlayLeftHand, - SteamVrOverlayPosition.RIGHT_HAND.name to strings.steamVrOverlayRightHand, - SteamVrOverlayPosition.SCREEN_CENTER.name to strings.steamVrOverlayScreenCenter, - ), - onSelect = { overlayPosition = SteamVrOverlayPosition.valueOf(it) }, - ) - SystemAudioLanguagePicker( - label = strings.steamVrOverlaySize, - selected = overlayScale.toString(), - options = steamVrOverlayScalePercents.map { - it.toString() to if (it == 100) strings.steamVrOverlayDefaultSize else "$it%" - }, - onSelect = { overlayScale = it.toInt() }, - ) - Text(strings.steamVrOverlaySizeHint, style = MaterialTheme.typography.bodySmall, - color = MaterialTheme.colorScheme.onSurfaceVariant) - Text(strings.steamVrOverlayHint, style = MaterialTheme.typography.bodySmall, - color = MaterialTheme.colorScheme.onSurfaceVariant) + Column(Modifier.fillMaxSize()) { + Column( + Modifier.weight(1f).fillMaxWidth().testTag("systemAudioSettingsScroll") + .verticalScroll(rememberScrollState()).padding(16.dp), + verticalArrangement = Arrangement.spacedBy(8.dp), + ) { + Text(strings.settings, style = MaterialTheme.typography.titleLarge) + Column { + SystemAudioLanguagePicker( + label = strings.systemAudioSourceLanguage, + selected = current.sourceLanguage, + options = listOf("auto" to strings.systemAudioAutomaticLanguage) + + systemAudioSourceLanguages.toList(), + optionTestTag = "systemAudioSourceLanguage", + onSelect = { apply(current.copy(sourceLanguage = it)) }, + ) + SystemAudioLanguagePicker( + label = strings.targetLanguage, + selected = current.targetLanguage, + options = availableTargetLanguages.map { it to it }, + optionTestTag = "systemAudioTargetLanguage", + onSelect = { apply(current.copy(targetLanguage = it)) }, + ) + Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { + Text( + strings.steamVrOverlay, + Modifier.weight(1f), + style = MaterialTheme.typography.labelLarge, + ) + Switch( + checked = current.steamVrOverlayEnabled, + onCheckedChange = { + apply(current.copy(steamVrOverlayEnabled = it)) + }, + modifier = Modifier.semantics { contentDescription = strings.steamVrOverlay }, + ) + } + if (current.steamVrOverlayEnabled) { + SystemAudioLanguagePicker( + label = strings.steamVrOverlayPosition, + selected = current.steamVrOverlayPosition.name, + options = listOf( + SteamVrOverlayPosition.LEFT_HAND.name to strings.steamVrOverlayLeftHand, + SteamVrOverlayPosition.RIGHT_HAND.name to strings.steamVrOverlayRightHand, + SteamVrOverlayPosition.SCREEN_CENTER.name to strings.steamVrOverlayScreenCenter, + ), + optionTestTag = "steamVrOverlayPosition", + onSelect = { + apply( + current.copy( + steamVrOverlayPosition = SteamVrOverlayPosition.valueOf(it), + ), + ) + }, + ) + SystemAudioLanguagePicker( + label = strings.steamVrOverlaySize, + selected = current.steamVrOverlayScalePercent.toString(), + options = steamVrOverlayScalePercents.map { + it.toString() to if (it == 100) strings.steamVrOverlayDefaultSize else "$it%" + }, + optionTestTag = "steamVrOverlaySize", + onSelect = { + apply(current.copy(steamVrOverlayScalePercent = it.toInt())) + }, + ) + Text( + strings.steamVrOverlaySizeHint, + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + SystemAudioLanguagePicker( + label = strings.steamVrOverlayFontSize, + selected = current.steamVrOverlayFontSizePercent.toString(), + options = steamVrOverlayFontSizePercents.map { + it.toString() to + if (it == 100) strings.steamVrOverlayDefaultFontSize else "$it%" + }, + optionTestTag = "steamVrOverlayFontSize", + onSelect = { + apply(current.copy(steamVrOverlayFontSizePercent = it.toInt())) + }, + ) + Text( + strings.steamVrOverlayFontSizeHint, + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + Text( + strings.steamVrOverlayHint, + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { + Text( + strings.systemAudioWindowOpacity, + Modifier.weight(1f), + style = MaterialTheme.typography.labelLarge, + ) + Text("${current.opacityPercent}%", style = MaterialTheme.typography.labelLarge) + } + Slider( + value = current.opacityPercent.toFloat(), + onValueChange = { + apply(current.copy(opacityPercent = it.roundToInt())) + }, + valueRange = 30f..100f, + modifier = Modifier.fillMaxWidth().semantics { + contentDescription = strings.systemAudioWindowOpacity + stateDescription = "${current.opacityPercent}%" + }, + ) + } } - Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { - Text(strings.systemAudioWindowOpacity, Modifier.weight(1f), style = MaterialTheme.typography.labelLarge) - Text("$opacity%", style = MaterialTheme.typography.labelLarge) + Row( + Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp), + horizontalArrangement = Arrangement.End, + ) { + TextButton(onClick = onClose) { Text(strings.done) } } - Slider( - value = opacity.toFloat(), - onValueChange = { opacity = it.roundToInt() }, - valueRange = 30f..100f, - modifier = Modifier.fillMaxWidth().semantics { - contentDescription = strings.systemAudioWindowOpacity - stateDescription = "$opacity%" - }, - ) - } - Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) { - TextButton(onClick = onDismiss) { Text(strings.cancel) } - TextButton(onClick = { - onSave(config.copy( - sourceLanguage = source, targetLanguage = target, opacityPercent = opacity, - steamVrOverlayEnabled = overlayEnabled, steamVrOverlayPosition = overlayPosition, - steamVrOverlayScalePercent = overlayScale, - ).normalized()) - }) { Text(strings.save) } } } } @@ -363,6 +443,7 @@ private fun SystemAudioLanguagePicker( label: String, selected: String, options: List>, + optionTestTag: String, onSelect: (String) -> Unit, ) { var expanded by remember { mutableStateOf(false) } @@ -370,10 +451,13 @@ private fun SystemAudioLanguagePicker( Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) { Text(label, Modifier.weight(1f), style = MaterialTheme.typography.labelLarge) Box(Modifier.weight(1.6f)) { - OutlinedButton(onClick = { expanded = true }, modifier = Modifier.fillMaxWidth().semantics { - contentDescription = label - stateDescription = selectedLabel - }) { + OutlinedButton( + onClick = { expanded = true }, + modifier = Modifier.fillMaxWidth().testTag("$optionTestTag-picker").semantics { + contentDescription = label + stateDescription = selectedLabel + }, + ) { Text(selectedLabel, Modifier.weight(1f)) Icon(Icons.Default.ArrowDropDown, contentDescription = null) } @@ -382,6 +466,7 @@ private fun SystemAudioLanguagePicker( DropdownMenuItem( text = { Text(text) }, leadingIcon = { if (value == selected) Icon(Icons.Default.Check, contentDescription = null) }, + modifier = Modifier.testTag("$optionTestTag-option-$value"), onClick = { onSelect(value); expanded = false }, ) } diff --git a/composeApp/src/desktopTest/kotlin/com/vrcmc/app/audio/SteamVrSubtitleOverlayTest.kt b/composeApp/src/desktopTest/kotlin/com/vrcmc/app/audio/SteamVrSubtitleOverlayTest.kt index 60b6fa1..3eef724 100644 --- a/composeApp/src/desktopTest/kotlin/com/vrcmc/app/audio/SteamVrSubtitleOverlayTest.kt +++ b/composeApp/src/desktopTest/kotlin/com/vrcmc/app/audio/SteamVrSubtitleOverlayTest.kt @@ -198,6 +198,32 @@ class SteamVrSubtitleOverlayTest { } } + @Test + fun independentFontSizePresetsScaleRenderedTextSize() { + val sample = content.copy(captions = listOf(SystemAudioCaption(1, "Font size"))) + val default = SteamVrSubtitleRenderer.render(sample) + val foreground = Color(0xEEEAF4).rgb + fun glyphHeight(image: BufferedImage): Int { + val rows = (0 until image.height).filter { y -> + (0 until image.width).any { x -> image.getRGB(x, y) == foreground } + } + assertTrue(rows.isNotEmpty()) + return rows.last() - rows.first() + 1 + } + val defaultHeight = glyphHeight(default) + for (fontSize in steamVrOverlayFontSizePercents) { + val rendered = SteamVrSubtitleRenderer.render(sample, fontSizePercent = fontSize) + assertEquals( + SteamVrSubtitleRenderer.WIDTH to SteamVrSubtitleRenderer.HEIGHT, + rendered.width to rendered.height, + ) + val height = glyphHeight(rendered) + if (fontSize < 100) assertTrue(height < defaultHeight) + if (fontSize == 100) assertEquals(defaultHeight, height) + if (fontSize > 100) assertTrue(height > defaultHeight) + } + } + @Test fun rendererWrapsMultilingualCaptionsAndUploadsStraightRgbaWithTransparentCorners() { for (dark in listOf(true, false)) { diff --git a/composeApp/src/desktopTest/kotlin/com/vrcmc/app/audio/SystemAudioSubtitleWindowTest.kt b/composeApp/src/desktopTest/kotlin/com/vrcmc/app/audio/SystemAudioSubtitleWindowTest.kt index 2cd7565..c593a38 100644 --- a/composeApp/src/desktopTest/kotlin/com/vrcmc/app/audio/SystemAudioSubtitleWindowTest.kt +++ b/composeApp/src/desktopTest/kotlin/com/vrcmc/app/audio/SystemAudioSubtitleWindowTest.kt @@ -1,7 +1,6 @@ package com.vrcmc.app import androidx.compose.runtime.* -import androidx.compose.material3.MaterialTheme import androidx.compose.ui.graphics.toAwtImage import androidx.compose.ui.semantics.SemanticsProperties import androidx.compose.ui.semantics.SemanticsActions @@ -38,6 +37,9 @@ class SystemAudioSubtitleWindowTest { } } + private fun SemanticsNodeInteraction.performClickOnUiThread() = + performSemanticsAction(SemanticsActions.OnClick) { it() } + private fun assertRoundedWindow(window: Window) { val dwm = Native.load("dwmapi", SubtitleDwmTestApi::class.java) Memory(Int.SIZE_BYTES.toLong()).use { value -> @@ -86,14 +88,33 @@ class SystemAudioSubtitleWindowTest { .performSemanticsAction(SemanticsActions.OnClick) { it() } waitForSelection(LocaleStringsZhHans.systemAudioSourceLanguage, "自动") compose.onNodeWithContentDescription(LocaleStringsZhHans.targetLanguage).assertIsDisplayed() - compose.onNodeWithText(LocaleStringsZhHans.cancel) - .performSemanticsAction(SemanticsActions.OnClick) { it() } - compose.waitUntil(5_000) { compose.onAllNodesWithTag("systemAudioLanguageSettings").fetchSemanticsNodes().isEmpty() } + compose.onNodeWithText(LocaleStringsZhHans.cancel).assertDoesNotExist() + compose.onNodeWithText(LocaleStringsZhHans.save).assertDoesNotExist() lateinit var subtitleWindow: Window + lateinit var settingsWindow: Window compose.runOnIdle { subtitleWindow = Window.getWindows().single { it.isShowing && (it as? Frame)?.title == "VRCMC · 听别人说话" } + settingsWindow = Window.getWindows().single { + it.isShowing && (it as? Frame)?.title == "VRCMC · 设置" + } + assertTrue(settingsWindow is Frame) + assertFalse(settingsWindow is java.awt.Dialog) + val settingsFrame = settingsWindow as Frame + assertTrue(settingsFrame.isUndecorated) + assertFalse(settingsFrame.isResizable) + assertTrue(settingsWindow.isAlwaysOnTop) + assertEquals(1f, settingsWindow.opacity) + assertEquals(subtitleWindow.width, settingsWindow.width) + assertEquals(subtitleWindow.height, settingsWindow.height) + assertEquals(subtitleWindow.location, settingsWindow.location) + assertRoundedWindow(settingsWindow) + } + compose.onNodeWithText(LocaleStringsZhHans.done) + .performSemanticsAction(SemanticsActions.OnClick) { it() } + compose.waitUntil(5_000) { compose.onAllNodesWithTag("systemAudioLanguageSettings").fetchSemanticsNodes().isEmpty() } + compose.runOnIdle { assertTrue(subtitleWindow.isAlwaysOnTop) assertTrue((subtitleWindow as Frame).isUndecorated) assertTrue((subtitleWindow as Frame).isResizable) @@ -113,6 +134,12 @@ class SystemAudioSubtitleWindowTest { compose.onNodeWithContentDescription(LocaleStringsZhHans.settings) .performSemanticsAction(SemanticsActions.OnClick) { it() } waitForSelection(LocaleStringsZhHans.systemAudioWindowOpacity, "80%") + compose.runOnIdle { + settingsWindow = Window.getWindows().single { + it.isShowing && (it as? Frame)?.title == "VRCMC · 设置" + } + assertEquals(1f, settingsWindow.opacity) + } compose.onNodeWithTag("systemAudioSettingsScroll") .performSemanticsAction(SemanticsActions.ScrollBy) { it(0f, 500f) } compose.waitUntil(5_000) { @@ -123,17 +150,15 @@ class SystemAudioSubtitleWindowTest { .assertIsDisplayed() .performSemanticsAction(SemanticsActions.SetProgress) { it(65f) } waitForSelection(LocaleStringsZhHans.systemAudioWindowOpacity, "65%") - compose.runOnIdle { - assertEquals(.8f, subtitleWindow.opacity) - assertEquals(80, config.opacityPercent) - } - compose.onNodeWithText(LocaleStringsZhHans.save).assertIsDisplayed() - .performSemanticsAction(SemanticsActions.OnClick) { it() } - compose.waitUntil(5_000) { subtitleWindow.opacity == .65f } + compose.waitUntil(5_000) { subtitleWindow.opacity == .65f && config.opacityPercent == 65 } compose.runOnIdle { assertEquals(65, config.opacityPercent) assertRoundedWindow(subtitleWindow) + assertEquals(subtitleWindow.location, settingsWindow.location) } + compose.onNodeWithText(LocaleStringsZhHans.done) + .performSemanticsAction(SemanticsActions.OnClick) { it() } + compose.waitUntil(5_000) { compose.onAllNodesWithTag("systemAudioLanguageSettings").fetchSemanticsNodes().isEmpty() } compose.onNodeWithContentDescription(LocaleStringsZhHans.minimizeWindow) .performSemanticsAction(SemanticsActions.OnClick) { it() } compose.waitUntil(5_000) { (subtitleWindow as Frame).extendedState and Frame.ICONIFIED != 0 } @@ -167,77 +192,120 @@ class SystemAudioSubtitleWindowTest { } @Test - fun steamVrSettingsSaveAllPresetsShareOpacityAndCancelWithoutApplying() { + fun steamVrSettingsApplyImmediatelyInAnIndependentOpaqueWindow() { assumeTrue(Platform.isWindows()) var config by mutableStateOf(SystemAudioLanguageConfig()) var visible by mutableStateOf(true) - var saves = 0 + var changes = 0 compose.setContent { - MaterialTheme { - if (visible) SystemAudioLanguageSettingsDialog( - config = config, strings = LocaleStringsZhHans, - onSave = { config = it; saves++; visible = false }, - onDismiss = { visible = false }, - ) + if (visible) SystemAudioLanguageSettingsWindow( + config = config, + strings = LocaleStringsZhHans, + dark = true, + onConfigChange = { config = it; changes++ }, + onClose = { visible = false }, + ) + } + compose.waitUntil(5_000) { + compose.onAllNodesWithContentDescription(LocaleStringsZhHans.systemAudioSourceLanguage) + .fetchSemanticsNodes().isNotEmpty() + } + compose.onNodeWithText(LocaleStringsZhHans.cancel).assertDoesNotExist() + compose.onNodeWithText(LocaleStringsZhHans.save).assertDoesNotExist() + lateinit var settingsWindow: Window + compose.runOnIdle { + settingsWindow = Window.getWindows().single { + it.isShowing && (it as? Frame)?.title == "VRCMC · 设置" } + assertTrue(settingsWindow is Frame) + assertFalse(settingsWindow is java.awt.Dialog) + val settingsFrame = settingsWindow as Frame + assertTrue(settingsFrame.isUndecorated) + assertFalse(settingsFrame.isResizable) + assertTrue(settingsWindow.isAlwaysOnTop) + assertEquals(1f, settingsWindow.opacity) + assertEquals(0, settingsWindow.background.alpha) + assertEquals(360, settingsWindow.width) + assertEquals(480, settingsWindow.height) + assertRoundedWindow(settingsWindow) } - compose.onNodeWithContentDescription(LocaleStringsZhHans.steamVrOverlay).assertIsOff().performClick() + compose.onNodeWithContentDescription(LocaleStringsZhHans.steamVrOverlay).assertIsOff().performClickOnUiThread() + compose.waitUntil(5_000) { config.steamVrOverlayEnabled } waitForSelection(LocaleStringsZhHans.steamVrOverlayPosition, "左手") waitForSelection(LocaleStringsZhHans.steamVrOverlaySize, LocaleStringsZhHans.steamVrOverlayDefaultSize) - for (scale in steamVrOverlayScalePercents + 150) { - compose.onNodeWithContentDescription(LocaleStringsZhHans.steamVrOverlaySize).performScrollTo().performClick() - val label = if (scale == 100) LocaleStringsZhHans.steamVrOverlayDefaultSize else "$scale%" - compose.onNodeWithText(label).performScrollTo().performClick() - waitForSelection(LocaleStringsZhHans.steamVrOverlaySize, label) + waitForSelection(LocaleStringsZhHans.steamVrOverlayFontSize, LocaleStringsZhHans.steamVrOverlayDefaultFontSize) + compose.onNodeWithContentDescription(LocaleStringsZhHans.steamVrOverlaySize) + .performClickOnUiThread() + compose.waitUntil(5_000) { + compose.onAllNodesWithTag("steamVrOverlaySize-option-150").fetchSemanticsNodes().isNotEmpty() + } + val sizeTag = "steamVrOverlaySize-option-150" + compose.onNodeWithTag(sizeTag).performClickOnUiThread() + compose.waitUntil(5_000) { config.steamVrOverlayScalePercent == 150 } + compose.waitUntil(5_000) { compose.onAllNodesWithTag(sizeTag).fetchSemanticsNodes().isEmpty() } + waitForSelection(LocaleStringsZhHans.steamVrOverlaySize, "150%") + + compose.onNodeWithContentDescription(LocaleStringsZhHans.steamVrOverlayFontSize) + .performClickOnUiThread() + compose.waitUntil(5_000) { + compose.onAllNodesWithTag("steamVrOverlayFontSize-option-175").fetchSemanticsNodes().isNotEmpty() } - for (label in listOf("右手", "屏幕中间", "左手", "屏幕中间")) { - compose.onNodeWithContentDescription(LocaleStringsZhHans.steamVrOverlayPosition).performScrollTo().performClick() - compose.onNodeWithText(label).performClick() - waitForSelection(LocaleStringsZhHans.steamVrOverlayPosition, label) + val fontSizeTag = "steamVrOverlayFontSize-option-175" + compose.onNodeWithTag(fontSizeTag).performClickOnUiThread() + compose.waitUntil(5_000) { config.steamVrOverlayFontSizePercent == 175 } + compose.waitUntil(5_000) { compose.onAllNodesWithTag(fontSizeTag).fetchSemanticsNodes().isEmpty() } + waitForSelection(LocaleStringsZhHans.steamVrOverlayFontSize, "175%") + + compose.onNodeWithContentDescription(LocaleStringsZhHans.steamVrOverlayPosition) + .performClickOnUiThread() + val positionTag = "steamVrOverlayPosition-option-${SteamVrOverlayPosition.SCREEN_CENTER.name}" + compose.waitUntil(5_000) { compose.onAllNodesWithTag(positionTag).fetchSemanticsNodes().isNotEmpty() } + compose.onNodeWithTag(positionTag).performClickOnUiThread() + compose.waitUntil(5_000) { config.steamVrOverlayPosition == SteamVrOverlayPosition.SCREEN_CENTER } + compose.waitUntil(5_000) { + compose.onAllNodesWithTag(positionTag).fetchSemanticsNodes().isEmpty() } - compose.onNodeWithContentDescription(LocaleStringsZhHans.systemAudioWindowOpacity).performScrollTo() + waitForSelection(LocaleStringsZhHans.steamVrOverlayPosition, "屏幕中间") + compose.onNodeWithContentDescription(LocaleStringsZhHans.systemAudioWindowOpacity) .performSemanticsAction(SemanticsActions.SetProgress) { it(55f) } waitForSelection(LocaleStringsZhHans.systemAudioWindowOpacity, "55%") - compose.runOnIdle { assertEquals(SystemAudioLanguageConfig(), config) } - val image = compose.onNodeWithTag("systemAudioLanguageSettings").captureToImage() - val output = File("build/test-screenshots/system-audio-steamvr-settings.png") - output.parentFile.mkdirs() - ImageIO.write(image.toAwtImage(), "png", output) - compose.onNodeWithText(LocaleStringsZhHans.save).performScrollTo().performClick() - compose.waitUntil(5_000) { saves == 1 } val expected = SystemAudioLanguageConfig( - opacityPercent = 55, steamVrOverlayEnabled = true, steamVrOverlayPosition = SteamVrOverlayPosition.SCREEN_CENTER, + opacityPercent = 55, + steamVrOverlayEnabled = true, + steamVrOverlayPosition = SteamVrOverlayPosition.SCREEN_CENTER, steamVrOverlayScalePercent = 150, + steamVrOverlayFontSizePercent = 175, ) compose.runOnIdle { assertEquals(expected, config) - val provider = providerById("microsoft_edge_web") - val services = SystemAudioListeningSettings(VoiceInputConfig(), provider, defaultProviderConfig(provider), listOf("English")) - assertEquals(services.withLanguages(SystemAudioLanguageConfig()), services.withLanguages(config)) - visible = true + assertTrue(changes >= 5) } - waitForSelection(LocaleStringsZhHans.steamVrOverlayPosition, "屏幕中间") - waitForSelection(LocaleStringsZhHans.steamVrOverlaySize, "150%") - compose.onNodeWithContentDescription(LocaleStringsZhHans.steamVrOverlaySize).performScrollTo().performClick() - compose.onNodeWithText("75%").performScrollTo().performClick() - waitForSelection(LocaleStringsZhHans.steamVrOverlaySize, "75%") - compose.onNodeWithContentDescription(LocaleStringsZhHans.steamVrOverlay).assertIsOn().performScrollTo().performClick() - compose.onNodeWithContentDescription(LocaleStringsZhHans.steamVrOverlayPosition).assertDoesNotExist() - compose.onNodeWithContentDescription(LocaleStringsZhHans.steamVrOverlaySize).assertDoesNotExist() - compose.onNodeWithText(LocaleStringsZhHans.cancel).performScrollTo().performClick() - compose.runOnIdle { - assertEquals(expected, config) - assertEquals(1, saves) - visible = true + val image = compose.onNodeWithTag("systemAudioLanguageSettings").captureToImage() + val imageAwt = image.toAwtImage() + assertEquals(0, imageAwt.getRGB(0, 0) ushr 24) + assertEquals(0, imageAwt.getRGB(imageAwt.width - 1, 0) ushr 24) + val output = File("build/test-screenshots/system-audio-steamvr-settings.png") + output.parentFile.mkdirs() + ImageIO.write(imageAwt, "png", output) + val windowScreenshot = Robot().createScreenCapture( + Rectangle(settingsWindow.locationOnScreen, settingsWindow.size), + ) + ImageIO.write(windowScreenshot, "png", File("build/test-screenshots/system-audio-settings-window.png")) + compose.onNodeWithContentDescription(LocaleStringsZhHans.steamVrOverlay) + .assertIsOn().performClickOnUiThread() + compose.waitUntil(5_000) { !config.steamVrOverlayEnabled } + compose.waitUntil(5_000) { + compose.onAllNodesWithContentDescription(LocaleStringsZhHans.steamVrOverlayPosition) + .fetchSemanticsNodes().isEmpty() } - compose.onNodeWithContentDescription(LocaleStringsZhHans.steamVrOverlay).performScrollTo().performClick() - compose.onNodeWithText(LocaleStringsZhHans.save).performScrollTo().performClick() - compose.waitUntil(5_000) { saves == 2 } + compose.onNodeWithContentDescription(LocaleStringsZhHans.steamVrOverlaySize).assertDoesNotExist() compose.runOnIdle { assertEquals(expected.copy(steamVrOverlayEnabled = false), config) } + compose.onNodeWithText(LocaleStringsZhHans.done).performClickOnUiThread() + compose.waitUntil(5_000) { !visible } } @Test - fun settingsApplyLanguagesAndOpacityOnlyWhenSaved() { + fun settingsApplyLanguagesAndOpacityImmediatelyWithoutSaveOrCancel() { assumeTrue(Platform.isWindows()) val provider = providerById("microsoft_edge_web") val services = SystemAudioListeningSettings( @@ -245,15 +313,15 @@ class SystemAudioSubtitleWindowTest { ) var config by mutableStateOf(SystemAudioLanguageConfig()) var visible by mutableStateOf(true) - var saves = 0 + var changes = 0 compose.setContent { - MaterialTheme { - if (visible) SystemAudioLanguageSettingsDialog( - config = config, strings = LocaleStringsZhHans, - onSave = { config = it; saves++; visible = false }, - onDismiss = { visible = false }, - ) - } + if (visible) SystemAudioLanguageSettingsWindow( + config = config, + strings = LocaleStringsZhHans, + dark = false, + onConfigChange = { config = it; changes++ }, + onClose = { visible = false }, + ) } compose.waitUntil(5_000) { compose.onAllNodesWithContentDescription(LocaleStringsZhHans.systemAudioSourceLanguage).fetchSemanticsNodes().isNotEmpty() @@ -264,30 +332,39 @@ class SystemAudioSubtitleWindowTest { .assertIsDisplayed().assert(SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, "简体中文")) compose.onNodeWithContentDescription(LocaleStringsZhHans.systemAudioWindowOpacity) .assertIsDisplayed().assert(SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, "80%")) + compose.onNodeWithText(LocaleStringsZhHans.cancel).assertDoesNotExist() + compose.onNodeWithText(LocaleStringsZhHans.save).assertDoesNotExist() val screenshot = compose.onNodeWithTag("systemAudioLanguageSettings").captureToImage() val output = File("build/test-screenshots/system-audio-languages.png") output.parentFile.mkdirs() ImageIO.write(screenshot.toAwtImage(), "png", output) - compose.onNodeWithContentDescription(LocaleStringsZhHans.systemAudioSourceLanguage).performClick() - compose.waitUntil(5_000) { compose.onAllNodesWithText("English").fetchSemanticsNodes().isNotEmpty() } - compose.onNodeWithText("English").performScrollTo().performClick() + compose.onNodeWithContentDescription(LocaleStringsZhHans.systemAudioSourceLanguage).performClickOnUiThread() + compose.waitUntil(5_000) { + compose.onAllNodesWithTag("systemAudioSourceLanguage-option-en").fetchSemanticsNodes().isNotEmpty() + } + compose.onNodeWithTag("systemAudioSourceLanguage-option-en").performClickOnUiThread() + compose.waitUntil(5_000) { + compose.onAllNodesWithTag("systemAudioSourceLanguage-option-en").fetchSemanticsNodes().isEmpty() + } waitForSelection(LocaleStringsZhHans.systemAudioSourceLanguage, "English") - compose.onNodeWithContentDescription(LocaleStringsZhHans.targetLanguage).performClick() - compose.waitUntil(5_000) { compose.onAllNodesWithText("日本語").fetchSemanticsNodes().isNotEmpty() } - compose.onNodeWithText("日本語").performScrollTo().performClick() + compose.runOnIdle { assertEquals("en", config.sourceLanguage) } + compose.onNodeWithContentDescription(LocaleStringsZhHans.targetLanguage).performClickOnUiThread() + compose.waitUntil(5_000) { + compose.onAllNodesWithTag("systemAudioTargetLanguage-option-日本語").fetchSemanticsNodes().isNotEmpty() + } + compose.onNodeWithTag("systemAudioTargetLanguage-option-日本語") + .performClickOnUiThread() + compose.waitUntil(5_000) { + compose.onAllNodesWithTag("systemAudioTargetLanguage-option-日本語").fetchSemanticsNodes().isEmpty() + } waitForSelection(LocaleStringsZhHans.targetLanguage, "日本語") + compose.runOnIdle { assertEquals("日本語", config.targetLanguage) } compose.onNodeWithContentDescription(LocaleStringsZhHans.systemAudioWindowOpacity) .performSemanticsAction(SemanticsActions.SetProgress) { it(70f) } waitForSelection(LocaleStringsZhHans.systemAudioWindowOpacity, "70%") - compose.runOnIdle { - assertEquals(SystemAudioLanguageConfig(), config) - assertEquals(0, saves) - } - compose.onNodeWithText(LocaleStringsZhHans.save).performClick() - compose.waitUntil(5_000) { saves == 1 } compose.runOnIdle { assertEquals(SystemAudioLanguageConfig("en", "日本語", 70), config) - assertEquals(1, saves) + assertTrue(changes >= 3) val effective = services.withLanguages(config) assertEquals("en", effective.voice.language) assertEquals(listOf("日本語"), effective.languages) @@ -295,28 +372,8 @@ class SystemAudioSubtitleWindowTest { assertEquals(listOf("English"), services.languages) assertEquals(effective, services.withLanguages(config.copy(opacityPercent = 30))) } - compose.runOnIdle { visible = true } - compose.waitUntil(5_000) { - compose.onAllNodesWithContentDescription(LocaleStringsZhHans.systemAudioSourceLanguage).fetchSemanticsNodes().isNotEmpty() - } - compose.onNodeWithContentDescription(LocaleStringsZhHans.systemAudioSourceLanguage) - .assert(SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, "English")) - compose.onNodeWithContentDescription(LocaleStringsZhHans.targetLanguage) - .assert(SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, "日本語")) - compose.onNodeWithContentDescription(LocaleStringsZhHans.systemAudioWindowOpacity) - .assert(SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, "70%")) - .performSemanticsAction(SemanticsActions.SetProgress) { it(30f) } - waitForSelection(LocaleStringsZhHans.systemAudioWindowOpacity, "30%") - compose.onNodeWithContentDescription(LocaleStringsZhHans.systemAudioSourceLanguage).performClick() - compose.waitUntil(5_000) { compose.onAllNodesWithText("自动").fetchSemanticsNodes().isNotEmpty() } - compose.onNodeWithText("自动").performScrollTo().performClick() - waitForSelection(LocaleStringsZhHans.systemAudioSourceLanguage, "自动") - compose.onNodeWithText(LocaleStringsZhHans.cancel).performClick() - compose.waitUntil(5_000) { compose.onAllNodesWithTag("systemAudioLanguageSettings").fetchSemanticsNodes().isEmpty() } - compose.runOnIdle { - assertEquals(SystemAudioLanguageConfig("en", "日本語", 70), config) - assertEquals(1, saves) - } + compose.onNodeWithText(LocaleStringsZhHans.done).performClickOnUiThread() + compose.waitUntil(5_000) { !visible } } }