On macOS, add tabbing APIs

This should let the users control macOS tabbing and allow to create
windows in tab.

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
This commit is contained in:
Kirill Chibisov 2023-07-13 06:52:34 +00:00 committed by GitHub
parent b63164645b
commit c5941d105f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 295 additions and 2 deletions

View file

@ -24,6 +24,7 @@ mod menu_item;
mod pasteboard;
mod responder;
mod screen;
mod tab_group;
mod text_input_context;
mod version;
mod view;
@ -49,6 +50,7 @@ pub(crate) use self::pasteboard::{NSFilenamesPboardType, NSPasteboard, NSPastebo
pub(crate) use self::responder::NSResponder;
#[allow(unused_imports)]
pub(crate) use self::screen::{NSDeviceDescriptionKey, NSScreen};
pub(crate) use self::tab_group::NSWindowTabGroup;
pub(crate) use self::text_input_context::NSTextInputContext;
pub(crate) use self::version::NSAppKitVersion;
pub(crate) use self::view::{NSTrackingRectTag, NSView};

View file

@ -0,0 +1,28 @@
use objc2::foundation::{NSArray, NSObject};
use objc2::rc::{Id, Shared};
use objc2::{extern_class, extern_methods, msg_send_id, ClassType};
use super::NSWindow;
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct NSWindowTabGroup;
unsafe impl ClassType for NSWindowTabGroup {
type Super = NSObject;
}
);
extern_methods!(
unsafe impl NSWindowTabGroup {
#[sel(selectNextTab)]
pub fn selectNextTab(&self);
#[sel(selectPreviousTab)]
pub fn selectPreviousTab(&self);
pub fn tabbedWindows(&self) -> Id<NSArray<NSWindow, Shared>, Shared> {
unsafe { msg_send_id![self, windows] }
}
#[sel(setSelectedWindow:)]
pub fn setSelectedWindow(&self, window: &NSWindow);
}
);

View file

@ -6,7 +6,9 @@ use objc2::rc::{Id, Shared};
use objc2::runtime::Object;
use objc2::{extern_class, extern_methods, msg_send_id, ClassType};
use super::{NSButton, NSColor, NSEvent, NSPasteboardType, NSResponder, NSScreen, NSView};
use super::{
NSButton, NSColor, NSEvent, NSPasteboardType, NSResponder, NSScreen, NSView, NSWindowTabGroup,
};
extern_class!(
/// Main-Thread-Only!
@ -171,6 +173,12 @@ extern_methods!(
#[sel(setLevel:)]
pub fn setLevel(&self, level: NSWindowLevel);
#[sel(setAllowsAutomaticWindowTabbing:)]
pub fn setAllowsAutomaticWindowTabbing(val: bool);
#[sel(setTabbingIdentifier:)]
pub fn setTabbingIdentifier(&self, identifier: &NSString);
#[sel(setDocumentEdited:)]
pub fn setDocumentEdited(&self, val: bool);
@ -201,6 +209,20 @@ extern_methods!(
#[sel(isZoomed)]
pub fn isZoomed(&self) -> bool;
#[sel(allowsAutomaticWindowTabbing)]
pub fn allowsAutomaticWindowTabbing() -> bool;
#[sel(selectNextTab)]
pub fn selectNextTab(&self);
pub fn tabbingIdentifier(&self) -> Id<NSString, Shared> {
unsafe { msg_send_id![self, tabbingIdentifier] }
}
pub fn tabGroup(&self) -> Id<NSWindowTabGroup, Shared> {
unsafe { msg_send_id![self, tabGroup] }
}
#[sel(isDocumentEdited)]
pub fn isDocumentEdited(&self) -> bool;

View file

@ -2,6 +2,7 @@
use std::collections::VecDeque;
use std::f64;
use std::num::NonZeroUsize;
use std::ops;
use std::os::raw::c_void;
use std::ptr::NonNull;
@ -82,6 +83,8 @@ pub struct PlatformSpecificWindowBuilderAttributes {
pub disallow_hidpi: bool,
pub has_shadow: bool,
pub accepts_first_mouse: bool,
pub allows_automatic_window_tabbing: bool,
pub tabbing_identifier: Option<String>,
pub option_as_alt: OptionAsAlt,
}
@ -98,6 +101,8 @@ impl Default for PlatformSpecificWindowBuilderAttributes {
disallow_hidpi: false,
has_shadow: true,
accepts_first_mouse: true,
allows_automatic_window_tabbing: true,
tabbing_identifier: None,
option_as_alt: Default::default(),
}
}
@ -357,6 +362,12 @@ impl WinitWindow {
this.setTitle(&NSString::from_str(&attrs.title));
this.setAcceptsMouseMovedEvents(true);
if let Some(identifier) = pl_attrs.tabbing_identifier {
this.setTabbingIdentifier(&NSString::from_str(&identifier));
}
NSWindow::setAllowsAutomaticWindowTabbing(pl_attrs.allows_automatic_window_tabbing);
if attrs.content_protected {
this.setSharingType(NSWindowSharingType::NSWindowSharingNone);
}
@ -1394,6 +1405,46 @@ impl WindowExtMacOS for WinitWindow {
self.setHasShadow(has_shadow)
}
#[inline]
fn set_allows_automatic_window_tabbing(&self, enabled: bool) {
NSWindow::setAllowsAutomaticWindowTabbing(enabled);
}
#[inline]
fn allows_automatic_window_tabbing(&self) -> bool {
NSWindow::allowsAutomaticWindowTabbing()
}
#[inline]
fn set_tabbing_identifier(&self, identifier: &str) {
self.setTabbingIdentifier(&NSString::from_str(identifier))
}
#[inline]
fn tabbing_identifier(&self) -> String {
self.tabbingIdentifier().to_string()
}
#[inline]
fn select_next_tab(&self) {
self.tabGroup().selectNextTab();
}
#[inline]
fn select_previous_tab(&self) {
self.tabGroup().selectPreviousTab();
}
#[inline]
fn select_tab_at_index(&self, index: NonZeroUsize) {
let tab_group = self.tabGroup();
let windows = tab_group.tabbedWindows();
let index = index.get() - 1;
if index < windows.len() {
tab_group.setSelectedWindow(&windows[index]);
}
}
fn is_document_edited(&self) -> bool {
self.isDocumentEdited()
}