Prioritize user input
This commit is contained in:
parent
8583987a67
commit
c1b052c85d
33
src/main.rs
33
src/main.rs
@ -44,15 +44,21 @@ fn main() {
|
||||
let (tx2, rx2) = mpsc::sync_channel(1);
|
||||
let initial_polling_rate: u64 = 50;
|
||||
let initial_scroll_left: bool = true;
|
||||
let mut user_update = false;
|
||||
// polling thread
|
||||
thread::spawn(move || {
|
||||
let mut polling_rate = initial_polling_rate;
|
||||
let mut new_polling_rate = initial_polling_rate;
|
||||
let mut scroll_left = initial_scroll_left;
|
||||
let mut new_scroll_left = initial_scroll_left;
|
||||
let device_state = DeviceState::new();
|
||||
loop {
|
||||
// Poll for input
|
||||
let keys = device_state.get_keys(); // poll the keys
|
||||
(user_update, new_polling_rate, new_scroll_left) = detect_keys(keys, polling_rate, scroll_left);
|
||||
match rx2.try_recv() {
|
||||
Ok(new_scroll) => {
|
||||
scroll_left = new_scroll;
|
||||
Ok(recv_scroll) => {
|
||||
new_scroll_left = recv_scroll;
|
||||
}
|
||||
Err(mpsc::TryRecvError::Empty) => {
|
||||
//eprintln!("No new data. ;(");
|
||||
@ -62,11 +68,10 @@ fn main() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Poll for input
|
||||
let keys = device_state.get_keys(); // poll the keys
|
||||
(polling_rate, scroll_left) = detect_keys(keys, polling_rate, scroll_left);
|
||||
let _ = tx.send((polling_rate, scroll_left));
|
||||
thread::sleep(Duration::from_millis(100)); // poll every 50 ms
|
||||
scroll_left = new_scroll_left;
|
||||
polling_rate = new_polling_rate;
|
||||
let _ = tx.send((user_update, polling_rate, scroll_left));
|
||||
thread::sleep(Duration::from_millis(100)); // poll every 50 ms
|
||||
}
|
||||
});
|
||||
// main thread
|
||||
@ -76,9 +81,10 @@ fn main() {
|
||||
loop {
|
||||
// Get data from the polling thread
|
||||
match rx.try_recv() {
|
||||
Ok((new_rate, new_scroll)) => {
|
||||
Ok((new_user_update, new_rate, new_scroll)) => {
|
||||
polling_rate = new_rate;
|
||||
scroll_left = new_scroll;
|
||||
user_update = new_user_update;
|
||||
}
|
||||
Err(mpsc::TryRecvError::Empty) => {
|
||||
//eprintln!("No new data. ;(");
|
||||
@ -92,7 +98,7 @@ fn main() {
|
||||
|
||||
// Autoscroll: Autoscroll automatically toggles ths scrolling direction when the scrolling reaches a nonspace.
|
||||
if args.autoscroll {
|
||||
if !char::is_whitespace(input.chars().nth(0).unwrap()) | !char::is_whitespace(input.chars().nth(input.len() - 1).unwrap()) {
|
||||
if !user_update && (!char::is_whitespace(input.chars().nth(0).unwrap()) | !char::is_whitespace(input.chars().nth(input.len() - 1).unwrap())) { // user update takes priority
|
||||
scroll_left = !scroll_left;
|
||||
// Let the other thread know the scrolling has been changed
|
||||
match tx2.try_send(scroll_left) {
|
||||
@ -132,11 +138,12 @@ fn shift_right(mut input: String) -> String {
|
||||
return input;
|
||||
}
|
||||
|
||||
fn detect_keys(keys: Vec<Keycode>, mut polling_rate: u64, mut scroll_left: bool) -> (u64, bool) {
|
||||
fn detect_keys(keys: Vec<Keycode>, mut polling_rate: u64, mut scroll_left: bool) -> (bool, u64, bool) {
|
||||
let mut user_update = false;
|
||||
//detecting keydown events
|
||||
//println!("{} {} ", polling_rate, scroll_left);
|
||||
if keys.is_empty() {
|
||||
return (polling_rate, scroll_left);
|
||||
return (user_update, polling_rate, scroll_left);
|
||||
}
|
||||
if keys.contains(&Keycode::Up) {
|
||||
polling_rate = increase_poll(polling_rate);
|
||||
@ -146,11 +153,13 @@ fn detect_keys(keys: Vec<Keycode>, mut polling_rate: u64, mut scroll_left: bool)
|
||||
}
|
||||
if keys.contains(&Keycode::Left) {
|
||||
scroll_left = true;
|
||||
user_update = true;
|
||||
}
|
||||
if keys.contains(&Keycode::Right) {
|
||||
scroll_left = false;
|
||||
user_update = true;
|
||||
}
|
||||
return (polling_rate, scroll_left);
|
||||
return (user_update, polling_rate, scroll_left);
|
||||
}
|
||||
|
||||
fn increase_poll(mut polling_rate: u64) -> u64 {
|
||||
|
Loading…
Reference in New Issue
Block a user