mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 05:37:04 +00:00
Contract stubs: save answer records_file, DocumentMap page, UpdateCheckDialog::words (S-L159, S-L162, S-L164)
This commit is contained in:
@@ -49,9 +49,9 @@ pub const OFF_SCREEN: &str = "the document point is too far off the screen";
|
||||
/// The error of a point that is not finite (NaN or infinite): refused at the request, never queued.
|
||||
pub const NOT_FINITE: &str = "the point must be finite";
|
||||
|
||||
/// The application's map from a document point to the screen point its canvas shows it at; `None` when no canvas
|
||||
/// shows a document. Applications without documents pass `&|_| None`.
|
||||
pub type DocumentMap<'a> = &'a dyn Fn(Pos2) -> Option<Pos2>;
|
||||
/// The application's map from a page and a document point on it to the screen point its canvas shows it at; `None`
|
||||
/// when no canvas shows a document. Applications without documents pass `&|_, _| None`.
|
||||
pub type DocumentMap<'a> = &'a dyn Fn(usize, Pos2) -> Option<Pos2>;
|
||||
|
||||
/// A point of a request: screen points, or document points the application maps.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
@@ -59,8 +59,8 @@ pub type DocumentMap<'a> = &'a dyn Fn(Pos2) -> Option<Pos2>;
|
||||
pub enum At {
|
||||
/// Screen points.
|
||||
Screen(Pos2),
|
||||
/// Document points.
|
||||
Document(Pos2),
|
||||
/// Document points on page `page` (0 when the request names none).
|
||||
Document(Pos2, usize),
|
||||
}
|
||||
|
||||
impl At {
|
||||
@@ -70,7 +70,7 @@ impl At {
|
||||
let x = p.number_in("x", "x", range)?.unwrap_or(0.0) as f32;
|
||||
let y = p.number_in("y", "y", range)?.unwrap_or(0.0) as f32;
|
||||
Ok(match p.choice("coordinates", "coordinates", &["screen", "document"])?.unwrap_or("screen") {
|
||||
"document" => Self::Document(pos2(x, y)),
|
||||
"document" => Self::Document(pos2(x, y), p.uint_field("page")?.map_or(0, |page| usize::try_from(page).unwrap_or(usize::MAX))),
|
||||
_ => Self::Screen(pos2(x, y)),
|
||||
})
|
||||
}
|
||||
@@ -81,9 +81,9 @@ impl At {
|
||||
match self {
|
||||
Self::Screen(at) if at.x.is_finite() && at.y.is_finite() => Ok(at),
|
||||
Self::Screen(_) => Err(NOT_FINITE.to_owned()),
|
||||
Self::Document(at) if !(at.x.is_finite() && at.y.is_finite()) => Err(NOT_FINITE.to_owned()),
|
||||
Self::Document(at) => {
|
||||
let on_screen = map(at).ok_or_else(|| NO_DOCUMENT.to_owned())?;
|
||||
Self::Document(at, _) if !(at.x.is_finite() && at.y.is_finite()) => Err(NOT_FINITE.to_owned()),
|
||||
Self::Document(at, page) => {
|
||||
let on_screen = map(page, at).ok_or_else(|| NO_DOCUMENT.to_owned())?;
|
||||
let within = |v: f32| v.is_finite() && f64::from(v.abs()) <= MAX_SCREEN_POINT;
|
||||
if within(on_screen.x) && within(on_screen.y) { Ok(on_screen) } else { Err(OFF_SCREEN.to_owned()) }
|
||||
}
|
||||
@@ -92,7 +92,7 @@ impl At {
|
||||
}
|
||||
|
||||
/// The point fields every pointer request takes.
|
||||
const POINT: [&str; 3] = ["x", "y", "coordinates"];
|
||||
const POINT: [&str; 4] = ["x", "y", "coordinates", "page"];
|
||||
/// The modifier fields ([`crate::protocol::KEY_MODIFIERS`], top-level or under `modifiers`).
|
||||
const MODIFIERS: [&str; 5] = ["modifiers", "shift", "alt", "command", "ctrl"];
|
||||
|
||||
@@ -263,7 +263,7 @@ impl Release {
|
||||
/// `{button?, key?, x?, y?, coordinates?}`, no other fields. With neither `button` nor `key` everything held is
|
||||
/// released, the modifiers too; both is ``"`button` and `key` exclude each other"``.
|
||||
pub fn from_params(params: &Value) -> Result<Self, String> {
|
||||
let p = Params(params).only(None, &["button", "key", "x", "y", "coordinates"])?;
|
||||
let p = Params(params).only(None, &["button", "key", "x", "y", "coordinates", "page"])?;
|
||||
let p = Params(&p);
|
||||
let released = match (button(&p)?, key(&p)?) {
|
||||
(Some(_), Some(_)) => return Err("`button` and `key` exclude each other".into()),
|
||||
|
||||
@@ -33,7 +33,8 @@ struct Rig {
|
||||
}
|
||||
|
||||
/// Document points are screen points doubled, as a canvas at 50 % shows them.
|
||||
fn map(at: Pos2) -> Option<Pos2> {
|
||||
// P3(platform-w6): S-L162
|
||||
fn map(_page: usize, at: Pos2) -> Option<Pos2> {
|
||||
Some(pos2(at.x / 2.0, at.y / 2.0))
|
||||
}
|
||||
|
||||
@@ -208,7 +209,8 @@ fn injected_passes_are_the_passes_that_carry_a_step() {
|
||||
fn document_points_need_a_canvas() {
|
||||
let rig = Rig::new();
|
||||
let host = rig.host();
|
||||
let none = |_: Pos2| None;
|
||||
// P3(platform-w6): S-L162
|
||||
let none = |_: usize, _: Pos2| None;
|
||||
let refused = input_method("ui.move", &json!({"x": 1, "y": 1, "coordinates": "document"}), &host, ViewportId::ROOT, &none);
|
||||
assert_eq!(refused, Some(Err(NO_DOCUMENT.to_owned())));
|
||||
assert!(host.injected_idle());
|
||||
@@ -341,9 +343,9 @@ fn ui_ime_reaches_a_focused_text_field_as_a_preedit_then_a_commit_in_order() {
|
||||
#[trial]
|
||||
fn a_point_that_is_not_finite_is_refused() {
|
||||
for bad in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
|
||||
for at in [At::Screen(pos2(bad, 1.0)), At::Screen(pos2(1.0, bad)), At::Document(pos2(bad, 1.0))] {
|
||||
assert_eq!(at.resolve(&|p| Some(p)), Err(NOT_FINITE.to_owned()), "{at:?}");
|
||||
for at in [At::Screen(pos2(bad, 1.0)), At::Screen(pos2(1.0, bad)), At::Document(pos2(bad, 1.0), 0)] {
|
||||
assert_eq!(at.resolve(&|_, p| Some(p)), Err(NOT_FINITE.to_owned()), "{at:?}");
|
||||
}
|
||||
}
|
||||
assert_eq!(At::Screen(pos2(3.0, 4.0)).resolve(&|_| None), Ok(pos2(3.0, 4.0)));
|
||||
assert_eq!(At::Screen(pos2(3.0, 4.0)).resolve(&|_, _| None), Ok(pos2(3.0, 4.0)));
|
||||
}
|
||||
|
||||
@@ -192,6 +192,16 @@ impl Picked {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the page now holds the document as the file a finished save wrote: true for a
|
||||
/// path the service wrote (native hosts, the in-memory fake), false for a browser download
|
||||
/// and for every answer that is not a written path. The app sets its session path only when
|
||||
/// this is true.
|
||||
pub fn records_file(&self) -> bool {
|
||||
// P3(platform-w4): S-L159 a hosted browser page that holds the file answers true too; the
|
||||
// variants cannot say so yet.
|
||||
matches!(self, Picked::Paths(paths) if !paths.is_empty())
|
||||
}
|
||||
|
||||
/// Every chosen path; on wasm the files' names. Empty when cancelled or unavailable.
|
||||
pub fn paths(&self) -> Vec<PathBuf> {
|
||||
match self {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
//! host's `AppInfo` and the words from [`UpdateWords`].
|
||||
|
||||
use std::hash::{Hash as _, Hasher as _};
|
||||
use std::sync::OnceLock;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
use craft_theme::variant::Tone;
|
||||
use craft_ui::buttons::Link;
|
||||
@@ -380,6 +380,8 @@ pub struct UpdateCheckDialog {
|
||||
chosen: Option<CheckChoice>,
|
||||
open: bool,
|
||||
icon: IconCell,
|
||||
/// The words the app passed; the scope's [`UpdateWords`] when none.
|
||||
words: Option<Arc<UpdateWords>>,
|
||||
}
|
||||
|
||||
impl UpdateCheckDialog {
|
||||
@@ -397,9 +399,17 @@ impl UpdateCheckDialog {
|
||||
chosen: None,
|
||||
open: true,
|
||||
icon: IconCell::new(),
|
||||
words: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Shows these words instead of the scope's [`UpdateWords`] (an app passes its translated words).
|
||||
#[must_use]
|
||||
pub fn words(mut self, words: UpdateWords) -> Self {
|
||||
self.words = Some(Arc::new(words));
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets whether the install can update itself, for an app that knows better than
|
||||
/// [`self_update::is_self_updating`] for the host's app (the dialog asks that when this isn't
|
||||
/// called). Without it Download replaces Update and Restart.
|
||||
@@ -466,7 +476,7 @@ impl UpdateCheckDialog {
|
||||
/// Shows one pass. The choices are the footer buttons' meanings, in the order of the dialog's `picked`.
|
||||
fn present(&mut self, ui: &mut Ui) -> (DialogOutput, Vec<CheckChoice>) {
|
||||
let env = Env::of(ui);
|
||||
let words = env.words::<UpdateWords>();
|
||||
let words = self.words.clone().unwrap_or_else(|| env.words::<UpdateWords>());
|
||||
let ok = env.words::<CommonWords>().ok.clone();
|
||||
let subject = Subject::of(&env);
|
||||
let self_updating = *self.self_updating.get_or_insert_with(|| subject.app.is_some_and(self_update::is_self_updating));
|
||||
|
||||
Reference in New Issue
Block a user