1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-27 02:25:28 +03:00

tab bar: use UIItem for hit testing

This commit is contained in:
Wez Furlong 2021-10-03 11:22:09 -07:00
parent 68418b89ee
commit da30ecd5b3
4 changed files with 52 additions and 27 deletions

View File

@ -1,4 +1,4 @@
use crate::termwindow::{PaneInformation, TabInformation};
use crate::termwindow::{PaneInformation, TabInformation, UIItem, UIItemType};
use config::lua::{format_as_escapes, FormatItem};
use config::{ConfigHandle, TabBarColors};
use mlua::FromLua;
@ -17,7 +17,7 @@ pub struct TabBarState {
items: Vec<TabEntry>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TabBarItem {
None,
Tab(usize),
@ -320,14 +320,36 @@ impl TabBarState {
Self { line, items }
}
/// Determine which component the mouse is over
pub fn hit_test(&self, mouse_x: usize) -> TabBarItem {
pub fn compute_ui_items(
&self,
y: usize,
cell_height: usize,
cell_width: usize,
width: usize,
) -> Vec<UIItem> {
let mut items = vec![];
let mut last_x = 0;
for entry in self.items.iter() {
if mouse_x >= entry.x && mouse_x < entry.x + entry.width {
return entry.item;
}
items.push(UIItem {
x: last_x * cell_width,
width: entry.width * cell_width,
y,
height: cell_height,
item_type: UIItemType::TabBar(entry.item),
});
last_x += entry.width;
}
TabBarItem::None
items.push(UIItem {
x: last_x * cell_width,
width: width - (last_x * cell_width),
y,
height: cell_height,
item_type: UIItemType::TabBar(TabBarItem::None),
});
items
}
}

View File

@ -13,7 +13,7 @@ use crate::scripting::pane::PaneObject;
use crate::scrollbar::*;
use crate::selection::Selection;
use crate::shapecache::*;
use crate::tabbar::TabBarState;
use crate::tabbar::{TabBarItem, TabBarState};
use ::wezterm_term::input::MouseButton as TMB;
use ::window::*;
use anyhow::Context;
@ -109,7 +109,7 @@ pub enum TermWindowNotif {
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum UIItemType {
TabBar,
TabBar(TabBarItem),
AboveScrollThumb,
ScrollThumb,
BelowScrollThumb,

View File

@ -34,7 +34,7 @@ impl super::TermWindow {
fn leave_ui_item(&mut self, item: &UIItem) {
match item.item_type {
UIItemType::TabBar => {
UIItemType::TabBar(_) => {
self.update_title_post_status();
}
UIItemType::AboveScrollThumb
@ -46,7 +46,7 @@ impl super::TermWindow {
fn enter_ui_item(&mut self, item: &UIItem) {
match item.item_type {
UIItemType::TabBar => {}
UIItemType::TabBar(_) => {}
UIItemType::AboveScrollThumb
| UIItemType::BelowScrollThumb
| UIItemType::ScrollThumb
@ -193,7 +193,7 @@ impl super::TermWindow {
}
if let Some(item) = ui_item {
self.mouse_event_ui_item(item, pane, x, term_y, event, context);
self.mouse_event_ui_item(item, pane, term_y, event, context);
} else {
self.mouse_event_terminal(pane, x, term_y, event, context);
}
@ -285,15 +285,14 @@ impl super::TermWindow {
&mut self,
item: UIItem,
pane: Rc<dyn Pane>,
x: usize,
_y: i64,
event: MouseEvent,
context: &dyn WindowOps,
) {
self.last_ui_item.replace(item.clone());
match item.item_type {
UIItemType::TabBar => {
self.mouse_event_tab_bar(x, event, context);
UIItemType::TabBar(item) => {
self.mouse_event_tab_bar(item, event, context);
}
UIItemType::AboveScrollThumb => {
self.mouse_event_above_scroll_thumb(item, pane, event, context);
@ -310,9 +309,14 @@ impl super::TermWindow {
}
}
pub fn mouse_event_tab_bar(&mut self, x: usize, event: MouseEvent, context: &dyn WindowOps) {
pub fn mouse_event_tab_bar(
&mut self,
item: TabBarItem,
event: MouseEvent,
context: &dyn WindowOps,
) {
match event.kind {
WMEK::Press(MousePress::Left) => match self.tab_bar.hit_test(x) {
WMEK::Press(MousePress::Left) => match item {
TabBarItem::Tab(tab_idx) => {
self.activate_tab(tab_idx as isize).ok();
}
@ -325,13 +329,13 @@ impl super::TermWindow {
context.request_drag_move();
}
},
WMEK::Press(MousePress::Middle) => match self.tab_bar.hit_test(x) {
WMEK::Press(MousePress::Middle) => match item {
TabBarItem::Tab(tab_idx) => {
self.close_tab_idx(tab_idx).ok();
}
TabBarItem::NewTabButton | TabBarItem::None => {}
},
WMEK::Press(MousePress::Right) => match self.tab_bar.hit_test(x) {
WMEK::Press(MousePress::Right) => match item {
TabBarItem::Tab(_) => {
self.show_tab_navigator();
}

View File

@ -522,17 +522,16 @@ impl super::TermWindow {
};
// Register the tab bar location
self.ui_items.push(UIItem {
x: 0,
width: self.dimensions.pixel_width,
y: if self.config.tab_bar_at_bottom {
self.ui_items.append(&mut self.tab_bar.compute_ui_items(
if self.config.tab_bar_at_bottom {
avail_height - self.render_metrics.cell_size.height as usize
} else {
0
},
height: self.render_metrics.cell_size.height as usize,
item_type: UIItemType::TabBar,
});
self.render_metrics.cell_size.height as usize,
self.render_metrics.cell_size.width as usize,
self.dimensions.pixel_width,
));
self.render_screen_line_opengl(
RenderScreenLineOpenGLParams {