Skip to content

Doubao-powered voice input for Linux, Wayland and Omarchy

feat: refresh microphone devices in settings

quanrua62ec701b6cd
diff --git a/src/doubao_input/ui/settings_window.py b/src/doubao_input/ui/settings_window.py
index 1866bfb..929ca29 100644
--- a/src/doubao_input/ui/settings_window.py
+++ b/src/doubao_input/ui/settings_window.py
@@ -80,6 +80,28 @@ class SettingsWindow:
self.autostart = row(tr("Start in background at login", "登录桌面后后台启动"),
Gtk.Switch(active=settings.autostart))
+ section(tr("Audio", "音频"))
+ self.sources = [("", tr("System default", "系统默认"))]
+ if settings.microphone:
+ self.sources.append((settings.microphone, tr(
+ "Saved device (unavailable)", "已保存设备(当前不可用)")))
+ self.microphone = Gtk.DropDown.new_from_strings(
+ [label for _, label in self.sources])
+ self.microphone.set_selected(
+ [key for key, _ in self.sources].index(settings.microphone))
+ self.microphone.set_hexpand(True)
+ microphone_controls = Gtk.Box(spacing=8)
+ microphone_controls.append(self.microphone)
+ self.microphone_refresh = Gtk.Button(label=tr(
+ "Refresh devices", "刷新设备"))
+ self.microphone_refresh.connect(
+ "clicked", lambda *_: self._refresh_microphones(announce=True))
+ microphone_controls.append(self.microphone_refresh)
+ row(tr("Microphone", "麦克风"), microphone_controls)
+ box.append(Gtk.Label(xalign=0, wrap=True, label=tr(
+ "Changes save automatically. Choose System default to follow the desktop input device.",
+ "更改会自动保存;选择“系统默认”可跟随桌面的输入设备。")))
+
section(tr("Recognition service", "语音识别服务"))
self.asr_provider = Gtk.DropDown.new_from_strings([
tr("Doubao account", "豆包账号"),
@@ -207,18 +229,6 @@ class SettingsWindow:
"Tap once to start or stop dictation. Hold to talk and release to finish. Pressing the active trigger twice sends Enter without dictation; it can submit a message or execute a terminal command. Test it in a plain text editor first.",
"短按一次开始或停止听写,长按说话、松开结束。连续按两次当前触发键会在不听写的情况下直接发送回车,可能发送消息或执行终端命令。请先在普通文本编辑器中测试。")))
- section(tr("Audio", "音频"))
- sources = [("", tr("System default", "系统默认"))] + microphones()
- if settings.microphone and settings.microphone not in [key for key, _ in sources]:
- sources.append((settings.microphone, tr("Saved device (unavailable)", "已保存设备(当前不可用)")))
- self.sources = sources
- self.microphone = Gtk.DropDown.new_from_strings([label for _, label in sources])
- self.microphone.set_selected([key for key, _ in sources].index(settings.microphone))
- row(tr("Microphone", "麦克风"), self.microphone)
- box.append(Gtk.Label(xalign=0, wrap=True, label=tr(
- "Changes save automatically. Check your microphone from the setup page.",
- "更改会自动保存;可在引导页检查麦克风。")))
-
section(tr("Appearance", "外观"))
self.waveform_style = Gtk.DropDown.new_from_strings([
tr("Classic bars", "经典声柱"), tr("Soft waves", "柔和声浪"),
@@ -400,6 +410,29 @@ class SettingsWindow:
self._updating = False
self._sync_vibekey_details()
+ def _refresh_microphones(self, *, announce=False):
+ discovered = microphones()
+ sources = [("", tr("System default", "系统默认"))] + discovered
+ selected = self._settings.microphone
+ if selected and selected not in [key for key, _ in sources]:
+ sources.append((selected, tr(
+ "Saved device (unavailable)", "已保存设备(当前不可用)")))
+ was_updating = self._updating
+ self._updating = True
+ try:
+ self.sources = sources
+ self.microphone.set_model(Gtk.StringList.new(
+ [label for _, label in sources]))
+ self.microphone.set_selected(
+ [key for key, _ in sources].index(selected))
+ finally:
+ self._updating = was_updating
+ if announce:
+ count = len(discovered)
+ self.status.set_text(tr(
+ f"Microphone devices refreshed: {count} found.",
+ f"已刷新麦克风设备:找到 {count} 个。"))
+
def _language_changed(self, *_):
previous = self._settings.language
if not self._save_current() or self._settings.language == previous:
@@ -518,4 +551,5 @@ class SettingsWindow:
return not self._save_current()
def show(self):
+ self._refresh_microphones()
self.window.present()
diff --git a/tests/unit/test_setup_actions.py b/tests/unit/test_setup_actions.py
index 198cbed..87a618f 100644
--- a/tests/unit/test_setup_actions.py
+++ b/tests/unit/test_setup_actions.py
@@ -1,7 +1,7 @@
"""Setup interaction contracts without a desktop, microphone or account."""
from types import SimpleNamespace
import unittest
-from unittest.mock import Mock
+from unittest.mock import Mock, patch
from gi.repository import GLib
@@ -12,6 +12,49 @@ from doubao_input.ui.settings_window import SettingsWindow
class SetupActionsTest(unittest.TestCase):
+ def test_settings_show_rescans_microphones_before_presenting(self):
+ view = SimpleNamespace(_refresh_microphones=Mock(), window=Mock())
+
+ SettingsWindow.show(view)
+
+ view._refresh_microphones.assert_called_once_with()
+ view.window.present.assert_called_once_with()
+
+ def test_settings_microphone_refresh_preserves_saved_selection(self):
+ view = SimpleNamespace(
+ _settings=SimpleNamespace(microphone="usb-mic"),
+ _updating=False,
+ microphone=Mock(),
+ status=Mock(),
+ sources=[],
+ )
+ with patch("doubao_input.ui.settings_window.microphones", return_value=[
+ ("built-in", "Built-in microphone"),
+ ("usb-mic", "USB microphone"),
+ ]):
+ SettingsWindow._refresh_microphones(view, announce=True)
+
+ self.assertEqual([key for key, _ in view.sources], ["", "built-in", "usb-mic"])
+ view.microphone.set_model.assert_called_once()
+ view.microphone.set_selected.assert_called_once_with(2)
+ self.assertIn("2", view.status.set_text.call_args.args[0])
+ self.assertFalse(view._updating)
+
+ def test_settings_microphone_refresh_keeps_missing_saved_device(self):
+ view = SimpleNamespace(
+ _settings=SimpleNamespace(microphone="unplugged-mic"),
+ _updating=False,
+ microphone=Mock(),
+ status=Mock(),
+ sources=[],
+ )
+ with patch("doubao_input.ui.settings_window.microphones", return_value=[]):
+ SettingsWindow._refresh_microphones(view, announce=True)
+
+ self.assertEqual([key for key, _ in view.sources], ["", "unplugged-mic"])
+ view.microphone.set_selected.assert_called_once_with(1)
+ self.assertIn("0", view.status.set_text.call_args.args[0])
+
def test_hiding_preview_cancels_before_hiding(self):
events = []
actions = SimpleNamespace(is_preview_testing=lambda: True,