use std::time::{Duration, Instant};
use gpui::{
App, Global, Hsla, IntoElement, ParentElement, Pixels, Point, RenderOnce, Styled, Window,
anchored, deferred, div, point, px,
};
use crate::motion::{MotionSettings, Spring, cubic_bezier, elapsed};
use crate::theme::{Scheme, Theme, alpha};
pub const SCHEME_BLEND: f32 = 0.45;
#[derive(Default)]
struct SchemeBlend(u64);
impl Global for SchemeBlend {}
pub fn animate_scheme(scheme: Scheme, dark: bool, cx: &mut App) {
let settings = MotionSettings::get(cx);
let from = Theme::get(cx).scheme;
let generation = {
let blend = cx.default_global::<SchemeBlend>();
blend.0 += 1;
blend.0
};
if settings.reduced || from == scheme {
Theme::set_scheme(scheme, dark, cx);
return;
}
let start = Instant::now();
let ease = cubic_bezier(0.2, 0., 0., 1.);
cx.spawn(async move |cx| {
loop {
cx.background_executor()
.timer(Duration::from_millis(16))
.await;
let done = cx
.update(|cx| {
if cx.global::<SchemeBlend>().0 != generation {
return true;
}
let t = (elapsed(start, cx) / SCHEME_BLEND).min(1.);
let current = from.lerp(&scheme, ease(t));
cx.set_global(Theme {
scheme: current,
dark,
seed: None,
});
cx.refresh_windows();
t >= 1.
})
.unwrap_or(true);
if done {
break;
}
}
})
.detach();
}
pub fn animate_theme(dark: bool, seed: Option<Hsla>, cx: &mut App) {
let target = Theme::new(dark, seed);
animate_scheme(target.scheme, dark, cx);
if seed.is_some() {
let generation = cx.global::<SchemeBlend>().0;
cx.spawn(async move |cx| {
cx.background_executor()
.timer(Duration::from_secs_f32(SCHEME_BLEND + 0.1))
.await;
cx.update(|cx| {
let current = cx.global::<SchemeBlend>().0;
let theme = Theme::get(cx);
if current == generation && theme.scheme == target.scheme {
cx.set_global(Theme { seed, ..theme });
}
})
.ok();
})
.detach();
}
}
#[derive(Clone, Copy)]
struct Reveal {
origin: Point<Pixels>,
radius: f32,
color: Hsla,
scheme: Scheme,
dark: bool,
start: Instant,
swapped: Option<Instant>,
}
#[derive(Default)]
struct RevealState(Option<Reveal>);
impl Global for RevealState {}
const REVEAL_FADE: f32 = 0.2;
pub fn theme_reveal(scheme: Scheme, dark: bool, window: &mut Window, cx: &mut App) {
let origin = window.mouse_position();
theme_reveal_at(scheme, dark, origin, window, cx);
}
pub fn theme_reveal_at(
scheme: Scheme,
dark: bool,
origin: Point<Pixels>,
window: &mut Window,
cx: &mut App,
) {
if MotionSettings::get(cx).reduced {
Theme::set_scheme(scheme, dark, cx);
return;
}
let size = window.viewport_size();
let (x, y) = (f32::from(origin.x), f32::from(origin.y));
let (w, h) = (f32::from(size.width), f32::from(size.height));
let radius = x.max(w - x).hypot(y.max(h - y));
cx.set_global(RevealState(Some(Reveal {
origin,
radius,
color: scheme.surface_container,
scheme,
dark,
start: Instant::now(),
swapped: None,
})));
window.refresh();
}
#[derive(IntoElement, Default)]
pub struct ThemeReveal {
color: Option<Hsla>,
}
impl ThemeReveal {
pub fn new() -> Self {
Self::default()
}
pub fn color(mut self, color: Hsla) -> Self {
self.color = Some(color);
self
}
}
impl RenderOnce for ThemeReveal {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let Some(mut reveal) = cx.try_global::<RevealState>().and_then(|r| r.0) else {
return div().into_any_element();
};
let spring = Spring::SPATIAL_SLOW;
let t = elapsed(reveal.start, cx);
let progress = spring.step(t);
let mut opacity = 1.;
match reveal.swapped {
None if progress >= 0.999 => {
let (scheme, dark) = (reveal.scheme, reveal.dark);
cx.defer(move |cx| Theme::set_scheme(scheme, dark, cx));
reveal.swapped = Some(Instant::now());
cx.set_global(RevealState(Some(reveal)));
}
None => {}
Some(at) => {
let fade = elapsed(at, cx) / REVEAL_FADE;
if fade >= 1. {
cx.set_global(RevealState(None));
return div().into_any_element();
}
opacity = 1. - fade;
}
}
crate::motion::request_frame(window);
let r = (reveal.radius * progress.max(0.)).max(0.);
let color = alpha(self.color.unwrap_or(reveal.color), opacity);
let viewport = window.viewport_size();
deferred(
anchored().position(point(px(0.), px(0.))).child(
div()
.w(viewport.width)
.h(viewport.height)
.overflow_hidden()
.child(
div()
.absolute()
.left(reveal.origin.x - px(r))
.top(reveal.origin.y - px(r))
.size(px(r * 2.))
.rounded(px(r))
.bg(color),
),
),
)
.with_priority(2)
.into_any_element()
}
}