pub fn sel(name: &CStr) -> *const c_voidExpand description
Register (or look up) a selector.
Examples found in repository?
examples/scenes.rs (line 213)
209 unsafe fn text(ns: *mut c_void) -> String {
210 if ns.is_null() {
211 return String::new();
212 }
213 let p = unsafe { objc::send_ret_ptr(ns, objc::sel(c"UTF8String")) };
214 if p.is_null() {
215 return String::new();
216 }
217 unsafe { core::ffi::CStr::from_ptr(p as *const core::ffi::c_char) }
218 .to_string_lossy()
219 .into_owned()
220 }
221
222 /// `array[i]`, for an `NSArray`.
223 unsafe fn at(array: *mut c_void, i: u64) -> *mut c_void {
224 unsafe { objc::send_u64(array, objc::sel(c"objectAtIndex:"), i) }
225 }
226
227 unsafe fn count(array: *mut c_void) -> u64 {
228 if array.is_null() {
229 0
230 } else {
231 unsafe { objc::send_ret_u64(array, objc::sel(c"count")) }
232 }
233 }
234
235 unsafe extern "C" fn inspect(_: *mut c_void) {
236 unsafe {
237 let app = objc::shared_app();
238
239 let windows = objc::send(app, objc::sel(c"windows"));
240 let n = count(windows);
241 println!("windows: {n}");
242 for i in 0..n {
243 let w = at(windows, i);
244 let title = text(objc::send(w, objc::sel(c"title")));
245 let visible = objc::send_ret_bool(w, objc::sel(c"isVisible"));
246 let class = objc::send(w, objc::sel(c"class"));
247 let kind = text(objc::send(class, objc::sel(c"description")));
248 println!(" {kind:<24} visible={visible:<5} {title:?}");
249 }
250
251 let menu = objc::send(app, objc::sel(c"mainMenu"));
252 let items = objc::send(menu, objc::sel(c"itemArray"));
253 let n = count(items);
254 println!("menu bar: {n} menus");
255 for i in 0..n {
256 let item = at(items, i);
257 let title = text(objc::send(item, objc::sel(c"title")));
258 let sub = objc::send(item, objc::sel(c"submenu"));
259 let kids = objc::send(sub, objc::sel(c"itemArray"));
260 let entries: Vec<String> = (0..count(kids))
261 .map(|j| text(objc::send(at(kids, j), objc::sel(c"title"))))
262 .filter(|t| !t.is_empty())
263 .collect();
264 println!(" {title:?}: {}", entries.join(" · "));
265 }
266
267 // The menu-bar extra is an `NSStatusItem`, not a window or a menu,
268 // so it is counted separately or it would look like nothing was
269 // built for it.
270 let bar = objc::class(c"OBJC_CLASS_$_NSStatusBar");
271 let system = objc::send(bar, objc::sel(c"systemStatusBar"));
272 println!(
273 "status bar thickness: {}",
274 objc::send_ret_f64(system, objc::sel(c"thickness")),
275 );
276
277 std::process::exit(0);
278 }
279 }