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 { enum AnimPhase {
Shown, Shown,
Hide, Hide,
Hidden, Hidden,
Show, Show,
NoFocus,
} }
struct State { 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.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,10 +77,7 @@ impl Cursor {
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.borrow_mut(); let mut mut_state = self.state.borrow_mut();
mut_state.reset(); mut_state.reset_to(AnimPhase::Shown);
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))); mut_state.timer = Some(glib::timeout_add(500, move || anim_step(&state)));
} }
@ -82,6 +85,14 @@ impl Cursor {
self.start(); 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, pub fn draw(&self,
ctx: &cairo::Context, ctx: &cairo::Context,
shell: &Shell, shell: &Shell,
@ -106,8 +117,12 @@ impl Cursor {
}; };
ctx.rectangle(current_point.0, line_y, cursor_width, line_height); ctx.rectangle(current_point.0, line_y, cursor_width, line_height);
if state.anim_phase == AnimPhase::NoFocus {
ctx.stroke();
} else {
ctx.fill(); ctx.fill();
} }
}
} }
fn anim_step(state: &Arc<UiMutex<State>>) -> glib::Continue { fn anim_step(state: &Arc<UiMutex<State>>) -> glib::Continue {
@ -142,6 +157,7 @@ fn anim_step(state: &Arc<UiMutex<State>>) -> glib::Continue {
None None
} }
} }
AnimPhase::NoFocus => None,
}; };
SHELL!(&shell = { SHELL!(&shell = {

View File

@ -5,7 +5,7 @@ use pangocairo as pc;
use pango; use pango;
use pango::FontDescription; use pango::FontDescription;
use gdk::{ModifierType, EventKey, EventConfigure, EventButton, EventMotion, EventType, EventScroll, use gdk::{ModifierType, EventKey, EventConfigure, EventButton, EventMotion, EventType, EventScroll,
ScrollDirection}; ScrollDirection, EventFocus};
use gdk_sys; use gdk_sys;
use glib; use glib;
use gtk::prelude::*; use gtk::prelude::*;
@ -88,6 +88,8 @@ impl Shell {
self.drawing_area.connect_draw(gtk_draw); self.drawing_area.connect_draw(gtk_draw);
self.drawing_area.connect_key_press_event(gtk_key_press); self.drawing_area.connect_key_press_event(gtk_key_press);
self.drawing_area.connect_scroll_event(gtk_scroll_event); 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) { 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 { fn gtk_scroll_event(_: &DrawingArea, ev: &EventScroll) -> Inhibit {
SHELL!(shell = { SHELL!(shell = {
if !shell.mouse_enabled { if !shell.mouse_enabled {