Skip to content

Commit 2ec59c9

Browse files
committed
fix(client): 🐛 windows double input
1 parent 40ca959 commit 2ec59c9

File tree

6 files changed

+25
-11
lines changed

6 files changed

+25
-11
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fresh-client"
3-
version = "1.1.2"
3+
version = "1.1.3"
44
authors = ["David Frnoch <[email protected]>"]
55
edition = "2021"
66
repository = "https://github.com/lnxcz/fresh"

client/src/connection.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::input::Mode;
22
use common::{config::ClientConfig, proto::Sndr, socket::Socket};
3-
use crossterm::event::KeyCode;
4-
use std::{net::TcpStream, time::Instant};
3+
use std::{net::TcpStream};
54

65
pub struct State {
76
pub username: String,
87
pub room_name: String,
98
pub mode: Mode,
109
pub buffered_messages: Vec<String>,
11-
pub last_key: Option<(Instant, KeyCode)>,
10+
#[cfg(target_os = "windows")]
11+
pub last_key: Option<(std::time::Instant, crossterm::event::KeyCode)>,
1212
pub local_address: String,
1313
pub server_address: String,
1414
pub socket: Socket,

client/src/input.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
use common::proto::Sndr;
99
use crossterm::{event, event::Event, event::KeyCode};
1010
use log::trace;
11-
use std::time::Duration;
11+
use std::time::{Duration, Instant};
1212

1313
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1414
pub enum Mode {
@@ -126,11 +126,26 @@ fn input_key(event: event::KeyEvent, screen: &mut Screen, state: &mut State) {
126126
pub fn process_user_typing(screen: &mut Screen, state: &mut State) -> crossterm::Result<bool> {
127127
let mut should_refresh = false;
128128

129-
while event::poll(Duration::from_millis(0))? {
129+
while event::poll(Duration::default())? {
130130
let prev_mode = state.mode;
131131

132132
if let Ok(Event::Key(event)) = event::read() {
133133
trace!("event: {:?}", event);
134+
135+
//FIXME: This is a hack to prevent the same key from being processed twice on windows
136+
#[cfg(target_os = "windows")]
137+
{
138+
if let Some((last_time, last_key)) = state.last_key {
139+
if event.code == last_key && last_time.elapsed() < Duration::from_millis(100) {
140+
// Continue the loop without processing the event
141+
continue;
142+
}
143+
}
144+
145+
// Update the last key pressed and its time
146+
state.last_key = Some((Instant::now(), event.code));
147+
}
148+
134149
match state.mode {
135150
Mode::Command | Mode::Delete => command_key(event, screen, state),
136151
Mode::Insert => input_key(event, screen, state),
@@ -143,8 +158,6 @@ pub fn process_user_typing(screen: &mut Screen, state: &mut State) -> crossterm:
143158
write_mode_line(screen, state);
144159
}
145160
should_refresh = true;
146-
147-
break;
148161
}
149162

150163
Ok(should_refresh)

client/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ fn main() {
105105
mode: Mode::Insert,
106106
local_address: String::default(),
107107
buffered_messages: Vec::new(),
108+
#[cfg(target_os = "windows")]
108109
last_key: None,
109110
server_address: socket.get_addr().unwrap(),
110111
socket,

server/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fresh-server"
3-
version = "1.1.2"
3+
version = "1.1.3"
44
authors = ["David Frnoch <[email protected]>"]
55
edition = "2021"
66
repository = "https://github.com/lnxcz/fresh"

0 commit comments

Comments
 (0)