use objc::runtime::Object;
use objc::{class, msg_send, sel, sel_impl};
use objc_id::Id;
use block::ConcreteBlock;
use crate::color::Color;
use crate::foundation::{id, NSString, NSUInteger};
use crate::image::Image;
#[derive(Debug)]
pub enum RowActionStyle {
Regular,
Destructive
}
impl Default for RowActionStyle {
fn default() -> Self {
RowActionStyle::Regular
}
}
impl From<RowActionStyle> for NSUInteger {
fn from(style: RowActionStyle) -> Self {
match style {
RowActionStyle::Regular => 0,
RowActionStyle::Destructive => 1
}
}
}
#[derive(Debug)]
pub struct RowAction(pub Id<Object>);
impl RowAction {
pub fn new<F>(title: &str, style: RowActionStyle, handler: F) -> Self
where
F: Fn(RowAction, usize) + 'static
{
let title = NSString::new(title);
let block = ConcreteBlock::new(move |action: id, row: NSUInteger| {
let action = RowAction(unsafe { Id::from_ptr(action) });
handler(action, row as usize);
});
let block = block.copy();
let style = style as NSUInteger;
RowAction(unsafe {
let cls = class!(NSTableViewRowAction);
Id::from_ptr(msg_send![cls, rowActionWithStyle:style
title:&*title
handler:block
])
})
}
pub fn set_title(&mut self, title: &str) {
let title = NSString::new(title);
unsafe {
let _: () = msg_send![&*self.0, setTitle:&*title];
}
}
pub fn set_background_color<C: AsRef<Color>>(&mut self, color: C) {
let color: id = color.as_ref().into();
unsafe {
let _: () = msg_send![&*self.0, setBackgroundColor: color];
}
}
pub fn set_style(&mut self, style: RowActionStyle) {
let style = style as NSUInteger;
unsafe {
let _: () = msg_send![&*self.0, setStyle: style];
}
}
pub fn set_image(&mut self, image: Image) {
unsafe {
let _: () = msg_send![&*self.0, setImage:&*image.0];
}
}
}