1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-23 15:04:36 +03:00

refactor: visual bell easing to a helper

This commit is contained in:
Wez Furlong 2022-02-06 17:23:54 -07:00
parent 75e785e01e
commit 7b4c3d0397
4 changed files with 75 additions and 27 deletions

View File

@ -1,7 +1,7 @@
use crate::*;
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/easing-function>
#[derive(Debug, Deserialize, Serialize, Clone)]
#[derive(Debug, Deserialize, Serialize, Clone, Copy)]
pub enum EasingFunction {
Linear,
CubicBezier(f32, f32, f32, f32),

View File

@ -0,0 +1,64 @@
use config::EasingFunction;
use std::time::{Duration, Instant};
#[derive(Debug)]
pub struct ColorEase {
in_duration: f32,
in_function: EasingFunction,
out_duration: f32,
out_function: EasingFunction,
start: Option<Instant>,
}
impl ColorEase {
pub fn new(
in_duration_ms: u64,
in_function: EasingFunction,
out_duration_ms: u64,
out_function: EasingFunction,
start: Option<Instant>,
) -> Self {
Self {
in_duration: Duration::from_millis(in_duration_ms).as_secs_f32(),
in_function,
out_duration: Duration::from_millis(out_duration_ms).as_secs_f32(),
out_function,
start,
}
}
pub fn intensity_continuous(&mut self) -> f32 {
match self.intensity_one_shot() {
Some(intensity) => intensity,
None => {
// Start a new cycle
self.start.replace(Instant::now());
self.intensity_one_shot().expect("just started")
}
}
}
pub fn intensity_one_shot(&mut self) -> Option<f32> {
let start = self.start?;
let elapsed = start.elapsed().as_secs_f32();
let intensity = if elapsed < self.in_duration {
Some(
self.in_function
.evaluate_at_position(elapsed / self.in_duration),
)
} else {
let completion = (elapsed - self.in_duration) / self.out_duration;
if completion >= 1.0 {
None
} else {
Some(1.0 - self.out_function.evaluate_at_position(completion))
}
};
if intensity.is_none() {
self.start.take();
}
intensity
}
}

View File

@ -25,6 +25,7 @@ use wezterm_gui_subcommands::*;
use wezterm_toast_notification::*;
mod cache;
mod colorease;
mod customglyph;
mod download;
mod frontend;

View File

@ -1,4 +1,5 @@
use super::box_model::*;
use crate::colorease::ColorEase;
use crate::customglyph::{BlockKey, *};
use crate::glium::texture::SrgbTexture2d;
use crate::glyphcache::{CachedGlyph, GlyphCache};
@ -342,33 +343,15 @@ impl super::TermWindow {
let mut per_pane = self.pane_state(pane.pane_id());
if let Some(ringing) = per_pane.bell_start {
if config.visual_bell.target == target {
let elapsed = ringing.elapsed().as_secs_f32();
let mut color_ease = ColorEase::new(
config.visual_bell.fade_in_duration_ms,
config.visual_bell.fade_in_function,
config.visual_bell.fade_out_duration_ms,
config.visual_bell.fade_out_function,
Some(ringing),
);
let in_duration =
Duration::from_millis(config.visual_bell.fade_in_duration_ms).as_secs_f32();
let out_duration =
Duration::from_millis(config.visual_bell.fade_out_duration_ms).as_secs_f32();
let intensity = if elapsed < in_duration {
Some(
config
.visual_bell
.fade_in_function
.evaluate_at_position(elapsed / in_duration),
)
} else {
let completion = (elapsed - in_duration) / out_duration;
if completion >= 1.0 {
None
} else {
Some(
1.0 - config
.visual_bell
.fade_out_function
.evaluate_at_position(completion),
)
}
};
let intensity = color_ease.intensity_one_shot();
match intensity {
None => {