Skip to content

Commit

Permalink
feat(ui): Clear mouse-over highlight on FocusLost and FocusGained
Browse files Browse the repository at this point in the history
… events
  • Loading branch information
sarub0b0 committed Jun 25, 2023
1 parent 4bdc504 commit 5d4edba
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
10 changes: 7 additions & 3 deletions src/ui/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ impl<'a> Tab<'a> {
}

pub fn activate_next_widget(&mut self) {
self.mouse_over_widget_index = None;
self.clear_mouse_over();

self.active_widget_index =
(self.active_widget_index + 1) % self.activatable_widget_indices.len();
}

pub fn activate_prev_widget(&mut self) {
self.mouse_over_widget_index = None;
self.clear_mouse_over();

let activatable_widget_len = self.activatable_widget_indices.len();

Expand Down Expand Up @@ -139,12 +139,16 @@ impl<'a> Tab<'a> {
.enumerate()
.find(|(_, w)| w.widget.id() == id)
{
self.mouse_over_widget_index = None;
self.clear_mouse_over();

self.active_widget_index = index;
}
}

pub fn clear_mouse_over(&mut self) {
self.mouse_over_widget_index = None;
}

pub fn find_widget(&self, id: &str) -> Option<&Widget<'a>> {
self.widgets.iter().find_map(|w| {
if w.widget.id() == id {
Expand Down
14 changes: 11 additions & 3 deletions src/ui/widget/complex/multiple_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ impl<'a> SelectForm<'a> {
}

fn toggle_active_form(&mut self) {
self.mouse_over_widget_index = None;
self.clear_mouse_over();

if self.active_form_index == LIST_FORM_ID {
self.active_form_index = SELECTED_FORM_ID
Expand All @@ -385,7 +385,7 @@ impl<'a> SelectForm<'a> {
}

fn activate_form_by_index(&mut self, index: usize) {
self.mouse_over_widget_index = None;
self.clear_mouse_over();

self.active_form_index = index;
}
Expand Down Expand Up @@ -503,10 +503,14 @@ impl<'a> SelectForm<'a> {
}

fn on_key_event(&mut self, ev: KeyEvent) -> EventResult {
self.mouse_over_widget_index = None;
self.clear_mouse_over();

self.active_form_mut().on_key_event(ev)
}

fn clear_mouse_over(&mut self) {
self.mouse_over_widget_index = None;
}
}

type RenderBlockInjection = Rc<dyn Fn(&MultipleSelect, bool) -> Block<'static>>;
Expand Down Expand Up @@ -722,6 +726,10 @@ impl<'a> MultipleSelect<'a> {
pub fn select_all(&mut self) {
self.selected_widget.select_all();
}

pub fn clear_mouse_over(&mut self) {
self.selected_widget.clear_mouse_over();
}
}

impl WidgetTrait for MultipleSelect<'_> {
Expand Down
32 changes: 27 additions & 5 deletions src/ui/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ impl<'a> Window<'a> {
pub fn activate_widget_by_id(&mut self, id: &str) {
self.active_tab_mut().activate_widget_by_id(id)
}

pub fn clear_mouse_over(&mut self) {
self.mouse_over_tab_index = None;
}
}

// Render
Expand Down Expand Up @@ -416,18 +420,36 @@ impl Window<'_> {
UserEvent::Key(ev) => self.on_key_event(ev),
UserEvent::Mouse(ev) => self.on_mouse_event(ev),
UserEvent::FocusLost => {
self.mouse_over_tab_index = None;
self.clear_mouse_over();
self.active_tab_mut().clear_mouse_over();

if let Some(id) = &self.open_popup_id {
if let Some(Widget::MultipleSelect(w)) =
self.popups.iter_mut().find(|w| w.id() == id)
{
w.clear_mouse_over();
}
}
EventResult::Nop
}
UserEvent::FocusGained => {
self.mouse_over_tab_index = None;
self.clear_mouse_over();
self.active_tab_mut().clear_mouse_over();
if let Some(id) = &self.open_popup_id {
if let Some(Widget::MultipleSelect(w)) =
self.popups.iter_mut().find(|w| w.id() == id)
{
w.clear_mouse_over();
}
}

EventResult::Nop
}
}
}

pub fn on_key_event(&mut self, ev: KeyEvent) -> EventResult {
self.mouse_over_tab_index = None;
self.clear_mouse_over();

if let Some(id) = &self.open_popup_id {
if let Some(popup) = self.popups.iter_mut().find(|w| w.id() == id) {
Expand Down Expand Up @@ -490,12 +512,12 @@ impl Window<'_> {
EventResult::Nop
}
AreaKind::Widgets => {
self.mouse_over_tab_index = None;
self.clear_mouse_over();

self.active_tab_mut().on_mouse_event(ev)
}
AreaKind::OutSide => {
self.mouse_over_tab_index = None;
self.clear_mouse_over();

EventResult::Ignore
}
Expand Down

0 comments on commit 5d4edba

Please sign in to comment.