ui_access

source of pkg::alloy::window::headless

1// A window with no platform behind it, for tests: a list of events2// replayed through the program's handler, and the frames it presents kept3// for the test to look at.45import std::vector;6import std::string;7import std::option;8import std::time;910import window;1112// A frame the program presented.13exp type Frame = struct {14    exp size: Size,15    exp pixels: Vector<u32>,16};1718// How a headless run went.19exp type Replay<S> = struct {20    exp state: S, // as the handler left it21    exp frames: Vector<Frame>, // each one presented, in order22    exp redraws: u32,23    exp wakes: u32, // the times `wake_at` changed the deadline24    exp closed: bool, // whether the program closed the window25    exp text_input: Option<Area>, // the input method's, as the program left it26    exp secret_input: bool, // whether the input method was told a password is typed27    exp access: AccessTree, // the accessibility tree the program sent28    exp announcements: Vector<Announcement>, // what screen readers were given to say29    exp clipboard: String, // the clipboard, kept for the run30    // the file dialogs the program asked for, which the events answer31    exp dialogs: Vector<FileDialog>,32    // each entry the program put the page's history at (`set_history`), in33    // order34    exp history: Vector<HistoryEntry>,35    // the language the program said its content is in, and whether it36    // reads right to left (`set_language`)37    exp language: String = String::from(&"en"),38    exp right_to_left: bool = false,39};4041// Runs the handler as `run` does, over the events instead of a42// platform's. The window opens at the configuration's size and the scale43// (`Scaled` when the scale is not 1, then `Resized` and `Redraw`); after44// each event it delivers a Redraw when the event resized the window or45// the handler asked for one, and a Clipboard when it asked for the46// clipboard's text. It stops when the window closes or the events end.47// The window's clock stands at 0 but as the events move it: a Redraw48// among them moves it to the Redraw's time, giving the Timer `wake_at`49// asked for on the way at its time, and every other event comes at the50// time the clock is at.51exp fn run_headless<S>(52    config: &WindowConfig,53    scale: f32,54    state: S,55    handler: |&| (&var S, &var Window, &Event),56    events: &[Event]57) -> Replay<S> {58    var replay = Replay<S> {59        .state = move state,60        .frames = Vector<Frame>::empty(),61        .redraws = 0,62        .wakes = 0,63        .closed = false,64        .text_input = ::None,65        .secret_input = false,66        .access = AccessTree::empty(),67        .announcements = Vector<Announcement>::empty(),68        .clipboard = String::empty(),69        .dialogs = Vector<FileDialog>::empty(),70        .history = Vector<HistoryEntry>::empty()71    };72    var window = Window::opening(&config, 1.0);73    window.clock = ::None;74    if (scale != 1.0) {75        step(&var replay, &var window, &handler, &Event::Scaled(scale));76    }77    const size = physical(&config.size, scale);78    step(&var replay, &var window, &handler, &Event::Resized(size));79    for (events) |&event| {80        if (event is ::Redraw |t|) {81            advance(&var replay, &var window, &handler, t);82            step(&var replay, &var window, &handler, &Event::Redraw(window.time));83        } else {84            step(&var replay, &var window, &handler, &event);85        }86    }87    return move replay;88}8990// Moves the clock to the time, giving the Timers asked for up to it.91fn advance<S>(92    replay: &var Replay<S>,93    window: &var Window,94    handler: &|&| (&var S, &var Window, &Event),95    until: Duration96) {97    while (!replay.closed && window.wake is ::Some |at| && at.nanoseconds <= until.nanoseconds) {98        if (at.nanoseconds > window.time.nanoseconds) {99            window.time = at;100        }101        window.wake = ::None;102        step(&var replay, &var window, &handler, &Event::Timer);103    }104    if (until.nanoseconds > window.time.nanoseconds) {105        window.time = until;106    }107}108109fn step<S>(110    replay: &var Replay<S>,111    window: &var Window,112    handler: &|&| (&var S, &var Window, &Event),113    event: &Event114) {115    if (deliver(&var replay, &var window, &handler, &event)) {116        deliver(&var replay, &var window, &handler, &Event::Clipboard(copy replay.clipboard));117    }118    const resized = event is ::Resized;119    if (resized || window.redraw_requested) {120        window.redraw_requested = false;121        replay.redraws += 1;122        deliver(&var replay, &var window, &handler, &Event::Redraw(window.time));123    }124}125126fn deliver<S>(127    replay: &var Replay<S>,128    window: &var Window,129    handler: &|&| (&var S, &var Window, &Event),130    event: &Event131) -> bool {132    if (replay.closed) {133        return false;134    }135    window.apply(&event);136    handler(&var replay.state, &var window, &event);137    var asked = window.take_requests();138    replay.text_input = window.text_input;139    replay.secret_input = window.secret_input;140    if (asked.wake) {141        replay.wakes += 1;142    }143    if (asked.clipboard is ::Some |move text|) {144        replay.clipboard = move text;145    }146    for (asked.access.items()) |&var update| {147        replay.access.apply(move update);148    }149    for (asked.announcements.items()) |&var said| {150        replay.announcements.push(move said);151    }152    for (asked.dialogs.items()) |&var dialog| {153        replay.dialogs.push(move dialog.dialog);154    }155    if (asked.history && window.history is ::Some |&entry|) {156        replay.history.push(copy entry);157    }158    if (asked.language) {159        replay.language = copy window.language;160        replay.right_to_left = window.right_to_left;161    }162    if (window.presented) {163        window.presented = false;164        replay.frames.push(Frame {165            .size = window.frame_size,166            .pixels = Vector<u32>::from(&window.frame)167        });168    }169    if (asked.close) {170        replay.closed = true;171    }172    // whether the clipboard's text was asked for173    return asked.clipboard_wanted;174}