use std::sync::Arc;
use super::model::{Artifact, CommitStatus, View};
#[derive(Clone, Debug)]
pub struct FrameToken {
pub(crate) instance: Arc<()>,
pub(crate) generation: u64,
pub(crate) serial: u64,
pub(crate) revision: u64,
}
impl PartialEq for FrameToken {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.instance, &other.instance)
&& self.generation == other.generation
&& self.serial == other.serial
&& self.revision == other.revision
}
}
impl Eq for FrameToken {}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Frame {
pub(crate) token: FrameToken,
artifact: Arc<Artifact>,
commit_status: CommitStatus,
confirmed_commit_status: CommitStatus,
optimistic: bool,
position: f64,
velocity: f64,
target: View,
scroll: [usize; 3],
width: u32,
height: u32,
reduced_motion: bool,
animating: bool,
}
impl Frame {
#[must_use]
pub fn new(
token: FrameToken,
artifact: Arc<Artifact>,
commit_status: CommitStatus,
confirmed_commit_status: CommitStatus,
optimistic: bool,
velocity: f64,
position: f64,
) -> Self {
Self {
token,
artifact,
commit_status,
confirmed_commit_status,
optimistic,
velocity,
position,
target: View::Source,
scroll: [0, 0, 0],
width: 0,
height: 0,
reduced_motion: false,
animating: false,
}
}
pub fn set_target(&mut self, target: View) {
self.target = target;
}
pub fn set_scroll(&mut self, scroll: [usize; 3]) {
self.scroll = scroll;
}
pub fn set_viewport(&mut self, width: u32, height: u32) {
self.width = width;
self.height = height;
}
pub fn set_reduced_motion(&mut self, reduced_motion: bool) {
self.reduced_motion = reduced_motion;
}
pub fn set_animating(&mut self, animating: bool) {
self.animating = animating;
}
#[must_use]
pub fn token(&self) -> FrameToken {
self.token.clone()
}
#[must_use]
pub fn artifact(&self) -> &Arc<Artifact> {
&self.artifact
}
#[must_use]
pub const fn commit_status(&self) -> CommitStatus {
self.commit_status
}
#[must_use]
pub const fn confirmed_commit_status(&self) -> CommitStatus {
self.confirmed_commit_status
}
#[must_use]
pub const fn optimistic(&self) -> bool {
self.optimistic
}
#[must_use]
pub const fn position(&self) -> f64 {
self.position
}
#[must_use]
pub const fn velocity(&self) -> f64 {
self.velocity
}
#[must_use]
pub const fn target(&self) -> View {
self.target
}
#[must_use]
pub const fn scroll(&self) -> [usize; 3] {
self.scroll
}
#[must_use]
pub const fn width(&self) -> u32 {
self.width
}
#[must_use]
pub const fn height(&self) -> u32 {
self.height
}
#[must_use]
pub const fn reduced_motion(&self) -> bool {
self.reduced_motion
}
#[must_use]
pub const fn animating(&self) -> bool {
self.animating
}
#[must_use]
pub const fn status_label(&self) -> &'static str {
if !self.optimistic {
return self.commit_status.label();
}
match self.commit_status {
CommitStatus::Proposed => "Proposal pending",
CommitStatus::Running => "Run pending",
CommitStatus::Failed => "Failure pending",
CommitStatus::Committed => "Commit pending",
}
}
#[must_use]
pub const fn scene_revision(&self) -> u64 {
self.token.revision
}
#[must_use]
pub fn hit_test(&self, x: f64, y: f64) -> Option<View> {
let width = f64::from(self.width);
let height = f64::from(self.height);
if !x.is_finite()
|| !y.is_finite()
|| x < 0.0
|| x >= width
|| y < height - 56.0
|| y >= height
{
return None;
}
let third = width / 3.0;
if x < third {
Some(View::Source)
} else if x < third + third {
Some(View::Proposal)
} else {
Some(View::Evidence)
}
}
}