2020-02-28 13:34:34 +11:00
|
|
|
//! Implements an NSToolbar, which is one of those macOS niceties
|
|
|
|
//! that makes it feel... "proper".
|
|
|
|
//!
|
|
|
|
//! UNFORTUNATELY, this is a very old and janky API. So... yeah.
|
|
|
|
|
2020-03-12 13:45:35 +11:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
2020-02-28 13:34:34 +11:00
|
|
|
|
2020-03-12 13:45:35 +11:00
|
|
|
use objc_id::ShareId;
|
2020-03-13 06:33:41 +11:00
|
|
|
use objc::{msg_send, sel, sel_impl};
|
2020-02-28 13:34:34 +11:00
|
|
|
|
2020-03-18 10:55:09 +11:00
|
|
|
use crate::foundation::{id, NSString};
|
2020-03-07 14:35:18 +11:00
|
|
|
use crate::constants::TOOLBAR_PTR;
|
2020-03-13 06:33:41 +11:00
|
|
|
use crate::toolbar::class::register_toolbar_class;
|
|
|
|
use crate::toolbar::handle::ToolbarHandle;
|
2020-03-12 13:45:35 +11:00
|
|
|
use crate::toolbar::traits::ToolbarController;
|
2020-03-13 06:33:41 +11:00
|
|
|
use crate::toolbar::types::{ToolbarDisplayMode, ToolbarSizeMode};
|
2020-02-28 13:34:34 +11:00
|
|
|
|
2020-03-07 14:35:18 +11:00
|
|
|
/// A wrapper for `NSToolbar`. Holds (retains) pointers for the Objective-C runtime
|
|
|
|
/// where our `NSToolbar` and associated delegate live.
|
2020-03-12 13:45:35 +11:00
|
|
|
pub struct Toolbar<T> {
|
2020-03-13 06:33:41 +11:00
|
|
|
/// A pointer that we "forget" until dropping this struct. This allows us to keep the retain
|
|
|
|
/// count of things appropriate until the Toolbar is done.
|
2020-03-12 13:45:35 +11:00
|
|
|
internal_callback_ptr: *const RefCell<T>,
|
2020-03-13 06:33:41 +11:00
|
|
|
|
|
|
|
/// An internal identifier used by the toolbar. We cache it here in case users want it.
|
2020-03-12 13:45:35 +11:00
|
|
|
pub identifier: String,
|
2020-03-13 06:33:41 +11:00
|
|
|
|
|
|
|
/// The Objective-C runtime controller (the toolbar, really - it does double duty).
|
|
|
|
pub objc_controller: ToolbarHandle,
|
|
|
|
|
|
|
|
/// The user supplied controller.
|
2020-03-12 13:45:35 +11:00
|
|
|
pub controller: Rc<RefCell<T>>
|
2020-02-28 13:34:34 +11:00
|
|
|
}
|
|
|
|
|
2020-03-12 13:45:35 +11:00
|
|
|
impl<T> Toolbar<T> where T: ToolbarController + 'static {
|
2020-02-28 13:34:34 +11:00
|
|
|
/// Creates a new `NSToolbar` instance, configures it appropriately, injects an `NSObject`
|
|
|
|
/// delegate wrapper, and retains the necessary Objective-C runtime pointers.
|
2020-03-12 13:45:35 +11:00
|
|
|
pub fn new<S: Into<String>>(identifier: S, controller: T) -> Self {
|
|
|
|
let identifier = identifier.into();
|
|
|
|
let controller = Rc::new(RefCell::new(controller));
|
|
|
|
|
|
|
|
let internal_callback_ptr = {
|
|
|
|
let cloned = Rc::clone(&controller);
|
|
|
|
Rc::into_raw(cloned)
|
2020-02-28 13:34:34 +11:00
|
|
|
};
|
|
|
|
|
2020-03-12 13:45:35 +11:00
|
|
|
let objc_controller = unsafe {
|
2020-03-13 06:33:41 +11:00
|
|
|
let delegate_class = register_toolbar_class::<T>();
|
2020-03-18 10:55:09 +11:00
|
|
|
let identifier = NSString::new(&identifier);
|
2020-03-12 13:45:35 +11:00
|
|
|
let alloc: id = msg_send![delegate_class, alloc];
|
|
|
|
let toolbar: id = msg_send![alloc, initWithIdentifier:identifier];
|
|
|
|
|
|
|
|
(&mut *toolbar).set_ivar(TOOLBAR_PTR, internal_callback_ptr as usize);
|
|
|
|
let _: () = msg_send![toolbar, setDelegate:toolbar];
|
2020-02-28 13:34:34 +11:00
|
|
|
|
2020-03-12 13:45:35 +11:00
|
|
|
ShareId::from_ptr(toolbar)
|
2020-02-28 13:34:34 +11:00
|
|
|
};
|
|
|
|
|
2020-03-13 06:33:41 +11:00
|
|
|
{
|
|
|
|
let mut c = controller.borrow_mut();
|
|
|
|
(*c).did_load(ToolbarHandle(objc_controller.clone()));
|
|
|
|
}
|
|
|
|
|
2020-02-28 13:34:34 +11:00
|
|
|
Toolbar {
|
2020-03-12 13:45:35 +11:00
|
|
|
internal_callback_ptr: internal_callback_ptr,
|
|
|
|
identifier: identifier,
|
2020-03-13 06:33:41 +11:00
|
|
|
objc_controller: ToolbarHandle(objc_controller),
|
2020-03-12 13:45:35 +11:00
|
|
|
controller: controller
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 06:33:41 +11:00
|
|
|
/// Indicates whether the toolbar shows the separator between the toolbar and the main window
|
|
|
|
/// contents.
|
|
|
|
pub fn set_shows_baseline_separator(&self, shows: bool) {
|
|
|
|
self.objc_controller.set_shows_baseline_separator(shows);
|
2020-02-28 13:34:34 +11:00
|
|
|
}
|
|
|
|
|
2020-03-13 06:33:41 +11:00
|
|
|
/// Sets the toolbar's display mode.
|
|
|
|
pub fn set_display_mode(&self, mode: ToolbarDisplayMode) {
|
|
|
|
self.objc_controller.set_display_mode(mode);
|
2020-02-28 13:34:34 +11:00
|
|
|
}
|
|
|
|
|
2020-03-13 06:33:41 +11:00
|
|
|
/// Sets the toolbar's size mode.
|
|
|
|
pub fn set_size_mode(&self, mode: ToolbarSizeMode) {
|
|
|
|
self.objc_controller.set_size_mode(mode);
|
2020-02-28 13:34:34 +11:00
|
|
|
}
|
|
|
|
|
2020-03-13 06:33:41 +11:00
|
|
|
/// Set whether the toolbar is visible or not.
|
|
|
|
pub fn set_visible(&self, visibility: bool) {
|
|
|
|
self.objc_controller.set_visible(visibility);
|
|
|
|
}
|
2020-02-28 13:34:34 +11:00
|
|
|
}
|
|
|
|
|
2020-03-13 06:33:41 +11:00
|
|
|
impl<T> Drop for Toolbar<T> {
|
|
|
|
/// A bit of extra cleanup for delegate callback pointers.
|
|
|
|
/// Note: this currently doesn't check to see if it needs to be removed from a Window it's
|
|
|
|
/// attached to. In theory this is fine... in practice (and in Rust) it might be wonky, so
|
|
|
|
/// worth circling back on at some point.
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
let _ = Rc::from_raw(self.internal_callback_ptr);
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 13:34:34 +11:00
|
|
|
}
|