Make cursor reset animation state on input

This commit is contained in:
daa 2017-03-26 00:38:22 +03:00
parent e9839b641d
commit e020a5825b
2 changed files with 21 additions and 12 deletions

View File

@ -37,7 +37,7 @@ enum AnimPhase {
Show, Show,
} }
pub struct State { struct State {
alpha: Alpha, alpha: Alpha,
anim_phase: AnimPhase, anim_phase: AnimPhase,
@ -45,13 +45,18 @@ pub struct State {
} }
impl State { impl State {
pub fn new() -> State { fn new() -> State {
State { State {
alpha: Alpha(1.0), alpha: Alpha(1.0),
anim_phase: AnimPhase::Shown, anim_phase: AnimPhase::Shown,
timer: None, timer: None,
} }
} }
fn reset(&mut self) {
self.alpha = Alpha(1.0);
self.anim_phase = AnimPhase::Shown;
}
} }
pub struct Cursor { pub struct Cursor {
@ -61,18 +66,22 @@ pub struct Cursor {
impl Cursor { impl Cursor {
pub fn new() -> Cursor { pub fn new() -> Cursor {
Cursor { Cursor { state: Arc::new(Mutex::new(State::new())) }
state: Arc::new(Mutex::new(State::new())),
}
} }
pub fn start(&mut self) { pub fn start(&mut self) {
let state = self.state.clone(); let state = self.state.clone();
let mut mut_state = self.state.lock().unwrap(); let mut mut_state = self.state.lock().unwrap();
if mut_state.timer.is_none() { mut_state.reset();
mut_state.timer = Some(glib::timeout_add(100, move || anim_step(&state))); if let Some(timer_id) = mut_state.timer {
glib::source_remove(timer_id);
} }
mut_state.timer = Some(glib::timeout_add(500, move || anim_step(&state)));
}
pub fn reset_state(&mut self) {
self.start();
} }
pub fn draw(&self, pub fn draw(&self,
@ -103,7 +112,6 @@ impl Cursor {
} }
} }
// [TODO]: Reset animation phase on events - 2017-03-24 11:33
fn anim_step(state: &Arc<Mutex<State>>) -> glib::Continue { fn anim_step(state: &Arc<Mutex<State>>) -> glib::Continue {
let moved_state = state.clone(); let moved_state = state.clone();
let mut mut_state = state.lock().unwrap(); let mut mut_state = state.lock().unwrap();
@ -111,7 +119,7 @@ fn anim_step(state: &Arc<Mutex<State>>) -> glib::Continue {
let next_event = match mut_state.anim_phase { let next_event = match mut_state.anim_phase {
AnimPhase::Shown => { AnimPhase::Shown => {
mut_state.anim_phase = AnimPhase::Hide; mut_state.anim_phase = AnimPhase::Hide;
Some(100) Some(60)
} }
AnimPhase::Hide => { AnimPhase::Hide => {
if !mut_state.alpha.hide(0.3) { if !mut_state.alpha.hide(0.3) {
@ -125,7 +133,7 @@ fn anim_step(state: &Arc<Mutex<State>>) -> glib::Continue {
AnimPhase::Hidden => { AnimPhase::Hidden => {
mut_state.anim_phase = AnimPhase::Show; mut_state.anim_phase = AnimPhase::Show;
Some(100) Some(60)
} }
AnimPhase::Show => { AnimPhase::Show => {
if !mut_state.alpha.show(0.3) { if !mut_state.alpha.show(0.3) {
@ -143,9 +151,9 @@ fn anim_step(state: &Arc<Mutex<State>>) -> glib::Continue {
shell.on_redraw(&RepaintMode::Area(point)); shell.on_redraw(&RepaintMode::Area(point));
}); });
if let Some(timeout) = next_event { if let Some(timeout) = next_event {
mut_state.timer = Some(glib::timeout_add(timeout, move || anim_step(&moved_state) )); mut_state.timer = Some(glib::timeout_add(timeout, move || anim_step(&moved_state)));
glib::Continue(false) glib::Continue(false)
} else { } else {

View File

@ -219,6 +219,7 @@ fn gtk_key_press(_: &DrawingArea, ev: &EventKey) -> Inhibit {
if let Some(input) = convert_key(ev) { if let Some(input) = convert_key(ev) {
SHELL!(shell = { SHELL!(shell = {
shell.nvim().input(&input).expect("Error run input command to nvim"); shell.nvim().input(&input).expect("Error run input command to nvim");
shell.cursor.reset_state();
}); });
Inhibit(true) Inhibit(true)
} else { } else {