use objc::runtime::Object;
use objc::{msg_send, sel, sel_impl};
use objc_id::ShareId;
use crate::foundation::id;
use crate::layout::constraint::LayoutConstraint;
#[derive(Clone, Debug)]
pub enum LayoutAnchorY {
Uninitialized,
Top(ShareId<Object>),
Bottom(ShareId<Object>),
Center(ShareId<Object>)
}
impl Default for LayoutAnchorY {
fn default() -> Self {
Self::Uninitialized
}
}
impl LayoutAnchorY {
pub(crate) fn top(view: id) -> Self {
Self::Top(unsafe { ShareId::from_ptr(msg_send![view, topAnchor]) })
}
pub(crate) fn bottom(view: id) -> Self {
Self::Bottom(unsafe { ShareId::from_ptr(msg_send![view, bottomAnchor]) })
}
pub(crate) fn center(view: id) -> Self {
Self::Center(unsafe { ShareId::from_ptr(msg_send![view, centerYAnchor]) })
}
fn constraint_with<F>(&self, anchor_to: &LayoutAnchorY, handler: F) -> LayoutConstraint
where
F: Fn(&ShareId<Object>, &ShareId<Object>) -> id
{
match (self, anchor_to) {
(Self::Top(from), Self::Top(to))
| (Self::Top(from), Self::Bottom(to))
| (Self::Top(from), Self::Center(to))
| (Self::Bottom(from), Self::Bottom(to))
| (Self::Bottom(from), Self::Top(to))
| (Self::Bottom(from), Self::Center(to))
| (Self::Center(from), Self::Center(to))
| (Self::Center(from), Self::Top(to))
| (Self::Center(from), Self::Bottom(to)) => LayoutConstraint::new(handler(from, to)),
(Self::Uninitialized, Self::Uninitialized) => {
panic!("Attempted to create constraints with uninitialized \"from\" and \"to\" y anchors.");
},
(Self::Uninitialized, _) => {
panic!("Attempted to create constraints with an uninitialized \"from\" y anchor.");
},
(_, Self::Uninitialized) => {
panic!("Attempted to create constraints with an uninitialized \"to\" y anchor.");
}
}
}
pub fn constraint_equal_to(&self, anchor_to: &LayoutAnchorY) -> LayoutConstraint {
self.constraint_with(anchor_to, |from, to| unsafe {
msg_send![*from, constraintEqualToAnchor:&**to]
})
}
pub fn constraint_greater_than_or_equal_to(&self, anchor_to: &LayoutAnchorY) -> LayoutConstraint {
self.constraint_with(anchor_to, |from, to| unsafe {
msg_send![*from, constraintGreaterThanOrEqualToAnchor:&**to]
})
}
pub fn constraint_less_than_or_equal_to(&self, anchor_to: &LayoutAnchorY) -> LayoutConstraint {
self.constraint_with(anchor_to, |from, to| unsafe {
msg_send![*from, constraintLessThanOrEqualToAnchor:&**to]
})
}
}