diff options
author | qepasa <pawelpalenica11@gmail.com> | 2021-10-12 14:37:54 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-12 22:37:54 +0100 |
commit | 0710594588aa768b934f925704dc57486dc0a157 (patch) | |
tree | 5b2cd4c9e94ecbda277454b77698ebaa837fab6c /default-plugins/tab-bar | |
parent | a6453f111ea62e6332c919224d3a0fb19e35c249 (diff) |
feat(plugin): Add mouse events for plugins (#629)
* feat(plugin): Add mouse events for plugins
* Add double click support in strider
* Add support for mouse clicks in tab-bar and fix bug in strider with selecting past the list of files and random double click action
* continue working on mouse support for tab bar
* finish tab change
* fix fmt and fix bug in strider double-click
* fix clippy
* cleanup dbgs and logs
* fix clippy
* noop change to rerun e2e tests
* Rebase and fix mouse click behavior in tab-bar and strider after rebase
* fix fmt
* remove dbgs and and comment in tab-line/main.rs
* cargo fmt
* Code review suggestions
* rebase fix
* fix clippy
* fix mouse selection for tabs in tab-bar
Diffstat (limited to 'default-plugins/tab-bar')
-rw-r--r-- | default-plugins/tab-bar/src/main.rs | 49 |
1 files changed, 45 insertions, 4 deletions
diff --git a/default-plugins/tab-bar/src/main.rs b/default-plugins/tab-bar/src/main.rs index 850781261..c34693523 100644 --- a/default-plugins/tab-bar/src/main.rs +++ b/default-plugins/tab-bar/src/main.rs @@ -1,6 +1,9 @@ mod line; mod tab; +use std::cmp::{max, min}; +use std::convert::TryInto; + use zellij_tile::prelude::*; use crate::line::tab_line; @@ -15,7 +18,10 @@ pub struct LinePart { #[derive(Default)] struct State { tabs: Vec<TabInfo>, + active_tab_idx: usize, mode_info: ModeInfo, + mouse_click_pos: usize, + should_render: bool, } static ARROW_SEPARATOR: &str = ""; @@ -25,13 +31,34 @@ register_plugin!(State); impl ZellijPlugin for State { fn load(&mut self) { set_selectable(false); - subscribe(&[EventType::TabUpdate, EventType::ModeUpdate]); + subscribe(&[ + EventType::TabUpdate, + EventType::ModeUpdate, + EventType::Mouse, + ]); } fn update(&mut self, event: Event) { match event { Event::ModeUpdate(mode_info) => self.mode_info = mode_info, - Event::TabUpdate(tabs) => self.tabs = tabs, + Event::TabUpdate(tabs) => { + // tabs are indexed starting from 1 so we need to add 1 + self.active_tab_idx = (&tabs).iter().position(|t| t.active).unwrap() + 1; + self.tabs = tabs; + } + Event::Mouse(me) => match me { + Mouse::LeftClick(_, col) => { + self.mouse_click_pos = col; + self.should_render = true; + } + Mouse::ScrollUp(_) => { + switch_tab_to(min(self.active_tab_idx + 1, self.tabs.len()) as u32); + } + Mouse::ScrollDown(_) => { + switch_tab_to(max(self.active_tab_idx.saturating_sub(1), 1) as u32); + } + _ => {} + }, _ => unimplemented!(), // FIXME: This should be unreachable, but this could be cleaner } } @@ -70,8 +97,21 @@ impl ZellijPlugin for State { self.mode_info.capabilities, ); let mut s = String::new(); - for bar_part in tab_line { - s = format!("{}{}", s, bar_part.part); + let mut len_cnt = 0; + dbg!(&tab_line); + for (idx, bar_part) in tab_line.iter().enumerate() { + s = format!("{}{}", s, &bar_part.part); + + if self.should_render + && self.mouse_click_pos > len_cnt + && self.mouse_click_pos <= len_cnt + bar_part.len + && idx > 2 + { + // First three elements of tab_line are "Zellij", session name and empty thing, hence the idx > 2 condition. + // Tabs are indexed starting from 1, therefore we need subtract 2 below. + switch_tab_to(TryInto::<u32>::try_into(idx).unwrap() - 2); + } + len_cnt += bar_part.len; } match self.mode_info.palette.cyan { PaletteColor::Rgb((r, g, b)) => { @@ -81,5 +121,6 @@ impl ZellijPlugin for State { println!("{}\u{1b}[48;5;{}m\u{1b}[0K", s, color); } } + self.should_render = false; } } |