mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
At rest the field is 78 pt. Open, it runs from the rows' 14 pt inset to the time: 179 pt, 69 columns at the dictation HUD's 2.6 pt pitch, three fewer for each further digit on the time. It is the same field widening, not a second one arriving. Its width is a track on the outline's own width spring and never passes the inset from a wall still on its way, so its free end is the wall's motion 14 pt inside it, every frame. Scope::span holds the field by the end beside the time: the plate takes the width at once, the dots are the whole columns that fit, counted from the held end, every dot that stays keeps its place and its phosphor, and a column that arrives is lit from the voice's history in the same step. The shader takes the half-column offset between the dots' middle and the plate's. The island's column count and this one share Tuning::columns_in. In a left corner the time sits on the field's free side, so there the field keeps its resting width.
781 lines
28 KiB
Rust
781 lines
28 KiB
Rust
use std::time::{Duration, Instant};
|
||
|
||
use super::*;
|
||
use crate::rune_hud::dot_scope::Tuning;
|
||
|
||
#[test]
|
||
fn dot_uniform_layout_matches_wgsl() {
|
||
assert_eq!(std::mem::offset_of!(DotUniforms, dark), 60);
|
||
assert_eq!(std::mem::offset_of!(DotUniforms, base), 64);
|
||
assert_eq!(std::mem::offset_of!(DotUniforms, shadow), 128);
|
||
assert_eq!(std::mem::offset_of!(DotUniforms, plate_layout), 144);
|
||
assert_eq!(std::mem::offset_of!(DotUniforms, island), 160);
|
||
assert_eq!(std::mem::offset_of!(DotUniforms, field), 176);
|
||
}
|
||
|
||
#[test]
|
||
fn only_gnome_and_kde_on_wayland_keep_the_primary_display() {
|
||
for desktops in ["GNOME", "ubuntu:GNOME", "KDE"] {
|
||
assert!(keeps_primary_display(Some(desktops), true), "{desktops}");
|
||
assert!(!keeps_primary_display(Some(desktops), false), "{desktops}");
|
||
}
|
||
for desktops in [None, Some("sway"), Some("XFCE")] {
|
||
assert!(!keeps_primary_display(desktops, true));
|
||
assert!(!keeps_primary_display(desktops, false));
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn dot_scope_window_fits_the_largest_plate() {
|
||
let largest = |key| {
|
||
dot_scope::KNOBS
|
||
.iter()
|
||
.find(|knob| knob.key == key)
|
||
.map(|knob| knob.max)
|
||
.unwrap()
|
||
};
|
||
let widest = largest("dot-width").max(largest("dot-processing-width"));
|
||
assert!(widest + 24.0 <= WINDOW.0);
|
||
// The plate's middle sits in the window's; its shadow reaches about
|
||
// 16 pt below it.
|
||
assert!(largest("dot-height") / 2.0 + 19.0 <= WINDOW.1 / 2.0);
|
||
}
|
||
|
||
/// A pose at `reveal`, the rest settled.
|
||
fn revealed(reveal: f32) -> dot_scope::Pose {
|
||
dot_scope::Pose {
|
||
width: 112.0,
|
||
height: 26.0,
|
||
opacity: 1.0,
|
||
scale: 1.0,
|
||
softness: 0.0,
|
||
cool: 0.0,
|
||
fail: 0.0,
|
||
glow: 0.0,
|
||
grid: 1.0,
|
||
live: 0.0,
|
||
reveal,
|
||
level: 0.0,
|
||
clock: 0.0,
|
||
done: 0.0,
|
||
shift: 0.0,
|
||
}
|
||
}
|
||
|
||
/// Labs' ears grow from the housing's height to the menu bar's as they open,
|
||
/// so folded back (a window shown for the frame before it grows, or ordered
|
||
/// out after it folded) they are the notch itself, with no black lip under
|
||
/// the camera, and open they end on the bar's bottom edge.
|
||
#[test]
|
||
fn the_ears_grow_from_the_housing_to_the_menu_bar() {
|
||
let ears = Island {
|
||
notch: [185.0, 32.0],
|
||
bar: 34.0,
|
||
width: 253.0,
|
||
top: -64.0,
|
||
ears: true,
|
||
};
|
||
let shape = |reveal| island_shape(ears, &revealed(reveal));
|
||
assert_eq!(shape(0.0)[1], 32.0, "folded, it is the housing");
|
||
assert_eq!(shape(0.0)[3], NOTCH_RADIUS, "with the notch's corners");
|
||
assert_eq!(shape(1.0)[1], 34.0, "open, it reaches the menu bar's edge");
|
||
assert_eq!(shape(1.0)[3], EARS_RADIUS);
|
||
assert_eq!(shape(1.4)[1], 34.0, "a bounce past full stops there");
|
||
assert!(
|
||
(shape(0.5)[1] - 33.0).abs() < 1e-6,
|
||
"and halfway is halfway"
|
||
);
|
||
// Without a menu bar to open to, it keeps the housing's height.
|
||
let unmeasured = Island { bar: 0.0, ..ears };
|
||
assert_eq!(island_shape(unmeasured, &revealed(1.0))[1], 32.0);
|
||
}
|
||
|
||
/// The macOS HUD's window, which the captures use on every OS so they line
|
||
/// up with the Metal reference frames.
|
||
const CAPTURE_DOT_WINDOW: (f32, f32) = (320.0, 128.0);
|
||
|
||
/// A dictation, as the listener reports it, at 60 fps: HUD Lab's synthetic
|
||
/// voice for 3 s, then transcribing, processing, and done.
|
||
fn schedule(model: &mut IndicatorModel, start: Instant) -> Vec<DotFrame> {
|
||
let mut events = vec![
|
||
(0.0, HudEvent::Started),
|
||
(3.0, HudEvent::Submitted { job_id: 1 }),
|
||
(3.6, HudEvent::Processing { job_id: 1 }),
|
||
(4.4, HudEvent::JobCompleted { job_id: 1 }),
|
||
];
|
||
events.reverse();
|
||
(0..330)
|
||
.map(|n| {
|
||
let t = n as f32 / 60.0;
|
||
let now = start + Duration::from_secs_f32(t);
|
||
while events.last().is_some_and(|(at, _)| *at <= t) {
|
||
model.handle(events.pop().unwrap().1, now, false);
|
||
}
|
||
if t < 3.0 {
|
||
// A microphone's RMS for the demo voice's loudness.
|
||
let rms = 10_f32.powf(dot_scope::demo_loudness(t) / 20.0);
|
||
let meter = HudEvent::Meter {
|
||
average: rms,
|
||
peak: (rms * 3.0).min(1.0),
|
||
};
|
||
model.handle(meter, now, false);
|
||
}
|
||
model.step(now, 1.0 / 60.0, false)
|
||
})
|
||
.collect()
|
||
}
|
||
|
||
/// A 1512 × 982 point 14-inch MacBook Pro: a 32 pt housing under a 34 pt
|
||
/// menu bar, 185 pt wide.
|
||
fn macbook() -> crate::rune_hud::hud_placement::Screen {
|
||
crate::rune_hud::hud_placement::Screen {
|
||
frame: crate::rune_hud::hud_placement::Rect {
|
||
x: 0.0,
|
||
y: 0.0,
|
||
width: 1512.0,
|
||
height: 982.0,
|
||
},
|
||
visible: crate::rune_hud::hud_placement::Rect {
|
||
x: 0.0,
|
||
y: 0.0,
|
||
width: 1512.0,
|
||
height: 948.0,
|
||
},
|
||
safe_top: 32.0,
|
||
aux_left: 663.5,
|
||
aux_right: 663.5,
|
||
bar: 24.0,
|
||
}
|
||
}
|
||
|
||
/// An Apple Studio Display at its default 2560 × 1440 points: no notch, a
|
||
/// 24 pt menu bar.
|
||
fn studio_display() -> crate::rune_hud::hud_placement::Screen {
|
||
crate::rune_hud::hud_placement::Screen {
|
||
frame: crate::rune_hud::hud_placement::Rect {
|
||
x: 0.0,
|
||
y: 0.0,
|
||
width: 2560.0,
|
||
height: 1440.0,
|
||
},
|
||
visible: crate::rune_hud::hud_placement::Rect {
|
||
x: 0.0,
|
||
y: 0.0,
|
||
width: 2560.0,
|
||
height: 1416.0,
|
||
},
|
||
bar: 24.0,
|
||
..Default::default()
|
||
}
|
||
}
|
||
|
||
/// Where `screen` puts the capture window for `tuning`'s placement.
|
||
fn place_on(
|
||
screen: &crate::rune_hud::hud_placement::Screen,
|
||
tuning: Tuning,
|
||
) -> crate::rune_hud::hud_placement::Placed {
|
||
let (width, height) = CAPTURE_DOT_WINDOW;
|
||
let content = crate::rune_hud::hud_placement::Content {
|
||
window: (f64::from(width), f64::from(height)),
|
||
plate: (f64::from(width) - 24.0, f64::from(dot_scope::PLATE_HEIGHT)),
|
||
island_spread: f64::from(tuning.island_spread),
|
||
};
|
||
// `HEX_HUD_EARS=1` places Labs' notch indicator instead.
|
||
if std::env::var("HEX_HUD_EARS").as_deref() == Ok("1")
|
||
&& let Some(placed) = crate::rune_hud::hud_placement::ears(screen, content)
|
||
{
|
||
return placed;
|
||
}
|
||
crate::rune_hud::hud_placement::place(tuning.placement, screen, content, false)
|
||
}
|
||
|
||
/// What the screen shows behind the window.
|
||
#[derive(Clone, Copy)]
|
||
struct Backdrop {
|
||
/// How far the window's top sits below the screen's, pt.
|
||
drop: f32,
|
||
/// The menu bar's height.
|
||
bar: f32,
|
||
/// The notch's width and height, if any.
|
||
notch: Option<(f32, f32)>,
|
||
}
|
||
|
||
fn backdrop(
|
||
screen: &crate::rune_hud::hud_placement::Screen,
|
||
placed: &crate::rune_hud::hud_placement::Placed,
|
||
) -> Backdrop {
|
||
Backdrop {
|
||
drop: (screen.frame.y + screen.frame.height - placed.top_left.1) as f32,
|
||
bar: screen.bar_height() as f32,
|
||
notch: screen
|
||
.notch()
|
||
.map(|notch| (notch.width as f32, notch.height as f32)),
|
||
}
|
||
}
|
||
|
||
/// Renders a dictation through the real shaders offscreen, as PNG frames
|
||
/// at 60 fps over a desktop colour, from a fixed schedule:
|
||
///
|
||
/// ```sh
|
||
/// HEX_HUD_FRAMES=/tmp/frames cargo test --bin hex rune_hud_frames -- --ignored
|
||
/// ```
|
||
///
|
||
/// `HEX_HUD_TUNING` overrides Dot scope's tuning as JSON, such as
|
||
/// `{"processing": "pulse", "placement": "notch"}`, placed on a 14-inch
|
||
/// MacBook Pro, or with `HEX_HUD_SCREEN=studio` on a Studio Display (no
|
||
/// notch); `HEX_HUD_EARS=1` draws Labs' notch indicator; `HEX_HUD_THEME=light` draws over a light desktop. `HEX_HUD_DUMP=<file>` also writes
|
||
/// every frame's shader inputs, for rendering the same frames elsewhere.
|
||
#[test]
|
||
#[ignore = "writes frames for a review clip"]
|
||
fn rune_hud_frames() {
|
||
let out = std::path::PathBuf::from(std::env::var("HEX_HUD_FRAMES").expect("HEX_HUD_FRAMES"));
|
||
std::fs::create_dir_all(&out).unwrap();
|
||
let dark = std::env::var("HEX_HUD_THEME").as_deref() != Ok("light");
|
||
let mut fields = serde_json::to_value(Tuning::default()).unwrap();
|
||
if let Ok(json) = std::env::var("HEX_HUD_TUNING") {
|
||
let overrides: serde_json::Value = serde_json::from_str(&json).unwrap();
|
||
for (key, value) in overrides.as_object().unwrap() {
|
||
fields[key] = value.clone();
|
||
}
|
||
}
|
||
let tuning: Tuning = serde_json::from_value(fields).unwrap();
|
||
let start = Instant::now();
|
||
let screen = if std::env::var("HEX_HUD_SCREEN").as_deref() == Ok("studio") {
|
||
studio_display()
|
||
} else {
|
||
macbook()
|
||
};
|
||
let placed = place_on(&screen, tuning);
|
||
let layout = placed.layout;
|
||
let backdrop = backdrop(&screen, &placed);
|
||
let mut model = IndicatorModel::new(tuning, start);
|
||
model.set_island(layout.island);
|
||
let frames = schedule(&mut model, start);
|
||
|
||
let instance =
|
||
wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env());
|
||
let adapter = block_on(instance.request_adapter(&Default::default())).expect("a GPU adapter");
|
||
// `HEX_HUD_BUFFERS=1` draws through the uniform buffer, as a device
|
||
// without immediates does.
|
||
let features = if std::env::var("HEX_HUD_BUFFERS").as_deref() == Ok("1") {
|
||
wgpu::Features::empty()
|
||
} else {
|
||
adapter.features() & Painter::FEATURES
|
||
};
|
||
let (device, queue) = block_on(adapter.request_device(&wgpu::DeviceDescriptor {
|
||
required_features: features,
|
||
required_limits: wgpu::Limits {
|
||
max_immediate_size: if features.is_empty() {
|
||
0
|
||
} else {
|
||
adapter.limits().max_immediate_size
|
||
},
|
||
..Default::default()
|
||
},
|
||
..Default::default()
|
||
}))
|
||
.unwrap();
|
||
let format = wgpu::TextureFormat::Rgba8Unorm;
|
||
let gpu = Painter::new(&device, format);
|
||
let scale = 2.0;
|
||
let window = CAPTURE_DOT_WINDOW;
|
||
let size = ((window.0 * scale) as u32, (window.1 * scale) as u32);
|
||
let extent = wgpu::Extent3d {
|
||
width: size.0,
|
||
height: size.1,
|
||
depth_or_array_layers: 1,
|
||
};
|
||
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
||
label: None,
|
||
size: extent,
|
||
mip_level_count: 1,
|
||
sample_count: 1,
|
||
dimension: wgpu::TextureDimension::D2,
|
||
format,
|
||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
|
||
view_formats: &[],
|
||
});
|
||
let view = texture.create_view(&Default::default());
|
||
let row = (size.0 * 4).div_ceil(256) * 256;
|
||
let readback = device.create_buffer(&wgpu::BufferDescriptor {
|
||
label: None,
|
||
size: u64::from(row * size.1),
|
||
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
|
||
mapped_at_creation: false,
|
||
});
|
||
let target = Target::whole(size, scale);
|
||
let mut dump = Vec::new();
|
||
dump.extend(size.0.to_le_bytes());
|
||
dump.extend(size.1.to_le_bytes());
|
||
for (n, frame) in frames.iter().enumerate() {
|
||
dump.extend(bytes(&DotUniforms::new(frame, layout, target)));
|
||
let mut packed = packed_dots(frame);
|
||
packed.resize(DOT_BYTES as usize, 0);
|
||
dump.extend(packed);
|
||
let mut encoder = device.create_command_encoder(&Default::default());
|
||
let uniforms = gpu.write(frame, layout, target, |buffer, data| {
|
||
queue.write_buffer(buffer, 0, data)
|
||
});
|
||
gpu.draw(&mut encoder, &view, &uniforms);
|
||
encoder.copy_texture_to_buffer(
|
||
texture.as_image_copy(),
|
||
wgpu::TexelCopyBufferInfo {
|
||
buffer: &readback,
|
||
layout: wgpu::TexelCopyBufferLayout {
|
||
offset: 0,
|
||
bytes_per_row: Some(row),
|
||
rows_per_image: None,
|
||
},
|
||
},
|
||
extent,
|
||
);
|
||
queue.submit([encoder.finish()]);
|
||
readback
|
||
.slice(..)
|
||
.map_async(wgpu::MapMode::Read, |result| result.unwrap());
|
||
device.poll(wgpu::PollType::wait_indefinitely()).unwrap();
|
||
let mapped = readback.slice(..).get_mapped_range().unwrap();
|
||
let mut rgba = Vec::with_capacity((size.0 * size.1 * 4) as usize);
|
||
for y in 0..size.1 {
|
||
rgba.extend_from_slice(&mapped[(y * row) as usize..][..(size.0 * 4) as usize]);
|
||
}
|
||
drop(mapped);
|
||
readback.unmap();
|
||
write_over_desktop(
|
||
&out.join(format!("{n:04}.png")),
|
||
size,
|
||
scale,
|
||
&rgba,
|
||
dark,
|
||
backdrop,
|
||
);
|
||
}
|
||
if let Ok(path) = std::env::var("HEX_HUD_DUMP") {
|
||
std::fs::write(path, dump).unwrap();
|
||
}
|
||
}
|
||
|
||
/// Premultiplied RGBA over the desktop colour, the menu bar, and the notch
|
||
/// (`backdrop`), as an RGB PNG.
|
||
fn write_over_desktop(
|
||
path: &std::path::Path,
|
||
size: (u32, u32),
|
||
scale: f32,
|
||
rgba: &[u8],
|
||
dark: bool,
|
||
backdrop: Backdrop,
|
||
) {
|
||
let (desktop, menu_bar): ([f32; 3], [f32; 3]) = if dark {
|
||
([0.11, 0.11, 0.13], [0.2, 0.2, 0.22])
|
||
} else {
|
||
([0.93, 0.93, 0.94], [0.98, 0.98, 0.98])
|
||
};
|
||
let behind = |x: u32, y: u32| {
|
||
// In points from the screen's top middle.
|
||
let px = (x as f32 + 0.5) / scale - size.0 as f32 / scale / 2.0;
|
||
let py = (y as f32 + 0.5) / scale + backdrop.drop;
|
||
if let Some((width, height)) = backdrop.notch {
|
||
// The notch's bottom corners are rounded 8 pt.
|
||
let radius = 8.0;
|
||
let dx = (px.abs() - (width / 2.0 - radius)).max(0.0);
|
||
let dy = (py - (height - radius)).max(0.0);
|
||
if px.abs() <= width / 2.0 && py <= height && dx.hypot(dy) <= radius {
|
||
return [0.0; 3];
|
||
}
|
||
}
|
||
if py < backdrop.bar { menu_bar } else { desktop }
|
||
};
|
||
let rgb: Vec<u8> = rgba
|
||
.chunks(4)
|
||
.enumerate()
|
||
.flat_map(|(i, p)| {
|
||
let under = behind(i as u32 % size.0, i as u32 / size.0);
|
||
let a = f32::from(p[3]) / 255.0;
|
||
(0..3).map(move |c| {
|
||
let v = f32::from(p[c]) / 255.0 + under[c] * (1.0 - a);
|
||
(v * 255.0).round().clamp(0.0, 255.0) as u8
|
||
})
|
||
})
|
||
.collect();
|
||
let file = std::fs::File::create(path).unwrap();
|
||
let mut encoder = png::Encoder::new(file, size.0, size.1);
|
||
encoder.set_color(png::ColorType::Rgb);
|
||
encoder.set_depth(png::BitDepth::Eight);
|
||
encoder
|
||
.write_header()
|
||
.unwrap()
|
||
.write_image_data(&rgb)
|
||
.unwrap();
|
||
}
|
||
|
||
fn block_on<F: std::future::Future>(future: F) -> F::Output {
|
||
let mut future = std::pin::pin!(future);
|
||
let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
|
||
loop {
|
||
if let std::task::Poll::Ready(output) = future.as_mut().poll(&mut cx) {
|
||
return output;
|
||
}
|
||
std::thread::yield_now();
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn a_frame_less_start_hides_once() {
|
||
let start = Instant::now();
|
||
let mut first_frame = FirstFrame::default();
|
||
first_frame.start(start, 7);
|
||
let early = first_frame.poll(start + Duration::from_millis(100), 7, None, None);
|
||
assert!(early.hide.is_none());
|
||
assert_eq!(early.reason, None);
|
||
let resolved = first_frame.poll(start + FIRST_FRAME, 7, None, Some("dx12 on Intel"));
|
||
assert!(resolved.hide.is_some(), "the attempt presented nothing");
|
||
assert!(
|
||
resolved
|
||
.reason
|
||
.as_deref()
|
||
.is_some_and(|reason| reason.contains("presented no frame")),
|
||
"{:?}",
|
||
resolved.reason
|
||
);
|
||
let after = first_frame.poll(start + FIRST_FRAME + Duration::from_secs(1), 7, None, None);
|
||
assert!(after.hide.is_none());
|
||
}
|
||
|
||
#[test]
|
||
fn a_new_start_does_not_hide_on_the_previous_reason() {
|
||
let start = Instant::now();
|
||
let mut first_frame = FirstFrame::default();
|
||
first_frame.start(start, 0);
|
||
assert!(
|
||
first_frame
|
||
.poll(start + FIRST_FRAME, 0, None, None)
|
||
.hide
|
||
.is_some()
|
||
);
|
||
// The last known reason stays in Settings, but it can't hide the next
|
||
// start before that start has had its own first-frame window.
|
||
let next = start + FIRST_FRAME + Duration::from_millis(1);
|
||
first_frame.start(next, 0);
|
||
let tick = first_frame.poll(next + Duration::from_millis(250), 0, None, None);
|
||
assert!(tick.hide.is_none());
|
||
assert!(
|
||
tick.reason.is_some(),
|
||
"the last attempt's reason still shows"
|
||
);
|
||
let just_before =
|
||
first_frame.poll(next + FIRST_FRAME - Duration::from_millis(1), 0, None, None);
|
||
assert!(just_before.hide.is_none());
|
||
}
|
||
|
||
#[test]
|
||
fn a_second_start_while_pending_restarts_the_attempt() {
|
||
let start = Instant::now();
|
||
let mut first_frame = FirstFrame::default();
|
||
first_frame.start(start, 5);
|
||
let second = start + Duration::from_millis(900);
|
||
first_frame.start(second, 5);
|
||
// 1.5 s after the first start is only 600 ms into the second.
|
||
let early = first_frame.poll(start + FIRST_FRAME, 5, None, None);
|
||
assert!(
|
||
early.hide.is_none(),
|
||
"the second start gets its own first-frame window"
|
||
);
|
||
// The second attempt's baseline is its own: one frame since then presents.
|
||
let resolved = first_frame.poll(second + FIRST_FRAME, 6, None, None);
|
||
assert!(resolved.hide.is_none());
|
||
assert_eq!(resolved.reason, None);
|
||
}
|
||
|
||
#[test]
|
||
fn an_overlay_failure_never_hides_on_its_own() {
|
||
let start = Instant::now();
|
||
let mut first_frame = FirstFrame::default();
|
||
first_frame.start(start, 0);
|
||
// Rune clears the failure it replaced on the overlay thread; a poll right
|
||
// after the start still sees the previous attempt's. A failure is only a
|
||
// reason for Settings and the log: it never hides anything by itself.
|
||
for at in [
|
||
Duration::from_millis(1),
|
||
Duration::from_millis(250),
|
||
FIRST_FRAME - Duration::from_millis(1),
|
||
] {
|
||
let check = first_frame.poll(start + at, 0, Some("device lost"), None);
|
||
assert!(check.hide.is_none());
|
||
assert_eq!(check.reason.as_deref(), Some("device lost"));
|
||
}
|
||
// Even after the attempt resolves, a failure still set doesn't hide on its
|
||
// own: the frame that presented is what matters.
|
||
let presented = first_frame.poll(start + FIRST_FRAME, 1, Some("device lost"), None);
|
||
assert!(presented.hide.is_none());
|
||
assert_eq!(presented.reason.as_deref(), Some("device lost"));
|
||
}
|
||
|
||
#[test]
|
||
fn an_attempt_hides_only_once() {
|
||
let start = Instant::now();
|
||
let mut first_frame = FirstFrame::default();
|
||
first_frame.start(start, 0);
|
||
assert!(
|
||
first_frame
|
||
.poll(start + FIRST_FRAME, 0, None, None)
|
||
.hide
|
||
.is_some()
|
||
);
|
||
assert!(
|
||
first_frame
|
||
.poll(
|
||
start + FIRST_FRAME + Duration::from_millis(250),
|
||
0,
|
||
None,
|
||
None
|
||
)
|
||
.hide
|
||
.is_none()
|
||
);
|
||
assert!(
|
||
first_frame
|
||
.poll(
|
||
start + FIRST_FRAME + Duration::from_millis(500),
|
||
0,
|
||
Some("device lost"),
|
||
None
|
||
)
|
||
.hide
|
||
.is_none(),
|
||
"a later failure doesn't hide the attempt again"
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn the_next_start_hides_again_after_a_hide() {
|
||
let start = Instant::now();
|
||
let mut first_frame = FirstFrame::default();
|
||
first_frame.start(start, 3);
|
||
assert!(
|
||
first_frame
|
||
.poll(start + FIRST_FRAME, 3, None, None)
|
||
.hide
|
||
.is_some(),
|
||
"the first attempt presented nothing"
|
||
);
|
||
// The recovery is per start: the next one gets its own hide if it is
|
||
// frame-less too.
|
||
let next = start + FIRST_FRAME + Duration::from_secs(1);
|
||
first_frame.start(next, 4);
|
||
assert!(
|
||
first_frame
|
||
.poll(next + FIRST_FRAME, 4, None, None)
|
||
.hide
|
||
.is_some(),
|
||
"the next attempt presented nothing either"
|
||
);
|
||
}
|
||
|
||
/// A recovery hide is queued for the runtime thread to apply on the overlay's
|
||
/// own thread, so a dictation that starts in between must keep the HUD its own
|
||
/// start asked for (#1793).
|
||
#[test]
|
||
fn a_stale_attempts_hide_leaves_a_newer_start_alone() {
|
||
let start = Instant::now();
|
||
let mut first_frame = FirstFrame::default();
|
||
first_frame.start(start, 4);
|
||
let hide = first_frame
|
||
.poll(start + FIRST_FRAME, 4, None, None)
|
||
.hide
|
||
.expect("the attempt presented nothing");
|
||
assert!(first_frame.should_apply_hide(hide, 4));
|
||
|
||
// The next dictation starts while that hide is still queued: its start
|
||
// re-asserts the HUD, so the stale hide must not run.
|
||
let next = start + FIRST_FRAME + Duration::from_millis(120);
|
||
first_frame.start(next, 4);
|
||
assert!(
|
||
!first_frame.should_apply_hide(hide, 4),
|
||
"a later start owns the HUD now"
|
||
);
|
||
|
||
// That start's own frame-less attempt still hides, and only it.
|
||
let next_hide = first_frame
|
||
.poll(next + FIRST_FRAME, 4, None, None)
|
||
.hide
|
||
.expect("the new attempt presented nothing either");
|
||
assert_ne!(next_hide.attempt, hide.attempt, "each attempt has its tag");
|
||
assert!(first_frame.should_apply_hide(next_hide, 4));
|
||
}
|
||
|
||
/// The hide is applied a turn after it is decided, and a HUD that presented its
|
||
/// first frame in between is up: hiding it would take away the HUD the user is
|
||
/// watching, and report a failure that did not happen (#1793).
|
||
#[test]
|
||
fn a_hide_is_dropped_when_a_frame_presented_after_the_poll() {
|
||
let start = Instant::now();
|
||
let mut first_frame = FirstFrame::default();
|
||
first_frame.start(start, 0);
|
||
let hide = first_frame
|
||
.poll(start + FIRST_FRAME, 0, None, None)
|
||
.hide
|
||
.expect("the attempt presented nothing");
|
||
|
||
// The first frame lands between the poll and the queued hide.
|
||
assert!(
|
||
!first_frame.should_apply_hide(hide, 1),
|
||
"one frame since the poll means the HUD is drawing"
|
||
);
|
||
// The same poll's hide still applies when nothing presented.
|
||
assert!(first_frame.should_apply_hide(hide, 0));
|
||
}
|
||
|
||
/// `reshow_due` is the recording's own recovery: a HUD Rune declined or Hex hid
|
||
/// is asked for once, after its frames stop, and never again in that recording
|
||
/// (#1793).
|
||
#[test]
|
||
fn a_recording_asks_again_only_when_its_frames_stop() {
|
||
let stale = RESHOW_AFTER + Duration::from_millis(1);
|
||
let fresh = Duration::from_millis(16);
|
||
// Frames are coming: nothing to do.
|
||
assert!(!reshow_due(Some(fresh), false));
|
||
// Frames stopped: ask, once.
|
||
assert!(reshow_due(Some(stale), false));
|
||
assert!(!reshow_due(Some(stale), true));
|
||
// A start sets the clock, so a frame the previous dictation left can't make
|
||
// it look late.
|
||
assert!(!reshow_due(None, false));
|
||
// A recovery hide re-arms the ask and restarts the clock: the meter right
|
||
// after it is not late, and the next stale one asks once more (#1793).
|
||
assert!(!reshow_due(Some(Duration::ZERO), false));
|
||
assert!(reshow_due(Some(stale), false));
|
||
}
|
||
|
||
/// A HUD as `install` builds it, without its overlay: the state `handle`
|
||
/// touches, and no GPU or Rune. `shown` is what `Hud::show` records once Rune
|
||
/// took the show; a HUD with no overlay can't ask it.
|
||
fn hud(shown: bool) -> Hud {
|
||
Hud {
|
||
model: IndicatorModel::new(dot_scope::lab(), Instant::now()),
|
||
drawn: None,
|
||
layout: Layout::default(),
|
||
position: IndicatorPosition::Top,
|
||
this: None,
|
||
shown: Arc::new(AtomicBool::new(shown)),
|
||
reduce: false,
|
||
animating: Arc::new(AtomicBool::new(false)),
|
||
#[cfg(target_os = "macos")]
|
||
placed: None,
|
||
#[cfg(not(target_os = "macos"))]
|
||
edge_placed: None,
|
||
#[cfg(not(target_os = "macos"))]
|
||
followed: None,
|
||
#[cfg(not(target_os = "macos"))]
|
||
gpu: Arc::default(),
|
||
#[cfg(target_os = "windows")]
|
||
watch: Arc::default(),
|
||
#[cfg(any(target_os = "windows", test))]
|
||
last_drawn: None,
|
||
#[cfg(any(target_os = "windows", test))]
|
||
armed: false,
|
||
#[cfg(any(target_os = "windows", test))]
|
||
polled_at: None,
|
||
#[cfg(target_os = "windows")]
|
||
stepped_at: None,
|
||
#[cfg(target_os = "windows")]
|
||
reshown: false,
|
||
}
|
||
}
|
||
|
||
/// An armed timer draws the next level itself, so a meter only updates the
|
||
/// model; a timer that isn't armed draws nothing, so the meter's update draws
|
||
/// one frame. Loudness moves either way.
|
||
#[test]
|
||
fn an_armed_timer_draws_for_meters_and_a_stopped_one_lets_a_meter_draw() {
|
||
let mut hud = hud(true);
|
||
hud.armed = true;
|
||
let draws = windows_pacing::meter_update(
|
||
&mut hud,
|
||
HudEvent::Meter {
|
||
average: 0.5,
|
||
peak: 0.8,
|
||
},
|
||
);
|
||
assert!(!draws, "the armed timer's frame shows the level");
|
||
assert!((hud.model.loudness() - dot_scope::loudness(0.5)).abs() < f32::EPSILON);
|
||
|
||
hud.armed = false;
|
||
let draws = windows_pacing::meter_update(
|
||
&mut hud,
|
||
HudEvent::Meter {
|
||
average: 0.1,
|
||
peak: 0.2,
|
||
},
|
||
);
|
||
assert!(draws, "nothing else draws the level, so this meter does");
|
||
assert!((hud.model.loudness() - dot_scope::loudness(0.1)).abs() < f32::EPSILON);
|
||
}
|
||
|
||
/// The timer arms from a start's own time, before any frame it draws, so the
|
||
/// first frame after the show asks for the next one. It stops arming once no
|
||
/// frame has drawn for `RESHOW_AFTER`: a show that fails draws nothing, and an
|
||
/// always-asking timer would turn Rune's loop at 60 Hz with Hex idle. A hidden
|
||
/// HUD arms nothing either.
|
||
#[test]
|
||
fn the_timer_arms_from_a_start_and_stops_when_frames_stop() {
|
||
let mut hud = hud(true);
|
||
hud.animating.store(true, Ordering::Release);
|
||
// `handle(Started)` is where a start's arming reference comes from.
|
||
hud.handle(HudEvent::Started);
|
||
assert!(
|
||
hud.last_drawn.is_some_and(|at| at.elapsed() < RESHOW_AFTER),
|
||
"the start is the first reference"
|
||
);
|
||
// `Hud::show` records the show once Rune takes it; this HUD has no overlay.
|
||
hud.shown.store(true, Ordering::Release);
|
||
windows_pacing::poll(&mut hud, 0.0, &mut rune::Tx::default());
|
||
assert!(hud.armed, "the start's frame asks for the next");
|
||
|
||
// Frames stop: the reference ages past `RESHOW_AFTER`.
|
||
hud.last_drawn = Some(Instant::now() - RESHOW_AFTER - Duration::from_millis(1));
|
||
windows_pacing::poll(&mut hud, 0.0, &mut rune::Tx::default());
|
||
assert!(!hud.armed, "a stale reference asks for nothing");
|
||
|
||
// Hidden, even a fresh reference arms nothing.
|
||
hud.last_drawn = Some(Instant::now());
|
||
hud.shown.store(false, Ordering::Release);
|
||
windows_pacing::poll(&mut hud, 0.0, &mut rune::Tx::default());
|
||
assert!(!hud.armed);
|
||
}
|
||
|
||
/// Every start asks Rune for the HUD again, macOS included: `handle` clears
|
||
/// `shown` before `show` asks, so a start isn't gated to the OSes whose own
|
||
/// recovery needed it. Re-adding that gate would strand a HUD Rune hid itself.
|
||
#[test]
|
||
fn a_start_clears_shown_on_every_os() {
|
||
let mut hud = hud(true);
|
||
hud.handle(HudEvent::Started);
|
||
assert!(!hud.shown.load(Ordering::Acquire));
|
||
}
|
||
|
||
/// Rune runs a poll before the frame's render, so a stall longer than
|
||
/// `RESHOW_AFTER` leaves the next poll looking at a stale `last_drawn` even
|
||
/// though the frame it is part of draws: that frame is the one the previous
|
||
/// poll drew. It must arm the next, or a HUD with no meters to restart it
|
||
/// (Transcribing, Completed, Failed) freezes on screen and never fades out.
|
||
#[test]
|
||
fn a_frame_drawn_since_the_last_poll_keeps_the_timer_armed() {
|
||
let mut hud = hud(true);
|
||
hud.animating.store(true, Ordering::Release);
|
||
let polled = Instant::now() - RESHOW_AFTER - RESHOW_AFTER;
|
||
hud.polled_at = Some(polled);
|
||
// Older than RESHOW_AFTER, but the frame drew after that poll.
|
||
hud.last_drawn = Some(polled + Duration::from_millis(1));
|
||
assert!(
|
||
hud.last_drawn
|
||
.is_some_and(|at| at.elapsed() >= RESHOW_AFTER),
|
||
"the reference is stale on its own"
|
||
);
|
||
windows_pacing::poll(&mut hud, 0.0, &mut rune::Tx::default());
|
||
assert!(hud.armed, "a frame since the last poll keeps the timer");
|
||
|
||
// Nothing drew since the last poll and the reference is stale: stop.
|
||
hud.last_drawn = Some(Instant::now() - RESHOW_AFTER - Duration::from_millis(1));
|
||
windows_pacing::poll(&mut hud, 0.0, &mut rune::Tx::default());
|
||
assert!(!hud.armed, "no frame since the last poll, and a stale one");
|
||
}
|