mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-17 23:58:24 +00:00
Merge pull request #2427 from storytold/fix/page-keeps-its-view
M8: going back to a page shows it where it was left, not fitted again
This commit is contained in:
6 files changed
+65
-9
No files matched your search
@@ -53,9 +53,8 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
return Ok(json!({"id": id, "divider": true}));
|
||||
}
|
||||
let ds = s.ds_mut()?;
|
||||
ds.page = id;
|
||||
ds.open_page(id);
|
||||
ds.selection.clear();
|
||||
ds.zoom_to_fit();
|
||||
Ok(json!({"id": id}))
|
||||
}),
|
||||
spec!("page.select", "Go to page", "{id | index}", false, |s, p| {
|
||||
@@ -74,10 +73,9 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
return Err(bad("page.select", "a divider isn't a page to open"));
|
||||
}
|
||||
let ds = s.ds_mut()?;
|
||||
ds.page = id;
|
||||
ds.open_page(id);
|
||||
ds.selection.clear();
|
||||
ds.text_edit = None;
|
||||
ds.zoom_to_fit();
|
||||
ok()
|
||||
}),
|
||||
spec!("page.rename", "Rename page", "{id?, name} (an empty page named with a leading hyphen becomes a divider)", true, |s, p| {
|
||||
|
||||
@@ -187,9 +187,8 @@ fn page_step(s: &mut Session, dir: i64) -> Result<Value> {
|
||||
}
|
||||
};
|
||||
let ds = s.ds_mut()?;
|
||||
ds.page = pg;
|
||||
ds.open_page(pg);
|
||||
ds.selection.clear();
|
||||
ds.zoom_to_fit();
|
||||
Ok(json!({"page": pg}))
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use super::view_state::ViewState;
|
||||
use super::{Arc, BTreeSet, Deserialize, Document, NodeId, Serialize, cmd, components, file_access, guides, library, rotation_origin, vectoredit};
|
||||
use super::{
|
||||
Arc, BTreeMap, BTreeSet, Deserialize, Document, NodeId, Serialize, cmd, components, file_access, guides, library, rotation_origin, vectoredit,
|
||||
};
|
||||
|
||||
/// One undo step.
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -114,6 +116,9 @@ pub struct DocState {
|
||||
/// The file on disk as this tab last read or wrote it: a save refuses to replace what another
|
||||
/// app (figcraft-cli, say) wrote to it since (#1330).
|
||||
pub(crate) on_disk: Option<library::Stamp>,
|
||||
/// Where each page's view was left in this session (its centre and zoom; see
|
||||
/// [`Self::open_page`]; not saved).
|
||||
page_views: BTreeMap<NodeId, (f64, f64, f64)>,
|
||||
}
|
||||
|
||||
/// A version shown on the canvas in place of the document: read-only until it's restored or the
|
||||
@@ -178,6 +183,7 @@ impl DocState {
|
||||
last_duplicate: None,
|
||||
images_left_in_file: None,
|
||||
on_disk: None,
|
||||
page_views: BTreeMap::new(),
|
||||
};
|
||||
st.zoom_to_fit();
|
||||
st
|
||||
@@ -311,6 +317,21 @@ impl DocState {
|
||||
}
|
||||
|
||||
/// Fit the current page's content (or centre on the origin when empty).
|
||||
/// Show `page` at the view it was left at in this session, or one that fits its content the
|
||||
/// first time; the page shown till now keeps its view for when it comes back. The page shown
|
||||
/// already keeps its view.
|
||||
pub fn open_page(&mut self, page: NodeId) {
|
||||
if page == self.page {
|
||||
return;
|
||||
}
|
||||
self.page_views.insert(self.page, (self.view.cx, self.view.cy, self.view.zoom));
|
||||
self.page = page;
|
||||
match self.page_views.get(&page) {
|
||||
Some(&(cx, cy, zoom)) => (self.view.cx, self.view.cy, self.view.zoom) = (cx, cy, zoom),
|
||||
None => self.zoom_to_fit(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn zoom_to_fit(&mut self) {
|
||||
match self.doc.page_content_bounds(self.page) {
|
||||
Some(b) => self.view.fit(b, 1.0),
|
||||
|
||||
@@ -668,6 +668,8 @@ mod widget_shapes_tests;
|
||||
#[cfg(test)]
|
||||
mod widgets_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod page_view_tests;
|
||||
#[cfg(test)]
|
||||
mod selection_reorder_tests;
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
//! Each page keeps its view in a session: going back to a page shows it where it was left, not
|
||||
//! fitted again (a new page is still fitted); going to the page shown changes nothing.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::Session;
|
||||
|
||||
fn view(s: &mut Session) -> (f64, f64, f64) {
|
||||
let v: Value = s.execute("view.get", &json!({})).unwrap();
|
||||
(v["cx"].as_f64().unwrap(), v["cy"].as_f64().unwrap(), v["zoom"].as_f64().unwrap())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn going_back_to_a_page_shows_it_where_it_was_left() {
|
||||
let mut s = Session::new();
|
||||
s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 400, "height": 300})).unwrap();
|
||||
let first = s.page().unwrap();
|
||||
s.execute("view.set", &json!({"zoom": 2.5, "cx": 1000, "cy": 500})).unwrap();
|
||||
let second = crate::params::id_of(&s.execute("page.add", &json!({})).unwrap()["id"]).unwrap();
|
||||
assert_ne!(view(&mut s), (1000.0, 500.0, 2.5), "a new page is fitted, not shown at the last page's view");
|
||||
s.execute("shape.ellipse", &json!({"x": 5000, "y": 5000, "width": 100, "height": 100})).unwrap();
|
||||
s.execute("view.set", &json!({"zoom": 0.5, "cx": -200, "cy": 40})).unwrap();
|
||||
|
||||
s.execute("page.select", &json!({"id": first})).unwrap();
|
||||
assert_eq!(view(&mut s), (1000.0, 500.0, 2.5), "the first page as it was left");
|
||||
s.execute("view.nextPage", &json!({})).unwrap();
|
||||
assert_eq!(s.page().unwrap(), second);
|
||||
assert_eq!(view(&mut s), (-200.0, 40.0, 0.5), "the second page as it was left");
|
||||
// The page shown: nothing moves.
|
||||
s.execute("view.set", &json!({"zoom": 3.0, "cx": 7, "cy": 8})).unwrap();
|
||||
s.execute("page.select", &json!({"id": second})).unwrap();
|
||||
assert_eq!(view(&mut s), (7.0, 8.0, 3.0));
|
||||
s.execute("view.prevPage", &json!({})).unwrap();
|
||||
assert_eq!(view(&mut s), (1000.0, 500.0, 2.5));
|
||||
}
|
||||
Reference in new issue
Block a user