Add ability to change button contents. Fix crash when using image templates.
/// A newly allocated item which can be added to a bar.
fn create_button(&mut self, image: Option<&TouchbarImage>, text: Option<&str>, cb: ButtonCb) -> ItemId {0}
+ /// Changes the image and/or text of a button
+ ///
+ /// # Arguments
+ ///
+ /// * `item` - Button item to change
+ /// * `image` - New image to draw on button (optional)
+ /// * `text` - New text to draw on button (optional)
+ ///
+ fn update_button(&mut self, item: &ItemId, image: Option<&TouchbarImage>, text: Option<&str>) {}
+
/// Changes the width of an existing button
///
/// Set a fixed width for a button, in pixels.
unsafe {
let cls = Class::get("NSImage").unwrap();
let image: *mut Object = msg_send![cls, imageNamed: ImageTemplate::objc(template)];
+ let _ = msg_send![image, retain];
image as TouchbarImage
}
}
}
}
+ fn update_button(&mut self, item: &ItemId, image: Option<&TouchbarImage>, text: Option<&str>) {
+ unsafe {
+ let item = *item as *mut Object;
+ let btn: *mut Object = msg_send![item, view];
+ if let Some(image) = image {
+ let image = *image as *mut Object;
+ let _ = msg_send![btn, setImage: image];
+ let _ = msg_send![image, release];
+ }
+ if let Some(text) = text {
+ let objc_text = NSString::alloc(nil).init_str(text);
+ let _ = msg_send![btn, setTitle: objc_text];
+ let _ = msg_send![objc_text, release];
+ }
+ }
+ }
+
fn update_button_width(&mut self, button_id: &ItemId, width: u32) {
unsafe {
let item: *mut Object = *button_id as *mut Object;