Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor the handling of idle state #227

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 31 additions & 38 deletions service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ use std::{rc::Rc, sync::mpsc, thread};

/// Starts the afrim.
pub fn run(config: Config, mut frontend: impl Frontend) -> Result<()> {
// State.
let mut is_ctrl_released = true;
let mut idle = false;

// Configuration of the afrim.
let memory = utils::build_map(
config
.extract_data()
Expand Down Expand Up @@ -43,71 +48,59 @@ pub fn run(config: Config, mut frontend: impl Frontend) -> Result<()> {
.into_iter()
.for_each(|(name, ast)| translator.register(name, ast));

let mut is_special_pressed = false;

frontend.set_page_size(page_size);
frontend.update_screen(rdev::display_size().unwrap());

let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let mut idle = false;
let mut is_ctrl_released = false;

rdev::listen(move |event| {
idle = match event.event_type {
EventType::KeyPress(E_Key::Pause) => true,
EventType::KeyRelease(E_Key::Pause) => false,
EventType::KeyPress(E_Key::ControlLeft | E_Key::ControlRight) => {
is_ctrl_released = false;
idle
}
EventType::KeyRelease(E_Key::ControlLeft | E_Key::ControlRight)
if is_ctrl_released =>
{
!idle
}
EventType::KeyRelease(E_Key::ControlLeft | E_Key::ControlRight) => {
is_ctrl_released = true;
idle
}
_ => idle,
};
if !idle {
tx.send(event)
.unwrap_or_else(|e| eprintln!("Could not send event {:?}", e));
}
tx.send(event)
.unwrap_or_else(|e| eprintln!("Could not send event {:?}", e));
})
.expect("Could not listen");
});

for event in rx.iter() {
match event.event_type {
EventType::MouseMove { x, y } => {
frontend.update_position((x, y));
// Handling of idle state.
EventType::KeyPress(E_Key::Pause) => {
idle = true;
}
EventType::KeyRelease(E_Key::Pause) => {
idle = false;
}
EventType::KeyPress(E_Key::ControlLeft | E_Key::ControlRight) => {
is_ctrl_released = false;
}
EventType::KeyPress(E_Key::ControlLeft) => {
is_special_pressed = true;
EventType::KeyRelease(E_Key::ControlLeft | E_Key::ControlRight) if is_ctrl_released => {
idle = !idle;
}
EventType::KeyRelease(E_Key::ControlLeft) => {
is_special_pressed = false;
EventType::KeyRelease(E_Key::ControlLeft | E_Key::ControlRight) => {
is_ctrl_released = true;
}
EventType::KeyRelease(E_Key::ShiftRight) if is_special_pressed => {
_ if idle => (),
// Handling of special functions.
EventType::KeyRelease(E_Key::ShiftRight) if !is_ctrl_released => {
frontend.next_predicate()
}
EventType::KeyRelease(E_Key::ShiftLeft) if is_special_pressed => {
EventType::KeyRelease(E_Key::ShiftLeft) if !is_ctrl_released => {
frontend.previous_predicate()
}
EventType::KeyRelease(E_Key::Space) if is_special_pressed => {
EventType::KeyRelease(E_Key::Space) if !is_ctrl_released => {
rdev::simulate(&EventType::KeyRelease(E_Key::ControlLeft))
.expect("We couldn't cancel the special function key");
is_special_pressed = false;

if let Some((_code, _remaining_code, text)) = frontend.get_selected_predicate() {
preprocessor.commit(text.to_owned());
frontend.clear_predicates();
}
}
_ if is_special_pressed => (),
_ if !is_ctrl_released => (),
// GUI events.
EventType::MouseMove { x, y } => {
frontend.update_position((x, y));
}
// Process events.
_ => {
let (changed, _committed) = preprocessor.process(convert::from_event(event));

Expand Down
Loading