mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 05:37:04 +00:00
Framework follow-ups F-24, F-25, F-28, F-33 (app red 6): the title bar's title takes no width of its own, so a fill tab strip gets the free width; relative-name drops keep their bytes (Frames::drop_with_bytes); unfocused page keys go to the window's document canvas; ScriptedFiles::written
This commit is contained in:
@@ -359,6 +359,8 @@ struct ScriptState {
|
||||
files: HashMap<PathBuf, Vec<u8>>,
|
||||
/// Paths a write to is refused ([`ScriptedFiles::failing_writes`]).
|
||||
refused: HashSet<PathBuf>,
|
||||
/// The path of every write that took, in order ([`ScriptedFiles::written`]).
|
||||
written: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
impl ScriptedFiles {
|
||||
@@ -442,6 +444,12 @@ impl ScriptedFiles {
|
||||
self.state().files.get(path).cloned()
|
||||
}
|
||||
|
||||
/// The path of every write so far that took, in the order of the writes: a path written twice is listed twice. A
|
||||
/// refused write and a file put there with [`with_file`](ScriptedFiles::with_file) are not listed.
|
||||
pub fn written(&self) -> Vec<PathBuf> {
|
||||
self.state().written.clone()
|
||||
}
|
||||
|
||||
fn state(&self) -> std::sync::MutexGuard<'_, ScriptState> {
|
||||
self.state.lock().unwrap_or_else(|e| e.into_inner())
|
||||
}
|
||||
@@ -496,6 +504,7 @@ impl Files for ScriptedFiles {
|
||||
return Err(FileError::PermissionDenied(path.to_owned()));
|
||||
}
|
||||
state.files.insert(path.to_owned(), bytes.to_vec());
|
||||
state.written.push(path.to_owned());
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
|
||||
@@ -276,6 +276,18 @@ mod tests {
|
||||
assert_eq!(files.file(std::path::Path::new("/docs/b.pdf")).as_deref(), Some(&b"new"[..]));
|
||||
}
|
||||
|
||||
/// The written paths list each write that took, in order, and nothing else: a list that took in a refused write or a
|
||||
/// file the test put there would let "nothing was written outside the folder" fail, or pass, on the wrong paths.
|
||||
#[test]
|
||||
fn written_lists_the_writes_that_took_in_order() {
|
||||
let files = ScriptedFiles::new().with_file("/docs/kept.pdf", b"old".to_vec()).failing_writes("/docs/refused.pdf");
|
||||
for path in ["/out/b.pdf", "/docs/refused.pdf", "/out/a.pdf", "/out/b.pdf"] {
|
||||
let _ = files.write(std::path::Path::new(path), b"new", WriteIntent::Save);
|
||||
}
|
||||
let written: Vec<std::path::PathBuf> = ["/out/b.pdf", "/out/a.pdf", "/out/b.pdf"].map(Into::into).into();
|
||||
assert_eq!(files.written(), written);
|
||||
}
|
||||
|
||||
#[cfg(feature = "ui")]
|
||||
#[test]
|
||||
fn installed_host_survives_a_memory_reset() {
|
||||
|
||||
@@ -305,6 +305,30 @@ fn page_keys_turn_and_go_to_the_ends_in_a_view_that_is_not_continuous() {
|
||||
assert_eq!(state.borrow().current, 0, "Home goes to the first page");
|
||||
}
|
||||
|
||||
/// With nothing focused the window's document canvas is its key target: the page keys and the arrows no tool takes
|
||||
/// page the view, as they do while it has the focus. They used to reach only the window, where no view read them.
|
||||
#[trial]
|
||||
fn page_keys_and_arrows_page_the_view_with_nothing_focused() {
|
||||
let (mut fixture, state, _) = layout_rig(
|
||||
5,
|
||||
state_with(|s| {
|
||||
s.continuous = false;
|
||||
s.fit = Fit::Page;
|
||||
}),
|
||||
);
|
||||
assert_eq!(fixture.focused_stop(), None, "nothing has the focus");
|
||||
fixture.key(Key::PageDown, Modifiers::NONE);
|
||||
fixture.idle(1);
|
||||
assert_eq!(state.borrow().current, 1, "Page Down turns one page");
|
||||
fixture.key(Key::ArrowRight, Modifiers::NONE);
|
||||
fixture.idle(1);
|
||||
assert_eq!(state.borrow().current, 2, "Right turns one page");
|
||||
fixture.key(Key::End, Modifiers::NONE);
|
||||
fixture.idle(1);
|
||||
assert_eq!(state.borrow().current, 4, "End goes to the last page");
|
||||
assert_eq!(fixture.focused_stop(), None, "the keys leave the focus where it was");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn arrows_go_to_the_next_page_and_scroll_a_line_in_a_continuous_view() {
|
||||
// Right, then Down, in a view whose key line is `key_line`: the page reached and how far Down scrolled.
|
||||
|
||||
@@ -559,7 +559,15 @@ impl<'a> Canvas<'a> {
|
||||
}
|
||||
}
|
||||
if self.keys_to_body {
|
||||
keys.nudge = cx.intents().take_nudge();
|
||||
// With nothing focused the window's arrows are the canvas's too: no tool here takes them.
|
||||
keys.nudge = cx.intents().take_nudge().or_else(|| {
|
||||
let mut nudge = None;
|
||||
crate::runtime::keys::with_window_intents(&ctx, false, |window| {
|
||||
nudge = window.take_nudge();
|
||||
nudge.is_some()
|
||||
});
|
||||
nudge
|
||||
});
|
||||
}
|
||||
// A pan or a scroll takes over from an ease, here or in a tool.
|
||||
let mut steer = motion.steer.take();
|
||||
|
||||
@@ -99,13 +99,19 @@ pub(crate) fn seen(ctx: &Context) -> Seen {
|
||||
Seen { hovered, dropped: at_hand, reading }
|
||||
}
|
||||
|
||||
/// `file` as the drop delivers it at once: its path, or the bytes of a pathless file. `None` for a pathless file whose
|
||||
/// bytes can be read only asynchronously (the web).
|
||||
/// `file` as the drop delivers it at once: its path when that is absolute, or else its bytes. A browser hands a file
|
||||
/// over by a relative name that locates nothing, with its bytes. `None` for a file without an absolute path whose bytes
|
||||
/// can be read only asynchronously (the web).
|
||||
fn at_once(file: &DroppedFileHandle) -> Option<OsFile> {
|
||||
let path = file.path();
|
||||
let usable = !path.as_os_str().is_empty();
|
||||
let bytes = if usable { None } else { bytes_now(file)? };
|
||||
Some(OsFile { name: name_of(path), path: usable.then(|| path.to_path_buf()), mime: String::new(), bytes })
|
||||
let name = name_of(path);
|
||||
if path.is_absolute() {
|
||||
return Some(OsFile { name, path: Some(path.to_path_buf()), mime: String::new(), bytes: None });
|
||||
}
|
||||
let bytes = bytes_now(file)?;
|
||||
// A relative path whose bytes cannot be read is all the drop has: it is passed on, and opening it reports why it fails.
|
||||
let path = (bytes.is_none() && !path.as_os_str().is_empty()).then(|| path.to_path_buf());
|
||||
Some(OsFile { name, path, mime: String::new(), bytes })
|
||||
}
|
||||
|
||||
/// The bytes of a pathless dropped file, read now; none inside when the read fails.
|
||||
@@ -162,3 +168,28 @@ impl egui::DroppedFile for DroppedPath {
|
||||
Box::pin(std::future::ready(Err("there is no file system on the web".to_owned())))
|
||||
}
|
||||
}
|
||||
|
||||
/// A dropped file the test fixture hands over by a relative name with its bytes at hand, as a browser does.
|
||||
#[cfg(feature = "testing")]
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct DroppedBytes {
|
||||
pub(crate) name: PathBuf,
|
||||
pub(crate) bytes: Vec<u8>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "testing")]
|
||||
impl egui::DroppedFile for DroppedBytes {
|
||||
fn path(&self) -> &Path {
|
||||
&self.name
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn bytes(&self) -> Result<Vec<u8>, String> {
|
||||
Ok(self.bytes.clone())
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
fn bytes_async(&self) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Vec<u8>, String>> + '_>> {
|
||||
Box::pin(std::future::ready(Ok(self.bytes.clone())))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,12 +97,13 @@ pub struct ImeSeen {
|
||||
|
||||
/// The files the OS hovers over or drops on a frame's window, by name; a hovered file also has the media type the OS gave.
|
||||
/// `read` are pathless files dropped in the frame whose bytes the page reads after it, by name with the bytes the read
|
||||
/// gives.
|
||||
/// gives. `given` are files dropped in the frame by a relative name with their bytes at hand, as a browser hands them over.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub(crate) struct OsFiles {
|
||||
pub hovered: Vec<(String, String)>,
|
||||
pub dropped: Vec<String>,
|
||||
pub read: Vec<(String, Vec<u8>)>,
|
||||
pub given: Vec<(String, Vec<u8>)>,
|
||||
}
|
||||
|
||||
/// The viewport facts a frame reports: a window-focus event also sets the root viewport's focus fact, as a host
|
||||
@@ -184,8 +185,14 @@ pub(crate) fn run_frame(
|
||||
) -> FrameResult {
|
||||
let screen = Rect::from_min_size(Pos2::ZERO, screen);
|
||||
let hovered_files = files.hovered.iter().map(|(name, mime)| egui::HoveredFile { path: Some(name.into()), mime: mime.clone() }).collect();
|
||||
let dropped_files =
|
||||
files.dropped.iter().map(|name| Arc::new(super::files::DroppedPath(name.into())) as egui::DroppedFileHandle).collect();
|
||||
let dropped_files = files
|
||||
.dropped
|
||||
.iter()
|
||||
.map(|name| Arc::new(super::files::DroppedPath(name.into())) as egui::DroppedFileHandle)
|
||||
.chain(files.given.into_iter().map(|(name, bytes)| {
|
||||
Arc::new(super::files::DroppedBytes { name: name.into(), bytes }) as egui::DroppedFileHandle
|
||||
}))
|
||||
.collect();
|
||||
// The web drop of a pathless file: its reads open in the pass of the drop, and their bytes arrive after it.
|
||||
if !files.read.is_empty() {
|
||||
super::files::open_reads(ctx, viewport, files.read.len());
|
||||
|
||||
@@ -211,8 +211,8 @@ pub(crate) fn register_stop(ctx: &Context, stop: Stop) {
|
||||
/// nudges, Escape as its `Intent::Cancel`). `keys` is `KeyUse::CANVAS`, or a variant such as
|
||||
/// `KeyUse::CANVAS.with_keep(KeepKeys::Tab).with_edits(..)` while the canvas edits text in place and Tab and the
|
||||
/// clipboard are its own. With nothing focused the canvas is the key target: a bare Tab it keeps, and the clipboard
|
||||
/// events and edit chords of its edit set, stay in egui's input for the app, and nothing toggles or fires with them.
|
||||
/// With the canvas focused, Tab it keeps stays in egui too, and its edit set arrives as `Intent::Edit`
|
||||
/// events and edit chords of its edit set, stay in egui's input for the app, and nothing toggles or fires with them;
|
||||
/// Page Up and Down, Home and End arrive as its `Intent::Move`, as while it has the focus. With the canvas focused, Tab it keeps stays in egui too, and its edit set arrives as `Intent::Edit`
|
||||
/// (`keys::intents_of`), as for any focused stop. Call it each pass the canvas shows.
|
||||
pub fn host_canvas(ctx: &Context, id: Id, keys: KeyUse) {
|
||||
register_stop(ctx, Stop::new(id, keys.with_chrome_key(true)).host());
|
||||
@@ -224,6 +224,12 @@ pub(crate) fn canvas_keys(ctx: &Context, viewport: ViewportId) -> Option<KeyUse>
|
||||
with_viewport(ctx, viewport, |state| state.stops.iter().find(|rec| rec.stop.keys.chrome_key).map(|rec| rec.stop.keys))
|
||||
}
|
||||
|
||||
/// The document canvas the running viewport registered in its last pass (`KeyUse::chrome_key`): the window's key
|
||||
/// target while nothing has the focus. Read by the begin hook, after it rotated the registrations.
|
||||
pub(crate) fn document_canvas(ctx: &Context) -> Option<StopRec> {
|
||||
with(ctx, |state| state.last_stops.iter().find(|rec| rec.stop.keys.chrome_key).cloned())
|
||||
}
|
||||
|
||||
/// Whether `rec`, the focused stop, is a canvas control's: a non-text member of the document canvas's arena, as the
|
||||
/// end hook recorded the focus (`FocusedRecord::root`). Its leftover Escape is the window's, as the canvas's own is.
|
||||
pub(crate) fn is_canvas_member(ctx: &Context, rec: &StopRec) -> bool {
|
||||
|
||||
@@ -84,7 +84,9 @@ pub(crate) fn begin(ctx: &Context, press_captured: bool) -> EscapeRouting {
|
||||
} else {
|
||||
// With nothing focused, a plain Enter submits to the window, as an unfocused Escape cancels: a tool's pending
|
||||
// edit or a dialog's default button takes it. The arrows nudge the window's canvas content (a tool or the
|
||||
// session control on a canvas), unless a modal layer is open.
|
||||
// session control on a canvas), unless a modal layer is open. The window's document canvas is its key target
|
||||
// (`focus::host_canvas`): Page Up and Down, Home and End page it as they do while it has the focus.
|
||||
let canvas = if modal { None } else { focus::document_canvas(ctx) };
|
||||
for (press, taken) in batch.presses.iter().zip(&mut consumed).filter(|(press, _)| !press.canvas) {
|
||||
if press.key == Key::Enter && press.mods.is_none() && !press.repeat {
|
||||
push(&mut routed, None, Intent::Submit, false);
|
||||
@@ -92,6 +94,12 @@ pub(crate) fn begin(ctx: &Context, press_captured: bool) -> EscapeRouting {
|
||||
} else if !modal && let Some(nudge) = nudge_of(press.key, press.mods) {
|
||||
routed.entry(None).or_default().nudge = Some(nudge);
|
||||
*taken = Taken::Yes;
|
||||
} else if let Some(canvas) = &canvas
|
||||
&& let Some(Intent::Move(mv @ (NavMove::PageUp | NavMove::PageDown | NavMove::Start | NavMove::End), mods)) =
|
||||
keys::intent(os, press.key, press.mods, canvas.stop.role)
|
||||
{
|
||||
push(&mut routed, Some(canvas.stop.id), Intent::Move(mv, mods), false);
|
||||
*taken = Taken::Yes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,6 +251,13 @@ impl Frames {
|
||||
self.files.read.push((name.to_owned(), bytes.to_vec()));
|
||||
}
|
||||
|
||||
/// The hovering files drop on the window in the next frame as a browser on the desktop hands a file over: by the
|
||||
/// relative name `name`, which locates no file, with its `bytes` at hand.
|
||||
pub fn drop_with_bytes(&mut self, name: &str, bytes: &[u8]) {
|
||||
self.files.hovered.clear();
|
||||
self.files.given.push((name.to_owned(), bytes.to_vec()));
|
||||
}
|
||||
|
||||
/// Keeps the clock still: a frame no longer moves it on, so time passes only where a test calls
|
||||
/// [`advance`](Self::advance). Held-pointer repeats, hover delays and animations then wait for the test.
|
||||
pub fn still_clock(&mut self) {
|
||||
@@ -293,6 +300,7 @@ impl Frames {
|
||||
hovered: self.files.hovered.clone(),
|
||||
dropped: std::mem::take(&mut self.files.dropped),
|
||||
read: std::mem::take(&mut self.files.read),
|
||||
given: std::mem::take(&mut self.files.given),
|
||||
};
|
||||
let mut result = fixture::run_frame(&self.ctx, self.time, self.screen, self.pixels_per_point, viewport, events, files, ui);
|
||||
match &mut self.paint {
|
||||
|
||||
@@ -359,6 +359,31 @@ fn a_pathless_drop_is_delivered_once_its_bytes_are_read() {
|
||||
assert!(drag::active::<OsFiles>(&frames.ctx).is_none(), "the drop ends the drag");
|
||||
}
|
||||
|
||||
/// A browser hands a dropped file over by a relative name, which locates no file, with its bytes. The drop used to keep
|
||||
/// only that name as a path, so nothing could be opened; it lands with the bytes in the frame of the drop.
|
||||
#[trial]
|
||||
fn a_drop_by_relative_name_lands_with_its_bytes() {
|
||||
let landed = RefCell::new(Vec::new());
|
||||
let mut frames = Frames::new();
|
||||
let pass = std::cell::Cell::new(0);
|
||||
let mut show = |ui: &mut Ui| {
|
||||
ReadDrop { common: Common::new(), landed: &landed, pass: pass.get() }.id_salt("window").show_at(ui, LIST);
|
||||
pass.set(pass.get() + 1);
|
||||
};
|
||||
frames.idle(2, &mut show);
|
||||
frames.hover_files(&["scan.pdf"]);
|
||||
frames.idle(2, &mut show);
|
||||
|
||||
frames.drop_with_bytes("scan.pdf", b"%PDF-1.7");
|
||||
pass.set(0);
|
||||
frames.idle(1, &mut show);
|
||||
assert_eq!(
|
||||
*landed.borrow(),
|
||||
vec![(0, vec![("scan.pdf".to_owned(), Some(b"%PDF-1.7".to_vec()))])],
|
||||
"the file lands once, with its bytes, in the drop frame"
|
||||
);
|
||||
}
|
||||
|
||||
/// Where the window's inner rect sits on the desktop in the desktop-pointer trial.
|
||||
const WINDOW_AT: Pos2 = Pos2::new(100.0, 50.0);
|
||||
|
||||
|
||||
@@ -79,7 +79,9 @@ impl Arrangement for Gap {
|
||||
let room_start = self.body_width - room.width();
|
||||
let (from, width) = Title::span(self.centre - room_start - self.margin, free, size.x);
|
||||
out.push(Rect::from_min_size(room.min + vec2(self.margin + from, (room.height() - size.y) / 2.0), vec2(width, size.y)));
|
||||
vec2(room.width(), size.y)
|
||||
// The title takes no width of its own: it lies in what the leading content leaves, so a member that fills the
|
||||
// row (a document tab strip) takes the free width and the title the rest, if any.
|
||||
vec2(0.0, size.y)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -128,3 +128,42 @@ fn the_window_title_is_sent_once_per_change() {
|
||||
});
|
||||
assert!(titles(&out).is_empty(), "an embedded shell sends no title");
|
||||
}
|
||||
|
||||
/// The leading content of a bar is a row as wide as the bar: a document tab strip beside the Home and Open buttons
|
||||
/// takes the width they and the title leave, and its tabs show and are in the tree. The strip used to be laid out at
|
||||
/// its minimum there, with no tab drawn or reachable.
|
||||
#[trial]
|
||||
fn a_document_tab_strip_in_the_bar_takes_the_free_width() {
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::Show as _;
|
||||
use craft_ui::tabs::{DocumentTab, DocumentTabs};
|
||||
use craft_ui::testing::Fixture;
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
let layout = craft_window::Layout { left: vec![], right: vec![Caption::Minimize, Caption::Maximize, Caption::Close] };
|
||||
let frame = Frame::Integrated(Captions { family: CaptionFamily::Windows, layout });
|
||||
let tabs = [DocumentTab::new(0_usize, "One.pdf"), DocumentTab::new(1, "Two.pdf"), DocumentTab::new(2, "Three.pdf")];
|
||||
let _ = TitleBar::new().frame(frame).show_titled(
|
||||
ui,
|
||||
Title::new("One.pdf"),
|
||||
|ui, _| {
|
||||
Button::new("Home").show(ui);
|
||||
DocumentTabs::new("Documents", &tabs).reorderable(true).selected(0).show(ui);
|
||||
Button::new("Open").show(ui);
|
||||
},
|
||||
|ui, _| {
|
||||
Button::new("Settings").show(ui);
|
||||
},
|
||||
);
|
||||
});
|
||||
fixture.room(vec2(1400.0, 300.0)).settle();
|
||||
let home = fixture.by_label("Home").expect("the Home button").rect;
|
||||
let open = fixture.by_label("Open").expect("the Open button").rect;
|
||||
let tabs: Vec<Rect> = fixture.by_role(Role::TAB).into_iter().map(|tab| tab.rect).collect();
|
||||
assert_eq!(tabs.len(), 3, "every tab is in the tree: {tabs:?}");
|
||||
for tab in &tabs {
|
||||
assert!(tab.width() > 0.0 && tab.left() >= home.right() && tab.right() <= open.left(), "a tab shows between Home and Open: {tab:?}");
|
||||
}
|
||||
let strip = fixture.by_role(Role::TAB_LIST).first().expect("the strip's node").rect;
|
||||
let settings = fixture.by_label("Settings").expect("the Settings button").rect;
|
||||
assert!(strip.width() > (settings.left() - home.right()) / 2.0, "the strip takes the free width: {strip:?} up to {settings:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user