gardesk/garfield / 90c8f00

Browse files

ui: add middle-click to close tab

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
90c8f008f790479bcbf1640a5ca76dac0674fce2
Parents
8f57948
Tree
330d7e6

2 changed files

StatusFile+-
M garfield/src/app.rs 23 13
M garfield/src/ui/tab_bar.rs 13 0
garfield/src/app.rsmodified
@@ -3,7 +3,7 @@
33
 use garfield::ui::pane::SplitDirection;
44
 use garfield::ui::{AddressBar, Breadcrumb, HelpModal, Pane, Sidebar, StatusBar, TabBar, TabInfo, Toolbar, ToolbarAction, ViewMode, TAB_BAR_HEIGHT, TOOLBAR_HEIGHT};
55
 use anyhow::Result;
6
-use gartk_core::{InputEvent, Key, Point, Rect, Theme};
6
+use gartk_core::{InputEvent, Key, MouseButton, Point, Rect, Theme};
77
 use gartk_render::{Renderer, Surface, TextStyle};
88
 use gartk_x11::{Connection, EventLoop, EventLoopConfig, Window, WindowConfig};
99
 use std::path::PathBuf;
@@ -232,7 +232,7 @@ impl App {
232232
                 }
233233
                 InputEvent::MousePress(mouse_event) => {
234234
                     let pos = Point::new(mouse_event.position.x, mouse_event.position.y);
235
-                    self.handle_mouse_press(pos, &mouse_event.modifiers);
235
+                    self.handle_mouse_press(pos, &mouse_event.modifiers, mouse_event.button);
236236
                     ev.request_redraw();
237237
                 }
238238
                 InputEvent::MouseRelease(mouse_event) => {
@@ -283,25 +283,35 @@ impl App {
283283
     }
284284
 
285285
     /// Handle mouse press.
286
-    fn handle_mouse_press(&mut self, pos: Point, modifiers: &gartk_core::Modifiers) {
286
+    fn handle_mouse_press(&mut self, pos: Point, modifiers: &gartk_core::Modifiers, button: Option<MouseButton>) {
287287
         // Check help modal first (clicking outside closes it)
288288
         if self.help_modal.on_click(pos) {
289289
             return;
290290
         }
291291
 
292
-        // Check tab bar clicks - try to start tab drag first
293
-        if self.tab_bar.start_drag(pos) {
294
-            // Started potential tab drag, don't switch yet
295
-            return;
292
+        // Handle middle-click on tab bar to close tab
293
+        if button == Some(MouseButton::Middle) {
294
+            if let Some(index) = self.tab_bar.tab_at_point(pos) {
295
+                self.close_tab(index);
296
+                return;
297
+            }
296298
         }
297299
 
298
-        // Handle tab bar close button clicks (non-drag)
299
-        if let Some((tab_index, is_close)) = self.tab_bar.on_click(pos) {
300
-            if is_close {
301
-                self.close_tab(tab_index);
300
+        // Check tab bar clicks - try to start tab drag first (left click only)
301
+        if button == Some(MouseButton::Left) || button.is_none() {
302
+            if self.tab_bar.start_drag(pos) {
303
+                // Started potential tab drag, don't switch yet
304
+                return;
305
+            }
306
+
307
+            // Handle tab bar close button clicks (non-drag)
308
+            if let Some((tab_index, is_close)) = self.tab_bar.on_click(pos) {
309
+                if is_close {
310
+                    self.close_tab(tab_index);
311
+                }
312
+                // Tab selection happens on mouse release if not dragged
313
+                return;
302314
             }
303
-            // Tab selection happens on mouse release if not dragged
304
-            return;
305315
         }
306316
 
307317
         // Check toolbar clicks
garfield/src/ui/tab_bar.rsmodified
@@ -277,6 +277,19 @@ impl TabBar {
277277
         self.dragging_tab
278278
     }
279279
 
280
+    /// Get the tab index at a given point (if any).
281
+    pub fn tab_at_point(&self, pos: Point) -> Option<usize> {
282
+        if !self.bounds.contains_point(pos) {
283
+            return None;
284
+        }
285
+        for (i, tab_bounds) in self.tab_bounds.iter().enumerate() {
286
+            if tab_bounds.contains_point(pos) {
287
+                return Some(i);
288
+            }
289
+        }
290
+        None
291
+    }
292
+
280293
     /// Render the tab bar.
281294
     pub fn render(&self, renderer: &Renderer) -> anyhow::Result<()> {
282295
         let theme = renderer.theme();