-
Notifications
You must be signed in to change notification settings - Fork 21
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
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d6e2c43
Add animations. Add new param auto_shrink for auto height
filtsin 7fbc01b
Fix clippy warning
filtsin 6358ef9
Update size calculation
filtsin eb13330
Add new parameters
filtsin 0afbdd1
Wrap animator in main proceed thread
filtsin 6335f79
Add animation section in config
filtsin 51603d8
Fix last_y for actions in list_view
filtsin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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