2020-02-28 13:34:34 +11:00
|
|
|
//! Implements an NSToolbar wrapper, which is one of those macOS niceties
|
|
|
|
//! that makes it feel... "proper".
|
|
|
|
//!
|
|
|
|
//! UNFORTUNATELY, this is a very old and janky API. So... yeah.
|
|
|
|
|
|
|
|
use objc_id::Id;
|
|
|
|
use objc::runtime::Object;
|
|
|
|
use objc::{class, msg_send, sel, sel_impl};
|
|
|
|
|
2020-03-18 10:55:09 +11:00
|
|
|
use crate::foundation::{id, CGSize, NSString};
|
2020-02-28 13:34:34 +11:00
|
|
|
use crate::button::Button;
|
|
|
|
|
|
|
|
/// A wrapper for `NSWindow`. Holds (retains) pointers for the Objective-C runtime
|
|
|
|
/// where our `NSWindow` and associated delegate live.
|
2020-03-12 13:45:35 +11:00
|
|
|
pub struct ToolbarItem {
|
|
|
|
pub identifier: String,
|
2020-02-28 13:34:34 +11:00
|
|
|
pub inner: Id<Object>,
|
|
|
|
pub button: Option<Button>
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:45:35 +11:00
|
|
|
impl ToolbarItem {
|
2020-02-28 13:34:34 +11:00
|
|
|
/// Creates a new `NSWindow` instance, configures it appropriately (e.g, titlebar appearance),
|
|
|
|
/// 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) -> Self {
|
|
|
|
let identifier = identifier.into();
|
|
|
|
|
2020-02-28 13:34:34 +11:00
|
|
|
let inner = unsafe {
|
2020-03-18 10:55:09 +11:00
|
|
|
let identifr = NSString::new(&identifier);
|
2020-02-28 13:34:34 +11:00
|
|
|
let alloc: id = msg_send![class!(NSToolbarItem), alloc];
|
2020-03-12 13:45:35 +11:00
|
|
|
let item: id = msg_send![alloc, initWithItemIdentifier:identifr];
|
2020-02-28 13:34:34 +11:00
|
|
|
Id::from_ptr(item)
|
|
|
|
};
|
|
|
|
|
|
|
|
ToolbarItem {
|
|
|
|
identifier: identifier,
|
|
|
|
inner: inner,
|
|
|
|
button: None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:45:35 +11:00
|
|
|
/// Sets the title for this item.
|
2020-02-28 13:34:34 +11:00
|
|
|
pub fn set_title(&mut self, title: &str) {
|
|
|
|
unsafe {
|
2020-03-18 10:55:09 +11:00
|
|
|
let title = NSString::new(title);
|
2020-02-28 13:34:34 +11:00
|
|
|
let _: () = msg_send![&*self.inner, setTitle:title];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:45:35 +11:00
|
|
|
/// Sets and takes ownership of the button for this item.
|
2020-02-28 13:34:34 +11:00
|
|
|
pub fn set_button(&mut self, button: Button) {
|
|
|
|
button.set_bezel_style(11);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let _: () = msg_send![&*self.inner, setView:&*button.inner];
|
|
|
|
}
|
|
|
|
|
|
|
|
self.button = Some(button);
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:45:35 +11:00
|
|
|
/// Sets the minimum size for this button.
|
2020-02-28 13:34:34 +11:00
|
|
|
pub fn set_min_size(&mut self, width: f64, height: f64) {
|
|
|
|
unsafe {
|
2020-03-18 10:55:09 +11:00
|
|
|
let size = CGSize::new(width.into(), height.into());
|
2020-02-28 13:34:34 +11:00
|
|
|
let _: () = msg_send![&*self.inner, setMinSize:size];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:45:35 +11:00
|
|
|
/// Sets the maximum size for this button.
|
2020-02-28 13:34:34 +11:00
|
|
|
pub fn set_max_size(&mut self, width: f64, height: f64) {
|
|
|
|
unsafe {
|
2020-03-18 10:55:09 +11:00
|
|
|
let size = CGSize::new(width.into(), height.into());
|
2020-02-28 13:34:34 +11:00
|
|
|
let _: () = msg_send![&*self.inner, setMaxSize:size];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|