summary history branches tags files
commit:d50c794ceae305d414975eac931312d3428229ab
author:Trevor Bentley
committer:Trevor Bentley
date:Mon Jul 24 19:47:25 2017 +0200
parents:6a1d4765e729787a6e0dd0e1ead72d2222fc1014
Add ability to change button contents.  Fix crash when using image templates.
diff --git a/src/interface.rs b/src/interface.rs
line changes: +10/-0
index 0561455..f100bc5
--- a/src/interface.rs
+++ b/src/interface.rs
@@ -413,6 +413,16 @@ pub trait TTouchbar {
     /// 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.

diff --git a/src/touchbar.rs b/src/touchbar.rs
line changes: +18/-0
index ece400c..a0ed5c4
--- a/src/touchbar.rs
+++ b/src/touchbar.rs
@@ -655,6 +655,7 @@ impl TTouchbar for Touchbar {
         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
         }
     }
@@ -686,6 +687,23 @@ impl TTouchbar for Touchbar {
         }
     }
 
+    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;