app: put automatic recording at the center of the window

The titlebar offered "Start recording" although the recorder starts with a
game; Settings takes that button's place. The two-tab sidebar becomes a
status bar that also names the encoder, and the blue square beside the name
is gone.

Match cards show a champion tile: initials on a colour derived from the
name until there is an image cache. History, review and settings lists get
a scrollbar that follows the viewport in the same frame.

Settings keeps the one switch a player can act on, launch at startup, and
says in plain words that recording starts with a game. The recorder's own
toggles and the client poll interval move to Advanced.
This commit is contained in:
vhaudiquet committed 2026-11-15 23:34:12 +01:00
1 parent 01a92f1edf
commit e7a36c3129
6 files changed
+328 -265

No files matched your search

+98 -125
View File
@@ -3,7 +3,7 @@
use std::sync::mpsc::{Receiver, TryRecvError, channel}; use std::sync::mpsc::{Receiver, TryRecvError, channel};
use std::time::Duration; use std::time::Duration;
use rune::tok::{radius, type_}; use rune::tok::type_;
use rune::{Cx, El, Key, KeyEvent, Tx, col, el, row, text}; use rune::{Cx, El, Key, KeyEvent, Tx, col, el, row, text};
use crate::history::{self, QueueCategory, Timeline}; use crate::history::{self, QueueCategory, Timeline};
@@ -114,6 +114,9 @@ pub struct App {
pub games_loaded: bool, pub games_loaded: bool,
pub filter: Option<QueueCategory>, pub filter: Option<QueueCategory>,
pub review: screens::review::Review, pub review: screens::review::Review,
pub scroll: ui::ScrollBar,
pub highlights_scroll: ui::ScrollBar,
pub events_scroll: ui::ScrollBar,
pub notice: Option<Notice>, pub notice: Option<Notice>,
pub autostart: bool, pub autostart: bool,
pub logs: String, pub logs: String,
@@ -146,6 +149,9 @@ impl App {
games_loaded: false, games_loaded: false,
filter: None, filter: None,
review: screens::review::Review::default(), review: screens::review::Review::default(),
scroll: ui::ScrollBar::new(),
highlights_scroll: ui::ScrollBar::new(),
events_scroll: ui::ScrollBar::new(),
notice: None, notice: None,
autostart, autostart,
logs: String::new(), logs: String::new(),
@@ -248,7 +254,8 @@ impl App {
} }
let (tx, rx) = channel(); let (tx, rx) = channel();
std::thread::spawn(move || { std::thread::spawn(move || {
let _ = tx.send(history::load_all()); let games = history::load_all();
let _ = tx.send(games);
}); });
self.games_rx = Some(rx); self.games_rx = Some(rx);
} }
@@ -509,6 +516,10 @@ pub fn on_key(app: &mut App, event: &KeyEvent, _tx: &mut Tx) -> bool {
app.screen = Screen::History; app.screen = Screen::History;
true true
} }
(Key::Escape, _) if app.screen == Screen::Settings => {
app.screen = Screen::History;
true
}
(Key::Text(t), true) if t.eq_ignore_ascii_case("r") => { (Key::Text(t), true) if t.eq_ignore_ascii_case("r") => {
app.refresh_games(); app.refresh_games();
app.daemon.get_status(); app.daemon.get_status();
@@ -534,86 +545,83 @@ fn status_color(p: Paint, app: &App) -> (rune::Color, String) {
(p.muted(), app.status.label()) (p.muted(), app.status.label())
} }
fn nav(p: Paint, label: &str, key: &str, active: bool, to: Screen) -> El { /// The thin bar under the screens: daemon state on the left, the encoder on the right.
el().key(key) fn statusbar(p: Paint, app: &App) -> El {
.role("tab")
.label(label)
.checked(active)
.focusable(true)
.w_full()
.h(32.0)
.px(10.0)
.center_y()
.radius(radius::MD)
.fill(if active {
p.t.indicator.alpha(0.5)
} else {
rune::Color::TRANSPARENT
})
.hover(p.t.hover)
.press(p.t.press)
.on_press(move |app: &mut App| {
if to != Screen::Review {
screens::review::leave(app);
}
app.screen = to;
})
.child(
text(label)
.key("label")
.font(type_::BODY)
.color(if active { p.t.text } else { p.t.text_secondary })
.a11y_hidden(),
)
}
fn sidebar(p: Paint, app: &App) -> El {
let (dot_color, status) = status_color(p, app); let (dot_color, status) = status_color(p, app);
col() let name = crate::settings::EncoderPreset::encoder_name(&app.encoders.current)
.key("sidebar") .map(str::to_string)
.w(196.0) .or_else(|| {
.h_full() app.settings
.padding(10.0) .as_ref()
.gap(4.0) .map(|settings| settings.video.encoder_preset.label().to_string())
});
let recording = match (name, app.settings.as_ref().map(|s| s.video.frame_rate)) {
(Some(name), Some(fps)) => Some(format!("{name} · {fps} fps")),
(Some(name), None) => Some(name),
_ => None,
};
let mut bar = row()
.key("statusbar")
.h(30.0)
.w_full()
.px(14.0)
.gap(8.0)
.center_y()
.fill(p.t.bg) .fill(p.t.bg)
.border_sides([0.0, p.hair, 0.0, 0.0], p.t.separator) .border_sides([p.hair, 0.0, 0.0, 0.0], p.t.separator)
.child(nav( .child(ui::dot(dot_color))
p,
"Match history",
"nav-history",
app.screen != Screen::Settings,
Screen::History,
))
.child(nav(
p,
"Settings",
"nav-settings",
app.screen == Screen::Settings,
Screen::Settings,
))
.child(el().grow(1.0))
.child( .child(
row() text(status)
.key("connection") .key("status-text")
.gap(7.0) .font(body(11.5))
.center_y() .color(p.muted()),
.padding(6.0)
.child(ui::dot(dot_color))
.child(
text(status)
.key("status-text")
.font(body(11.5))
.color(p.muted()),
),
) )
.child(el().grow(1.0));
if let Some(note) = recording {
bar = bar.child(
text(note)
.key("encoder-note")
.font(body(11.0))
.color(p.muted()),
);
}
bar
} }
fn titlebar(p: Paint, app: &App) -> El { fn titlebar(p: Paint, app: &App) -> El {
let recording = app.status.is_recording; let actions = if app.screen == Screen::Settings {
let label = if recording { row().key("actions").gap(8.0).center_y().child(ui::button(
"Stop recording" p,
"back",
"← Match history",
false,
|app: &mut App| app.screen = Screen::History,
))
} else { } else {
"Start recording" row()
.key("actions")
.gap(8.0)
.center_y()
.child(ui::button(
p,
"refresh",
"Refresh",
false,
|app: &mut App| {
app.refresh_games();
app.daemon.get_status();
},
))
.child(ui::button(
p,
"open-settings",
"Settings",
false,
|app: &mut App| {
screens::review::leave(app);
app.screen = Screen::Settings;
},
))
}; };
row() row()
.key("titlebar") .key("titlebar")
@@ -625,47 +633,12 @@ fn titlebar(p: Paint, app: &App) -> El {
.justify_between() .justify_between()
.window_drag() .window_drag()
.child( .child(
row() text("League Recorder")
.key("brand") .key("title")
.gap(8.0) .font(heading(13.0))
.center_y() .color(p.t.text),
.child(el().key("mark").size(18.0, 18.0).radius(5.0).fill(p.accent))
.child(
text("League Recorder")
.key("title")
.font(heading(13.0))
.color(p.t.text),
),
)
.child(
row()
.key("actions")
.gap(8.0)
.center_y()
.child(ui::button(
p,
"record-toggle",
label,
!recording,
|app: &mut App| {
if app.status.is_recording {
app.daemon.stop_recording();
} else {
app.daemon.start_recording();
}
},
))
.child(ui::button(
p,
"refresh",
"Refresh",
false,
|app: &mut App| {
app.refresh_games();
app.daemon.get_status();
},
)),
) )
.child(actions)
} }
fn notice(p: Paint, notice: &Notice) -> El { fn notice(p: Paint, notice: &Notice) -> El {
@@ -715,26 +688,26 @@ pub fn view(app: &App, cx: &mut Cx<App>) -> El {
Screen::History => screens::history::view(app, p), Screen::History => screens::history::view(app, p),
Screen::Review => screens::review::view(app, p), Screen::Review => screens::review::view(app, p),
}; };
let mut main = col().key("main").grow(1.0).min_h(0.0).h_full().fill(p.t.bg); let mut main = col().key("main").grow(1.0).min_h(0.0).w_full().fill(p.t.bg);
if let Some(banner) = &app.notice { if let Some(banner) = &app.notice {
main = main.child(notice(p, banner)); main = main.child(notice(p, banner));
} }
main = main.child(content); main = main.child(content);
col() let mut window = col()
.key("window") .key("window")
.w_full() .w_full()
.h_full() .h_full()
.fill(p.t.bg) .fill(p.t.bg)
.child(titlebar(p, app)) .child(titlebar(p, app))
.child( .child(main)
row() .child(statusbar(p, app));
.key("body") window = match app.screen {
.grow(1.0) Screen::Review => window
.min_h(0.0) .child(app.highlights_scroll.thumb(p, "highlights-thumb"))
.w_full() .child(app.events_scroll.thumb(p, "events-thumb")),
.child(sidebar(p, app)) _ => window.child(app.scroll.thumb(p, "scroll-thumb")),
.child(main), };
) window
} }
/// Whether this is a headless run (`--snap out.png`, `--tree`, …) rather than a window. /// Whether this is a headless run (`--snap out.png`, `--tree`, …) rather than a window.
+5
View File
@@ -62,6 +62,11 @@ fn main() {
app.settings_tab = app::SettingsTab::Daemon; app.settings_tab = app::SettingsTab::Daemon;
}), }),
("review", |app| app::demo_review(app)), ("review", |app| app::demo_review(app)),
("many", |app| {
app.screen = app::Screen::History;
app.games = (0..30).flat_map(|_| crate::history::sample()).collect();
app.games_loaded = true;
}),
], ],
setup: Some(app::setup), setup: Some(app::setup),
poll: Some(app::poll), poll: Some(app::poll),
+77 -32
View File
@@ -112,15 +112,17 @@ pub fn view(app: &App, p: Paint) -> El {
.child(filters), .child(filters),
) )
.child( .child(
col() app.scroll.follow(
.key("list-scroller") col()
.w_full() .key("list-scroller")
.grow(1.0) .w_full()
.min_h(0.0) .grow(1.0)
.scroll(rune::ScrollSpec { .min_h(0.0)
stick_bottom: false, .scroll(rune::ScrollSpec {
}) stick_bottom: false,
.child(list), })
.child(list),
),
) )
} }
@@ -236,30 +238,37 @@ fn game_card(p: Paint, game: &Timeline, index: usize) -> El {
.center_y() .center_y()
.justify_between() .justify_between()
.child( .child(
col() row()
.key("identity") .key("identity")
.gap(2.0) .gap(space::S3)
.center_y()
.child(portrait(p, game.champion().as_deref()))
.child( .child(
text(champion) col()
.key("champion") .key("identity-labels")
.font(heading(16.0)) .gap(2.0)
.color(p.t.text), .child(
) text(champion)
.child( .key("champion")
text(format!( .font(heading(16.0))
"{} · {}", .color(p.t.text),
game.queue_label(), )
format::duration(duration) .child(
)) text(format!(
.key("queue") "{} · {}",
.font(body(11.5)) game.queue_label(),
.color(p.muted()), format::duration(duration)
) ))
.child( .key("queue")
text(format::relative_time(game.start_time)) .font(body(11.5))
.key("when") .color(p.muted()),
.font(body(11.0)) )
.color(p.muted()), .child(
text(format::relative_time(game.start_time))
.key("when")
.font(body(11.0))
.color(p.muted()),
),
), ),
) )
.child( .child(
@@ -290,7 +299,6 @@ fn game_card(p: Paint, game: &Timeline, index: usize) -> El {
), ),
) )
} }
fn stat(p: Paint, value: &str, label: &str) -> El { fn stat(p: Paint, value: &str, label: &str) -> El {
col() col()
.key(format!("stat-{label}")) .key(format!("stat-{label}"))
@@ -304,3 +312,40 @@ fn stat(p: Paint, value: &str, label: &str) -> El {
) )
.child(text(label).key("label").font(body(10.5)).color(p.muted())) .child(text(label).key("label").font(body(10.5)).color(p.muted()))
} }
/// A square champion tile. There is no Data Dragon image cache yet, so the tile carries the
/// champion's initials on a colour derived from the name: the same champion always gets the
/// same tile.
fn portrait(p: Paint, champion: Option<&str>) -> El {
let name = champion.unwrap_or_default();
let hue = name
.bytes()
.fold(2166136261u32, |hash, byte| {
hash.wrapping_mul(16777619) ^ byte as u32
})
.rem_euclid(360) as f32;
let fill = rune::oklch(0.36, 0.075, hue);
let ink = rune::oklch(0.94, 0.02, hue);
el().key("portrait")
.size(52.0, 52.0)
.radius(radius::MD)
.fill(fill)
.border(p.hair, ink.alpha(0.18))
.a11y_hidden()
.child(text(initials(name)).font(heading(17.0)).color(ink).center())
}
/// Up to two letters from the champion's name: `Kai'Sa` is KS, `Jinx` is J.
fn initials(name: &str) -> String {
let letters: Vec<char> = name
.split(|c: char| !c.is_alphanumeric())
.filter(|word| !word.is_empty())
.filter_map(|word| word.chars().next())
.take(2)
.collect();
if letters.is_empty() {
"?".into()
} else {
letters.into_iter().collect()
}
}
+63 -108
View File
@@ -5,7 +5,7 @@ use rune::{El, TextEdit, col, row, text};
use rune_kit::Select; use rune_kit::Select;
use crate::app::{App, SettingsTab}; use crate::app::{App, SettingsTab};
use crate::settings::{AudioSource, AudioSourceType, EncoderPreset, Quality, Settings}; use crate::settings::{AudioSource, AudioSourceType, Quality, Settings};
use crate::ui::{self, Paint, body, heading}; use crate::ui::{self, Paint, body, heading};
/// The daemon's settings plus the text fields the user is typing into. /// The daemon's settings plus the text fields the user is typing into.
@@ -132,14 +132,6 @@ impl Form {
} }
} }
fn encoder_label(preset: &EncoderPreset) -> &'static str {
match preset {
EncoderPreset::Nvenc { .. } => "NVIDIA NVENC",
EncoderPreset::Amf { .. } => "AMD AMF",
EncoderPreset::X264 { .. } => "x264 (Software)",
}
}
const ENCODERS: [&str; 3] = ["NVIDIA NVENC", "AMD AMF", "x264 (Software)"]; const ENCODERS: [&str; 3] = ["NVIDIA NVENC", "AMD AMF", "x264 (Software)"];
const QUALITIES: [&str; 4] = ["Low", "Medium", "High", "Ultra"]; const QUALITIES: [&str; 4] = ["Low", "Medium", "High", "Ultra"];
const CONTAINERS: [&str; 4] = ["mp4", "mkv", "mov", "flv"]; const CONTAINERS: [&str; 4] = ["mp4", "mkv", "mov", "flv"];
@@ -210,7 +202,7 @@ fn tabs(p: Paint, app: &App) -> El {
(SettingsTab::General, "General"), (SettingsTab::General, "General"),
(SettingsTab::Recording, "Recording"), (SettingsTab::Recording, "Recording"),
(SettingsTab::Audio, "Audio"), (SettingsTab::Audio, "Audio"),
(SettingsTab::Daemon, "Daemon"), (SettingsTab::Daemon, "Advanced"),
] { ] {
row = row.child(ui::tab( row = row.child(ui::tab(
p, p,
@@ -239,7 +231,7 @@ pub fn view(app: &App, p: Paint) -> El {
SettingsTab::General => general(app, p, &kit, &mut layer, content), SettingsTab::General => general(app, p, &kit, &mut layer, content),
SettingsTab::Recording => recording(app, p, &kit, &mut layer, content), SettingsTab::Recording => recording(app, p, &kit, &mut layer, content),
SettingsTab::Audio => audio(app, p, &kit, &mut layer, content), SettingsTab::Audio => audio(app, p, &kit, &mut layer, content),
SettingsTab::Daemon => daemon_tab(app, p, content), SettingsTab::Daemon => daemon_tab(app, p, &kit, &mut layer, content),
}; };
let body = col() let body = col()
@@ -295,15 +287,17 @@ pub fn view(app: &App, p: Paint) -> El {
) )
.child(tabs(p, app)) .child(tabs(p, app))
.child( .child(
col() app.scroll.follow(
.key("scroller") col()
.w_full() .key("scroller")
.grow(1.0) .w_full()
.min_h(0.0) .grow(1.0)
.scroll(rune::ScrollSpec { .min_h(0.0)
stick_bottom: false, .scroll(rune::ScrollSpec {
}) stick_bottom: false,
.child(content), })
.child(content),
),
) )
.child(save_footer(p, app)); .child(save_footer(p, app));
@@ -323,71 +317,33 @@ fn save_label(app: &App) -> String {
} }
/// The last line: where settings are written and when they take effect. /// The last line: where settings are written and when they take effect.
fn save_footer(p: Paint, app: &App) -> El { fn save_footer(p: Paint, _app: &App) -> El {
row() row().key("footer").w_full().center_y().child(
.key("footer") text("Settings are stored by the recorder and survive its restart.")
.w_full() .key("footer-note")
.center_y()
.justify_between()
.child(
text("Settings are stored by the record-daemon and survive its restart.")
.key("footer-note")
.font(body(11.5))
.color(p.muted()),
)
.child(
text(format!(
"Log level: {} · encoder: {}",
app.form.draft.daemon.log_level,
if app.encoders.current.is_empty() {
"auto"
} else {
&app.encoders.current
},
))
.key("log-level-note")
.font(body(11.5)) .font(body(11.5))
.color(p.muted()), .color(p.muted()),
) )
} }
fn general(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec<El>, content: El) -> El { fn general(_app: &App, p: Paint, _kit: &rune_kit::Kit, _layer: &mut Vec<El>, content: El) -> El {
let d = &app.form.draft.daemon;
content content
.child(ui::card( .child(ui::card(
p, p,
Some("Recording"), Some("Recording"),
vec![ vec![
ui::toggle_row( text("Recording starts by itself when a game begins and stops when the game ends.")
p, .key("auto-note")
"auto-record", .font(body(12.5))
"Start recording automatically", .color(p.muted())
d.auto_record, .px(space::S3)
|app: &mut App| { .py(space::S3),
app.form.draft.daemon.auto_record ^= true; text("A finished game shows up in the match history on its own.")
app.form.dirty = true; .key("history-note")
}, .font(body(12.5))
), .color(p.muted())
ui::toggle_row( .px(space::S3)
p, .pb(space::S3),
"monitor-client",
"Watch for the League Client",
d.monitor_client,
|app: &mut App| {
app.form.draft.daemon.monitor_client ^= true;
app.form.dirty = true;
},
),
ui::toggle_row(
p,
"save-timeline",
"Keep the event timeline",
d.save_timeline,
|app: &mut App| {
app.form.draft.daemon.save_timeline ^= true;
app.form.dirty = true;
},
),
], ],
)) ))
.child(ui::card( .child(ui::card(
@@ -396,8 +352,8 @@ fn general(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec<El>, conten
vec![ui::toggle_row( vec![ui::toggle_row(
p, p,
"autostart", "autostart",
"Launch at login", "Launch League Recorder at startup",
app.autostart, _app.autostart,
|app: &mut App| { |app: &mut App| {
let next = !app.autostart; let next = !app.autostart;
match crate::os::set_autostart(next) { match crate::os::set_autostart(next) {
@@ -414,35 +370,9 @@ fn general(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec<El>, conten
}, },
)], )],
)) ))
.child(ui::card(
p,
Some("League Client"),
vec![
text_row(
p,
kit,
"poll-interval",
"Client poll interval",
"How often the daemon checks the League Client, in milliseconds",
&app.form.poll_interval,
|app| &mut app.form.poll_interval,
),
select_row(
p,
kit,
layer,
"log-level",
"Log level",
"How much the daemon writes to its log",
&LOG_LEVELS,
Some(d.log_level.clone()),
app,
),
],
))
.child( .child(
row() row()
.key("general-danger") .key("general-actions")
.gap(8.0) .gap(8.0)
.center_y() .center_y()
.child(ui::button( .child(ui::button(
@@ -465,7 +395,6 @@ fn general(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec<El>, conten
)), )),
) )
} }
fn recording(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec<El>, content: El) -> El { fn recording(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec<El>, content: El) -> El {
let v = &app.form.draft.video; let v = &app.form.draft.video;
let o = &app.form.draft.output; let o = &app.form.draft.output;
@@ -496,7 +425,7 @@ fn recording(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec<El>, cont
"Encoder", "Encoder",
"Hardware encoding is faster; x264 uses the CPU", "Hardware encoding is faster; x264 uses the CPU",
&ENCODERS, &ENCODERS,
Some(encoder_label(&v.encoder_preset).to_string()), Some(v.encoder_preset.label().to_string()),
app, app,
), ),
select_row( select_row(
@@ -937,7 +866,7 @@ fn audio(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec<El>, content:
)) ))
} }
fn daemon_tab(app: &App, p: Paint, content: El) -> El { fn daemon_tab(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec<El>, content: El) -> El {
let status_row = |key: &str, label: &'static str, value: String| { let status_row = |key: &str, label: &'static str, value: String| {
row() row()
.key(key) .key(key)
@@ -991,6 +920,32 @@ fn daemon_tab(app: &App, p: Paint, content: El) -> El {
status_row("daemon-log", "Log file", crate::daemon::log_path()), status_row("daemon-log", "Log file", crate::daemon::log_path()),
], ],
)) ))
.child(ui::card(
p,
Some("Recorder"),
vec![
text_row(
p,
kit,
"poll-interval",
"Client poll interval",
"How often the recorder checks the League Client, in milliseconds",
&app.form.poll_interval,
|app| &mut app.form.poll_interval,
),
select_row(
p,
kit,
layer,
"log-level",
"Log level",
"How much the recorder writes to its log",
&LOG_LEVELS,
Some(app.form.draft.daemon.log_level.clone()),
app,
),
],
))
.child( .child(
row() row()
.key("daemon-actions") .key("daemon-actions")
+18
View File
@@ -90,6 +90,24 @@ impl EncoderPreset {
} }
} }
/// The name a daemon-reported encoder id (`.current`) stands for.
pub fn encoder_name(id: &str) -> Option<&'static str> {
match id {
"jim_nvenc" => Some("NVIDIA NVENC"),
"amd_amf_h264" => Some("AMD AMF"),
"x264" => Some("x264 (Software)"),
_ => None,
}
}
pub fn label(&self) -> &'static str {
match self {
EncoderPreset::Nvenc { .. } => "NVIDIA NVENC",
EncoderPreset::Amf { .. } => "AMD AMF",
EncoderPreset::X264 { .. } => "x264 (Software)",
}
}
/// This preset with the encoder changed, keeping the bitrate where both have one. /// This preset with the encoder changed, keeping the bitrate where both have one.
pub fn with_kind(&self, kind: &str) -> EncoderPreset { pub fn with_kind(&self, kind: &str) -> EncoderPreset {
let bitrate = match self { let bitrate = match self {
+67
View File
@@ -345,3 +345,70 @@ pub fn dot(color: Color) -> El {
.fill(color) .fill(color)
.a11y_hidden() .a11y_hidden()
} }
/// A scroll viewport's scrollbar. The viewport reports where it is; the thumb follows in the
/// same frame, so a drag scrolls the list and the bar together.
pub struct ScrollBar {
area: rune::Live<[f32; 4]>,
thumb: rune::Live<[f32; 4]>,
last: std::rc::Rc<std::cell::Cell<rune::ScrollPos>>,
}
impl Default for ScrollBar {
fn default() -> Self {
Self::new()
}
}
impl ScrollBar {
pub fn new() -> Self {
Self {
area: rune::Live::new([0.0; 4]),
thumb: rune::Live::new([0.0; 4]),
last: std::rc::Rc::new(std::cell::Cell::new(rune::ScrollPos::default())),
}
}
/// Make this viewport's scrolling move the thumb.
pub fn follow(&self, viewport: El) -> El {
let reported = self.area.clone();
let area = self.area.clone();
let last = self.last.clone();
let on_layout_last = self.last.clone();
let on_layout_thumb = self.thumb.clone();
let thumb = self.thumb.clone();
viewport
.on_layout_tx(move |_app: &mut App, rect, tx| {
reported.set(rect);
place(&on_layout_thumb, rect, on_layout_last.get());
tx.live_only();
})
.on_scroll_offset(move |at: rune::ScrollPos| {
last.set(at);
place(&thumb, area.get(), at);
})
}
/// The thumb, drawn over the window. `key` must differ per bar on one screen.
pub fn thumb(&self, p: Paint, key: &str) -> El {
el().key(key)
.absolute(0.0, 0.0)
.size(6.0, 0.0)
.radius(3.0)
.fill(p.t.text_secondary.alpha(0.5))
.live_rect(&self.thumb)
.a11y_hidden()
}
}
/// Put the thumb where `at` says the viewport is, inside `area`'s window rect.
fn place(thumb: &rune::Live<[f32; 4]>, [x, y, w, h]: [f32; 4], at: rune::ScrollPos) {
let content = at.viewport[1] + at.max[1];
if h <= 1.0 || w <= 1.0 || at.max[1] <= 0.5 || content <= 0.0 {
thumb.set([0.0, 0.0, 0.0, 0.0]);
return;
}
let height = (h * at.viewport[1] / content).clamp(28.0, h);
let progress = (at.offset[1] / at.max[1]).clamp(0.0, 1.0);
thumb.set([x + w - 9.0, y + progress * (h - height), 6.0, height]);
}