Files
LLeMbas-TTY/term/src/input.rs
T
Wez Furlong 8f1f1a65ea window: handle opengl context loss on windows.
With this commit, we now survive a reinstall or upgrade of the nvidia
drivers on my Windows sytem without crashing.

This commit allows notifying the application of the context loss
so the application can either try to reinit opengl or open a new
window as a replacement and init opengl there.

I've not had success at reinitializing opengl after a driver upgrade;
it seems to be persistently stuck in a state where it fails to allocate
a vertex buffer.

SO, the state we have now is that we try to reinit opengl on a new
window, and if that fails, leave it set to the software renderer.

This isn't a perfect UX, but it is better than terminating!

refs: https://github.com/wez/wezterm/issues/156
2020-06-16 21:17:48 -07:00

81 lines
2.1 KiB
Rust

// clippy hates bitflags
#![cfg_attr(feature = "cargo-clippy", allow(clippy::suspicious_arithmetic_impl, clippy::redundant_field_names))]
use super::VisibleRowIndex;
#[cfg(feature = "use_serde")]
use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant};
pub use termwiz::input::KeyCode;
pub use termwiz::input::Modifiers as KeyModifiers;
#[cfg_attr(feature = "use_serde", derive(Deserialize, Serialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum MouseButton {
Left,
Middle,
Right,
WheelUp(usize),
WheelDown(usize),
None,
}
#[cfg_attr(feature = "use_serde", derive(Deserialize, Serialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum MouseEventKind {
Press,
Release,
Move,
}
#[cfg_attr(feature = "use_serde", derive(Deserialize, Serialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct MouseEvent {
pub kind: MouseEventKind,
pub x: usize,
pub y: VisibleRowIndex,
pub button: MouseButton,
pub modifiers: KeyModifiers,
}
/// This is a little helper that keeps track of the "click streak",
/// which is the number of successive clicks of the same mouse button
/// within the `CLICK_INTERVAL`. The streak is reset to 1 each time
/// the mouse button differs from the last click, or when the elapsed
/// time exceeds `CLICK_INTERVAL`.
#[derive(Debug, Clone)]
pub struct LastMouseClick {
pub button: MouseButton,
time: Instant,
pub streak: usize,
}
/// The multi-click interval, measured in milliseconds
const CLICK_INTERVAL: u64 = 500;
impl LastMouseClick {
pub fn new(button: MouseButton) -> Self {
Self {
button,
time: Instant::now(),
streak: 1,
}
}
pub fn add(&self, button: MouseButton) -> Self {
let now = Instant::now();
let streak = if button == self.button
&& now.duration_since(self.time) <= Duration::from_millis(CLICK_INTERVAL)
{
self.streak + 1
} else {
1
};
Self {
button,
time: now,
streak,
}
}
}