hex/src/rune_ui/speech_model.rs
Dax 7a2c78ace2
feat(labs): model switch shortcut (#2679)
One key swaps the dictation model and language with a saved other one, and back, installed only, through the same save and reload path the speech-model picker uses. Requested in #2654.
2026-11-24 12:57:57 -05:00

186 lines
5.6 KiB
Rust

//! The speech model and language a row in General switches to: the
//! picker's installed GGUF models plus the row's empty choice, then a
//! language the model supports.
use rune::{El, row};
use super::controls::{Choice, Paint, fixed_select, select, setting_row};
use super::settings::toggle_menu;
use super::{Menu, Ui};
use crate::app_settings::KeyboardModelChoice;
use crate::desktop::i18n::tr;
use crate::transcription::models::{self as transcription_models, ModelDefinition};
/// One row's keys and copy.
pub(super) struct SpeechRow<'a> {
/// The row's key.
pub(super) key: &'a str,
/// The model select's key.
pub(super) model_key: &'a str,
/// The language select's key.
pub(super) language_key: &'a str,
pub(super) title: &'a str,
pub(super) description: Option<&'a str>,
/// The saved choice; None shows `empty_label`.
pub(super) choice: Option<KeyboardModelChoice>,
/// What the empty choice says: keyboard-language rows say "Don't switch",
/// the Other model row says "None".
pub(super) empty_label: &'a str,
pub(super) model_menu: Menu,
pub(super) language_menu: Menu,
}
/// A setting row choosing its empty choice or an installed GGUF model, then
/// the model's language. `language_for` picks the language a new choice starts
/// on, given the model and its last-used language; `save` writes the choice
/// or clears it with None.
pub(super) fn setting(
ui: &Ui,
p: Paint,
keys: SpeechRow<'_>,
language_for: impl Fn(&ModelDefinition, &str) -> String,
save: impl Fn(&mut Ui, Option<KeyboardModelChoice>) + Clone + 'static,
layer: &mut Vec<El>,
) -> El {
let SpeechRow {
key,
model_key,
language_key,
title,
description,
choice,
empty_label,
model_menu,
language_menu,
} = keys;
let mut choices = vec![Choice {
label: empty_label.into(),
detail: None,
selected: choice.is_none(),
choose: {
let save = save.clone();
Box::new(move |ui| {
ui.open = None;
save(ui, None);
})
},
}];
for model in transcription_models::all_models() {
let fallback = ui.settings.transcription_for_model(model.id).language;
if !model.available() || !model.runtime.is_gguf() || !ui.picker.installed(&model, &fallback)
{
continue;
}
let language = match &choice {
Some(choice) if model.supports_language(&choice.language) => choice.language.clone(),
_ => language_for(&model, &fallback),
};
let id = model.id;
let save = save.clone();
choices.push(Choice {
label: model.name.to_string(),
detail: None,
selected: choice.as_ref().is_some_and(|choice| choice.model == id),
choose: Box::new(move |ui| {
ui.open = None;
save(
ui,
Some(KeyboardModelChoice {
model: id,
language: language.clone(),
}),
);
}),
});
}
let label = choice.as_ref().map_or_else(
|| empty_label.to_string(),
|choice| {
let model = transcription_models::definition(choice.model);
if ui.picker.installed(&model, &choice.language) {
model.name.to_string()
} else {
format!("{} · {}", model.name, tr("Not installed"))
}
},
);
let mut controls = row().gap(8.0).center_y();
if let Some(choice) = &choice {
controls = controls.child(language(
ui,
p,
language_key,
choice,
language_menu,
save.clone(),
layer,
));
}
setting_row(
p,
key,
title,
description,
controls.child(select(
p,
model_key,
tr("Speech model"),
&label,
ui.open == Some(model_menu),
choices,
move |ui| toggle_menu(ui, model_menu),
layer,
)),
)
}
/// A chosen model's language: a select, or fixed for one-language models.
fn language(
ui: &Ui,
p: Paint,
key: &str,
choice: &KeyboardModelChoice,
menu: Menu,
save: impl Fn(&mut Ui, Option<KeyboardModelChoice>) + Clone + 'static,
layer: &mut Vec<El>,
) -> El {
let current = transcription_models::language_name(&choice.language);
let codes = transcription_models::menu_order(
&transcription_models::definition(choice.model).language_choices(),
);
if codes.len() <= 1 {
return fixed_select(p, key, tr("Language"), current);
}
let choices = codes
.into_iter()
.map(|code| {
let save = save.clone();
let model = choice.model;
Choice {
label: transcription_models::language_name(code).to_owned(),
detail: None,
selected: code == choice.language,
choose: Box::new(move |ui| {
ui.open = None;
save(
ui,
Some(KeyboardModelChoice {
model,
language: code.to_owned(),
}),
);
}),
}
})
.collect();
select(
p,
key,
tr("Language"),
current,
ui.open == Some(menu),
choices,
move |ui| toggle_menu(ui, menu),
layer,
)
}