mirror of
https://github.com/storytold/soundcraft.git
synced 2026-12-02 20:22:27 -05:00
Merge pull request #1415 from ZIDAN44/fix/1376-arrows-scroll-to-selection
fix(ui): Left and Right arrows centre the Edit window on the selection (#1376)
This commit is contained in:
3 files changed
+140
No files matched your search
@@ -472,6 +472,11 @@ pub fn show(app: &mut crate::SoundApp, ui: &mut egui::Ui, window: crate::MainWin
|
||||
// and bottom edges take frames (`drop_edges` keeps its default).
|
||||
style.new_dock_zones = true;
|
||||
style.side_edge_zones = false;
|
||||
// The MIDI body's rect stands for a piano roll on screen (the arrow keys go to it), so only a
|
||||
// frame that draws the panel records one: open behind another tab, the old rect is gone.
|
||||
if edit {
|
||||
ui.ctx().data_mut(|data| data.remove::<egui::Rect>(egui::Id::unique("soundcraft-midi-body")));
|
||||
}
|
||||
let output = DockArea::new(area(edit)).show_with_limits(
|
||||
ui,
|
||||
&workspace.layout,
|
||||
|
||||
@@ -232,6 +232,11 @@ fn fixed_more(app: &mut SoundApp, key: Key, m: Modifiers, mac: bool) -> bool {
|
||||
let _ = app.run(id, json!({}));
|
||||
true
|
||||
}
|
||||
// With an edit selection (a selected clip makes one), Left/Right centre the Edit window on its start/end.
|
||||
// Over the piano roll the arrows move its notes instead.
|
||||
Key::ArrowLeft | Key::ArrowRight if plain && app.ui.window == MainWindow::Edit && !piano_roll_has_pointer(app) => {
|
||||
centre_on_selection(app, key == Key::ArrowRight)
|
||||
}
|
||||
// Commands Keyboard Focus: single-key editing (Edit window only).
|
||||
k if !m.command
|
||||
&& !m.alt
|
||||
@@ -393,6 +398,30 @@ fn tab_clip(app: &mut SoundApp, back: bool) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Scroll the Edit window so the edit selection's start (or `end`) is in its middle. Returns false, leaving
|
||||
/// the key unhandled, when there is only an insertion point.
|
||||
fn centre_on_selection(app: &mut SoundApp, end: bool) -> bool {
|
||||
let s = app.engine.session();
|
||||
let sel = s.edit.selection;
|
||||
if sel.start >= sel.end {
|
||||
return false;
|
||||
}
|
||||
let [x0, _, x1, _] = app.edit_layout.timeline;
|
||||
let half = f64::from((x1 - x0).max(0.0)) / 2.0 * s.edit.zoom.samples_per_px.max(0.01);
|
||||
let at = if end { sel.end } else { sel.start };
|
||||
let _ = app.run("view.scroll", json!({"to": at.saturating_sub(soundcraft_time::to_samples(half)).max(0)}));
|
||||
true
|
||||
}
|
||||
|
||||
/// Whether the pointer is over the MIDI editor, whose piano roll moves the selected notes with the arrow keys.
|
||||
fn piano_roll_has_pointer(app: &SoundApp) -> bool {
|
||||
app.ui.show_midi_editor
|
||||
&& app.docking_context.as_ref().is_some_and(|c| {
|
||||
let body = c.data(|d| d.get_temp::<egui::Rect>(egui::Id::unique("soundcraft-midi-body")));
|
||||
c.pointer_hover_pos().zip(body).is_some_and(|(p, r)| r.contains(p))
|
||||
})
|
||||
}
|
||||
|
||||
fn digit(key: Key) -> u8 {
|
||||
key.name().parse().unwrap_or(0)
|
||||
}
|
||||
|
||||
@@ -113,3 +113,109 @@ fn cmd_equals_toggles_mix_and_edit_without_zooming() {
|
||||
assert_eq!(ctx.zoom_factor(), 1.25, "{os:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// One full app frame on a 1600×1000 screen.
|
||||
fn app_frame(ctx: &egui::Context, app: &mut SoundApp, events: Vec<egui::Event>) {
|
||||
let screen_rect = Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1600.0, 1000.0)));
|
||||
ctx.run_ui(egui::RawInput { screen_rect, events, ..Default::default() }, |ui| {
|
||||
app.logic(ui.ctx());
|
||||
app.ui(ui);
|
||||
})
|
||||
.textures_delta
|
||||
.clear();
|
||||
}
|
||||
|
||||
fn arrow(key: Key) -> Vec<egui::Event> {
|
||||
[true, false].map(|pressed| egui::Event::Key { key, physical_key: None, pressed, repeat: false, modifiers: Modifiers::NONE }).to_vec()
|
||||
}
|
||||
|
||||
/// Where `at` is drawn in the Edit window's timeline, and the timeline's middle (#1376).
|
||||
fn x_and_middle(app: &SoundApp, at: i64) -> (f32, f32) {
|
||||
let tl = soundcraft_ui_egui::edit_window::rect(app.edit_layout.timeline);
|
||||
(soundcraft_ui_egui::edit_window::x_of(app.engine.session(), tl, at), tl.center().x)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn left_and_right_arrows_centre_the_edit_window_on_the_selected_clip() {
|
||||
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
|
||||
let ctx = egui::Context::default();
|
||||
let (clip_id, clip) = app.engine.session().tracks.iter().flat_map(|t| t.clips()).find(|c| c.start > 100_000).map(|c| (c.id, c.range())).unwrap();
|
||||
app.run("view.zoom_set", json!({"samples_per_px": 10.0})).unwrap();
|
||||
app.run("view.scroll", json!({"to": 0})).unwrap();
|
||||
for _ in 0..3 {
|
||||
app_frame(&ctx, &mut app, vec![]);
|
||||
}
|
||||
app.run("edit.select", json!({"clips": [clip_id.0]})).unwrap();
|
||||
assert!(x_and_middle(&app, clip.start).0 > app.edit_layout.timeline[2], "the clip starts off-screen");
|
||||
|
||||
app_frame(&ctx, &mut app, arrow(Key::ArrowLeft));
|
||||
let (x, middle) = x_and_middle(&app, clip.start);
|
||||
assert!((x - middle).abs() < 1.0, "Left: the clip start at {x}, the middle at {middle}");
|
||||
app_frame(&ctx, &mut app, arrow(Key::ArrowRight));
|
||||
let (x, middle) = x_and_middle(&app, clip.end);
|
||||
assert!((x - middle).abs() < 1.0, "Right: the clip end at {x}, the middle at {middle}");
|
||||
|
||||
// An insertion point alone leaves the view where it is.
|
||||
app.run("edit.select", json!({"start": clip.start})).unwrap();
|
||||
let scroll = app.engine.session().edit.zoom.scroll;
|
||||
app_frame(&ctx, &mut app, arrow(Key::ArrowLeft));
|
||||
app_frame(&ctx, &mut app, arrow(Key::ArrowRight));
|
||||
assert_eq!(app.engine.session().edit.zoom.scroll, scroll);
|
||||
}
|
||||
|
||||
/// Over the piano roll the arrows move the selected note and leave the Edit window's view alone.
|
||||
#[test]
|
||||
fn the_piano_roll_keeps_the_plain_arrows() {
|
||||
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
|
||||
app.ui.show_midi_editor = true;
|
||||
app.run("edit.select", json!({"tracks": ["Keys"], "start": 0, "end": 480_000})).unwrap();
|
||||
app.run("view.zoom_set", json!({"samples_per_px": 10.0})).unwrap();
|
||||
app.run("view.scroll", json!({"to": 1_000_000})).unwrap();
|
||||
let ctx = egui::Context::default();
|
||||
for _ in 0..6 {
|
||||
app_frame(&ctx, &mut app, vec![]);
|
||||
}
|
||||
let body = ctx.data(|d| d.get_temp::<egui::Rect>(egui::Id::unique("soundcraft-midi-body"))).unwrap();
|
||||
app_frame(&ctx, &mut app, vec![egui::Event::PointerMoved(body.center())]);
|
||||
app.midi.selected = vec![0];
|
||||
let note_start = |app: &SoundApp| {
|
||||
let clip = soundcraft_ui_egui::midi_editor::target_clip(app).unwrap();
|
||||
match &app.engine.session().find_clip(clip).unwrap().1.content {
|
||||
soundcraft_model::ClipContent::Midi { sequence } => sequence.notes[app.midi.selected[0]].start,
|
||||
_ => panic!("not a MIDI clip"),
|
||||
}
|
||||
};
|
||||
let (before, scroll) = (note_start(&app), app.engine.session().edit.zoom.scroll);
|
||||
app_frame(&ctx, &mut app, arrow(Key::ArrowRight));
|
||||
assert!(note_start(&app) > before, "the note moved right");
|
||||
assert_eq!(app.engine.session().edit.zoom.scroll, scroll, "the Edit window kept its view");
|
||||
}
|
||||
|
||||
/// The MIDI editor open behind another tab has no piano roll on screen: the panel in front of it takes no
|
||||
/// arrows, so they centre the Edit window there as anywhere else.
|
||||
#[test]
|
||||
fn a_midi_editor_behind_another_tab_leaves_the_arrows_to_the_edit_window() {
|
||||
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
|
||||
app.ui.show_midi_editor = true;
|
||||
let (clip_id, clip) = app.engine.session().tracks.iter().flat_map(|t| t.clips()).find(|c| c.start > 100_000).map(|c| (c.id, c.range())).unwrap();
|
||||
app.run("view.zoom_set", json!({"samples_per_px": 10.0})).unwrap();
|
||||
app.run("view.scroll", json!({"to": 0})).unwrap();
|
||||
let ctx = egui::Context::default();
|
||||
for _ in 0..6 {
|
||||
app_frame(&ctx, &mut app, vec![]);
|
||||
}
|
||||
let body = ctx.data(|d| d.get_temp::<egui::Rect>(egui::Id::unique("soundcraft-midi-body"))).unwrap();
|
||||
app.run("ui.dock", json!({"operation": "open", "panel": "history"})).unwrap();
|
||||
app.run("ui.dock", json!({"operation": "move", "panel": "history", "target": "midi", "zone": "tab"})).unwrap();
|
||||
for _ in 0..3 {
|
||||
app_frame(&ctx, &mut app, vec![]);
|
||||
}
|
||||
assert!(app.ui.show_midi_editor, "the MIDI editor is still open");
|
||||
assert_ne!(app.ui.docking.layout.panel_state(&soundcraft_ui_egui::docking::Panel::Midi), craft_ui::docking::PanelState::Front);
|
||||
app_frame(&ctx, &mut app, vec![egui::Event::PointerMoved(body.center())]);
|
||||
app.run("edit.select", json!({"clips": [clip_id.0]})).unwrap();
|
||||
|
||||
app_frame(&ctx, &mut app, arrow(Key::ArrowLeft));
|
||||
let (x, middle) = x_and_middle(&app, clip.start);
|
||||
assert!((x - middle).abs() < 1.0, "Left: the clip start at {x}, the middle at {middle}");
|
||||
}
|
||||
Reference in new issue
Block a user