Files
Cagire/src/state/editor.rs
Raphaël Forment b47c789612
Some checks failed
Deploy Website / deploy (push) Failing after 4m48s
Feat: continue refactoring
2026-02-01 13:39:25 +01:00

105 lines
2.3 KiB
Rust

use std::cell::RefCell;
use std::ops::RangeInclusive;
use cagire_ratatui::Editor;
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum PatternField {
Length,
Speed,
}
#[derive(Clone, Copy, PartialEq, Eq, Default)]
pub enum PatternPropsField {
#[default]
Name,
Length,
Speed,
Quantization,
SyncMode,
}
impl PatternPropsField {
pub fn next(&self) -> Self {
match self {
Self::Name => Self::Length,
Self::Length => Self::Speed,
Self::Speed => Self::Quantization,
Self::Quantization => Self::SyncMode,
Self::SyncMode => Self::SyncMode,
}
}
pub fn prev(&self) -> Self {
match self {
Self::Name => Self::Name,
Self::Length => Self::Name,
Self::Speed => Self::Length,
Self::Quantization => Self::Speed,
Self::SyncMode => Self::Quantization,
}
}
}
pub struct EditorContext {
pub bank: usize,
pub pattern: usize,
pub step: usize,
pub editor: Editor,
pub selection_anchor: Option<usize>,
pub copied_steps: Option<CopiedSteps>,
pub show_stack: bool,
pub stack_cache: RefCell<Option<StackCache>>,
}
#[derive(Clone)]
pub struct StackCache {
pub cursor_line: usize,
pub lines_hash: u64,
pub result: String,
}
#[derive(Clone)]
pub struct CopiedSteps {
pub bank: usize,
pub pattern: usize,
pub steps: Vec<CopiedStepData>,
}
#[derive(Clone)]
pub struct CopiedStepData {
pub script: String,
pub active: bool,
pub source: Option<usize>,
pub original_index: usize,
pub name: Option<String>,
}
impl EditorContext {
pub fn selection_range(&self) -> Option<RangeInclusive<usize>> {
let anchor = self.selection_anchor?;
let a = anchor.min(self.step);
let b = anchor.max(self.step);
Some(a..=b)
}
pub fn clear_selection(&mut self) {
self.selection_anchor = None;
}
}
impl Default for EditorContext {
fn default() -> Self {
Self {
bank: 0,
pattern: 0,
step: 0,
editor: Editor::new(),
selection_anchor: None,
copied_steps: None,
show_stack: false,
stack_cache: RefCell::new(None),
}
}
}