Disable cursor when window not in focus

This commit is contained in:
daa84 2017-03-31 12:49:14 +03:00
parent e80c26ba27
commit 0bd7356cd6
2 changed files with 44 additions and 8 deletions

View File

@ -30,11 +30,13 @@ impl Alpha {
}
}
#[derive(PartialEq)]
enum AnimPhase {
Shown,
Hide,
Hidden,
Show,
NoFocus,
}
struct State {
@ -53,9 +55,13 @@ impl State {
}
}
fn reset(&mut self) {
fn reset_to(&mut self, phase: AnimPhase) {
self.alpha = Alpha(1.0);
self.anim_phase = AnimPhase::Shown;
self.anim_phase = phase;
if let Some(timer_id) = self.timer {
glib::source_remove(timer_id);
self.timer = None;
}
}
}
@ -71,16 +77,21 @@ impl Cursor {
pub fn start(&mut self) {
let state = self.state.clone();
let mut mut_state = self.state.borrow_mut();
mut_state.reset();
if let Some(timer_id) = mut_state.timer {
glib::source_remove(timer_id);
}
mut_state.reset_to(AnimPhase::Shown);
mut_state.timer = Some(glib::timeout_add(500, move || anim_step(&state)));
}
pub fn reset_state(&mut self) {
self.start();
}
pub fn enter_focus(&mut self) {
self.start();
}
pub fn leave_focus(&mut self) {
self.state.borrow_mut().reset_to(AnimPhase::NoFocus);
}
pub fn draw(&self,
ctx: &cairo::Context,
@ -106,7 +117,11 @@ impl Cursor {
};
ctx.rectangle(current_point.0, line_y, cursor_width, line_height);
ctx.fill();
if state.anim_phase == AnimPhase::NoFocus {
ctx.stroke();
} else {
ctx.fill();
}
}
}
@ -142,6 +157,7 @@ fn anim_step(state: &Arc<UiMutex<State>>) -> glib::Continue {
None
}
}
AnimPhase::NoFocus => None,
};
SHELL!(&shell = {

View File

@ -5,7 +5,7 @@ use pangocairo as pc;
use pango;
use pango::FontDescription;
use gdk::{ModifierType, EventKey, EventConfigure, EventButton, EventMotion, EventType, EventScroll,
ScrollDirection};
ScrollDirection, EventFocus};
use gdk_sys;
use glib;
use gtk::prelude::*;
@ -88,6 +88,8 @@ impl Shell {
self.drawing_area.connect_draw(gtk_draw);
self.drawing_area.connect_key_press_event(gtk_key_press);
self.drawing_area.connect_scroll_event(gtk_scroll_event);
self.drawing_area.connect_focus_in_event(gtk_focus_in);
self.drawing_area.connect_focus_out_event(gtk_focus_out);
}
pub fn add_configure_event(&mut self) {
@ -138,6 +140,24 @@ impl Shell {
}
}
fn gtk_focus_in(_: &DrawingArea, _: &EventFocus) -> Inhibit {
SHELL!(shell = {
shell.cursor.enter_focus();
let point = shell.model.cur_point();
shell.on_redraw(&RepaintMode::Area(point));
});
Inhibit(false)
}
fn gtk_focus_out(_: &DrawingArea, _: &EventFocus) -> Inhibit {
SHELL!(shell = {
shell.cursor.leave_focus();
let point = shell.model.cur_point();
shell.on_redraw(&RepaintMode::Area(point));
});
Inhibit(false)
}
fn gtk_scroll_event(_: &DrawingArea, ev: &EventScroll) -> Inhibit {
SHELL!(shell = {
if !shell.mouse_enabled {