Skip to content

Commit

Permalink
Merge branch 'refactor-tui-wapper-util'
Browse files Browse the repository at this point in the history
  • Loading branch information
sarub0b0 committed May 25, 2023
2 parents a60678d + 66851ff commit a2c2e69
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
24 changes: 17 additions & 7 deletions src/tui_wrapper/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,25 @@ pub fn key_event_to_code(key: KeyEvent) -> KeyCode {
_ => key.code,
}
}
#[inline]
pub fn mouse_pos(ev: MouseEvent) -> (u16, u16) {
(ev.column, ev.row)

pub trait MousePosition {
fn position(&self) -> (u16, u16);
}

impl MousePosition for MouseEvent {
fn position(&self) -> (u16, u16) {
(self.column, self.row)
}
}

#[inline]
pub fn contains(chunk: Rect, point: (u16, u16)) -> bool {
let (px, py) = point;
(chunk.left() <= px && px < chunk.right()) && (chunk.top() <= py && py < chunk.bottom())
pub trait RectContainsPoint {
fn contains_point(&self, point: (u16, u16)) -> bool;
}

impl RectContainsPoint for Rect {
fn contains_point(&self, (x, y): (u16, u16)) -> bool {
(self.left() <= x && x < self.right()) && (self.top() <= y && y < self.bottom())
}
}

pub fn child_window_chunk(width_rate: u16, height_rate: u16, chunk: Rect) -> Rect {
Expand Down
12 changes: 6 additions & 6 deletions src/tui_wrapper/widget/complex/multiple_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::rc::Rc;

use crate::tui_wrapper::{
event::EventResult,
util::{contains, key_event_to_code, mouse_pos},
util::{key_event_to_code, MousePosition, RectContainsPoint},
widget::*,
Window,
};
Expand Down Expand Up @@ -456,14 +456,14 @@ impl<'a> SelectForm<'a> {
}

fn on_mouse_event(&mut self, ev: MouseEvent) -> EventResult {
let pos = mouse_pos(ev);
let pos = ev.position();

let (chunks, _) = self.chunks_and_arrow();

if contains(chunks[0], pos) {
if chunks[0].contains_point(pos) {
self.focus(0);
self.list_widget.on_mouse_event(ev)
} else if contains(chunks[2], pos) {
} else if chunks[2].contains_point(pos) {
self.focus(1);
self.selected_widget.on_mouse_event(ev)
} else {
Expand Down Expand Up @@ -725,9 +725,9 @@ impl WidgetTrait for MultipleSelect<'_> {

let chunks = &self.inner_chunks;

let ret = if contains(chunks[LAYOUT_INDEX_FOR_INPUT_FORM], pos) {
let ret = if chunks[LAYOUT_INDEX_FOR_INPUT_FORM].contains_point(pos) {
self.input_widget.on_mouse_event(ev)
} else if contains(chunks[LAYOUT_INDEX_FOR_SELECT_FORM], pos) {
} else if chunks[LAYOUT_INDEX_FOR_SELECT_FORM].contains_point(pos) {
self.selected_widget.on_mouse_event(ev)
} else {
EventResult::Nop
Expand Down
6 changes: 3 additions & 3 deletions src/tui_wrapper/widget/complex/single_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
event::UserEvent,
tui_wrapper::{
event::{EventResult, InnerCallback},
util::contains,
util::RectContainsPoint,
widget::*,
Window,
},
Expand Down Expand Up @@ -255,9 +255,9 @@ impl WidgetTrait for SingleSelect<'_> {

let chunks = &self.inner_chunks;

if contains(chunks[LAYOUT_INDEX_FOR_INPUT_FORM], pos) {
if chunks[LAYOUT_INDEX_FOR_INPUT_FORM].contains_point(pos) {
self.input_widget.on_mouse_event(ev)
} else if contains(chunks[LAYOUT_INDEX_FOR_SELECT_FORM], pos) {
} else if chunks[LAYOUT_INDEX_FOR_SELECT_FORM].contains_point(pos) {
self.selected_widget.on_mouse_event(ev)
} else {
EventResult::Nop
Expand Down
12 changes: 6 additions & 6 deletions src/tui_wrapper/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{

use super::{
event::{EventResult, InnerCallback},
util::{self, child_window_chunk, key_event_to_code},
util::{child_window_chunk, key_event_to_code, MousePosition, RectContainsPoint},
widget::{RenderTrait, Widget, WidgetTrait},
Tab,
};
Expand Down Expand Up @@ -445,15 +445,15 @@ impl Window<'_> {
let focused_view_id = self.focused_widget_id().to_string();
let mut focus_widget_id = None;

let result = if util::contains(self.tab_chunk(), pos) {
let result = if self.tab_chunk().contains_point(pos) {
self.on_click_tab(ev);
EventResult::Nop
} else if util::contains(self.chunks()[self.layout_index.contents], pos) {
} else if self.chunks()[self.layout_index.contents].contains_point(pos) {
if let Some(w) = self
.focused_tab_mut()
.as_mut_widgets()
.iter_mut()
.find(|w| util::contains(w.chunk(), pos))
.find(|w| w.chunk().contains_point(pos))
{
focus_widget_id = if w.id() != focused_view_id {
Some(w.id().to_string())
Expand All @@ -480,7 +480,7 @@ impl Window<'_> {
return;
}

let pos = util::mouse_pos(ev);
let pos = ev.position();

let chunk = Self::tab_block().inner(self.tab_chunk());
let divider_width = 1;
Expand All @@ -495,7 +495,7 @@ impl Window<'_> {

let title_chunk = Rect::new(x, y, w, h);

if util::contains(title_chunk, pos) {
if title_chunk.contains_point(pos) {
self.focus_tab(i + 1);
break;
}
Expand Down

0 comments on commit a2c2e69

Please sign in to comment.