zed/crates/gpui/src/platform/mac.rs

101 lines
2.1 KiB
Rust
Raw Normal View History

mod appearance;
mod atlas;
mod dispatcher;
mod event;
mod fonts;
2021-02-20 23:05:36 +00:00
mod geometry;
mod image_cache;
mod platform;
mod renderer;
mod screen;
mod sprite_cache;
2022-09-08 17:07:11 +00:00
mod status_item;
2021-02-20 23:05:36 +00:00
mod window;
2023-01-26 01:05:57 +00:00
use cocoa::{
base::{id, nil, BOOL, NO, YES},
foundation::{NSAutoreleasePool, NSNotFound, NSString, NSUInteger},
};
2021-02-20 23:05:36 +00:00
pub use dispatcher::Dispatcher;
pub use fonts::FontSystem;
use platform::{MacForegroundPlatform, MacPlatform};
pub use renderer::Surface;
2023-01-26 01:05:57 +00:00
use std::{ops::Range, rc::Rc, sync::Arc};
2021-02-20 23:05:36 +00:00
use window::Window;
pub(crate) fn platform() -> Arc<dyn super::Platform> {
Arc::new(MacPlatform::new())
2021-02-20 23:05:36 +00:00
}
2021-04-02 21:26:53 +00:00
2023-02-24 05:36:17 +00:00
pub(crate) fn foreground_platform() -> Rc<dyn super::ForegroundPlatform> {
Rc::new(MacForegroundPlatform::new())
}
2021-02-20 23:05:36 +00:00
trait BoolExt {
fn to_objc(self) -> BOOL;
}
2021-02-20 23:05:36 +00:00
impl BoolExt for bool {
fn to_objc(self) -> BOOL {
if self {
YES
} else {
NO
}
}
}
2023-01-26 01:05:57 +00:00
#[repr(C)]
#[derive(Copy, Clone, Debug)]
struct NSRange {
pub location: NSUInteger,
pub length: NSUInteger,
}
impl NSRange {
fn invalid() -> Self {
Self {
location: NSNotFound as NSUInteger,
length: 0,
}
}
fn is_valid(&self) -> bool {
self.location != NSNotFound as NSUInteger
}
fn to_range(self) -> Option<Range<usize>> {
if self.is_valid() {
let start = self.location as usize;
let end = start + self.length as usize;
Some(start..end)
} else {
None
}
}
}
impl From<Range<usize>> for NSRange {
fn from(range: Range<usize>) -> Self {
NSRange {
location: range.start as NSUInteger,
length: range.len() as NSUInteger,
}
}
}
unsafe impl objc::Encode for NSRange {
fn encode() -> objc::Encoding {
let encoding = format!(
"{{NSRange={}{}}}",
NSUInteger::encode().as_str(),
NSUInteger::encode().as_str()
);
unsafe { objc::Encoding::from_str(&encoding) }
}
}
unsafe fn ns_string(string: &str) -> id {
NSString::alloc(nil).init_str(string).autorelease()
}