2020-03-18 12:19:56 +11:00
|
|
|
//! A `ViewHandle` represents an underlying `NSView`. You're passed a reference to one during your
|
|
|
|
//! `ViewController::did_load()` method. This method is safe to store and use, however as it's
|
|
|
|
//! UI-specific it's not thread safe.
|
|
|
|
//!
|
|
|
|
//! You can use this struct to configure how a view should look and layout. It implements
|
|
|
|
//! AutoLayout - for more information, see the AutoLayout tutorial.
|
2020-03-05 13:33:11 +11:00
|
|
|
|
|
|
|
use objc_id::ShareId;
|
|
|
|
use objc::runtime::Object;
|
2020-03-07 14:35:18 +11:00
|
|
|
use objc::{msg_send, sel, sel_impl};
|
2020-03-05 13:33:11 +11:00
|
|
|
|
2020-03-18 12:19:56 +11:00
|
|
|
use crate::foundation::{id, YES, NSArray, NSString};
|
2020-03-07 14:35:18 +11:00
|
|
|
use crate::color::Color;
|
2020-03-18 12:19:56 +11:00
|
|
|
use crate::constants::BACKGROUND_COLOR;
|
2020-03-13 12:18:32 +11:00
|
|
|
use crate::layout::{Layout, LayoutAnchorX, LayoutAnchorY, LayoutAnchorDimension};
|
2020-03-07 14:35:18 +11:00
|
|
|
use crate::pasteboard::PasteboardType;
|
2020-03-05 13:33:11 +11:00
|
|
|
|
2020-03-12 11:56:17 +11:00
|
|
|
/// A clone-able handler to a `ViewController` reference in the Objective C runtime. We use this
|
|
|
|
/// instead of a stock `View` for easier recordkeeping, since it'll need to hold the `View` on that
|
|
|
|
/// side anyway.
|
|
|
|
#[derive(Debug, Default, Clone)]
|
2020-03-13 12:18:32 +11:00
|
|
|
pub struct ViewHandle {
|
|
|
|
/// A pointer to the Objective-C runtime view controller.
|
|
|
|
pub objc: Option<ShareId<Object>>,
|
|
|
|
|
|
|
|
/// A pointer to the Objective-C runtime top layout constraint.
|
|
|
|
pub top: LayoutAnchorY,
|
|
|
|
|
|
|
|
/// A pointer to the Objective-C runtime leading layout constraint.
|
|
|
|
pub leading: LayoutAnchorX,
|
|
|
|
|
|
|
|
/// A pointer to the Objective-C runtime trailing layout constraint.
|
|
|
|
pub trailing: LayoutAnchorX,
|
|
|
|
|
|
|
|
/// A pointer to the Objective-C runtime bottom layout constraint.
|
|
|
|
pub bottom: LayoutAnchorY,
|
|
|
|
|
|
|
|
/// A pointer to the Objective-C runtime width layout constraint.
|
|
|
|
pub width: LayoutAnchorDimension,
|
|
|
|
|
|
|
|
/// A pointer to the Objective-C runtime height layout constraint.
|
|
|
|
pub height: LayoutAnchorDimension,
|
|
|
|
|
|
|
|
/// A pointer to the Objective-C runtime center X layout constraint.
|
|
|
|
pub center_x: LayoutAnchorX,
|
|
|
|
|
|
|
|
/// A pointer to the Objective-C runtime center Y layout constraint.
|
|
|
|
pub center_y: LayoutAnchorY
|
|
|
|
}
|
2020-03-05 13:33:11 +11:00
|
|
|
|
2020-03-12 11:56:17 +11:00
|
|
|
impl ViewHandle {
|
2020-03-13 12:18:32 +11:00
|
|
|
pub(crate) fn new(object: ShareId<Object>) -> Self {
|
|
|
|
let view: id = unsafe {
|
|
|
|
msg_send![&*object, view]
|
|
|
|
};
|
|
|
|
|
|
|
|
ViewHandle {
|
|
|
|
objc: Some(object),
|
|
|
|
top: LayoutAnchorY::new(unsafe { msg_send![view, topAnchor] }),
|
|
|
|
leading: LayoutAnchorX::new(unsafe { msg_send![view, leadingAnchor] }),
|
|
|
|
trailing: LayoutAnchorX::new(unsafe { msg_send![view, trailingAnchor] }),
|
|
|
|
bottom: LayoutAnchorY::new(unsafe { msg_send![view, bottomAnchor] }),
|
|
|
|
width: LayoutAnchorDimension::new(unsafe { msg_send![view, widthAnchor] }),
|
|
|
|
height: LayoutAnchorDimension::new(unsafe { msg_send![view, heightAnchor] }),
|
|
|
|
center_x: LayoutAnchorX::new(unsafe { msg_send![view, centerXAnchor] }),
|
|
|
|
center_y: LayoutAnchorY::new(unsafe { msg_send![view, centerYAnchor] }),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 11:56:17 +11:00
|
|
|
/// Call this to set the background color for the backing layer.
|
|
|
|
pub fn set_background_color(&self, color: Color) {
|
2020-03-13 12:18:32 +11:00
|
|
|
if let Some(objc) = &self.objc {
|
2020-03-12 11:56:17 +11:00
|
|
|
unsafe {
|
2020-03-13 12:18:32 +11:00
|
|
|
let view: id = msg_send![*objc, view];
|
2020-03-12 11:56:17 +11:00
|
|
|
(*view).set_ivar(BACKGROUND_COLOR, color.into_platform_specific_color());
|
|
|
|
let _: () = msg_send![view, setNeedsDisplay:YES];
|
|
|
|
}
|
|
|
|
}
|
2020-03-05 13:33:11 +11:00
|
|
|
}
|
|
|
|
|
2020-03-12 11:56:17 +11:00
|
|
|
/// Register this view for drag and drop operations.
|
2020-03-07 14:35:18 +11:00
|
|
|
pub fn register_for_dragged_types(&self, types: &[PasteboardType]) {
|
2020-03-13 12:18:32 +11:00
|
|
|
if let Some(objc) = &self.objc {
|
2020-03-05 13:33:11 +11:00
|
|
|
unsafe {
|
2020-03-18 12:19:56 +11:00
|
|
|
let types: NSArray = types.into_iter().map(|t| {
|
|
|
|
// This clone probably doesn't need to be here, but it should also be cheap as
|
|
|
|
// this is just an enum... and this is not an oft called method.
|
|
|
|
let x: NSString = t.clone().into();
|
|
|
|
x.into_inner()
|
|
|
|
}).collect::<Vec<id>>().into();
|
2020-03-05 13:33:11 +11:00
|
|
|
|
2020-03-13 12:18:32 +11:00
|
|
|
let view: id = msg_send![*objc, view];
|
2020-03-18 12:19:56 +11:00
|
|
|
let _: () = msg_send![view, registerForDraggedTypes:types.into_inner()];
|
2020-03-05 13:33:11 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-13 12:18:32 +11:00
|
|
|
|
|
|
|
pub fn add_subview<T: Layout>(&self, subview: &T) {
|
|
|
|
if let Some(this) = &self.objc {
|
|
|
|
if let Some(subview_controller) = subview.get_backing_node() {
|
|
|
|
unsafe {
|
|
|
|
let _: () = msg_send![*this, addChildViewController:&*subview_controller];
|
|
|
|
|
|
|
|
let subview: id = msg_send![&*subview_controller, view];
|
|
|
|
let view: id = msg_send![*this, view];
|
|
|
|
let _: () = msg_send![view, addSubview:subview];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-12 11:56:17 +11:00
|
|
|
}
|