fix(timeline): restore the C++ block length anchors and repair pointer edits

The two BlockCore length setters swapped their anchors relative to the
C++ semantics they document, so the ported edit commands produced wrong
geometry on the live UI paths: roll edits kept the seam still, slides
left negative in-points, and trims wrote the timeline in-point into
media_in (playing the wrong media content).

Adopt three stored-range primitives in block.rs:

- set_length_and_media_out: in fixed, out moves, media untouched
  (resize, trim-out, gaps growing rightward).
- set_length_and_media_in: in fixed, out moves, media_in += old-new
  (resize-with-media-in, splice right half, ripple trim-in).
- set_length_keeping_out (new): out fixed, in moves, media_in +=
  old-new (trim-in body and out-neighbour, slide out-neighbour,
  ripple trim-in of the trailing block).

Point every command at the primitive matching its intent (undopointer,
undogeneral, undoripple, undosplit, graphops, cli, nodeops) and fix the
two real defects the swap hid:

- TrackReplaceBlockWithGapCommand grew a following gap rightward,
  swallowing whatever followed it: the "dragging one clip moves
  unrelated clips" regression. The gap now grows leftward over the
  removed block's span; regression test in domain_test.
- The ripple/splice trims now advance media_in instead of rewriting it,
  and BlockSplitCommand writes both halves' ranges and media
  explicitly (the second half continues from the split point).

Rewrite the KNOWN-SWAP expectations to the correct geometry (roll moves
the seam, slide has no negative in-point, insert-gaps grows rightward,
resize-with-media-in yields media_in = 20) and add the missing media
assertions. TrackSlideCommand documents that the caller positions the
sliding blocks (the stored model has no track layout).
This commit is contained in:
2026-09-24 12:41:29 +08:00
parent 415262a7b2
commit d88e2d6ec1
14 changed files with 374 additions and 252 deletions
+23 -11
View File
@@ -37,8 +37,9 @@ use crate::util::{
block_add_to_graph, block_connect, block_connected_input, block_disconnect_input,
block_enabled, block_gap_create, block_in, block_kind, block_length, block_next, block_out,
block_previous, block_range, block_remove_from_graph, block_set_enabled, block_set_in,
block_set_length_and_media_in, block_set_length_and_media_out, block_set_range, block_track,
clip_media_in, clip_set_media_in, same_block, track_append_block, track_create,
block_set_length_and_media_in, block_set_length_and_media_out, block_set_length_keeping_out,
block_set_range, block_track, clip_media_in, clip_set_media_in, same_block, track_append_block,
track_create,
track_insert_block_after, track_insert_block_before, track_replace_block,
track_ripple_remove_block, tracklist_append, tracklist_remove, tracklist_track_at,
tracklist_track_count, tracklist_type, transition_offsets,
@@ -865,11 +866,17 @@ impl TrackReplaceBlockWithGapCommand {
if let Some(gap) = &self.existing_gap {
// Extend an existing gap. In the module world the block's in/out
// points are stored on the block (Olive derives them from the track
// order), so the extension must keep the gap's in point anchored —
// an out-anchored `set_length_and_media_out` would push the in point
// negative.
// order), so the extension must grow towards the block's span:
// a gap that PRECEDES the block grows rightward (in anchored),
// while a gap that merely FOLLOWS it must grow leftward to start
// at the block's in point (out anchored) — growing it at the out
// would swallow whatever follows the gap.
new_gap_length = new_gap_length + block_length(gap);
block_set_length_and_media_in(gap, new_gap_length);
if previous_is_a_gap {
block_set_length_and_media_out(gap, new_gap_length);
} else {
block_set_length_keeping_out(gap, new_gap_length);
}
track_ripple_remove_block(&self.track, &self.block);
self.existing_gap_precedes =
previous.as_ref().map(|p| same_block(gap, p)).unwrap_or(false);
@@ -882,7 +889,7 @@ impl TrackReplaceBlockWithGapCommand {
// (the module stores positions on the block; the gap
// fills the block's stored span).
block_set_in(gap, self.block_in_point);
block_set_length_and_media_in(gap, new_gap_length);
block_set_length_and_media_out(gap, new_gap_length);
}
}
if let Some(gap) = &self.our_gap {
@@ -935,9 +942,14 @@ impl TrackReplaceBlockWithGapCommand {
track_insert_block_before(&self.track, &self.block, gap);
}
// Restore the gap's original length (in-anchored: its in point was
// untouched by the redo extension).
block_set_length_and_media_in(gap, original_gap_length);
// Restore the gap's original length on the same anchor the redo
// grew it from (the in point for a preceding gap, the out point
// for a following one).
if self.existing_gap_precedes {
block_set_length_and_media_out(gap, original_gap_length);
} else {
block_set_length_keeping_out(gap, original_gap_length);
}
self.existing_gap = None;
}
} else {
@@ -1191,7 +1203,7 @@ impl TrackListInsertGaps {
// explicitly.
let gap_in = g.before.as_ref().map(block_out).unwrap_or(self.point);
block_set_in(&g.gap, gap_in);
block_set_length_and_media_in(&g.gap, self.length);
block_set_length_and_media_out(&g.gap, self.length);
block_add_to_graph(&g.gap, g.entry.take());
track_insert_block_after(&g.track, &g.gap, g.before.as_ref());
}
+36 -26
View File
@@ -34,7 +34,7 @@ use crate::undocommon::{
use crate::util::{
block_add_to_graph, block_gap_create, block_in, block_kind, block_length, block_next,
block_out, block_previous, block_remove_from_graph, block_set_in,
block_set_length_and_media_in, block_set_length_and_media_out, block_track,
block_set_length_and_media_out, block_set_length_keeping_out, block_track,
track_append_block, track_insert_block_after, track_insert_block_before, track_length,
track_ripple_remove_block, tracklist_track_at, tracklist_track_count, BlockKind, NodeRef,
};
@@ -159,18 +159,20 @@ impl BlockTrimCommand {
if self.we_created_adjacent_ {
// We shortened but don't have a viable adjacent to lengthen, so create
// one filling exactly the space the trim freed: for a trim-in it
// spans [in - diff, in), for a trim-out [out, out + diff). The
// module stores positions on the block, so both the in point and
// the length are written explicitly.
// one filling exactly the space the trim freed. The module stores
// positions on the block (Olive derives the inserted gap's position
// from the track order), so the in point is the freed space's start
// and the length closes the gap up to the block's new edge:
// trim-in frees [in, in + diff) (the gap is inserted before the
// block), trim-out frees [out - diff, out) (inserted after it).
self.adjacent_ = Some(block_gap_create(&self.track.project));
if let Some(gap) = &self.adjacent_ {
if self.mode == MovementMode::TrimIn {
block_set_in(gap, block_in(&self.block) - self.trim_diff_);
block_set_in(gap, block_in(&self.block));
} else {
block_set_in(gap, block_out(&self.block));
block_set_in(gap, block_out(&self.block) - self.trim_diff_);
}
block_set_length_and_media_in(gap, self.trim_diff_);
block_set_length_and_media_out(gap, self.trim_diff_);
}
} else if let Some(adjacent) = &self.adjacent_ {
// Determine if we're removing the adjacent
@@ -187,8 +189,10 @@ impl BlockTrimCommand {
}
if self.mode == MovementMode::TrimIn {
block_set_length_and_media_in(&self.block, self.new_length);
// Trim-in keeps the out fixed and moves the in (media follows).
block_set_length_keeping_out(&self.block, self.new_length);
} else {
// Trim-out keeps the in fixed and moves the out (media untouched).
block_set_length_and_media_out(&self.block, self.new_length);
}
@@ -221,9 +225,13 @@ impl BlockTrimCommand {
let adjacent_length = block_length(adjacent) + self.trim_diff_;
if self.mode == MovementMode::TrimIn {
// The previous neighbour grows/shrinks at its out edge
// to meet the trimmed in point.
block_set_length_and_media_out(adjacent, adjacent_length);
} else {
block_set_length_and_media_in(adjacent, adjacent_length);
// The next neighbour grows/shrinks at its in edge to
// meet the trimmed out point (its content end anchored).
block_set_length_keeping_out(adjacent, adjacent_length);
}
}
}
@@ -264,14 +272,14 @@ impl BlockTrimCommand {
if self.mode == MovementMode::TrimIn {
block_set_length_and_media_out(adjacent, adjacent_length);
} else {
block_set_length_and_media_in(adjacent, adjacent_length);
block_set_length_keeping_out(adjacent, adjacent_length);
}
}
}
}
if self.mode == MovementMode::TrimIn {
block_set_length_and_media_in(&self.block, self.old_length_);
block_set_length_keeping_out(&self.block, self.old_length_);
} else {
block_set_length_and_media_out(&self.block, self.old_length_);
}
@@ -365,7 +373,7 @@ impl TrackSlideCommand {
// of the first block. Positions are stored on the block in
// the module model, so both ends are written explicitly.
block_set_in(gap, block_in(&self.blocks[0]) + self.movement);
block_set_length_and_media_in(gap, Rational::new(0, 1) - self.movement);
block_set_length_and_media_out(gap, Rational::new(0, 1) - self.movement);
}
self.we_created_in_adjacent_ = true;
} else {
@@ -381,7 +389,7 @@ impl TrackSlideCommand {
// (movement > 0): the gap fills [last_out, last_out + movement)
// after the last block.
block_set_in(gap, block_out(self.blocks.last().expect("non-empty blocks")));
block_set_length_and_media_in(gap, self.movement);
block_set_length_and_media_out(gap, self.movement);
}
self.we_created_out_adjacent_ = true;
} else {
@@ -390,6 +398,12 @@ impl TrackSlideCommand {
}
/// `redo`: apply the slide.
///
/// The C++ derives the sliding blocks' new positions from the track
/// layout (the resized neighbours shift them implicitly); the
/// stored-range model has no layout, so the CALLER positions the
/// `blocks` by `movement` before running this command — it only owns
/// the adjacent resize/insert/remove half of the slide.
pub fn redo(&mut self) {
// We will always have an in adjacent if there was a valid slide
if self.we_created_in_adjacent_ {
@@ -452,16 +466,15 @@ impl TrackSlideCommand {
self.we_removed_out_adjacent_ = true;
} else {
// Simply resize the adjacent
block_set_length_and_media_in(
adjacent,
block_length(adjacent) - self.movement,
);
// Simply resize the adjacent: its in edge follows the last
// sliding block's out, so the content end stays anchored.
block_set_length_keeping_out(adjacent, block_length(adjacent) - self.movement);
}
}
}
/// `undo`: revert the slide.
/// `undo`: revert the slide (the caller restores the moved blocks
/// alongside this, mirroring how it positioned them for the redo).
pub fn undo(&mut self) {
if self.we_created_in_adjacent_ {
// We created this, so we can remove it now
@@ -508,11 +521,8 @@ impl TrackSlideCommand {
Some(self.blocks.last().expect("non-empty blocks")),
);
} else {
// Simply resize the adjacent
block_set_length_and_media_in(
adjacent,
block_length(adjacent) + self.movement,
);
// Simply resize the adjacent (mirror of the redo).
block_set_length_keeping_out(adjacent, block_length(adjacent) + self.movement);
}
}
}
@@ -625,7 +635,7 @@ impl TrackPlaceBlockCommand {
self.gap_ = Some(block_gap_create(&self.timeline.project));
if let Some(gap) = &self.gap_ {
block_set_in(gap, track_length(&track));
block_set_length_and_media_in(gap, in_ - track_length(&track));
block_set_length_and_media_out(gap, in_ - track_length(&track));
}
}
if let Some(gap) = &self.gap_ {
+22 -20
View File
@@ -41,8 +41,8 @@ use crate::undocommon::{
use crate::util::{
block_add_to_graph, block_gap_create, block_in, block_kind, block_length, block_next,
block_out, block_previous, block_remove_from_graph, block_set_in,
block_set_length_and_media_in, block_set_length_and_media_out, block_track,
sequence_all_tracks, sequence_track_list,
block_set_length_and_media_in, block_set_length_and_media_out, block_set_length_keeping_out,
block_track, sequence_all_tracks, sequence_track_list,
track_insert_block_after, track_locked, track_nearest_block_after_or_at,
track_nearest_block_before_or_at, track_prepend_block, track_ripple_remove_block,
tracklist_track_at, tracklist_track_count, BlockKind, NodeRef,
@@ -335,10 +335,11 @@ impl TrackRippleRemoveAreaCommand {
let cmd = self.splice_split_command_.as_mut().unwrap();
cmd.redo();
// Trim the in of the split (the second half produced by the
// split keeps the original out point; the in-end trim shifts
// its stored in point to the range out — the module equivalent
// of the C++ ripple shifting the remainder earlier).
// Trim the in of the split: the second half produced by the
// split keeps the original out point, so removing the first
// `range.length()` of it keeps the timeline in fixed at the
// range out and advances the media window past the removed
// content (in-anchored, media in moves with the length).
if let Some(split) = cmd.new_block() {
let new_len =
block_length(&split) - (self.range.out() - block_in(&split));
@@ -346,15 +347,15 @@ impl TrackRippleRemoveAreaCommand {
}
} else {
if let Some(t) = &self.trim_out_ {
// An out-end trim keeps the in point (in-anchored; the
// C++ setter name is kept, but the module's in/out are
// stored values, see the splice trim above).
block_set_length_and_media_in(&t.block, t.new_length);
// An out-end trim keeps the in point and the media window
// (in-anchored).
block_set_length_and_media_out(&t.block, t.new_length);
}
if let Some(t) = &self.trim_in_ {
// An in-end trim keeps the out point (out-anchored).
block_set_length_and_media_out(&t.block, t.new_length);
// An in-end trim keeps the out point and advances the media
// window past the removed head.
block_set_length_keeping_out(&t.block, t.new_length);
}
// Perform removals
@@ -389,12 +390,12 @@ impl TrackRippleRemoveAreaCommand {
} else {
if let Some(t) = &self.trim_out_ {
// In-anchored, matching the redo (see above).
block_set_length_and_media_in(&t.block, t.old_length);
block_set_length_and_media_out(&t.block, t.old_length);
}
if let Some(t) = &self.trim_in_ {
// Out-anchored, matching the redo (see above).
block_set_length_and_media_out(&t.block, t.old_length);
block_set_length_keeping_out(&t.block, t.old_length);
}
// Un-remove any blocks
@@ -699,14 +700,15 @@ impl TrackListRippleToolCommand {
if redo {
if wd.created_gap.is_none() {
let gap = block_gap_create(&track_copy.project);
// The gap takes the ripple movement's span ahead of
// `b`; positions are stored on the block in the
// The gap takes the ripple movement's span at `b`'s
// in point; positions are stored on the block in the
// module model, so both ends are written explicitly
// (approximation: the C++ ripple additionally shifts
// the successors, which the stored positions render
// as an overlap-free gap right before `b`).
// (approximation: the C++ layout additionally shifts
// `b` and its successors right by that span — the
// stored-range command leaves that shift to the
// caller).
block_set_in(&gap, block_in(&b));
block_set_length_and_media_in(
block_set_length_and_media_out(
&gap,
if self.ripple_movement < Rational::new(0, 1) {
rat_neg(self.ripple_movement)
+23 -19
View File
@@ -35,9 +35,9 @@ use oak_node::id::NodeId;
use oak_undo::undocommand::UndoCommand;
use crate::util::{
block_in, block_length, block_out, block_set_length_and_media_in,
block_set_length_and_media_out, block_track, track_insert_block_after,
track_ripple_remove_block, GraphBlockRange, NodeRef,
block_in, block_length, block_out, block_set_length_and_media_out, block_set_range, block_track,
clip_media_in, clip_set_media_in, track_insert_block_after, track_ripple_remove_block,
GraphBlockRange, NodeRef,
};
/// `BlockSplitCommand` — split one block at a point
@@ -177,10 +177,11 @@ impl BlockSplitCommand {
/// second half, and insert it after `block`.
///
/// The split keeps the ORIGINAL block anchored at its in-point (the
/// C++ `set_length_and_media_in` on the original) and anchors the
/// cloned `new_block` at its out-point (the C++
/// `set_length_and_media_out` on the copy — the copy carried the
/// original span, so the out point is preserved exactly).
/// C++ `set_length_and_media_out` on the original) and anchors the
/// cloned `new_block` between the split point and the original out
/// point (the C++ `set_length_and_media_in` on the copy — the copy
/// carried the original span, so the out point is preserved exactly),
/// with its media window continuing where the first half stops.
pub fn redo(&mut self) {
// Create the second half if redo is invoked without a preceding
// prepare() (the oakundo command path may call redo directly).
@@ -199,14 +200,19 @@ impl BlockSplitCommand {
let first_half_length = self.point - block_in;
let second_half_length = block_out - self.point;
// The second half continues the media exactly where the first half
// stops (C++ `ClipBlock::set_length_and_media_in` on the copy).
let media_in = clip_media_in(&self.block);
if let Some(new_block) = &self.new_block {
// In-anchored length for the first half keeps the original's in
// point (the C++ `set_length_and_media_in`); the out-anchored
// length for the second half keeps the copy's out point (the
// C++ `set_length_and_media_out`).
block_set_length_and_media_in(&self.block, first_half_length);
block_set_length_and_media_out(new_block, second_half_length);
// First half: the original's in point stays and its out lands on
// the split point (in-anchored, media unchanged).
block_set_length_and_media_out(&self.block, first_half_length);
// Second half: the split point up to the original out point. The
// stored-range model has no track layout to derive these from, so
// both ends are written explicitly along with the media window.
block_set_range(new_block, oak_core::TimeRange::new(self.point, self.point + second_half_length));
clip_set_media_in(new_block, media_in + first_half_length);
if let Some(track) = block_track(&self.block) {
track_insert_block_after(&track, new_block, Some(&self.block));
@@ -223,12 +229,10 @@ impl BlockSplitCommand {
/// and detach the whole copied subgraph from the project graph.
pub fn undo(&mut self) {
if let Some(track) = block_track(&self.block) {
// The redo shrank the original from its in point (it became the
// first half, in-anchored), so the restore grows it in-anchored
// too — the module stores the span on the block (the C++
// `set_length_and_media_out` derives positions from the track
// order and does not apply here).
block_set_length_and_media_in(&self.block, self.old_length);
// The redo shrank the original from its out point (it became the
// first half, in-anchored), so the restore grows it at the out
// again; the media window was never touched.
block_set_length_and_media_out(&self.block, self.old_length);
if let Some(new_block) = &self.new_block {
track_ripple_remove_block(&track, new_block);
}
+17 -5
View File
@@ -311,8 +311,8 @@ pub fn block_set_enabled(b: &NodeRef, enabled: bool) {
}
}
/// `block_set_length_and_media_out`: keep the out point anchored (the
/// in point shifts).
/// `block_set_length_and_media_out`: keep the in point anchored (the out
/// point shifts; the media in point does not move).
pub fn block_set_length_and_media_out(b: &NodeRef, len: Rational) {
let mut p = b.lock();
if let Some(core) = block_core_of_mut(&mut p, b.id) {
@@ -320,8 +320,9 @@ pub fn block_set_length_and_media_out(b: &NodeRef, len: Rational) {
}
}
/// `block_set_length_and_media_in`: keep the in point anchored (the
/// out point shifts).
/// `block_set_length_and_media_in`: keep the in point anchored (the out
/// point shifts) while the media in point follows the length change, so
/// the media out stays anchored.
pub fn block_set_length_and_media_in(b: &NodeRef, len: Rational) {
let mut p = b.lock();
if let Some(core) = block_core_of_mut(&mut p, b.id) {
@@ -329,6 +330,16 @@ pub fn block_set_length_and_media_in(b: &NodeRef, len: Rational) {
}
}
/// Keep the OUT point anchored (the in point shifts) and move the media
/// in point with it: the net of a head trim in the stored-range model
/// (the C++ derives the shifted in-point from the resized previous block).
pub fn block_set_length_keeping_out(b: &NodeRef, len: Rational) {
let mut p = b.lock();
if let Some(core) = block_core_of_mut(&mut p, b.id) {
core.set_length_keeping_out(len);
}
}
/// `block_set_in`: set the in point, keeping the length (the out follows).
pub fn block_set_in(b: &NodeRef, in_: Rational) {
let mut p = b.lock();
@@ -364,7 +375,8 @@ pub fn block_range(b: &NodeRef) -> Option<oak_core::TimeRange> {
/// Set the block's stored timeline range (a no-op for a stale/non-block
/// node). The range is written whole, so the caller can move the in/out
/// pair together without a media-in side effect (unlike
/// [`block_set_length_and_media_out`] / [`block_set_length_and_media_in`]).
/// [`block_set_length_and_media_in`] / [`block_set_length_keeping_out`],
/// which adjust the media in point with the length).
pub fn block_set_range(b: &NodeRef, range: oak_core::TimeRange) {
let mut p = b.lock();
if let Some(core) = block_core_of_mut(&mut p, b.id) {