Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adaptive height with animation #143

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ syslog = "6.0.0"
defaults = "0.2.0"
freedesktop-icon-lookup = "0.1.0"
sublime_fuzzy = "0.7.0"
keyframe = "1.1.1"

[profile.release-lto]
lto = true
Expand Down
122 changes: 122 additions & 0 deletions src/animation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
use keyframe::{ease_with_scaled_time, functions::EaseInOut};
use std::{
collections::HashMap,
time::{Duration, Instant},
};

#[derive(Default)]
pub struct Animator {
animations: HashMap<Animation, AnimationInner>,
remove_later: Vec<Animation>,
}

#[derive(Debug)]
struct AnimationInner {
start: f64,
end: f64,
current: f64,

start_time: Instant,
duration: Duration,
}

#[derive(Eq, PartialEq, Debug, Hash, Clone)]
pub enum Animation {
Height,
}

pub struct AnimationConfig {
pub height: Duration,
}

impl Animator {
pub fn new() -> Self {
Self::default()
}

pub fn add_animation(&mut self, name: Animation, start: f64, end: f64, duration: Duration) {
match self.animations.entry(name) {
std::collections::hash_map::Entry::Occupied(o) =>
panic!("Add already existed animation with {:?}. It is a bug. Please submit a bug to https://github.com/l4l/yofi/issues", o),
std::collections::hash_map::Entry::Vacant(v) => {
v.insert(AnimationInner::new(start, end, duration));
}
}
}

pub fn cancel_animation(&mut self, name: Animation) {
self.animations.remove(&name);
}

pub fn contains(&self, name: Animation) -> bool {
self.animations.contains_key(&name)
}

pub fn get_value(&mut self, name: Animation) -> Option<f64> {
Some(self.animations.get(&name)?.current)
}

pub fn proceed(&mut self) -> bool {
let current_time = Instant::now();

for name in std::mem::take(&mut self.remove_later).into_iter() {
self.animations.remove(&name).unwrap();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could get a panic here, reproduction:

seq 100 | cargo r dialog (debug build is important)

then quickly type any two digits

}

for (name, animation) in self.animations.iter_mut() {
let time = (current_time - animation.start_time).as_millis() as f64;

let mut start = animation.start;
let mut end = animation.end;

let flip = if start >= end {
std::mem::swap(&mut start, &mut end);
true
} else {
false
};

let mut value = ease_with_scaled_time(
EaseInOut,
start,
end,
time,
animation.duration.as_millis() as f64,
);

if value >= end {
self.remove_later.push(name.clone());
}

if flip {
let delta = value - start;
value = end - delta;
}

animation.current = value;
}

!self.animations.is_empty()
}

// Minimum time of proceed animation in event loop
pub fn proceed_step(&self) -> Option<Duration> {
if self.animations.is_empty() {
None
} else {
Some(Duration::from_millis(100))
}
}
}

impl AnimationInner {
fn new(start: f64, end: f64, duration: Duration) -> Self {
Self {
start,
end,
current: start,
start_time: Instant::now(),
duration,
}
}
}
17 changes: 17 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::ffi::CString;
use std::path::PathBuf;
use std::time::Duration;

use defaults::Defaults;
use serde::Deserialize;
Expand Down Expand Up @@ -30,6 +31,8 @@ pub struct Config {
#[def = "512"]
height: u32,
#[def = "false"]
auto_height: bool,
#[def = "false"]
force_window: bool,
window_offsets: Option<(i32, i32)>,
scale: Option<u16>,
Expand All @@ -44,6 +47,7 @@ pub struct Config {
corner_radius: Radius,

icon: Option<Icon>,
animation: Option<Animation>,

input_text: InputText,
list_items: ListItems,
Expand All @@ -54,6 +58,10 @@ impl Config {
self.icon = None;
}

pub fn animation(&self) -> &Option<Animation> {
&self.animation
}

pub fn set_prompt(&mut self, prompt: String) {
self.input_text.prompt = Some(prompt);
}
Expand Down Expand Up @@ -99,6 +107,8 @@ struct ListItems {
item_spacing: f32,
#[def = "10.0"]
icon_spacing: f32,
#[def = "true"]
show_default: bool,
}

#[derive(Defaults, Deserialize)]
Expand All @@ -110,6 +120,13 @@ struct Icon {
fallback_icon_path: Option<PathBuf>,
}

#[derive(Defaults, Deserialize)]
#[serde(default)]
pub struct Animation {
#[def = "500"]
height_duration_ms: u64,
}

fn config_path() -> PathBuf {
xdg::BaseDirectories::with_prefix(crate::prog_name!())
.expect("failed to get xdg dirs")
Expand Down
12 changes: 12 additions & 0 deletions src/config/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::rc::Rc;
use once_cell::unsync::Lazy;

use super::*;
use crate::animation::AnimationConfig;
use crate::desktop::IconConfig;
use crate::draw::{BgParams, InputTextParams, ListParams};
use crate::font::{Font, FontBackend, InnerFont};
Expand Down Expand Up @@ -80,6 +81,7 @@ impl<'a> From<&'a Config> for ListParams {
action_left_margin: config.list_items.action_left_margin,
item_spacing: config.list_items.item_spacing,
icon_spacing: config.list_items.icon_spacing,
show_default: config.list_items.show_default,
}
}
}
Expand All @@ -92,6 +94,7 @@ impl<'a> From<&'a Config> for BgParams {
(Some(c), None) => Some((c, DEFAULT_BG_BORDER_WIDTH)),
(None, Some(w)) => Some((DEFAULT_BG_BORDER_COLOR, w)),
};

BgParams {
width: config.width,
height: config.height,
Expand All @@ -110,6 +113,7 @@ impl<'a> From<&'a Config> for SurfaceParams {
force_window: config.force_window,
window_offsets: config.window_offsets,
scale: config.scale,
auto_height: config.auto_height,
}
}
}
Expand All @@ -123,6 +127,14 @@ impl<'a> From<&'a Config> for Option<IconConfig> {
}
}

impl<'a> From<&'a Config> for Option<AnimationConfig> {
fn from(config: &'a Config) -> Option<AnimationConfig> {
config.animation.as_ref().map(|c| AnimationConfig {
height: Duration::from_millis(c.height_duration_ms),
})
}
}

fn default_font() -> Font {
std::thread_local! {
static DEFAULT_FONT: Lazy<Font> = Lazy::new(|| Rc::new(InnerFont::default()));
Expand Down
6 changes: 4 additions & 2 deletions src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use raqote::{DrawOptions, Path, PathBuilder, Source, StrokeStyle};

pub use background::Params as BgParams;
pub use input_text::Params as InputTextParams;
pub use list_view::{ListItem, Params as ListParams};
pub use list_view::{ListItem, Params as ListParams, ADDITIONAL_CAP};

use crate::{style::Radius, Color};

pub use self::list_view::ListViewInfo;

pub type DrawTarget<'a> = raqote::DrawTarget<&'a mut [u32]>;

mod background;
Expand Down Expand Up @@ -43,7 +45,7 @@ impl<'a, It> Widget<'a, It> {
items: It,
skip_offset: usize,
selected_item: usize,
tx: Sender<usize>,
tx: Sender<ListViewInfo>,
params: &'a ListParams,
) -> Self {
Self::ListView(list_view::ListView::new(
Expand Down
Loading