feat: implement label and enabled in macOS TextMenuItem (#2)

* feat: implement label, enabled methods for macos TextMenuItem

* fix: using lossy version of from_utf8 and change the way we convert UTF8String to String
This commit is contained in:
Jason Tsai 2022-05-16 20:11:19 +08:00 committed by GitHub
parent a7b7b0cf34
commit 1ab9c9b9e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,6 @@
use cocoa::{
appkit::{NSButton, NSEventModifierFlags, NSMenuItem},
base::{id, nil, NO, YES},
appkit::NSButton,
base::{id, nil, BOOL, NO, YES},
foundation::NSString,
};
use objc::{
@ -10,6 +10,7 @@ use objc::{
runtime::{Class, Object, Sel},
sel, sel_impl,
};
use std::slice;
use std::sync::Once;
use std::{
collections::hash_map::DefaultHasher,
@ -73,19 +74,37 @@ impl TextMenuItem {
}
pub fn label(&self) -> String {
todo!()
unsafe {
let title: id = msg_send![self.ns_menu_item, title];
let data = title.UTF8String() as *const u8;
let len = title.len();
String::from_utf8_lossy(slice::from_raw_parts(data, len)).to_string()
}
}
pub fn set_label(&mut self, label: impl AsRef<str>) {
todo!()
unsafe {
let title = NSString::alloc(nil).init_str(label.as_ref());
self.ns_menu_item.setTitle_(title);
}
}
pub fn enabled(&self) -> bool {
todo!()
unsafe {
let enabled: BOOL = msg_send![self.ns_menu_item, enabled];
enabled
}
}
pub fn set_enabled(&mut self, enabled: bool) {
todo!()
unsafe {
let status = match enabled {
true => YES,
false => NO,
};
let () = msg_send![self.ns_menu_item, setEnabled: status];
}
}
pub fn id(&self) -> u64 {