Skip to content

Commit

Permalink
fix: Key events propagated better 🐛 (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdheepak authored Jan 7, 2024
1 parent 566dc04 commit 5a37246
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
2 changes: 2 additions & 0 deletions async/ratatui-counter/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub enum Action {
EnterInsert,
EnterProcessing,
ExitProcessing,
EnterHomeInput,
EnterHomeNormal,
Update,
}
//// ANCHOR_END: action_enum
2 changes: 2 additions & 0 deletions async/ratatui-counter/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ impl App {
Action::Quit => self.should_quit = true,
Action::Suspend => self.should_suspend = true,
Action::Resume => self.should_suspend = false,
Action::EnterHomeInput => self.mode = Mode::HomeInput,
Action::EnterHomeNormal => self.mode = Mode::Home,
Action::Resize(w, h) => {
tui.resize(Rect::new(0, 0, w, h))?;
tui.draw(|f| {
Expand Down
65 changes: 33 additions & 32 deletions async/ratatui-counter/src/components/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,11 @@ impl Component for Home {
Action::CompleteInput(s) => self.add(s),
Action::EnterNormal => {
self.mode = Mode::Normal;
return Ok(Some(Action::EnterHomeNormal));
},
Action::EnterInsert => {
self.mode = Mode::Insert;
return Ok(Some(Action::EnterHomeInput));
},
Action::EnterProcessing => {
self.mode = Mode::Processing;
Expand All @@ -281,38 +283,37 @@ impl Component for Home {
Ok(None)
}

fn handle_events(&mut self, event: Option<Event>) -> Result<Option<Action>> {
if let Some(Event::Mouse(MouseEvent { kind, column, row, modifiers })) = event {
// TODO: simulate better button clicks
self.increment_btn_state = ButtonState::Normal;
self.decrement_btn_state = ButtonState::Normal;
if column >= self.increment_rect.left()
&& column <= self.increment_rect.right()
&& row >= self.increment_rect.top()
&& row <= self.increment_rect.bottom()
{
if kind == MouseEventKind::Moved {
self.increment_btn_state = ButtonState::Hover;
} else if kind == MouseEventKind::Down(MouseButton::Left) {
self.increment_btn_state = ButtonState::Clicked;
return Ok(Some(Action::ScheduleIncrement));
} else if kind == MouseEventKind::Up(MouseButton::Left) {
self.increment_btn_state = ButtonState::Hover;
}
};
if column >= self.decrement_rect.left()
&& column <= self.decrement_rect.right()
&& row >= self.decrement_rect.top()
&& row <= self.decrement_rect.bottom()
{
if kind == MouseEventKind::Moved {
self.decrement_btn_state = ButtonState::Hover;
} else if kind == MouseEventKind::Down(MouseButton::Left) {
self.decrement_btn_state = ButtonState::Clicked;
return Ok(Some(Action::ScheduleDecrement));
} else if kind == MouseEventKind::Up(MouseButton::Left) {
self.decrement_btn_state = ButtonState::Hover;
}
fn handle_mouse_events(&mut self, event: MouseEvent) -> Result<Option<Action>> {
let MouseEvent { kind, column, row, modifiers } = event;
// TODO: simulate better button clicks
self.increment_btn_state = ButtonState::Normal;
self.decrement_btn_state = ButtonState::Normal;
if column >= self.increment_rect.left()
&& column <= self.increment_rect.right()
&& row >= self.increment_rect.top()
&& row <= self.increment_rect.bottom()
{
if kind == MouseEventKind::Moved {
self.increment_btn_state = ButtonState::Hover;
} else if kind == MouseEventKind::Down(MouseButton::Left) {
self.increment_btn_state = ButtonState::Clicked;
return Ok(Some(Action::ScheduleIncrement));
} else if kind == MouseEventKind::Up(MouseButton::Left) {
self.increment_btn_state = ButtonState::Hover;
}
};
if column >= self.decrement_rect.left()
&& column <= self.decrement_rect.right()
&& row >= self.decrement_rect.top()
&& row <= self.decrement_rect.bottom()
{
if kind == MouseEventKind::Moved {
self.decrement_btn_state = ButtonState::Hover;
} else if kind == MouseEventKind::Down(MouseButton::Left) {
self.decrement_btn_state = ButtonState::Clicked;
return Ok(Some(Action::ScheduleDecrement));
} else if kind == MouseEventKind::Up(MouseButton::Left) {
self.decrement_btn_state = ButtonState::Hover;
}
}
Ok(None)
Expand Down
1 change: 1 addition & 0 deletions async/ratatui-counter/src/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ use serde::{Deserialize, Serialize};
pub enum Mode {
#[default]
Home,
HomeInput,
}

0 comments on commit 5a37246

Please sign in to comment.