diff --git a/examples/window_tabbing.rs b/examples/window_tabbing.rs index b8aadf6a..d2640335 100644 --- a/examples/window_tabbing.rs +++ b/examples/window_tabbing.rs @@ -81,7 +81,14 @@ fn main() { } Key::Character(ch) => { if let Ok(index) = ch.parse::() { - windows.get(&window_id).unwrap().select_tab_at_index(index); + let index = index.get(); + // Select the last tab when pressing `9`. + let window = windows.get(&window_id).unwrap(); + if index == 9 { + window.select_tab_at_index(window.num_tabs() - 1) + } else { + window.select_tab_at_index(index - 1); + } } } _ => (), diff --git a/src/platform/macos.rs b/src/platform/macos.rs index 162d4e2f..7c65c345 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -1,4 +1,4 @@ -use std::{num::NonZeroUsize, os::raw::c_void}; +use std::os::raw::c_void; use objc2::rc::Id; @@ -55,7 +55,10 @@ pub trait WindowExtMacOS { /// Select the tab with the given index. /// /// Will no-op when the index is out of bounds. - fn select_tab_at_index(&self, index: NonZeroUsize); + fn select_tab_at_index(&self, index: usize); + + /// Get the number of tabs in the window tab group. + fn num_tabs(&self) -> usize; /// Get the window's edit state. /// @@ -140,10 +143,15 @@ impl WindowExtMacOS for Window { } #[inline] - fn select_tab_at_index(&self, index: NonZeroUsize) { + fn select_tab_at_index(&self, index: usize) { self.window.select_tab_at_index(index); } + #[inline] + fn num_tabs(&self) -> usize { + self.window.num_tabs() + } + #[inline] fn is_document_edited(&self) -> bool { self.window.is_document_edited() diff --git a/src/platform_impl/macos/window.rs b/src/platform_impl/macos/window.rs index 4e8303bb..86d7cee9 100644 --- a/src/platform_impl/macos/window.rs +++ b/src/platform_impl/macos/window.rs @@ -2,7 +2,6 @@ use std::collections::VecDeque; use std::f64; -use std::num::NonZeroUsize; use std::ops; use std::os::raw::c_void; use std::ptr::NonNull; @@ -1423,15 +1422,20 @@ impl WindowExtMacOS for WinitWindow { } #[inline] - fn select_tab_at_index(&self, index: NonZeroUsize) { + fn select_tab_at_index(&self, index: usize) { let tab_group = self.tabGroup(); let windows = tab_group.tabbedWindows(); - let index = index.get() - 1; if index < windows.len() { tab_group.setSelectedWindow(&windows[index]); } } + #[inline] + fn num_tabs(&self) -> usize { + let tab_group = self.tabGroup(); + tab_group.tabbedWindows().len() + } + fn is_document_edited(&self) -> bool { self.isDocumentEdited() }