From 5e3c20f39e2c5bdde007b281010ee7777060b5ac Mon Sep 17 00:00:00 2001 From: Pietro Brenna Date: Fri, 27 Mar 2020 20:59:59 +0100 Subject: [PATCH] Rate limito popup --- src/stato_finestra.rs | 50 +++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/src/stato_finestra.rs b/src/stato_finestra.rs index 798ded7..7ab77b4 100644 --- a/src/stato_finestra.rs +++ b/src/stato_finestra.rs @@ -1,3 +1,9 @@ +use gdk::WindowExt; +use gtk::prelude::*; +use std::rc::Rc; +use std::sync::RwLock; +use std::time::{Duration, Instant}; + #[derive(Eq, PartialEq, Debug, Copy, Clone)] pub enum PosizFinestra { Su, @@ -19,10 +25,15 @@ pub use StatoFull::*; pub struct StatoFinestra { posiz: PosizFinestra, full: StatoFull, + last_toggle: Option, } impl StatoFinestra { - pub fn new_arc(p: PosizFinestra, f: StatoFull) -> Arc> { - Arc::new(RwLock::new(StatoFinestra { full: f, posiz: p })) + pub fn new_arc(p: PosizFinestra, f: StatoFull) -> Rc> { + Rc::new(RwLock::new(StatoFinestra { + full: f, + posiz: p, + last_toggle: None, + })) } } pub trait OperazStato { @@ -30,8 +41,10 @@ pub trait OperazStato { fn get_pos(&self) -> PosizFinestra; fn set_full(&self, f: StatoFull); fn get_full(&self) -> StatoFull; + fn set_last_toggle(&self, instant: Option); + fn get_last_toggle(&self) -> Option; } -impl OperazStato for Arc> { +impl OperazStato for Rc> { fn set_pos(&self, p: PosizFinestra) { if let Ok(mut w_lock) = self.write() { w_lock.posiz = p; @@ -56,12 +69,27 @@ impl OperazStato for Arc> { Sconosciuto } } + fn get_last_toggle(&self) -> Option { + if let Ok(r_lock) = self.read() { + r_lock.last_toggle + } else { + None + } + } + fn set_last_toggle(&self, instant: Option) { + if let Ok(mut w_lock) = self.write() { + w_lock.last_toggle = instant; + } + } } -use gdk::WindowExt; -use gtk::prelude::*; -use std::sync::{Arc, RwLock}; -pub fn tira_su(window: >k::ApplicationWindow, stato: &Arc>, ts: u32) { +pub fn tira_su(window: >k::ApplicationWindow, stato: &Rc>, ts: u32) { + if let Some(instant) = stato.get_last_toggle() { + if instant.elapsed() < Duration::from_millis(300) { + return; + } + } + stato.set_last_toggle(Some(Instant::now())); let new_pos = match stato.get_pos() { Nascosta | Nascondendo => { // Shameless copy from guake @@ -98,7 +126,7 @@ pub fn tira_su(window: >k::ApplicationWindow, stato: &Arc>) { +pub fn butta_giu(window: >k::ApplicationWindow, stato: &Rc>) { stato.set_pos(Nascondendo); window.hide(); } @@ -106,7 +134,7 @@ pub fn butta_giu(window: >k::ApplicationWindow, stato: &Arc>, + stato: &Rc>, ) { let ts = gtk::get_current_event_time(); match stato.get_pos() { @@ -119,7 +147,7 @@ pub fn esegui_toggle( } } -pub fn focus_out(stato: &Arc>) -> gtk::Inhibit { +pub fn focus_out(stato: &Rc>) -> gtk::Inhibit { match stato.get_pos() { Nascondendo => { stato.set_pos(Nascosta); @@ -133,7 +161,7 @@ pub fn focus_out(stato: &Arc>) -> gtk::Inhibit { pub fn focus_in( window: >k::ApplicationWindow, - stato: &Arc>, + stato: &Rc>, ) -> gtk::Inhibit { stato.set_pos(Su); let s = calc_w_h(window);