mirror of
https://github.com/narrrl/proton-stream.git
synced 2026-11-04 16:57:00 +01:00
feat(core,player,app): full subtitle styling, applied live, with a preview
The subtitle style was three fixed choices. It is now size, position, font, bold and italic, free colours with opacity for the text, outline, box and shadow, outline thickness, shadow distance and blur, and an opt-in "Restyle styled subtitles" that repaints ASS dialogue too (sub-ass-override=force). The defaults are mpv's own, so a viewer who never opens the section sees no change. Settings has a section of its own with a sample line drawn on a bright and a dark picture. Every change also reaches the playing file at once, and is written when the mouse is let go rather than once per frame of a drag. The player's subtitle menu has size, position and a per-file delay; Z and X shift subtitles by 0.1 s, as in mpv. Every option is sent every time, so a change back to a default lands on a live player. A refused sub-border-style still skips the border options that depend on it, so an older libmpv loses the box rather than drawing a smear.
This commit is contained in:
1 parent
238ecd8220
commit
69d961d7b8
11 files changed
+940
-175
No files matched your search
@@ -235,8 +235,14 @@ pub enum Action {
|
||||
subtitles: bool,
|
||||
subtitle: Option<String>,
|
||||
},
|
||||
/// How plain subtitles look, from the next file.
|
||||
/// How subtitles look, for the file playing and every one after. Written
|
||||
/// to the preferences once the mouse is let go; see
|
||||
/// [`App::save_subtitle_style`].
|
||||
SetSubtitleStyle(pstr_core::prefs::SubtitleStyle),
|
||||
/// Shift the playing file's subtitles by this many seconds more.
|
||||
NudgeSubtitleDelay(f64),
|
||||
/// Put the playing file's subtitles back on the file's own timing.
|
||||
ResetSubtitleDelay,
|
||||
/// Play faster or slower, and keep doing so for the next file.
|
||||
SetSpeed(f64),
|
||||
/// Seek past openings and credits without being asked.
|
||||
@@ -656,6 +662,8 @@ pub struct App {
|
||||
/// Chapters already skipped on their own in this player, so seeking back
|
||||
/// into an opening on purpose is not undone a frame later.
|
||||
auto_skipped: (u64, Vec<i64>),
|
||||
/// The subtitle style changed and has not been written yet.
|
||||
subtitle_style_unsaved: bool,
|
||||
/// Whether the episode list is out on the player page.
|
||||
pub episode_drawer: bool,
|
||||
/// Set while the window is fullscreen, so `F` can toggle rather than only
|
||||
@@ -762,6 +770,7 @@ impl App {
|
||||
up_next: UpNext::default(),
|
||||
warmed: 0,
|
||||
auto_skipped: (0, Vec::new()),
|
||||
subtitle_style_unsaved: false,
|
||||
fullscreen: false,
|
||||
episode_drawer: false,
|
||||
opening: None,
|
||||
@@ -1439,6 +1448,21 @@ impl App {
|
||||
self.apply(ctx, Action::Play(target));
|
||||
}
|
||||
|
||||
/// Write the subtitle style, once whatever is changing it has let go.
|
||||
///
|
||||
/// A slider or a colour wheel changes the style every frame it is dragged,
|
||||
/// and each change is shown on the playing file straight away. Writing
|
||||
/// each one would be a file write per frame, so it waits for the mouse —
|
||||
/// which also covers a menu closed mid-drag, where the control that would
|
||||
/// have said "released" is no longer drawn.
|
||||
fn save_subtitle_style(&mut self, ctx: &egui::Context) {
|
||||
if self.subtitle_style_unsaved && !ctx.input(|input| input.pointer.any_down()) {
|
||||
self.subtitle_style_unsaved = false;
|
||||
self.engine
|
||||
.set_playback_prefs(self.engine.playback_prefs(), true);
|
||||
}
|
||||
}
|
||||
|
||||
fn send_player(&self, command: crate::playback::Command) {
|
||||
if let Some(playback) = &self.playback {
|
||||
playback.send(command);
|
||||
@@ -1943,7 +1967,28 @@ impl App {
|
||||
Action::SetSubtitleStyle(style) => {
|
||||
let mut prefs = self.engine.playback_prefs();
|
||||
prefs.subtitle_style = style;
|
||||
self.engine.set_playback_prefs(prefs, true);
|
||||
self.engine.set_playback_prefs(prefs, false);
|
||||
self.subtitle_style_unsaved = true;
|
||||
let style = self.engine.playback_prefs().subtitle_style;
|
||||
if let Some(playback) = &mut self.playback {
|
||||
playback.restyle_subtitles(&style);
|
||||
}
|
||||
}
|
||||
Action::NudgeSubtitleDelay(delta) => {
|
||||
if let Some(playback) = &mut self.playback {
|
||||
playback.set_subtitle_delay(playback.subtitle_delay + delta);
|
||||
let text = format!(
|
||||
"Subtitle delay {}",
|
||||
ui::format_delay(playback.subtitle_delay)
|
||||
);
|
||||
self.flash(ctx, text);
|
||||
}
|
||||
}
|
||||
Action::ResetSubtitleDelay => {
|
||||
if let Some(playback) = &mut self.playback {
|
||||
playback.set_subtitle_delay(0.0);
|
||||
self.flash(ctx, "Subtitle delay 0 s");
|
||||
}
|
||||
}
|
||||
Action::SetAppearance(appearance) => {
|
||||
self.engine.set_appearance(appearance);
|
||||
@@ -2136,6 +2181,7 @@ impl eframe::App for App {
|
||||
}
|
||||
self.warm_next();
|
||||
self.auto_skip(ctx);
|
||||
self.save_subtitle_style(ctx);
|
||||
self.window.observe(ctx);
|
||||
|
||||
let mut actions: Vec<Action> = Vec::new();
|
||||
@@ -2916,6 +2962,8 @@ fn shortcuts(
|
||||
Key::OpenBracket,
|
||||
Key::CloseBracket,
|
||||
Key::E,
|
||||
Key::Z,
|
||||
Key::X,
|
||||
Key::Escape,
|
||||
]
|
||||
.into_iter()
|
||||
@@ -2959,6 +3007,9 @@ fn shortcuts(
|
||||
// Escape leaves fullscreen first and the page second, which is what
|
||||
// it does everywhere else and what stops one press from both
|
||||
// un-maximising the window and hiding the film.
|
||||
// mpv's own keys for it, and the step mpv uses.
|
||||
Key::Z => actions.push(Action::NudgeSubtitleDelay(-0.1)),
|
||||
Key::X => actions.push(Action::NudgeSubtitleDelay(0.1)),
|
||||
Key::E => actions.push(Action::ToggleEpisodes),
|
||||
// The episode list is the innermost thing open, so it goes first.
|
||||
Key::Escape if drawer => actions.push(Action::ToggleEpisodes),
|
||||
|
||||
@@ -147,6 +147,10 @@ pub enum Command {
|
||||
/// Move subtitles, as a percentage of the window's height. See
|
||||
/// [`Playback::lift_subtitles`].
|
||||
SetSubtitlePosition(f64),
|
||||
/// Restyle subtitles, the line already showing included.
|
||||
SetSubtitleStyle(pstr_core::prefs::SubtitleStyle),
|
||||
/// Shift subtitles this many seconds later; negative is earlier.
|
||||
SetSubtitleDelay(f64),
|
||||
/// 1.0 is normal speed.
|
||||
SetSpeed(f64),
|
||||
/// Save the current frame to this file.
|
||||
@@ -184,6 +188,14 @@ pub struct Playback {
|
||||
/// Where subtitles were last put, so they are only moved when that changes
|
||||
/// rather than once a frame.
|
||||
subtitle_position: f64,
|
||||
/// How the viewer has subtitles drawn, as last handed to this player.
|
||||
pub subtitle_style: pstr_core::prefs::SubtitleStyle,
|
||||
/// Where the controls would lift subtitles to. The higher of this and the
|
||||
/// style's own position wins.
|
||||
lifted_position: f64,
|
||||
/// How far subtitles are shifted, in seconds. Per file rather than a
|
||||
/// preference: an offset belongs to one release, not to the viewer.
|
||||
pub subtitle_delay: f64,
|
||||
/// What the file contains. Empty until mpv has demuxed it.
|
||||
pub tracks: Vec<Track>,
|
||||
/// The file's chapters, in order. Empty for a file muxed without them.
|
||||
@@ -259,7 +271,10 @@ impl Playback {
|
||||
paused: false,
|
||||
volume: prefs.volume,
|
||||
muted: prefs.muted,
|
||||
subtitle_position: 100.0,
|
||||
subtitle_position: prefs.subtitle_style.position,
|
||||
subtitle_style: prefs.subtitle_style.clone(),
|
||||
lifted_position: 100.0,
|
||||
subtitle_delay: 0.0,
|
||||
speed: prefs.speed,
|
||||
tracks: Vec::new(),
|
||||
chapters: Vec::new(),
|
||||
@@ -282,17 +297,39 @@ impl Playback {
|
||||
/// the mouse is moving, which is exactly when someone is looking at the
|
||||
/// screen.
|
||||
pub fn lift_subtitles(&mut self, covered: f32, height: f32) {
|
||||
let position = if covered > 0.0 && height > 0.0 {
|
||||
self.lifted_position = if covered > 0.0 && height > 0.0 {
|
||||
(100.0 * (1.0 - f64::from(covered / height))).round()
|
||||
} else {
|
||||
100.0
|
||||
};
|
||||
let position = self.subtitle_position();
|
||||
if position != self.subtitle_position {
|
||||
self.subtitle_position = position;
|
||||
self.send(Command::SetSubtitlePosition(position));
|
||||
}
|
||||
}
|
||||
|
||||
/// Where subtitles go: where the style puts them, unless the controls
|
||||
/// would cover that, in which case just above the controls.
|
||||
fn subtitle_position(&self) -> f64 {
|
||||
self.subtitle_style.position.min(self.lifted_position)
|
||||
}
|
||||
|
||||
/// Restyle the subtitles of this file, now.
|
||||
pub fn restyle_subtitles(&mut self, style: &pstr_core::prefs::SubtitleStyle) {
|
||||
self.subtitle_style = style.clone();
|
||||
self.subtitle_position = self.subtitle_position();
|
||||
let mut style = style.clone();
|
||||
style.position = self.subtitle_position;
|
||||
self.send(Command::SetSubtitleStyle(style));
|
||||
}
|
||||
|
||||
/// Shift subtitles to `seconds`, rounded to the tenth a keypress moves.
|
||||
pub fn set_subtitle_delay(&mut self, seconds: f64) {
|
||||
self.subtitle_delay = (seconds * 10.0).round() / 10.0;
|
||||
self.send(Command::SetSubtitleDelay(self.subtitle_delay));
|
||||
}
|
||||
|
||||
/// Whether the picture is drawn in this window rather than mpv's own.
|
||||
pub fn is_embedded(&self) -> bool {
|
||||
self.video.is_some()
|
||||
@@ -494,7 +531,7 @@ fn from_prefs(
|
||||
.and_then(|p| p.subtitle_language.clone())
|
||||
.or_else(|| prefs.subtitle_language.clone()),
|
||||
subtitles: show.map(|p| p.subtitles).unwrap_or(prefs.subtitles),
|
||||
subtitle_style: prefs.subtitle_style,
|
||||
subtitle_style: prefs.subtitle_style.clone(),
|
||||
// Carried from file to file, like the volume: somebody who watches at
|
||||
// 1.25 watches the next episode at 1.25 too.
|
||||
options: vec![("speed".into(), format!("{:.2}", prefs.speed))],
|
||||
@@ -659,6 +696,11 @@ fn apply_command(
|
||||
Command::SetVolume(volume) => player.set_volume(volume),
|
||||
Command::SetMuted(muted) => player.set_muted(muted),
|
||||
Command::SetSubtitlePosition(percent) => player.set_subtitle_position(percent),
|
||||
Command::SetSubtitleStyle(style) => {
|
||||
player.apply_subtitle_style(&style);
|
||||
Ok(())
|
||||
}
|
||||
Command::SetSubtitleDelay(seconds) => player.set_subtitle_delay(seconds),
|
||||
Command::SetSpeed(speed) => player.set_speed(speed),
|
||||
Command::Screenshot(path) => player.screenshot(&path),
|
||||
Command::SelectTrack(kind, track) => select_track(engine, id, player, kind, track),
|
||||
|
||||
@@ -316,6 +316,68 @@ fn system_cjk_font() -> Option<(std::path::PathBuf, u32)> {
|
||||
None
|
||||
}
|
||||
|
||||
/// The font families installed here, by name, for the subtitle font picker.
|
||||
///
|
||||
/// `None` until they have been read. The first call starts that on a thread
|
||||
/// of its own and asks for a repaint when it is done: `fc-list` over a large
|
||||
/// font collection takes long enough to be seen as a stalled frame.
|
||||
pub fn font_families(ctx: &egui::Context) -> Option<&'static [String]> {
|
||||
static FAMILIES: std::sync::OnceLock<Vec<String>> = std::sync::OnceLock::new();
|
||||
static STARTED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
||||
if let Some(families) = FAMILIES.get() {
|
||||
return Some(families);
|
||||
}
|
||||
if !STARTED.swap(true, std::sync::atomic::Ordering::Relaxed) {
|
||||
let ctx = ctx.clone();
|
||||
let spawned = std::thread::Builder::new()
|
||||
.name("font-families".into())
|
||||
.spawn(move || {
|
||||
let _ = FAMILIES.set(system_font_families());
|
||||
ctx.request_repaint();
|
||||
});
|
||||
if let Err(error) = spawned {
|
||||
tracing::debug!("font families thread: {error}");
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn system_font_families() -> Vec<String> {
|
||||
let Ok(output) = std::process::Command::new("fc-list")
|
||||
.args([":", "family"])
|
||||
.output()
|
||||
else {
|
||||
return Vec::new();
|
||||
};
|
||||
font_families_from_fc_list(&String::from_utf8_lossy(&output.stdout))
|
||||
}
|
||||
|
||||
/// Elsewhere the picker takes a typed name only. mpv's font lookup there is
|
||||
/// the system's own, so any installed family's name works.
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
fn system_font_families() -> Vec<String> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
/// One family per line of `fc-list : family`, sorted and without repeats.
|
||||
///
|
||||
/// A line lists a family's names in every language it has, comma-separated,
|
||||
/// and one family appears once per style. The first name is the one libass
|
||||
/// matches on.
|
||||
#[cfg(any(target_os = "linux", test))]
|
||||
fn font_families_from_fc_list(listing: &str) -> Vec<String> {
|
||||
let mut families: Vec<String> = listing
|
||||
.lines()
|
||||
.filter_map(|line| line.split(',').next())
|
||||
.map(|family| family.trim().to_owned())
|
||||
.filter(|family| !family.is_empty() && !family.starts_with('.'))
|
||||
.collect();
|
||||
families.sort_by_key(|family| family.to_lowercase());
|
||||
families.dedup();
|
||||
families
|
||||
}
|
||||
|
||||
/// Every colour the app draws with, as egui wants them.
|
||||
///
|
||||
/// The *choice* of colours is `pstr_core::appearance` — one resolution shared
|
||||
@@ -881,6 +943,15 @@ const RAMP_STOPS: usize = 64;
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn each_font_family_is_listed_once_by_its_first_name() {
|
||||
let listing = "Noto Sans,Noto Sans Regular\nDejaVu Sans\nnoto sans mono\nNoto Sans,Noto Sans Bold\n\n.LastResort\n";
|
||||
assert_eq!(
|
||||
font_families_from_fc_list(listing),
|
||||
["DejaVu Sans", "Noto Sans", "noto sans mono"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_variable_fonts_named_instance_is_not_taken_for_a_face() {
|
||||
assert_eq!(fontconfig_face(262_144), 0);
|
||||
|
||||
@@ -206,6 +206,18 @@ pub fn format_speed(speed: f64) -> String {
|
||||
format!("{}×", text.trim_end_matches('0').trim_end_matches('.'))
|
||||
}
|
||||
|
||||
/// `+0.3 s`, `−1.2 s` or `0 s`: a subtitle delay, signed so the direction
|
||||
/// reads without knowing which way mpv counts.
|
||||
pub fn format_delay(seconds: f64) -> String {
|
||||
if seconds.abs() < 0.05 {
|
||||
"0 s".to_owned()
|
||||
} else if seconds > 0.0 {
|
||||
format!("+{seconds:.1} s")
|
||||
} else {
|
||||
format!("−{:.1} s", -seconds)
|
||||
}
|
||||
}
|
||||
|
||||
/// `1.4 GiB`, for what a file costs to watch.
|
||||
pub fn format_size(bytes: i64) -> String {
|
||||
const UNITS: [&str; 4] = ["B", "KiB", "MiB", "GiB"];
|
||||
@@ -917,6 +929,13 @@ pub fn columns_of(available: f32, minimum: f32) -> Grid {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn a_subtitle_delay_reads_with_its_direction() {
|
||||
assert_eq!(super::format_delay(0.3), "+0.3 s");
|
||||
assert_eq!(super::format_delay(-1.2), "−1.2 s");
|
||||
assert_eq!(super::format_delay(0.0000001), "0 s");
|
||||
}
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -7,9 +7,7 @@
|
||||
|
||||
use pstr_core::appearance::{Accent, Appearance, Flavor, Tiles};
|
||||
use pstr_core::metadata::{MetadataConfig, ProviderId, TitleNames};
|
||||
use pstr_core::prefs::{
|
||||
PlaybackPrefs, SubtitleBackdrop, SubtitleColour, SubtitleSize, SubtitleStyle,
|
||||
};
|
||||
use pstr_core::prefs::{Colour, PlaybackPrefs, SubtitleBackdrop, SubtitleStyle};
|
||||
use pstr_player::LANGUAGES;
|
||||
|
||||
use crate::app::Action;
|
||||
@@ -60,6 +58,7 @@ pub fn show(
|
||||
ui.add_space(theme::space::M);
|
||||
appearance_form(ui, prefs.appearance, prefs.desktop, actions);
|
||||
playback_form(ui, prefs, actions);
|
||||
subtitle_form(ui, &prefs.playback.subtitle_style, actions);
|
||||
storage_form(ui, prefs, actions);
|
||||
metadata_form(ui, settings, api_key, actions);
|
||||
about(ui);
|
||||
@@ -317,7 +316,6 @@ fn playback_form(ui: &mut egui::Ui, prefs: Prefs<'_>, actions: &mut Vec<Action>)
|
||||
),
|
||||
|ui| {
|
||||
languages(ui, prefs.playback, actions);
|
||||
subtitle_style(ui, prefs.playback.subtitle_style, actions);
|
||||
let mut autoplay = prefs.autoplay;
|
||||
ui::widgets::settings_row(
|
||||
ui,
|
||||
@@ -431,52 +429,343 @@ fn languages(ui: &mut egui::Ui, playback: &PlaybackPrefs, actions: &mut Vec<Acti
|
||||
);
|
||||
}
|
||||
|
||||
/// How plain subtitles are drawn. A styled track keeps its own look apart
|
||||
/// from the size; see [`pstr_core::prefs::SubtitleStyle`].
|
||||
fn subtitle_style(ui: &mut egui::Ui, style: SubtitleStyle, actions: &mut Vec<Action>) {
|
||||
let mut chosen = style;
|
||||
ui::widgets::settings_row(
|
||||
/// How subtitles look, with a sample line to judge it by.
|
||||
///
|
||||
/// Every change reaches a playing file as it is made, so the film itself is
|
||||
/// the other preview. A styled track takes only the size and the position
|
||||
/// unless it is asked to take the rest; see [`SubtitleStyle`].
|
||||
fn subtitle_form(ui: &mut egui::Ui, style: &SubtitleStyle, actions: &mut Vec<Action>) {
|
||||
ui::widgets::settings_group(
|
||||
ui,
|
||||
"Subtitle size",
|
||||
Some("Styled subtitles are scaled too. From the next file."),
|
||||
"Subtitles",
|
||||
Some(
|
||||
"Changes show on the playing video straight away. Plain subtitles (SRT) take all of this; styled ones (ASS) only the size and the position, unless you restyle them below.",
|
||||
),
|
||||
|ui| {
|
||||
egui::ComboBox::from_id_salt("subtitle-size")
|
||||
.selected_text(chosen.size.label())
|
||||
.show_ui(ui, |ui| {
|
||||
for size in SubtitleSize::ALL {
|
||||
ui.selectable_value(&mut chosen.size, size, size.label());
|
||||
let mut chosen = style.clone();
|
||||
ui.add_space(theme::space::L);
|
||||
subtitle_preview(ui, &chosen);
|
||||
ui.add_space(theme::space::L);
|
||||
|
||||
ui::widgets::settings_row(ui, "Size", None, |ui| {
|
||||
percent_slider(
|
||||
ui,
|
||||
&mut chosen.scale,
|
||||
SubtitleStyle::MIN_SCALE..=SubtitleStyle::MAX_SCALE,
|
||||
);
|
||||
});
|
||||
ui::widgets::settings_row(
|
||||
ui,
|
||||
"Position",
|
||||
Some("How far up the picture the line sits."),
|
||||
|ui| subtitle_position_slider(ui, &mut chosen.position),
|
||||
);
|
||||
ui::widgets::settings_row(
|
||||
ui,
|
||||
"Font",
|
||||
Some("Any font installed here. The sample above is drawn in the app's own font."),
|
||||
|ui| {
|
||||
ui::widgets::toggle(ui, &mut chosen.italic, "Italic");
|
||||
ui::widgets::toggle(ui, &mut chosen.bold, "Bold");
|
||||
font_picker(ui, &mut chosen.font);
|
||||
},
|
||||
);
|
||||
ui::widgets::settings_row(ui, "Text colour", None, |ui| {
|
||||
colour_button(ui, &mut chosen.colour);
|
||||
});
|
||||
ui::widgets::settings_row(
|
||||
ui,
|
||||
"Background",
|
||||
Some("A box reads on bright scenes; an outline hides less of the picture."),
|
||||
|ui| {
|
||||
let choices =
|
||||
SubtitleBackdrop::ALL.map(|backdrop| (backdrop, backdrop.label()));
|
||||
if let Some(backdrop) = ui::widgets::segmented(ui, chosen.backdrop, &choices) {
|
||||
chosen.backdrop = backdrop;
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
ui::widgets::settings_row(
|
||||
ui,
|
||||
"Subtitle background",
|
||||
Some("A box reads on bright scenes. Plain subtitles only; styled ones keep their own."),
|
||||
|ui| {
|
||||
egui::ComboBox::from_id_salt("subtitle-backdrop")
|
||||
.selected_text(chosen.backdrop.label())
|
||||
.show_ui(ui, |ui| {
|
||||
for backdrop in SubtitleBackdrop::ALL {
|
||||
ui.selectable_value(&mut chosen.backdrop, backdrop, backdrop.label());
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
ui::widgets::settings_row(ui, "Subtitle colour", Some("Plain subtitles only."), |ui| {
|
||||
egui::ComboBox::from_id_salt("subtitle-colour")
|
||||
.selected_text(chosen.colour.label())
|
||||
.show_ui(ui, |ui| {
|
||||
for colour in SubtitleColour::ALL {
|
||||
ui.selectable_value(&mut chosen.colour, colour, colour.label());
|
||||
},
|
||||
);
|
||||
match chosen.backdrop {
|
||||
SubtitleBackdrop::Outline => {
|
||||
ui::widgets::settings_row(ui, "Outline", None, |ui| {
|
||||
colour_button(ui, &mut chosen.outline_colour);
|
||||
ui.add(
|
||||
egui::Slider::new(
|
||||
&mut chosen.outline_size,
|
||||
0.0..=SubtitleStyle::MAX_OUTLINE,
|
||||
)
|
||||
.step_by(0.05)
|
||||
.fixed_decimals(1),
|
||||
);
|
||||
});
|
||||
}
|
||||
SubtitleBackdrop::Box => {
|
||||
ui::widgets::settings_row(
|
||||
ui,
|
||||
"Box colour",
|
||||
Some("Its opacity is how much of the picture shows through."),
|
||||
|ui| colour_button(ui, &mut chosen.box_colour),
|
||||
);
|
||||
}
|
||||
}
|
||||
ui::widgets::settings_row(ui, "Shadow", Some("How far it falls. 0 is none."), |ui| {
|
||||
colour_button(ui, &mut chosen.shadow_colour);
|
||||
ui.add(
|
||||
egui::Slider::new(&mut chosen.shadow_offset, 0.0..=SubtitleStyle::MAX_SHADOW)
|
||||
.step_by(0.1)
|
||||
.fixed_decimals(1),
|
||||
);
|
||||
});
|
||||
ui::widgets::settings_row(
|
||||
ui,
|
||||
"Blur",
|
||||
Some("Softens the edges. Not shown in the sample."),
|
||||
|ui| {
|
||||
ui.add(
|
||||
egui::Slider::new(&mut chosen.blur, 0.0..=SubtitleStyle::MAX_BLUR)
|
||||
.step_by(0.1)
|
||||
.fixed_decimals(1),
|
||||
);
|
||||
},
|
||||
);
|
||||
ui::widgets::settings_row(
|
||||
ui,
|
||||
"Restyle styled subtitles",
|
||||
Some(
|
||||
"Repaint styled (ASS) dialogue in this style too. Signs, songs and karaoke can come out wrong.",
|
||||
),
|
||||
|ui| {
|
||||
ui::widgets::toggle(ui, &mut chosen.restyle_styled, "");
|
||||
},
|
||||
);
|
||||
ui::widgets::settings_row(ui, "Reset", Some("Back to mpv's own look."), |ui| {
|
||||
if ui
|
||||
.add_enabled(
|
||||
*style != SubtitleStyle::default(),
|
||||
egui::Button::new("Reset"),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
chosen = SubtitleStyle::default();
|
||||
}
|
||||
});
|
||||
});
|
||||
if chosen != style {
|
||||
actions.push(Action::SetSubtitleStyle(chosen));
|
||||
|
||||
if chosen != *style {
|
||||
actions.push(Action::SetSubtitleStyle(chosen.sanitized()));
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// A scale as a percentage slider, 100% being mpv's own size.
|
||||
pub(crate) fn percent_slider(
|
||||
ui: &mut egui::Ui,
|
||||
scale: &mut f64,
|
||||
range: std::ops::RangeInclusive<f64>,
|
||||
) -> egui::Response {
|
||||
let mut percent = *scale * 100.0;
|
||||
let response = ui.add(
|
||||
egui::Slider::new(&mut percent, range.start() * 100.0..=range.end() * 100.0)
|
||||
.step_by(5.0)
|
||||
.suffix("%")
|
||||
.fixed_decimals(0),
|
||||
);
|
||||
*scale = percent / 100.0;
|
||||
response
|
||||
}
|
||||
|
||||
/// Where the line sits, 0 at the top of the picture and 100 at the bottom —
|
||||
/// shown the other way up, as height above the bottom edge, which is how
|
||||
/// anyone would describe moving subtitles up.
|
||||
pub(crate) fn subtitle_position_slider(ui: &mut egui::Ui, position: &mut f64) -> egui::Response {
|
||||
let mut height = 100.0 - *position;
|
||||
let response = ui.add(
|
||||
egui::Slider::new(&mut height, 0.0..=90.0)
|
||||
.step_by(1.0)
|
||||
.suffix("% up")
|
||||
.fixed_decimals(0),
|
||||
);
|
||||
*position = 100.0 - height;
|
||||
response
|
||||
}
|
||||
|
||||
/// A swatch that opens a colour wheel with an opacity slider.
|
||||
fn colour_button(ui: &mut egui::Ui, colour: &mut Colour) {
|
||||
let [r, g, b, a] = colour.to_array();
|
||||
let mut picked = egui::Color32::from_rgba_unmultiplied(r, g, b, a);
|
||||
// The `Color32` form rather than `Hsva`, because egui remembers the hue
|
||||
// behind it: rebuilt from RGB each frame, black and grey have none, and
|
||||
// the wheel would snap back to red under the mouse.
|
||||
let response = egui::color_picker::color_edit_button_srgba(
|
||||
ui,
|
||||
&mut picked,
|
||||
egui::color_picker::Alpha::OnlyBlend,
|
||||
);
|
||||
// Only on a change: a round trip through premultiplied alpha can nudge
|
||||
// an untouched colour by a step.
|
||||
if response.changed() {
|
||||
*colour = Colour::from(picked.to_srgba_unmultiplied());
|
||||
}
|
||||
}
|
||||
|
||||
/// The font: the installed families to pick from, and a box to type any name
|
||||
/// into — the only way on a system whose families the app cannot list.
|
||||
fn font_picker(ui: &mut egui::Ui, font: &mut Option<String>) {
|
||||
let filter_id = egui::Id::new("subtitle-font-filter");
|
||||
let mut filter: String = ui.data(|data| data.get_temp(filter_id)).unwrap_or_default();
|
||||
let families = theme::font_families(ui.ctx());
|
||||
egui::ComboBox::from_id_salt("subtitle-font")
|
||||
.selected_text(font.as_deref().unwrap_or("Default"))
|
||||
.width(200.0)
|
||||
.height(320.0)
|
||||
// Not on any click: the search box is clicked into.
|
||||
.close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside)
|
||||
.show_ui(ui, |ui| {
|
||||
ui.add(
|
||||
egui::TextEdit::singleline(&mut filter)
|
||||
.hint_text("Search or type a font name")
|
||||
.desired_width(f32::INFINITY),
|
||||
)
|
||||
.request_focus();
|
||||
let typed = filter.trim();
|
||||
if ui.selectable_label(font.is_none(), "Default").clicked() {
|
||||
*font = None;
|
||||
ui.close();
|
||||
}
|
||||
let wanted = typed.to_lowercase();
|
||||
let mut exact = false;
|
||||
for family in families.unwrap_or_default() {
|
||||
if !family.to_lowercase().contains(&wanted) {
|
||||
continue;
|
||||
}
|
||||
exact |= family.eq_ignore_ascii_case(typed);
|
||||
let label = egui::RichText::new(family.as_str());
|
||||
if ui
|
||||
.selectable_label(font.as_deref() == Some(family.as_str()), label)
|
||||
.clicked()
|
||||
{
|
||||
*font = Some(family.clone());
|
||||
ui.close();
|
||||
}
|
||||
}
|
||||
if families.is_none() {
|
||||
ui.label(ui::muted("Reading the installed fonts…"));
|
||||
}
|
||||
if !typed.is_empty() && !exact && ui.button(format!("Use “{typed}”")).clicked() {
|
||||
*font = Some(typed.to_owned());
|
||||
ui.close();
|
||||
}
|
||||
});
|
||||
ui.data_mut(|data| data.insert_temp(filter_id, filter));
|
||||
}
|
||||
|
||||
/// A sample line, half on a bright sky and half on night, drawn in the
|
||||
/// chosen style — so whether it reads on both is visible before a film is.
|
||||
///
|
||||
/// An approximation: the app's own font rather than the chosen one, egui's
|
||||
/// slant for italic, and no blur. Proportions follow mpv's, where sizes are
|
||||
/// in pixels of a 38-pixel font.
|
||||
fn subtitle_preview(ui: &mut egui::Ui, style: &SubtitleStyle) {
|
||||
const HEIGHT: f32 = 132.0;
|
||||
/// mpv's `sub-font-size`, the unit every other size is relative to.
|
||||
const MPV_FONT: f32 = 38.0;
|
||||
let width = ui.available_width();
|
||||
let (rect, _) = ui.allocate_exact_size(egui::vec2(width, HEIGHT), egui::Sense::hover());
|
||||
let painter = ui.painter_at(rect);
|
||||
let radius = egui::CornerRadius::same(theme::radius::MD);
|
||||
|
||||
// Sky above haze on the left, a night street on the right.
|
||||
let half = egui::Rect::from_min_max(rect.min, egui::pos2(rect.center().x, rect.max.y));
|
||||
let mut sky = egui::Mesh::default();
|
||||
sky.colored_vertex(half.left_top(), egui::Color32::from_rgb(0x8E, 0xC5, 0xF0));
|
||||
sky.colored_vertex(half.right_top(), egui::Color32::from_rgb(0xB8, 0xDC, 0xF8));
|
||||
sky.colored_vertex(
|
||||
half.right_bottom(),
|
||||
egui::Color32::from_rgb(0xFA, 0xF7, 0xEE),
|
||||
);
|
||||
sky.colored_vertex(
|
||||
half.left_bottom(),
|
||||
egui::Color32::from_rgb(0xF2, 0xF0, 0xE8),
|
||||
);
|
||||
sky.add_triangle(0, 1, 2);
|
||||
sky.add_triangle(0, 2, 3);
|
||||
painter.rect_filled(rect, radius, egui::Color32::from_rgb(0x14, 0x16, 0x22));
|
||||
painter.add(sky);
|
||||
|
||||
let font_px = 22.0 * style.scale as f32;
|
||||
let unit = font_px / MPV_FONT;
|
||||
let family = if style.bold {
|
||||
theme::semibold()
|
||||
} else {
|
||||
egui::FontFamily::Proportional
|
||||
};
|
||||
let job = egui::text::LayoutJob::single_section(
|
||||
"Can you read this on both sides?".to_owned(),
|
||||
egui::TextFormat {
|
||||
font_id: egui::FontId::new(font_px, family),
|
||||
color: egui::Color32::PLACEHOLDER,
|
||||
italics: style.italic,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
let galley = painter.layout_job(job);
|
||||
let size = galley.size();
|
||||
// Bottom of the line at `position` percent down, as mpv places it, kept
|
||||
// inside the strip.
|
||||
let margin = 8.0;
|
||||
let bottom = rect.top() + HEIGHT * style.position as f32 / 100.0 - margin;
|
||||
let top = bottom
|
||||
.min(rect.bottom() - margin)
|
||||
.max(rect.top() + margin + size.y)
|
||||
- size.y;
|
||||
let at = egui::pos2(rect.center().x - size.x / 2.0, top);
|
||||
let colour = |colour: Colour| {
|
||||
let [r, g, b, a] = colour.to_array();
|
||||
egui::Color32::from_rgba_unmultiplied(r, g, b, a)
|
||||
};
|
||||
|
||||
let shadow = style.shadow_offset as f32 * unit;
|
||||
match style.backdrop {
|
||||
SubtitleBackdrop::Box => {
|
||||
let padding = 6.0 * unit;
|
||||
let line = egui::Rect::from_min_size(at, size).expand(padding);
|
||||
if shadow > 0.0 {
|
||||
painter.rect_filled(
|
||||
line.translate(egui::vec2(shadow, shadow)),
|
||||
0.0,
|
||||
colour(style.shadow_colour),
|
||||
);
|
||||
}
|
||||
painter.rect_filled(line, 0.0, colour(style.box_colour));
|
||||
}
|
||||
SubtitleBackdrop::Outline => {
|
||||
let outline = style.outline_size as f32 * unit;
|
||||
let rings = |offset: egui::Vec2, ink: egui::Color32| {
|
||||
if outline <= 0.0 {
|
||||
painter.galley_with_override_text_color(at + offset, galley.clone(), ink);
|
||||
return;
|
||||
}
|
||||
for step in 0..8 {
|
||||
let angle = step as f32 * std::f32::consts::TAU / 8.0;
|
||||
let ring = egui::vec2(angle.cos(), angle.sin()) * outline;
|
||||
painter.galley_with_override_text_color(
|
||||
at + offset + ring,
|
||||
galley.clone(),
|
||||
ink,
|
||||
);
|
||||
}
|
||||
};
|
||||
if shadow > 0.0 {
|
||||
rings(egui::vec2(shadow, shadow), colour(style.shadow_colour));
|
||||
}
|
||||
if outline > 0.0 {
|
||||
rings(egui::Vec2::ZERO, colour(style.outline_colour));
|
||||
}
|
||||
}
|
||||
}
|
||||
painter.galley_with_override_text_color(at, galley, colour(style.colour));
|
||||
}
|
||||
|
||||
/// The streaming cache: how big it may get, and a way to empty it.
|
||||
fn storage_form(ui: &mut egui::Ui, prefs: Prefs<'_>, actions: &mut Vec<Action>) {
|
||||
ui::widgets::settings_group(
|
||||
|
||||
@@ -36,6 +36,7 @@ const GROUPS: &[(&str, &[(&str, &str)])] = &[
|
||||
("N / P", "Next and previous episode"),
|
||||
("[ / ]", "Slower and faster"),
|
||||
("S", "Skip the opening or the credits"),
|
||||
("Z / X", "Subtitles 0.1 s earlier and later"),
|
||||
("E", "The list of episodes"),
|
||||
("F or double-click", "Fullscreen"),
|
||||
("Esc", "Leave fullscreen, then the player"),
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
//! and neither mutates: this module draws.
|
||||
|
||||
use egui::{Color32, CornerRadius, Rect, Sense, Stroke, Vec2, pos2};
|
||||
use pstr_core::prefs::SubtitleStyle;
|
||||
use pstr_player::{MAX_VOLUME, Track, TrackKind};
|
||||
|
||||
use crate::app::{Action, Adjacent, Page};
|
||||
@@ -470,6 +471,8 @@ fn track_menus(ui: &mut egui::Ui, playback: &Playback, actions: &mut Vec<Action>
|
||||
}
|
||||
ui.separator();
|
||||
track_options(ui, &subtitles, TrackKind::Subtitle, actions);
|
||||
ui.separator();
|
||||
subtitle_adjustments(ui, playback, actions);
|
||||
})
|
||||
.response
|
||||
.on_hover_text("Subtitle track");
|
||||
@@ -489,6 +492,49 @@ fn track_menus(ui: &mut egui::Ui, playback: &Playback, actions: &mut Vec<Action>
|
||||
}
|
||||
}
|
||||
|
||||
/// What is worth changing mid-episode without a trip to Settings: the size and
|
||||
/// place of the line, and its timing for a release that is out of step.
|
||||
fn subtitle_adjustments(ui: &mut egui::Ui, playback: &Playback, actions: &mut Vec<Action>) {
|
||||
let mut style = playback.subtitle_style.clone();
|
||||
egui::Grid::new("subtitle-adjustments")
|
||||
.num_columns(2)
|
||||
.spacing([theme::space::M, theme::space::S])
|
||||
.show(ui, |ui| {
|
||||
ui.label("Size");
|
||||
ui::settings::percent_slider(
|
||||
ui,
|
||||
&mut style.scale,
|
||||
SubtitleStyle::MIN_SCALE..=SubtitleStyle::MAX_SCALE,
|
||||
);
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Position");
|
||||
ui::settings::subtitle_position_slider(ui, &mut style.position);
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Delay");
|
||||
ui.horizontal(|ui| {
|
||||
if ui.button("−").on_hover_text("Earlier (Z)").clicked() {
|
||||
actions.push(Action::NudgeSubtitleDelay(-0.1));
|
||||
}
|
||||
ui.label(ui::format_delay(playback.subtitle_delay));
|
||||
if ui.button("+").on_hover_text("Later (X)").clicked() {
|
||||
actions.push(Action::NudgeSubtitleDelay(0.1));
|
||||
}
|
||||
if ui
|
||||
.add_enabled(playback.subtitle_delay != 0.0, egui::Button::new("Reset"))
|
||||
.clicked()
|
||||
{
|
||||
actions.push(Action::ResetSubtitleDelay);
|
||||
}
|
||||
});
|
||||
ui.end_row();
|
||||
});
|
||||
if style != playback.subtitle_style {
|
||||
actions.push(Action::SetSubtitleStyle(style.sanitized()));
|
||||
}
|
||||
}
|
||||
|
||||
fn track_options(ui: &mut egui::Ui, tracks: &[&Track], kind: TrackKind, actions: &mut Vec<Action>) {
|
||||
for track in tracks {
|
||||
if ui.selectable_label(track.selected, track.label()).clicked() {
|
||||
|
||||
Reference in new issue
Block a user