Skip to content

Commit 9d3d6c8

Browse files
feat: edit mode, page-wise follow, a chart with no lead-in lines up with the session (#132)
* feat: edit mode, page-wise follow, a chart with no lead-in lines up with the session Sprint branch (2026-11-18): builds with the side-by-side checkouts and cargo --config overrides of the studio-stack folder. Not for review yet; the mergeable PRs are cut from it later. (cherry picked from commit 81a5fb6) * chore(deps): session at its main (f186f34b), the revision Studio pins; formatting --------- Co-authored-by: fts-forge[bot] <345159799+fts-forge[bot]@users.noreply.github.com>
1 parent 7f3aaa1 commit 9d3d6c8

7 files changed

Lines changed: 170 additions & 8 deletions

File tree

‎Cargo.lock‎

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ architect = { git = "https://github.com/FastTrackStudios/architect", tag = "v0.1
141141
crdt = { git = "https://github.com/FastTrackStudios/architect", tag = "v0.14.0" }
142142
# The session primitive (FastTrackStudios/session): a chart is Keyflow's
143143
# data in a session (features/session). By rev until it is tagged.
144-
session = { git = "https://github.com/FastTrackStudios/session", rev = "a8ec37a454c767c0558973412c4d321746066d02", default-features = false }
144+
session = { git = "https://github.com/FastTrackStudios/session", rev = "f186f34b8451e3a1543ac78a2c99d1ab02c1b352", default-features = false }
145145
uuid = { version = "1", features = ["v4"] }
146146
tempfile = "3"
147147

‎features/extension/extension.toml‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ reads = ["documents"]
5858
writes = ["documents"]
5959
# The app's actions it runs: a chart's tab named for the chart (its
6060
# title), not "Chart".
61-
actions = ["WORKSPACE_TAB_SET"]
61+
actions = ["WORKSPACE_TAB_SET", "TOOLBAR_SHOW"]
6262

6363
[[actions]]
6464
id = "KEYFLOW_STATE"
@@ -277,6 +277,15 @@ group = "Keyflow"
277277
effect = "mutating"
278278
toggle = true
279279

280+
[[actions]]
281+
id = "KEYFLOW_EDIT_MODE"
282+
name = "Keyflow: edit mode"
283+
description = "Into or out of edit mode (`on`: true or false; with neither, the other way): in it the palettes show beside the chart and the note-input toolbar over it; out of it the pane is the chart alone, for reading and following playback."
284+
group = "Keyflow"
285+
effect = "mutating"
286+
toggle = true
287+
params = '{"type":"object","properties":{"on":{"type":"boolean"}}}'
288+
280289
[[actions]]
281290
id = "KEYFLOW_PALETTES"
282291
name = "Keyflow: palettes"

‎features/extension/src/lib.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,27 @@ impl Extension for Keyflow {
202202
}
203203
});
204204
ext.action("KEYFLOW_CHORD_EDIT", |_| async { chord_edit().await });
205+
// Edit mode: the palettes beside the chart and the note-input
206+
// toolbar over it, together. Off, the pane is the chart alone
207+
// (reading it, following playback).
208+
ext.action("KEYFLOW_EDIT_MODE", |args| async move {
209+
let shown = dispatch("KEYFLOW_STATE", Json::Null)
210+
.await
211+
.ok()
212+
.and_then(|s| s.get("palettes").and_then(Json::as_bool))
213+
.unwrap_or(true);
214+
let on = args.get("on").and_then(Json::as_bool).unwrap_or(!shown);
215+
if shown != on {
216+
dispatch("KEYFLOW_PALETTES", Json::Null).await?;
217+
}
218+
let place = if on { "view:keyflow/chart" } else { "hidden" };
219+
dispatch(
220+
"TOOLBAR_SHOW",
221+
serde_json::json!({ "id": "keyflow/note-input", "where": place }),
222+
)
223+
.await?;
224+
Ok(serde_json::json!({ "edit": on }))
225+
});
205226
// The chart's zoom from the keyboard, MuseScore's keys: a step of
206227
// the wheel's, 100%, or the pane's width.
207228
for (name, step) in [

‎features/extension/src/session_view.rs‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,29 @@ fn run_playhead() {
7070
let stale = starts
7171
.get(&doc)
7272
.is_none_or(|(_, at)| at.elapsed() >= STRUCTURE_EVERY);
73-
if stale && let Some(read) = runtime.block_on(chart_start_elsewhere(&doc)) {
73+
// Whether the chart writes its own lead-in (an intro, a
74+
// count-in): one that does not starts after the session's.
75+
let has_lead_in = crate::open_charts().get(&key).is_none_or(|open| {
76+
open.data.read(|s| {
77+
// Its first section past a count-in (which is
78+
// before the session's bar 1 either way).
79+
s.chart
80+
.sections
81+
.iter()
82+
.map(|section| &section.section.section_type)
83+
.find(|kind| !matches!(kind, keyflow::SectionType::CountIn))
84+
.is_none_or(|kind| {
85+
matches!(
86+
kind,
87+
keyflow::SectionType::Intro | keyflow::SectionType::Opening
88+
)
89+
})
90+
})
91+
});
92+
if stale
93+
&& let Some(read) =
94+
runtime.block_on(chart_start_elsewhere(&doc, has_lead_in))
95+
{
7496
starts.insert(doc.clone(), (read, Instant::now()));
7597
}
7698
starts.get(&doc).map_or(0.0, |(start, _)| *start)
@@ -93,14 +115,14 @@ fn run_playhead() {
93115

94116
/// The chart's start in a session another extension provides, through
95117
/// `fts.session`'s structure.
96-
async fn chart_start_elsewhere(doc: &str) -> Option<f64> {
118+
async fn chart_start_elsewhere(doc: &str, chart_has_lead_in: bool) -> Option<f64> {
97119
let source = ChartSource::Session {
98120
doc: doc.to_owned(),
99121
};
100122
let call = source.structure_call().ok()??;
101123
let handle = crate::handle().ok()?;
102124
let reply = crate::ask_session(&handle, &call).await.ok()?;
103-
ChartSource::chart_start_of(&reply).ok()
125+
ChartSource::chart_start_aligned(&reply, chart_has_lead_in).ok()
104126
}
105127

106128
#[cfg(test)]

‎features/session/src/lib.rs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,62 @@ impl ChartSource {
404404
}
405405
}
406406

407+
/// [`Self::chart_start_of`] for a chart that may leave the session's
408+
/// lead-in out.
409+
///
410+
/// The session's bar 1 can sit at the start of an intro (or a count-in)
411+
/// the chart does not write: the tracks play four bars, the chart's
412+
/// bar 1 is the verse. With `chart_has_lead_in` false and the session's
413+
/// bar 1 inside a region named as a lead-in (`Intro…`, `Count…`,
414+
/// `Click…`), the chart starts where the first region after it that is
415+
/// not one starts: the two then agree section by section. Otherwise as
416+
/// the session says.
417+
///
418+
/// # Errors
419+
///
420+
/// The reply is not the session's structure.
421+
pub fn chart_start_aligned(reply: &str, chart_has_lead_in: bool) -> Result<f64> {
422+
let Response::Structure {
423+
tempo,
424+
bar_one,
425+
regions,
426+
..
427+
} = response_of(reply).map_err(Error::Service)?
428+
else {
429+
return Err(Error::Service("not the structure".to_owned()));
430+
};
431+
let map = tempo
432+
.to_tempo_map()
433+
.map_err(|e| Error::Service(e.to_string()))?;
434+
let start = bar_one.map_or(0.0, |at| at.seconds(&map));
435+
if chart_has_lead_in {
436+
return Ok(start);
437+
}
438+
let lead_in = |name: &str| {
439+
let n = name.trim().to_ascii_lowercase();
440+
n.starts_with("intro") || n.starts_with("count") || n.starts_with("click")
441+
};
442+
let spans: Vec<(f64, f64, bool)> = regions
443+
.values()
444+
.map(|r| (r.start.seconds(&map), r.end.seconds(&map), lead_in(&r.name)))
445+
.collect();
446+
const NEAR: f64 = 1e-3;
447+
let Some(&(_, lead_end, _)) = spans
448+
.iter()
449+
.find(|(from, to, lead)| *lead && *from <= start + NEAR && start < *to - NEAR)
450+
else {
451+
return Ok(start);
452+
};
453+
Ok(spans
454+
.iter()
455+
.filter(|(from, _, lead)| !*lead && *from >= lead_end - NEAR)
456+
.map(|(from, _, _)| *from)
457+
.fold(None, |best: Option<f64>, from| {
458+
Some(best.map_or(from, |b| b.min(from)))
459+
})
460+
.unwrap_or(start))
461+
}
462+
407463
/// The call that replaces `delete` characters at `at` of the chart
408464
/// with `insert` (positions in Unicode scalars); `None` for a document.
409465
///

‎features/ui/src/paint_view.rs‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,8 @@ impl ChartPaintView {
671671
}
672672

673673
/// The chart scrolled, when the playhead moved into a measure out of
674-
/// sight, just far enough to show it (a page turned as it plays). Not
674+
/// sight, a page at a time: that measure's line goes to the top, so
675+
/// the lines after it are read ahead ([`turn_page`]). Not
675676
/// while note input has a cursor: the person typing leads the view.
676677
fn follow_playhead(
677678
&mut self,
@@ -696,7 +697,17 @@ impl ChartPaintView {
696697
let zoom = self.effective_zoom(view.zoom, ctx.geometry.width);
697698
let (w, h) = pane_size(ctx);
698699
let scroll = clamp_scroll(view.scroll, self.content, (w, h), zoom);
699-
let next = follow(scroll, target, (w / zoom, h / zoom));
700+
// The page the measure is on: playback shows that page whole (or
701+
// from its left edge and top, when it is larger than the pane),
702+
// never a sliver of it beside the page before.
703+
let mid = (x0 + x1) / 2.0;
704+
let page = (1..)
705+
.map_while(|n| self.view.page(n))
706+
.find(|&(px0, _, width, _)| mid >= px0 && mid <= px0 + width)
707+
.map(|(px0, py0, width, height)| {
708+
Rect::new(px0 * px, py0 * px, (px0 + width) * px, (py0 + height) * px)
709+
});
710+
let next = turn_page(scroll, target, page, (w / zoom, h / zoom));
700711
if next == scroll {
701712
return view;
702713
}
@@ -1798,6 +1809,39 @@ fn follow(scroll: (f64, f64), target: Rect, visible: (f64, f64)) -> (f64, f64) {
17981809
)
17991810
}
18001811

1812+
/// The scroll that follows playback into `target` (the measure playing) on
1813+
/// `page` (its page, when known): unchanged while that measure is in view.
1814+
/// Once it is not, the view goes to its page: the page's left edge and,
1815+
/// when the page fits the pane's height, its top, so the whole page is
1816+
/// read; on a page taller than the pane, the measure's line goes to the
1817+
/// top. With no page, sideways as [`follow`].
1818+
fn turn_page(
1819+
scroll: (f64, f64),
1820+
target: Rect,
1821+
page: Option<Rect>,
1822+
visible: (f64, f64),
1823+
) -> (f64, f64) {
1824+
let in_view = target.x0 >= scroll.0
1825+
&& target.x1 <= scroll.0 + visible.0
1826+
&& target.y0 >= scroll.1
1827+
&& target.y1 <= scroll.1 + visible.1;
1828+
if in_view {
1829+
return scroll;
1830+
}
1831+
let margin = (visible.1 * 0.04).min(24.0);
1832+
let Some(page) = page else {
1833+
let (x, _) = follow(scroll, target, visible);
1834+
return (x, target.y0 - margin);
1835+
};
1836+
let x = page.x0 - margin.min(12.0);
1837+
let y = if page.height() + margin <= visible.1 {
1838+
page.y0 - margin
1839+
} else {
1840+
target.y0 - margin
1841+
};
1842+
(x, y)
1843+
}
1844+
18011845
fn pane_size(ctx: &ViewCtx) -> (f64, f64) {
18021846
(
18031847
f64::from(ctx.geometry.width),
@@ -2004,6 +2048,15 @@ mod tests {
20042048
// In view: no scroll.
20052049
let bar = Rect::new(100.0, 100.0, 200.0, 160.0);
20062050
assert_eq!(follow((0.0, 0.0), bar, view), (0.0, 0.0));
2051+
// Playback turns a page: a measure below the view goes to its top.
2052+
assert_eq!(turn_page((0.0, 0.0), bar, None, view), (0.0, 0.0));
2053+
// A measure on the next page, out of view: that page from its edge.
2054+
let next = Rect::new(700.0, 100.0, 800.0, 160.0);
2055+
let page = Rect::new(650.0, 0.0, 1000.0, 280.0);
2056+
assert_eq!(
2057+
turn_page((0.0, 0.0), next, Some(page), view),
2058+
(638.0, -12.0)
2059+
);
20072060
// Below: scrolled until its bottom is in, with a margin.
20082061
let below = Rect::new(100.0, 500.0, 200.0, 560.0);
20092062
let (_, y) = follow((0.0, 0.0), below, view);

0 commit comments

Comments
 (0)