On macOS, add a way to query amount of tabbed windows

This should provide a way to iterate all the tabs and select the last
tab. The tab indicies are now zero based as any other sane index.

Follow-up-to: c5941d105f (add tabbing API)
This commit is contained in:
Kirill Chibisov 2023-07-14 08:01:40 +00:00 committed by GitHub
parent 06fb089633
commit 97434d8d80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 7 deletions

View file

@ -81,7 +81,14 @@ fn main() {
} }
Key::Character(ch) => { Key::Character(ch) => {
if let Ok(index) = ch.parse::<NonZeroUsize>() { if let Ok(index) = ch.parse::<NonZeroUsize>() {
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);
}
} }
} }
_ => (), _ => (),

View file

@ -1,4 +1,4 @@
use std::{num::NonZeroUsize, os::raw::c_void}; use std::os::raw::c_void;
use objc2::rc::Id; use objc2::rc::Id;
@ -55,7 +55,10 @@ pub trait WindowExtMacOS {
/// Select the tab with the given index. /// Select the tab with the given index.
/// ///
/// Will no-op when the index is out of bounds. /// 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. /// Get the window's edit state.
/// ///
@ -140,10 +143,15 @@ impl WindowExtMacOS for Window {
} }
#[inline] #[inline]
fn select_tab_at_index(&self, index: NonZeroUsize) { fn select_tab_at_index(&self, index: usize) {
self.window.select_tab_at_index(index); self.window.select_tab_at_index(index);
} }
#[inline]
fn num_tabs(&self) -> usize {
self.window.num_tabs()
}
#[inline] #[inline]
fn is_document_edited(&self) -> bool { fn is_document_edited(&self) -> bool {
self.window.is_document_edited() self.window.is_document_edited()

View file

@ -2,7 +2,6 @@
use std::collections::VecDeque; use std::collections::VecDeque;
use std::f64; use std::f64;
use std::num::NonZeroUsize;
use std::ops; use std::ops;
use std::os::raw::c_void; use std::os::raw::c_void;
use std::ptr::NonNull; use std::ptr::NonNull;
@ -1423,15 +1422,20 @@ impl WindowExtMacOS for WinitWindow {
} }
#[inline] #[inline]
fn select_tab_at_index(&self, index: NonZeroUsize) { fn select_tab_at_index(&self, index: usize) {
let tab_group = self.tabGroup(); let tab_group = self.tabGroup();
let windows = tab_group.tabbedWindows(); let windows = tab_group.tabbedWindows();
let index = index.get() - 1;
if index < windows.len() { if index < windows.len() {
tab_group.setSelectedWindow(&windows[index]); 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 { fn is_document_edited(&self) -> bool {
self.isDocumentEdited() self.isDocumentEdited()
} }