use super::core::EditorState;
use indicatrix_cut_core::{Edit, EditError};
use indicatrix_editor::{EditChange, NudgeOutcome, PinOutcome, scratch::ScratchDelta};
use std::{
sync::OnceLock,
time::{Duration, Instant},
};
pub(in crate::gui::editor) fn coalesce_timestamp() -> Duration {
static ORIGIN: OnceLock<Instant> = OnceLock::new();
ORIGIN.get_or_init(Instant::now).elapsed()
}
impl EditorState {
pub(in crate::gui::editor) fn apply(&mut self, edit: Edit) -> Result<(), EditError> {
self.session.apply(edit).map(|_| ())
}
#[cfg(test)]
pub(in crate::gui::editor) fn apply_coalescing(
&mut self,
edit: Edit,
key: u64,
) -> Result<(), EditError> {
self.session
.apply_coalescing(edit, key, coalesce_timestamp())
.map(|_| ())
}
pub(in crate::gui::editor) fn nudge_angles(
&mut self,
targets: &[usize],
delta_deg: f64,
) -> Result<Option<NudgeOutcome>, EditError> {
self.session
.nudge_displayed_angles(targets, delta_deg, coalesce_timestamp())
}
pub(in crate::gui::editor) fn set_tier_angle(
&mut self,
tier: usize,
angle_deg: f64,
now: Duration,
) -> Result<Option<EditChange>, EditError> {
self.session.set_tier_angle(tier, angle_deg, now)
}
pub(in crate::gui::editor) fn pin_tier_mast(
&mut self,
tier: usize,
mast: f64,
now: Duration,
) -> Result<Option<PinOutcome>, EditError> {
self.session.pin_tier_mast(tier, mast, now)
}
pub(in crate::gui::editor) fn rotate_tier_indices(
&mut self,
tier: usize,
k_teeth: i64,
now: Duration,
) -> Result<Option<EditChange>, EditError> {
self.session.rotate_tier_indices(tier, k_teeth, now)
}
pub(in crate::gui::editor) fn undo(&mut self) -> Result<bool, EditError> {
self.session.undo().map(|change| change.is_some())
}
#[cfg(test)]
pub(in crate::gui::editor) fn redo(&mut self) -> Result<bool, EditError> {
self.session.redo().map(|change| change.is_some())
}
pub(in crate::gui::editor) fn record_scratch_push(&self) -> ScratchDelta {
self.last_pushed_scratch
.borrow_mut()
.record(&self.session.design)
}
}