use lgwks_deps::gpui::{self, Bounds, Hsla, PathBuilder, Pixels, Point, Window, point, size};
use crate::design::Rgb;
use crate::ink_lab::{Effect, Shape};
use crate::native::{TILE_HEIGHT, TILE_WIDTH, narrow, pixels};
#[derive(Clone, Copy, Debug)]
pub(super) struct Placement {
left: f64,
top: f64,
scale: f64,
}
impl Placement {
pub(super) fn new(bounds: Bounds<Pixels>, design: (f64, f64)) -> Self {
let width = f64::from(bounds.size.width);
let height = f64::from(bounds.size.height);
let scale = (width / design.0).min(height / design.1);
Self {
left: f64::from(bounds.origin.x) + (width - design.0 * scale) * 0.5,
top: f64::from(bounds.origin.y) + (height - design.1 * scale) * 0.5,
scale,
}
}
pub(super) fn point(self, design_x: f64, design_y: f64) -> Point<Pixels> {
point(
pixels(self.left + design_x * self.scale),
pixels(self.top + design_y * self.scale),
)
}
pub(super) fn to_design(self, position: Point<Pixels>) -> (f64, f64) {
(
(f64::from(position.x) - self.left) / self.scale,
(f64::from(position.y) - self.top) / self.scale,
)
}
pub(super) fn length(self, design: f64) -> Pixels {
pixels(design * self.scale)
}
}
pub(super) struct Scene<'frame> {
pub(super) shapes: &'frame [Shape],
pub(super) effect: Effect,
pub(super) alpha: f64,
pub(super) ink: Rgb,
pub(super) accent: Rgb,
pub(super) placement: Placement,
}
impl Scene<'_> {
pub(super) fn paint(&self, window: &mut Window) -> u64 {
let ink = colour(self.ink, self.alpha);
let accent = colour(self.accent, self.alpha);
let mut failures = 0_u64;
for (index, shape) in self.shapes.iter().enumerate() {
let fill = if self.effect == Effect::Mark && index == 23 {
accent
} else {
ink
};
if !self.paint_shape(window, *shape, fill) {
failures = failures.saturating_add(1);
}
}
failures
}
fn paint_shape(&self, window: &mut Window, shape: Shape, fill: Hsla) -> bool {
let place = self.placement;
match shape {
Shape::Circle {
centre_x,
centre_y,
radius,
} => {
if radius > 0.0 {
let origin = place.point(centre_x - radius, centre_y - radius);
let side = place.length(radius * 2.0);
window.paint_quad(
gpui::fill(Bounds::new(origin, size(side, side)), fill)
.corner_radii(place.length(radius)),
);
}
true
}
Shape::Ellipse {
centre_x,
centre_y,
radius_x,
radius_y,
} => {
let radii = point(place.length(radius_x), place.length(radius_y));
let mut builder = PathBuilder::fill();
builder.move_to(place.point(centre_x + radius_x, centre_y));
builder.arc_to(
radii,
pixels(0.0),
false,
false,
place.point(centre_x - radius_x, centre_y),
);
builder.arc_to(
radii,
pixels(0.0),
false,
false,
place.point(centre_x + radius_x, centre_y),
);
builder.close();
builder
.build()
.map(|path| window.paint_path(path, fill))
.is_ok()
}
Shape::Quad { points } => {
let corners = points.map(|corner| place.point(corner[0], corner[1]));
let mut builder = PathBuilder::fill();
builder.add_polygon(&corners, true);
builder
.build()
.map(|path| window.paint_path(path, fill))
.is_ok()
}
Shape::Rect {
left,
top,
right,
bottom,
} => {
let origin = place.point(left, top);
let extent = size(place.length(right - left), place.length(bottom - top));
window.paint_quad(gpui::fill(Bounds::new(origin, extent), fill));
true
}
}
}
}
pub(super) const fn design_box(effect: Effect) -> (f64, f64) {
match effect {
Effect::Mark => (100.0, 100.0),
Effect::Loader => (60.0, 60.0),
_ => (TILE_WIDTH, TILE_HEIGHT),
}
}
pub(super) fn colour(rgb: Rgb, alpha: f64) -> Hsla {
Hsla::from(gpui::rgb(u32::from_be_bytes([
0, rgb.red, rgb.green, rgb.blue,
])))
.opacity(narrow(alpha.clamp(0.0, 1.0)))
}
#[cfg(test)]
mod tests {
use super::{Placement, design_box};
use crate::ink_lab::Effect;
use crate::runtime::Percentiles;
use lgwks_deps::gpui::{Bounds, point, px, size};
#[test]
fn percentiles_use_nearest_rank_and_report_empty_sets() {
let empty = Percentiles::of(&[]);
assert_eq!(empty.samples, 0, "empty set must say so");
let samples: Vec<f64> = (1..=100).map(f64::from).collect();
let stats = Percentiles::of(&samples);
assert_eq!(stats.samples, 100, "sample count");
assert!((stats.p50 - 50.0).abs() < 1e-9, "p50 {}", stats.p50);
assert!((stats.p99 - 99.0).abs() < 1e-9, "p99 {}", stats.p99);
assert!((stats.max - 100.0).abs() < 1e-9, "max {}", stats.max);
}
#[test]
fn placement_round_trips_pointer_coordinates() {
let bounds = Bounds::new(point(px(40.0), px(30.0)), size(px(248.0), px(168.0)));
let placement = Placement::new(bounds, design_box(Effect::Mark));
let (design_x, design_y) = placement.to_design(placement.point(59.34, 51.0));
assert!((design_x - 59.34).abs() < 1e-3, "x {design_x}");
assert!((design_y - 51.0).abs() < 1e-3, "y {design_y}");
}
}