Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions gallery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ fn Page(
let mut picked_photos = use_signal(Selection::new);
let mut tray_chosen = use_signal(Selection::new);
let mut viewing = use_signal(|| None::<usize>);
// Where a finger or the pointer last pressed among the photos, and the
// photo opened by it and that spot, which it flies out of and back into.
let mut pressed = use_hook(|| CopyValue::new(None::<(f64, f64)>));
let mut opened_from = use_signal(|| None::<(usize, Spot)>);

let edit = vec![
Item::new(CUT, "Cut").shortcut("⌘X").into(),
Expand Down Expand Up @@ -410,7 +414,14 @@ fn Page(
said.set(format!("Opened {key}."));
}
};
let photos = photos();
let photos: Vec<Photo> = photos()
.into_iter()
.enumerate()
.map(|(i, p)| match opened_from() {
Some((at, spot)) if at == i => p.from(spot),
_ => p,
})
.collect();
let photo_tiles: Vec<Tile> = photos
.iter()
.enumerate()
Expand Down Expand Up @@ -781,16 +792,27 @@ fn Page(
Text { dim: true, "{chosen().len()} of {shown.len()} chosen." }
}
Card { title: "Photos", class: "gallery-wide",
TileGrid { label: "Photos", tiles: photo_tiles, size: 88.0, fill: true, labels: false,
selection: picked_photos(), opens: true,
onselect: move |s| picked_photos.set(s),
onactivate: move |k: String| {
viewing.set(k.trim_start_matches("photo-").parse().ok());
} }
div { class: "gallery-photos",
onpointerdown: move |e: PointerEvent| {
let p = e.client_coordinates();
pressed.set(Some((p.x, p.y)));
},
TileGrid { label: "Photos", tiles: photo_tiles, size: 88.0, fill: true, labels: false,
selection: picked_photos(), opens: true,
onselect: move |s| picked_photos.set(s),
onactivate: move |k: String| {
let i = k.trim_start_matches("photo-").parse().ok();
// Opened by a press, it flies out of where it was pressed;
// by a key, it fades in.
let at = pressed.take();
opened_from.set(i.zip(at.map(|(x, y)| Spot::point(x, y))));
viewing.set(i);
} }
}
Stack { axis: Axis::Horizontal, align: Align::Center,
Text { dim: true, "Tap one to see it whole." }
Spacer {}
Button { onpress: move |_| viewing.set(Some(0)), "View Photos…" }
Button { onpress: move |_| { opened_from.set(None); viewing.set(Some(0)); }, "View Photos…" }
}
}
Card { title: "Tray",
Expand Down
70 changes: 70 additions & 0 deletions gallery/tests/blitz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3423,3 +3423,73 @@ fn a_dismissed_toast_sinks_away_then_goes() {
"taken out"
);
}

/// Frames by the gallery's own timer until the viewer has closed: the last
/// place its photo was drawn, if it was drawn going.
fn until_closed(doc: &mut DioxusDocument) -> Option<(f64, f64, f64, f64)> {
let mut last = None;
for _ in 0..100 {
if viewer(doc).is_none() {
return last;
}
last = Some(photo_frame(doc));
std::thread::sleep(std::time::Duration::from_millis(20));
settle(doc);
}
panic!("the viewer never closed")
}

/// With motion on, a photo opened by a tap flies out of where it was
/// tapped to fill the window, and closed, by its button or a drag down,
/// flies back into it before the viewer is gone. Opened by a button, with
/// no spot, it grows a little where it rests as it fades in.
#[test]
fn a_photo_flies_out_of_where_it_was_tapped_and_back_into_it() {
let mut doc = page_moving(1200, 800);
let photo = option_named(&doc, "Noon on the hill");
let (x, y, w, h) = rect(&doc, photo);
let spot = (x + w / 2.0, y + h / 2.0);
let middle = |(x, y, w, h): (f64, f64, f64, f64)| (x + w / 2.0, y + h / 2.0);
let close_to = |a: (f64, f64), b: (f64, f64), by: f64| (a.0 - b.0).hypot(a.1 - b.1) < by;
click(&mut doc, photo);
assert!(viewer(&doc).is_some(), "open");
let first = photo_frame(&doc);
assert!(first.2 < FITS.2 / 2.0, "small, as it begins: {first:?}");
assert!(
close_to(middle(first), spot, 300.0),
"near where it was tapped: {first:?}, {spot:?}"
);
rest(&mut doc);
assert!(near(photo_frame(&doc), FITS), "{:?}", photo_frame(&doc));
let style = attr(&doc, viewer(&doc).unwrap(), "style").unwrap();
assert!(style.contains("rgba(0, 0, 0, 1)"), "{style}");

// Closed by its button, it goes back into the spot, then is gone.
let close = labelled(&doc, ".unit-viewer button", "Close");
click(&mut doc, close);
assert!(viewer(&doc).is_some(), "drawn going first");
let last = until_closed(&mut doc).expect("drawn going");
assert!(last.2 < FITS.2 / 2.0, "shrunk: {last:?}");
assert!(close_to(middle(last), spot, 150.0), "{last:?}, {spot:?}");

// Dragged down far and let go, it flies back into the spot rather
// than on down.
let photo = option_named(&doc, "Noon on the hill");
click(&mut doc, photo);
rest(&mut doc);
drag_down(&mut doc, (600.0, 300.0), 200.0, 4, 16, 0);
let last = until_closed(&mut doc).expect("drawn going");
assert!(close_to(middle(last), spot, 150.0), "{last:?}, {spot:?}");

// Opened by a button, it has no spot: it grows where it rests.
open_photos(&mut doc);
let first = photo_frame(&doc);
assert!(
close_to(middle(first), middle(FITS), 2.0) && first.2 > FITS.2 * 0.9,
"{first:?}"
);
rest(&mut doc);
assert!(near(photo_frame(&doc), FITS), "{:?}", photo_frame(&doc));
key(&mut doc, Key::Named(NamedKey::Escape));
assert!(until_closed(&mut doc).is_some(), "fading where it is");
}
Loading
Loading