Skip to content

Commit

Permalink
Use typesafe grid and pixel units
Browse files Browse the repository at this point in the history
  • Loading branch information
fredizzimo committed Apr 14, 2024
1 parent d2f54c8 commit c154553
Show file tree
Hide file tree
Showing 16 changed files with 555 additions and 568 deletions.
10 changes: 5 additions & 5 deletions src/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use tokio::runtime::{Builder, Runtime};
use winit::event_loop::EventLoopProxy;

use crate::{
cmd_line::CmdLineSettings, dimensions::Dimensions, editor::start_editor, running_tracker::*,
settings::*, window::UserEvent,
cmd_line::CmdLineSettings, editor::start_editor, running_tracker::*, settings::*,
units::GridSize, window::UserEvent,
};
pub use handler::NeovimHandler;
use session::{NeovimInstance, NeovimSession};
Expand Down Expand Up @@ -169,7 +169,7 @@ pub async fn show_error_message(
nvim.echo(prepared_lines, true, vec![]).await
}

async fn launch(handler: NeovimHandler, grid_size: Option<Dimensions>) -> Result<NeovimSession> {
async fn launch(handler: NeovimHandler, grid_size: Option<GridSize<u32>>) -> Result<NeovimSession> {
let neovim_instance = neovim_instance()?;

let session = NeovimSession::new(neovim_instance, handler)
Expand Down Expand Up @@ -215,7 +215,7 @@ async fn launch(handler: NeovimHandler, grid_size: Option<Dimensions>) -> Result

// Triggers loading the user config

let grid_size = grid_size.map_or(DEFAULT_GRID_SIZE, |v| v.clamped_grid_size());
let grid_size = grid_size.map_or(DEFAULT_GRID_SIZE, |v| clamped_grid_size(&v));
let res = session
.neovim
.ui_attach(grid_size.width as i64, grid_size.height as i64, &options)
Expand Down Expand Up @@ -251,7 +251,7 @@ impl NeovimRuntime {
pub fn launch(
&mut self,
event_loop_proxy: EventLoopProxy<UserEvent>,
grid_size: Option<Dimensions>,
grid_size: Option<GridSize<u32>>,
) -> Result<()> {
let handler = start_editor(event_loop_proxy);
let runtime = self.runtime.as_ref().unwrap();
Expand Down
19 changes: 0 additions & 19 deletions src/dimensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,13 @@ use std::{
use serde::{Deserialize, Serialize};
use winit::dpi::PhysicalSize;

use crate::settings;

// Maybe this should be independent from serialization?
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
pub struct Dimensions {
pub width: u64,
pub height: u64,
}

impl Dimensions {
pub fn clamped_grid_size(&self) -> Self {
let min = settings::MIN_GRID_SIZE;
let max = settings::MAX_GRID_SIZE;
Dimensions {
width: self.width.clamp(min.width, max.width),
height: self.height.clamp(min.height, max.height),
}
}
}

impl Default for Dimensions {
fn default() -> Self {
settings::DEFAULT_GRID_SIZE
}
}

impl FromStr for Dimensions {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod profiling;
mod renderer;
mod running_tracker;
mod settings;
mod units;
mod utils;
mod window;

Expand Down
68 changes: 32 additions & 36 deletions src/renderer/animation_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use skia_safe::Point;
use crate::units::PixelPos;

#[allow(dead_code)]
pub fn ease_linear(t: f32) -> f32 {
Expand Down Expand Up @@ -73,11 +73,16 @@ pub fn ease(ease_func: fn(f32) -> f32, start: f32, end: f32, t: f32) -> f32 {
lerp(start, end, ease_func(t))
}

pub fn ease_point(ease_func: fn(f32) -> f32, start: Point, end: Point, t: f32) -> Point {
Point {
x: ease(ease_func, start.x, end.x, t),
y: ease(ease_func, start.y, end.y, t),
}
pub fn ease_point(
ease_func: fn(f32) -> f32,
start: PixelPos<f32>,
end: PixelPos<f32>,
t: f32,
) -> PixelPos<f32> {
PixelPos::new(
ease(ease_func, start.x, end.x, t),
ease(ease_func, start.y, end.y, t),
)
}

pub struct CriticallyDampedSpringAnimation {
Expand Down Expand Up @@ -205,79 +210,70 @@ mod test {

#[test]
fn test_ease_point_linear() {
let start = Point { x: 0.0, y: 0.0 };
let end = Point { x: 1.0, y: 1.0 };
let start = PixelPos::new(0.0, 0.0);
let end = PixelPos::new(1.0, 1.0);
assert_eq!(ease_point(ease_linear, start, end, 1.0), end);
}

#[test]
fn test_ease_point_in_quad() {
let start = Point { x: 0.0, y: 0.0 };
let end = Point { x: 1.0, y: 1.0 };
let start = PixelPos::new(0.0, 0.0);
let end = PixelPos::new(1.0, 1.0);
assert_eq!(ease_point(ease_in_quad, start, end, 1.0), end);
}

#[test]
fn test_ease_point_out_quad() {
let start = Point { x: 0.0, y: 0.0 };
let end = Point { x: 1.0, y: 1.0 };
let start = PixelPos::new(0.0, 0.0);
let end = PixelPos::new(1.0, 1.0);
assert_eq!(ease_point(ease_out_quad, start, end, 1.0), end);
}

#[test]
fn test_ease_point_in_out_quad() {
let start = Point { x: 0.0, y: 0.0 };
let end = Point { x: 1.0, y: 1.0 };
let expected = Point {
x: 0.68000007,
y: 0.68000007,
};
let start = PixelPos::new(0.0, 0.0);
let end = PixelPos::new(1.0, 1.0);
let expected = PixelPos::new(0.68000007, 0.68000007);
assert_eq!(ease_point(ease_in_out_quad, start, end, 1.0), end);
assert_eq!(ease_point(ease_in_out_quad, start, end, 1.4), expected);
}

#[test]
fn test_ease_point_in_cubic() {
let start = Point { x: 0.0, y: 0.0 };
let end = Point { x: 1.0, y: 1.0 };
let start = PixelPos::new(0.0, 0.0);
let end = PixelPos::new(1.0, 1.0);
assert_eq!(ease_point(ease_in_cubic, start, end, 1.0), end);
}

#[test]
fn test_ease_point_out_cubic() {
let start = Point { x: 0.0, y: 0.0 };
let end = Point { x: 1.0, y: 1.0 };
let start = PixelPos::new(0.0, 0.0);
let end = PixelPos::new(1.0, 1.0);
assert_eq!(ease_point(ease_out_cubic, start, end, 1.0), end);
}

#[test]
fn test_ease_point_in_out_cubic() {
let start = Point { x: 0.0, y: 0.0 };
let end = Point { x: 1.0, y: 1.0 };
let expected = Point {
x: 0.0625,
y: 0.0625,
};
let start = PixelPos::new(0.0, 0.0);
let end = PixelPos::new(1.0, 1.0);
let expected = PixelPos::new(0.0625, 0.0625);
assert_eq!(ease_point(ease_in_out_cubic, start, end, 1.0), end);
assert_eq!(ease_point(ease_in_out_cubic, start, end, 0.25), expected);
}

#[test]
fn test_ease_point_in_expo() {
let start = Point { x: 0.0, y: 0.0 };
let end = Point { x: 1.0, y: 1.0 };
let start = PixelPos::new(0.0, 0.0);
let end = PixelPos::new(1.0, 1.0);
assert_eq!(ease_point(ease_in_expo, start, end, 1.0), end);
assert_eq!(ease_point(ease_in_expo, start, end, 0.0), start);
}

#[test]
fn test_ease_point_out_expo() {
let start = Point { x: 0.0, y: 0.0 };
let end = Point { x: 1.0, y: 1.0 };
let expected = Point {
x: 0.9995117,
y: 0.9995117,
};
let start = PixelPos::new(0.0, 0.0);
let end = PixelPos::new(1.0, 1.0);
let expected = PixelPos::new(0.9995117, 0.9995117);
assert_eq!(ease_point(ease_out_expo, start, end, 1.0), end);
assert_eq!(ease_point(ease_out_expo, start, end, 1.1), expected);
}
Expand Down
Loading

0 comments on commit c154553

Please sign in to comment.