mac.rs

  1mod appearance;
  2mod atlas;
  3mod dispatcher;
  4mod event;
  5mod fonts;
  6mod geometry;
  7mod image_cache;
  8mod platform;
  9mod renderer;
 10mod screen;
 11mod sprite_cache;
 12mod status_item;
 13mod window;
 14
 15use cocoa::{
 16    base::{id, nil, BOOL, NO, YES},
 17    foundation::{NSAutoreleasePool, NSNotFound, NSString, NSUInteger},
 18};
 19pub use dispatcher::Dispatcher;
 20pub use fonts::FontSystem;
 21use platform::{MacForegroundPlatform, MacPlatform};
 22pub use renderer::Surface;
 23use std::{ops::Range, rc::Rc, sync::Arc};
 24use window::MacWindow;
 25
 26use crate::executor;
 27
 28pub(crate) fn platform() -> Arc<dyn super::Platform> {
 29    Arc::new(MacPlatform::new())
 30}
 31
 32pub(crate) fn foreground_platform(
 33    foreground: Rc<executor::Foreground>,
 34) -> Rc<dyn super::ForegroundPlatform> {
 35    Rc::new(MacForegroundPlatform::new(foreground))
 36}
 37
 38trait BoolExt {
 39    fn to_objc(self) -> BOOL;
 40}
 41
 42impl BoolExt for bool {
 43    fn to_objc(self) -> BOOL {
 44        if self {
 45            YES
 46        } else {
 47            NO
 48        }
 49    }
 50}
 51
 52#[repr(C)]
 53#[derive(Copy, Clone, Debug)]
 54struct NSRange {
 55    pub location: NSUInteger,
 56    pub length: NSUInteger,
 57}
 58
 59impl NSRange {
 60    fn invalid() -> Self {
 61        Self {
 62            location: NSNotFound as NSUInteger,
 63            length: 0,
 64        }
 65    }
 66
 67    fn is_valid(&self) -> bool {
 68        self.location != NSNotFound as NSUInteger
 69    }
 70
 71    fn to_range(self) -> Option<Range<usize>> {
 72        if self.is_valid() {
 73            let start = self.location as usize;
 74            let end = start + self.length as usize;
 75            Some(start..end)
 76        } else {
 77            None
 78        }
 79    }
 80}
 81
 82impl From<Range<usize>> for NSRange {
 83    fn from(range: Range<usize>) -> Self {
 84        NSRange {
 85            location: range.start as NSUInteger,
 86            length: range.len() as NSUInteger,
 87        }
 88    }
 89}
 90
 91unsafe impl objc::Encode for NSRange {
 92    fn encode() -> objc::Encoding {
 93        let encoding = format!(
 94            "{{NSRange={}{}}}",
 95            NSUInteger::encode().as_str(),
 96            NSUInteger::encode().as_str()
 97        );
 98        unsafe { objc::Encoding::from_str(&encoding) }
 99    }
100}
101
102unsafe fn ns_string(string: &str) -> id {
103    NSString::alloc(nil).init_str(string).autorelease()
104}