feat: show keyboard keys or the controller's own buttons in the hint bar
The hint bar always named Xbox buttons, so someone at a keyboard was told to press buttons they did not have, and owners of other pads got the wrong letters. Hints now name an action, and the keys or button icons come from the input used last: keyboard, or an Xbox, PlayStation, Switch or Steam Deck controller. Behind Steam, every pad is Steam's virtual one. remus reads the file Steam names in SteamVirtualGamepadInfo to find the physical controller, matching the slot in the pad's name, as SDL does. Icons are Kenney's Input Prompts (CC0), with the licence beside them.
This commit is contained in:
+84
-28
@@ -2,7 +2,7 @@
|
||||
//! through `slint::invoke_from_event_loop` + `with_app`.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashSet;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::future::Future;
|
||||
use std::num::NonZeroUsize;
|
||||
use std::path::PathBuf;
|
||||
@@ -19,6 +19,7 @@ use tokio::sync::Semaphore;
|
||||
use crate::config::{self, Config, Settings};
|
||||
use crate::palette::{Lab, Palette, Role, Tone};
|
||||
use crate::player::{Player, PlayerEvent};
|
||||
use crate::prompts::{self, Control, Family, Prompts};
|
||||
use crate::subsonic::{self, Album, Artist, Client, Playlist, SearchResult, Song};
|
||||
use crate::{Action, AppWindow, Area, Header, Hint, Item, Screen, Theme};
|
||||
|
||||
@@ -43,7 +44,7 @@ pub fn install(ui: &AppWindow, rt: Handle) {
|
||||
let app = App::new(ui.clone_strong(), rt);
|
||||
APP.with(|cell| *cell.borrow_mut() = Some(app));
|
||||
|
||||
ui.on_action(|a| with_app(move |app| app.action(a)));
|
||||
ui.on_action(|a| with_app(move |app| app.key_action(a)));
|
||||
ui.on_item_clicked(|i| with_app(move |app| app.item_clicked(i.max(0) as usize)));
|
||||
ui.on_wheel(|d| with_app(move |app| app.wheel(d)));
|
||||
ui.on_sidebar_clicked(|i| with_app(move |app| app.sidebar_clicked(i.max(0) as usize)));
|
||||
@@ -216,6 +217,10 @@ pub struct App {
|
||||
scrobbled: bool,
|
||||
login_focus: i32,
|
||||
toast_timer: Timer,
|
||||
/// Keyboard keys or the buttons of the controller used last.
|
||||
prompts: Prompts,
|
||||
/// Decoded hint icons, so a selection move does not parse SVG again.
|
||||
icons: RefCell<HashMap<(Prompts, Control), Vec<Image>>>,
|
||||
settings: Settings,
|
||||
fade: Fade,
|
||||
}
|
||||
@@ -263,6 +268,8 @@ impl App {
|
||||
scrobbled: false,
|
||||
login_focus: 0,
|
||||
toast_timer: Timer::default(),
|
||||
prompts: Prompts::Keyboard,
|
||||
icons: RefCell::default(),
|
||||
settings: config::load_settings(),
|
||||
fade: Fade {
|
||||
from: Palette::brand(),
|
||||
@@ -405,6 +412,30 @@ impl App {
|
||||
|
||||
// ---------------------------------------------------------------- input
|
||||
|
||||
/// A key pressed in the window.
|
||||
pub fn key_action(&mut self, a: Action) {
|
||||
self.set_prompts(Prompts::Keyboard);
|
||||
self.action(a);
|
||||
}
|
||||
|
||||
/// A button pressed on a controller of this family.
|
||||
pub fn pad_action(&mut self, a: Action, family: Family) {
|
||||
self.set_prompts(Prompts::Pad(family));
|
||||
self.action(a);
|
||||
}
|
||||
|
||||
/// A controller connected (its family) or the last one disconnected.
|
||||
pub fn pad_present(&mut self, family: Option<Family>) {
|
||||
self.set_prompts(family.map_or(Prompts::Keyboard, Prompts::Pad));
|
||||
}
|
||||
|
||||
fn set_prompts(&mut self, prompts: Prompts) {
|
||||
if prompts != self.prompts {
|
||||
self.prompts = prompts;
|
||||
self.render_hints();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn action(&mut self, a: Action) {
|
||||
match a {
|
||||
Action::Quit => {
|
||||
@@ -1225,26 +1256,32 @@ impl App {
|
||||
}
|
||||
|
||||
fn render_hints(&self) {
|
||||
let h = |k: &str, l: &str| Hint {
|
||||
key: k.into(),
|
||||
label: l.into(),
|
||||
};
|
||||
let mut hints = Vec::new();
|
||||
let mut h = |c: Control, l: &str| {
|
||||
let icons = self.hint_icons(c);
|
||||
// Nothing to press for it with this input, such as Steam + X on a keyboard.
|
||||
if !icons.is_empty() {
|
||||
hints.push(Hint {
|
||||
icons: ModelRc::new(VecModel::from(icons)),
|
||||
label: l.into(),
|
||||
});
|
||||
}
|
||||
};
|
||||
match self.screen {
|
||||
Screen::Login => {
|
||||
hints.push(h("A", if self.login_focus < 2 { "Next" } else { "Connect" }));
|
||||
h(Control::Activate, if self.login_focus < 2 { "Next" } else { "Connect" });
|
||||
}
|
||||
Screen::Browse => match self.area {
|
||||
Area::Sidebar => {
|
||||
hints.push(h("A", "Open"));
|
||||
hints.push(h("X", "Play / Pause"));
|
||||
hints.push(h("VIEW", "Search"));
|
||||
hints.push(h("MENU", "Now playing"));
|
||||
h(Control::Activate, "Open");
|
||||
h(Control::PlayPause, "Play / Pause");
|
||||
h(Control::Search, "Search");
|
||||
h(Control::NowPlaying, "Now playing");
|
||||
}
|
||||
Area::Search => {
|
||||
hints.push(h("A", "Search"));
|
||||
hints.push(h("B", "Back"));
|
||||
hints.push(h("STEAM + X", "Keyboard"));
|
||||
h(Control::Activate, "Search");
|
||||
h(Control::Back, "Back");
|
||||
h(Control::OnScreenKeyboard, "Keyboard");
|
||||
}
|
||||
_ => {
|
||||
let target = self
|
||||
@@ -1252,34 +1289,53 @@ impl App {
|
||||
.and_then(|p| p.entries.get(p.sel))
|
||||
.map(|e| &e.target);
|
||||
match target {
|
||||
Some(Target::Song(_)) => hints.push(h("A", "Play")),
|
||||
Some(Target::Setting(_)) => hints.push(h("A", "Change")),
|
||||
Some(_) => hints.push(h("A", "Open")),
|
||||
Some(Target::Song(_)) => h(Control::Activate, "Play"),
|
||||
Some(Target::Setting(_)) => h(Control::Activate, "Change"),
|
||||
Some(_) => h(Control::Activate, "Open"),
|
||||
None if self.current_page().is_some_and(|p| p.error.is_some()) => {
|
||||
hints.push(h("A", "Try again"))
|
||||
h(Control::Activate, "Try again")
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
hints.push(h("B", "Back"));
|
||||
h(Control::Back, "Back");
|
||||
if matches!(target, Some(Target::Song(_) | Target::Album(_) | Target::Playlist(_))) {
|
||||
hints.push(h("Y", "Add to queue"));
|
||||
h(Control::Queue, "Add to queue");
|
||||
}
|
||||
hints.push(h("X", "Play / Pause"));
|
||||
hints.push(h("LT RT", "Sections"));
|
||||
hints.push(h("MENU", "Now playing"));
|
||||
h(Control::PlayPause, "Play / Pause");
|
||||
h(Control::Sections, "Sections");
|
||||
h(Control::NowPlaying, "Now playing");
|
||||
}
|
||||
},
|
||||
Screen::NowPlaying => {
|
||||
hints.push(h("A", "Play selected"));
|
||||
hints.push(h("B", "Back"));
|
||||
hints.push(h("X", "Play / Pause"));
|
||||
hints.push(h("LB RB", "Skip"));
|
||||
hints.push(h("◀ ▶", "Seek"));
|
||||
h(Control::Activate, "Play selected");
|
||||
h(Control::Back, "Back");
|
||||
h(Control::PlayPause, "Play / Pause");
|
||||
h(Control::Skip, "Skip");
|
||||
h(Control::Seek, "Seek");
|
||||
}
|
||||
}
|
||||
self.ui.set_hints(ModelRc::new(VecModel::from(hints)));
|
||||
}
|
||||
|
||||
fn hint_icons(&self, control: Control) -> Vec<Image> {
|
||||
self.icons
|
||||
.borrow_mut()
|
||||
.entry((self.prompts, control))
|
||||
.or_insert_with(|| {
|
||||
prompts::icons(self.prompts, control)
|
||||
.iter()
|
||||
.filter_map(|svg| match Image::load_from_svg_data(svg) {
|
||||
Ok(img) => Some(img),
|
||||
Err(e) => {
|
||||
log::warn!("hint icon for {control:?}: {e:?}");
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.clone()
|
||||
}
|
||||
|
||||
fn toast(&mut self, text: String) {
|
||||
self.ui.set_toast(text.into());
|
||||
self.toast_timer
|
||||
|
||||
Reference in New Issue
Block a user