diff --git a/gallery/src/lib.rs b/gallery/src/lib.rs index 439c447..0b005ad 100644 --- a/gallery/src/lib.rs +++ b/gallery/src/lib.rs @@ -328,9 +328,8 @@ fn Page( let mut picked_photos = use_signal(Selection::new); let mut tray_chosen = use_signal(Selection::new); let mut viewing = use_signal(|| None::); - // 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)>)); + // The photo opened from the grid and where its tile is, which it flies + // out of and back into. let mut opened_from = use_signal(|| None::<(usize, Spot)>); let edit = vec![ @@ -792,23 +791,14 @@ fn Page( Text { dim: true, "{chosen().len()} of {shown.len()} chosen." } } Card { title: "Photos", class: "gallery-wide", - 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); - } } - } + 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), + onopen: move |(k, spot): (String, Option)| { + let i = k.trim_start_matches("photo-").parse().ok(); + opened_from.set(i.zip(spot)); + viewing.set(i); + } } Stack { axis: Axis::Horizontal, align: Align::Center, Text { dim: true, "Tap one to see it whole." } Spacer {} diff --git a/gallery/tests/blitz.rs b/gallery/tests/blitz.rs index fc8da06..ebb3348 100644 --- a/gallery/tests/blitz.rs +++ b/gallery/tests/blitz.rs @@ -3439,47 +3439,65 @@ fn until_closed(doc: &mut DioxusDocument) -> Option<(f64, f64, f64, f64)> { 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. +/// The picture of the photo tile named `name`: its box in the window. +fn tile_face(doc: &DioxusDocument, name: &str) -> (f64, f64, f64, f64) { + let tile = option_named(doc, name); + let inner = doc.inner.borrow(); + let faces = inner.query_selector_all(".unit-tile-face").unwrap(); + let face = faces + .into_iter() + .find(|&f| inner.get_node(f).unwrap().parent == Some(tile)) + .expect("a tile's picture"); + drop(inner); + rect(doc, face) +} + +/// With motion on, a photo opened from its tile, by a tap or a key, flies +/// out of the tile's picture 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() { +fn a_photo_flies_out_of_its_tile_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 face = tile_face(&doc, "Noon on the hill"); 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; + let like_face = |f: (f64, f64, f64, f64)| { + close_to(middle(f), middle(face), 12.0) && f.2 < face.2 * 1.5 && f.3 < face.3 * 1.5 + }; + let photo = option_named(&doc, "Noon on the hill"); click(&mut doc, photo); + settle(&mut doc); 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:?}" + like_face(first), + "the tile's picture, as it begins: {first:?}, {face:?}" ); 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. + // Closed by its button, it goes back into the tile, 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:?}"); + assert!(like_face(last), "{last:?}, {face:?}"); - // 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); + // Opened by Return, on the tile the keys are on, it flies out of that + // tile as well; dragged down far and let go, it flies back into it + // rather than on down. + key(&mut doc, Key::Named(NamedKey::Enter)); + settle(&mut doc); + assert!(viewer(&doc).is_some(), "Return opens it"); + let first = photo_frame(&doc); + assert!(like_face(first), "{first:?}, {face:?}"); 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:?}"); + assert!(like_face(last), "{last:?}, {face:?}"); // Opened by a button, it has no spot: it grows where it rests. open_photos(&mut doc); diff --git a/src/components/collection.rs b/src/components/collection.rs index 3ccc159..058d1ec 100644 --- a/src/components/collection.rs +++ b/src/components/collection.rs @@ -22,6 +22,8 @@ //! their `size`, and reads how many fit a row from its own width as the //! renderer reports it. +use std::rc::Rc; + use dioxus::html::Modifiers; use dioxus::prelude::*; @@ -31,6 +33,7 @@ use crate::menu::Entry; use crate::selection::{Did, Keyed, Pick, Selection, Shape}; use crate::theme::Density; use crate::unit::{Kind, Unit, use_focus, use_unit}; +use crate::zoom::Spot; /// How far apart a grid's tiles are, and its padding, in CSS pixels. const GAP: f64 = 8.0; @@ -135,6 +138,11 @@ struct Chooser { selection: Selection, onselect: EventHandler, onactivate: Option>, + /// In place of `onactivate`: given the key and where its picture is in + /// the window, a grid's `onopen`. + onopen: Option)>>, + /// Each tile's picture, as it was mounted: where it is, for `onopen`. + faces: Option>, multiple: bool, toggles: bool, /// A press alone opens what it lands on, as well as choosing it. @@ -175,6 +183,25 @@ impl Chooser { } fn activate(&self, key: &str) { + if let Some(open) = self.onopen { + let key = key.to_string(); + let face = self.faces.and_then(|f| f.peek().get(&key)); + let Some(face) = face else { + open.call((key, None)); + return; + }; + // The renderer says where the picture is when asked, which it + // answers after this turn. + dioxus::core::spawn_forever(async move { + let spot = face + .get_client_rect() + .await + .ok() + .map(|r| Spot::rect(r.origin.x, r.origin.y, r.size.width, r.size.height)); + open.call((key, spot)); + }); + return; + } if let Some(a) = self.onactivate { a.call(key.to_string()); } @@ -246,6 +273,31 @@ struct Sightings { seen: Vec, } +/// Each tile's picture as the renderer mounted it, by its key: what a grid +/// asks where a tile is on the screen as it opens it (`onopen`). +#[derive(Default)] +struct Faces(Vec<(String, Rc)>); + +impl Faces { + fn get(&self, key: &str) -> Option> { + self.0 + .iter() + .find(|(k, _)| k == key) + .map(|(_, m)| m.clone()) + } + + fn mounted(&mut self, key: &str, face: Rc) { + self.0.retain(|(k, _)| k != key); + self.0.push((key.to_string(), face)); + } + + /// The tile with `face` is gone; a new one of the same key may have + /// mounted already, and stays. + fn dropped(&mut self, face: &Rc) { + self.0.retain(|(_, m)| !Rc::ptr_eq(m, face)); + } +} + /// One tile the pointer found: its section and its place in it, and where /// it starts in the page and in the window. #[derive(Clone, Copy)] @@ -316,6 +368,11 @@ impl Sightings { /// /// `onselect` is given each new selection, `onactivate` the key of a tile /// opened, `onaction` the key of a tile whose corner button was pressed. +/// `onopen`, in place of `onactivate`, is given the key of a tile opened +/// with where its picture is in the window, by a press or a key alike: the +/// [`Spot`] a photo viewer flies it out of ([`crate::Photo::from`]). It is +/// `None` where the renderer cannot say, and is given a moment after the +/// press, once the renderer has said. /// `multiple: false` keeps one tile chosen at most; `toggles` makes every /// press put a tile in or out; `opens` makes a press alone (a click, a tap) /// open the tile, as a photo library's does, where a folder's opens by a @@ -332,6 +389,7 @@ pub fn TileGrid( selection: Selection, onselect: EventHandler, onactivate: Option>, + onopen: Option)>>, onaction: Option>, label: Option, #[props(default = true)] multiple: bool, @@ -359,6 +417,7 @@ pub fn TileGrid( // How many tiles a row holds, where the renderer reports the width. let mut measured = use_signal(|| None::); let sightings = use_hook(|| CopyValue::new(Sightings::default())); + let faces = use_hook(|| CopyValue::new(Faces::default())); let order: Vec = tiles.iter().map(|t| t.key.clone()).collect(); if sightings.peek().order != order { @@ -388,6 +447,8 @@ pub fn TileGrid( selection: selection.clone(), onselect, onactivate, + onopen, + faces: Some(faces), multiple, toggles, opens, @@ -485,6 +546,7 @@ pub fn TileGrid( fill, text, sightings, + faces, chooser: chooser.clone(), onaction, } @@ -507,11 +569,19 @@ fn TileView( fill: bool, text: f64, sightings: CopyValue, + faces: CopyValue, chooser: Chooser, onaction: Option>, ) -> Element { let unit = use_unit(); let key = tile.key.clone(); + // Its picture, as mounted: where it is when it is opened. + let face = use_hook(|| CopyValue::new(None::>)); + use_drop(move || { + if let Some(m) = face.peek().clone() { + faces.clone().write().dropped(&m); + } + }); let held = { let (chooser, key) = (chooser.clone(), key.clone()); crate::components::hold::use_hold(move |at| chooser.menu(unit, &key, at)) @@ -540,7 +610,7 @@ fn TileView( }; // Its own size, or its cell's where the tiles stretch, the picture // square either way. - let (style, face) = if fill { + let (style, face_style) = if fill { (String::new(), "aspect-ratio: 1 / 1".to_string()) } else { ( @@ -604,7 +674,15 @@ fn TileView( } } }, - div { class: "unit-tile-face", style: "{face}", + div { class: "unit-tile-face", style: "{face_style}", + onmounted: { + let key = key.clone(); + move |e: MountedEvent| { + let m = e.data(); + faces.clone().write().mounted(&key, m.clone()); + face.clone().set(Some(m)); + } + }, if let Some(src) = &tile.image { img { class: "unit-tile-image", src: "{src}", alt: "", "aria-hidden": "true" } } else { @@ -766,6 +844,8 @@ pub fn Table( selection: selection.clone(), onselect, onactivate, + onopen: None, + faces: None, multiple, toggles, opens: false,