use core_graphics::base::CGFloat;
use objc::runtime::Object;
use objc::{class, msg_send, sel, sel_impl};
use objc_id::ShareId;
use crate::foundation::{id, NO, YES};
use super::LayoutConstraintAnimatorProxy;
#[derive(Clone, Debug)]
pub struct LayoutConstraint {
pub constraint: ShareId<Object>,
pub offset: f64,
pub multiplier: f64,
pub priority: f64,
pub animator: LayoutConstraintAnimatorProxy
}
impl LayoutConstraint {
pub(crate) fn new(object: id) -> Self {
LayoutConstraint {
animator: LayoutConstraintAnimatorProxy::new(object),
constraint: unsafe { ShareId::from_ptr(object) },
offset: 0.0,
multiplier: 0.0,
priority: 0.0
}
}
pub fn offset<F: Into<f64>>(self, offset: F) -> Self {
let offset: f64 = offset.into();
unsafe {
let o = offset as CGFloat;
let _: () = msg_send![&*self.constraint, setConstant: o];
}
LayoutConstraint {
animator: self.animator,
constraint: self.constraint,
offset: offset,
multiplier: self.multiplier,
priority: self.priority
}
}
pub fn set_offset<F: Into<f64>>(&self, offset: F) {
let offset: f64 = offset.into();
unsafe {
let o = offset as CGFloat;
let _: () = msg_send![&*self.constraint, setConstant: o];
}
}
pub fn set_active(&self, active: bool) {
unsafe {
let _: () = msg_send![&*self.constraint, setActive:match active {
true => YES,
false => NO
}];
}
}
pub fn activate(constraints: &[LayoutConstraint]) {
unsafe {
let ids: Vec<&Object> = constraints.into_iter().map(|constraint| &*constraint.constraint).collect();
let constraints: id = msg_send![class!(NSArray), arrayWithObjects:ids.as_ptr() count:ids.len()];
let _: () = msg_send![class!(NSLayoutConstraint), activateConstraints: constraints];
}
}
pub fn deactivate(constraints: &[LayoutConstraint]) {
unsafe {
let ids: Vec<&Object> = constraints.into_iter().map(|constraint| &*constraint.constraint).collect();
let constraints: id = msg_send![class!(NSArray), arrayWithObjects:ids.as_ptr() count:ids.len()];
let _: () = msg_send![class!(NSLayoutConstraint), deactivateConstraints: constraints];
}
}
}