Skip to content

armgit / nota

Public Imported from GitHub
0

Nota — a free DAW for macOS, Windows and Linux. No subscription, no account — just open it and make music.

5.9 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (c) 2026 Egor Khindikaynen (Nota). See LICENSES/ for license terms.
//
// Keyboard clip moves (specs/039-arrangement-keyboard-moves): ←/→ nudge the selected clips by
// the grid step, ⌥←/⌥→ by a 64th (#302); ⌥↑/⌥↓ move them to the next track that takes them,
// keeping their exact position (#301's comment). Each key press is one undo step.

using System;
using System.Collections.Generic;
using System.Linq;
using Nota.Application.Editing;
using Nota.Application.Localization;

namespace Nota.App;

public partial class ArrangementView
{
    /// <summary>←/→: nudges the clip selection by the grid step (<paramref name="fine"/>: a 64th)
    /// as one block, stopping at the song start. False when no clip is selected.</summary>
    public bool NudgeSelectedClips(int dir, bool fine)
    {
        if (_engine is null || _sel.Count == 0) return false;
        // Starts come from the engine, not the drawn clips: a clip on a folded group's child is
        // still selected and moves with the block, like Duplicate and Copy already take it.
        var block = SelectedClipStarts();
        if (block.Count == 0) return false;
        double delta = ArrangementMoves.NudgeDelta(block.Min(b => b.start), fine ? ArrangementMoves.FineNudgeBeats : SnapStepBeats, dir);
        if (delta == 0) return true;   // already at the song start: the key is still ours
        if (MoveBlock(block.Select(b => (b.track, b.clip, b.track, b.start + delta)).ToArray()) is not { } move) return true;
        Refresh();
        ReselectPlaced(move);
        ClipsMoved?.Invoke(move);   // open editors follow their clips (index and START read-out)
        return true;
    }

    /// <summary>⌥↑/⌥↓: moves the clip selection to the nearest tracks above / below that take
    /// every clip (audio on audio, MIDI on instrument tracks), at the same beats. False when no
    /// clip is selected.</summary>
    public bool MoveSelectedClipsToTrack(int dir)
    {
        if (_engine is null || _sel.Count == 0) return false;
        // A group Link's partners (specs/076) stay on their own tracks: only the picked clips move.
        var block = SelectedClipStarts().Where(b => !IsLinkedPartner(b.track, b.clip))
            .Select(b => (b.track, b.clip, b.start, row: RowOfTrack(b.track))).ToList();
        if (block.Count == 0) return false;
        if (block.Any(b => b.row < 0))
        {
            // Rows are what ⌥↑/⌥↓ step through, and a folded group's children have none: moving
            // only the shown clips would split the block, so the whole move is refused.
            StatusMessage?.Invoke(Loc.T("arrangement.key_moves.unfold_the_group_to_move"));
            return true;
        }
        int shift = ArrangementMoves.RowShift(block.Select(b => b.row).Distinct().ToList(), _tracks.Count, dir, TakesClipsFrom);
        if (shift == 0)
        {
            StatusMessage?.Invoke(dir < 0 ? Loc.T("arrangement.key_moves.no_track_above_takes_these") : Loc.T("arrangement.key_moves.no_track_below_takes_these"));
            return true;
        }
        var moves = block.Select(b => (b.track, b.clip, _tracks[b.row + shift].Id, b.start)).ToArray();
        if (MoveBlock(moves) is not { } move) return true;
        if (_engine.LastMoveKeptDeviceAutomation())
            StatusMessage?.Invoke(Loc.T("arrangement.moved_across_tracks_device_automation"));
        Refresh();
        ReselectPlaced(move);
        ClipsMoved?.Invoke(move);
        if (SelTrackId > 0) RevealTrack(SelTrackId);
        AnnounceSelectedTrack();   // the destination track is now the active one
        return true;
    }

    /// <summary>Raised after a block of clips moved (drag, keys, drop onto new tracks) with where
    /// each clip of the touched tracks now is (null: carved away). Open editors hold clip indices,
    /// which a move can shift, so they re-target from this.</summary>
    public event Action<ClipBlockMove>? ClipsMoved;

    // One MoveClipBlock, plus where every clip of the touched tracks ended up; null if refused.
    internal ClipBlockMove? MoveBlock(
        (int trackId, int clipIndex, int destTrackId, double newStartBeat)[] moves)
    {
        if (_engine is null) return null;
        var touched = moves.SelectMany(m => new[] { m.trackId, m.destTrackId }).Distinct().ToList();
        var before = touched.ToDictionary(t => t, ClipStarts);
        if (!_engine.MoveClipBlock(moves)) return null;
        var after = touched.ToDictionary(t => t, ClipStarts);
        return new ClipBlockMove(ArrangementMoves.Reindex(before, moves.Select(m => (m.trackId, m.clipIndex)).ToList(),
            _engine.LastPlacedClips(), after), before, after);
    }

    internal void RaiseClipsMoved(ClipBlockMove move) => ClipsMoved?.Invoke(move);

    /// <summary>Each given track's clip starts, by index (a snapshot to compare after undo / redo).</summary>
    internal IReadOnlyDictionary<int, IReadOnlyList<double>> ClipStartsOf(IEnumerable<int> tracks)
        => tracks.ToDictionary(t => t, ClipStarts);

    private IReadOnlyList<double> ClipStarts(int trackId)
    {
        var starts = new List<double>();
        while (_engine!.TryGetClipInfo(trackId, starts.Count, out var ci)) starts.Add(ci.StartBeat);
        return starts;
    }

    // Every selected clip with its start, shown or not (a folded group hides its children's rows).
    private List<(int track, int clip, double start)> SelectedClipStarts()
    {
        var block = new List<(int, int, double)>();
        foreach (var (track, clip) in _sel)
            if (_engine!.TryGetClipInfo(track, clip, out var ci)) block.Add((track, clip, ci.StartBeat));
        return block;
    }

    // A row takes another row's clips when it holds the same kind of clip (a group or a return holds none).
    private bool TakesClipsFrom(int from, int to)
    {
        var src = _tracks[from];
        var dst = _tracks[to];
        return !dst.IsGroup && !dst.IsReturn && dst.IsInstrument == src.IsInstrument;
    }
}