cacao/appkit/toolbar/toolbar.rs

106 lines
3.8 KiB
Rust
Raw Normal View History

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.
use std::cell::RefCell;
use std::rc::Rc;
2020-02-28 13:34:34 +11:00
use objc_id::ShareId;
use objc::{msg_send, sel, sel_impl};
2020-02-28 13:34:34 +11:00
use crate::foundation::{id, NSString};
use crate::constants::TOOLBAR_PTR;
use crate::toolbar::class::register_toolbar_class;
use crate::toolbar::handle::ToolbarHandle;
use crate::toolbar::traits::ToolbarController;
use crate::toolbar::types::{ToolbarDisplayMode, ToolbarSizeMode};
2020-02-28 13:34:34 +11:00
/// A wrapper for `NSToolbar`. Holds (retains) pointers for the Objective-C runtime
/// where our `NSToolbar` and associated delegate live.
pub struct Toolbar<T> {
/// 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.
internal_callback_ptr: *const RefCell<T>,
/// An internal identifier used by the toolbar. We cache it here in case users want it.
pub identifier: String,
/// The Objective-C runtime controller (the toolbar, really - it does double duty).
pub objc_controller: ToolbarHandle,
/// The user supplied controller.
pub controller: Rc<RefCell<T>>
2020-02-28 13:34:34 +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.
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
};
let objc_controller = unsafe {
let delegate_class = register_toolbar_class::<T>();
let identifier = NSString::new(&identifier);
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
ShareId::from_ptr(toolbar)
2020-02-28 13:34:34 +11:00
};
{
let mut c = controller.borrow_mut();
(*c).did_load(ToolbarHandle(objc_controller.clone()));
}
2020-02-28 13:34:34 +11:00
Toolbar {
internal_callback_ptr: internal_callback_ptr,
identifier: identifier,
objc_controller: ToolbarHandle(objc_controller),
controller: controller
}
}
/// 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
}
/// 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
}
/// 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
}
/// 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
}
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
}