fix: Add missing button apis

This commit is contained in:
Michal Hornický 2023-05-07 00:46:31 +02:00
parent 589ff8c789
commit f2f6a1bcfa
5 changed files with 75 additions and 5 deletions

View file

@ -99,6 +99,6 @@ required-features = ["appkit"]
[[example]] [[example]]
name = "window_controller" name = "window_controller"
required-features = ["appkit"] required-features = ["appkit"]
[[exmaple]] [[example]]
name = "safe_area" name = "safe_area"
required-features = ["appkit"] required-features = ["appkit"]

View file

@ -50,7 +50,7 @@ impl ViewDelegate for ContentView {
self.content.top.constraint_equal_to(&view.safe_layout_guide.top), self.content.top.constraint_equal_to(&view.safe_layout_guide.top),
self.content.leading.constraint_equal_to(&view.safe_layout_guide.leading), self.content.leading.constraint_equal_to(&view.safe_layout_guide.leading),
self.content.trailing.constraint_equal_to(&view.safe_layout_guide.trailing), self.content.trailing.constraint_equal_to(&view.safe_layout_guide.trailing),
self.content.bottom.constraint_equal_to(&view.safe_layout_guide.bottom), self.content.bottom.constraint_equal_to(&view.safe_layout_guide.bottom)
]) ])
} }
} }
@ -59,5 +59,6 @@ fn main() {
App::new("com.test.window", BasicApp { App::new("com.test.window", BasicApp {
window: Window::default(), window: Window::default(),
content_view: View::with(ContentView::default()) content_view: View::with(ContentView::default())
}).run(); })
.run();
} }

View file

@ -205,12 +205,14 @@ impl<T> Window<T> {
} }
} }
/// Sets the subtitle (smaller text bellow the title on unified and expanded title bars) on the /// Sets the subtitle (smaller text bellow the title on unified and expanded title bars) on the
/// underlying window. When this property is an empty string, the system removes the subtitle /// underlying window. When this property is an empty string, the system removes the subtitle
/// from the window layout. Allocates and passes an `NSString` over to the Objective C runtime. /// from the window layout. Allocates and passes an `NSString` over to the Objective C runtime.
/// Does nothing when less than version 11. /// Does nothing when less than version 11.
pub fn set_subtittle(&self, subtitle: &str) { pub fn set_subtittle(&self, subtitle: &str) {
if !os::is_minimum_version(11) { return; } if !os::is_minimum_version(11) {
return;
}
unsafe { unsafe {
let subtitle = NSString::new(subtitle); let subtitle = NSString::new(subtitle);

View file

@ -92,3 +92,54 @@ impl From<NSUInteger> for BezelStyle {
} }
} }
} }
#[cfg(feature = "appkit")]
#[derive(Debug)]
pub enum ImagePosition {
NoImage,
ImageOnly,
ImageLeft,
ImageRight,
ImageBelow,
ImageAbove,
ImageOverlaps,
ImageLeading,
ImageTrailing,
Other(NSUInteger)
}
impl From<ImagePosition> for NSUInteger {
fn from(value: ImagePosition) -> Self {
match value {
ImagePosition::NoImage => 0,
ImagePosition::ImageOnly => 1,
ImagePosition::ImageLeft => 2,
ImagePosition::ImageRight => 3,
ImagePosition::ImageBelow => 4,
ImagePosition::ImageAbove => 5,
ImagePosition::ImageOverlaps => 6,
ImagePosition::ImageLeading => 7,
ImagePosition::ImageTrailing => 8,
ImagePosition::Other(o) => o
}
}
}
impl From<NSUInteger> for ImagePosition {
fn from(value: NSUInteger) -> Self {
use ImagePosition::*;
match value {
0 => NoImage,
1 => ImageOnly,
2 => ImageLeft,
3 => ImageRight,
4 => ImageBelow,
5 => ImageAbove,
6 => ImageOverlaps,
7 => ImageLeading,
8 => ImageTrailing,
o => Other(o)
}
}
}

View file

@ -175,6 +175,15 @@ impl Button {
} }
} }
/// Changes the text of the button
#[cfg(feature = "appkit")]
pub fn set_text(&self, text: &str) {
let title = NSString::new(text);
self.objc.with_mut(|obj| unsafe {
let _: () = msg_send![obj, setTitle:&*title];
});
}
/// Sets an image on the underlying button. /// Sets an image on the underlying button.
pub fn set_image(&mut self, image: Image) { pub fn set_image(&mut self, image: Image) {
self.objc.with_mut(|obj| unsafe { self.objc.with_mut(|obj| unsafe {
@ -184,6 +193,13 @@ impl Button {
self.image = Some(image); self.image = Some(image);
} }
pub fn set_image_position(&self, image_position: ImagePosition) {
let position: NSUInteger = image_position.into();
self.objc.with_mut(|obj| unsafe {
let _: () = msg_send![obj, setImagePosition: position];
});
}
/// Sets the bezel style for this button. Only supported on appkit. /// Sets the bezel style for this button. Only supported on appkit.
#[cfg(feature = "appkit")] #[cfg(feature = "appkit")]
pub fn set_bezel_style(&self, bezel_style: BezelStyle) { pub fn set_bezel_style(&self, bezel_style: BezelStyle) {