1// Foundation's values as AppKit passes them, for the macOS window2// (appkit.alloy) and its accessibility (nsaccessibility.alloy): points,3// sizes, rectangles and ranges by value, arrays, and the conversions4// between the view's pixels and the screen's points and between UTF-85// and the UTF-16 NSString counts in.67import std::ffi;8import std::string;910import window;11import unicode;12import pkg::alloy::objc;1314pub type NSPoint = struct {15 pub x: f64,16 pub y: f64,17};1819pub type NSRect = struct {20 pub x: f64,21 pub y: f64,22 pub width: f64,23 pub height: f64,24};2526// A range of an NSString's UTF-16 units.27pub type NSRange = struct {28 pub location: u64,29 pub length: u64,30};3132// NSNotFound33pub const NOT_FOUND: u64 = 0x7FFFFFFFFFFFFFFF;3435// Type encodings of the values above.36pub const RECT_CODE: &[u8] = &"{CGRect={CGPoint=dd}{CGSize=dd}}";37pub const POINT_CODE: &[u8] = &"{CGPoint=dd}";38pub const RANGE_CODE: &[u8] = &"{_NSRange=QQ}";3940// An autoreleased NSArray of the objects.41pub fn array(o: &Objc, objects: &[c_pointer]) -> c_pointer {42 const make = o.send<extern (c_pointer, c_pointer, c_pointer, u64) -> c_pointer>();43 return make(44 o.class(&"NSArray"),45 o.sel(&"arrayWithObjects:count:"),46 c_pointer::of(&objects),47 objects.length()48 );49}5051// An NSArray's object at the index.52pub fn object_at(o: &Objc, array: c_pointer, index: u64) -> c_pointer {53 return o.call_integer(array, &"objectAtIndex:", index);54}5556// An area of the view, in pixels from its top left, on the screen, in57// points from its bottom left, as accessibility and input methods take58// it.59pub fn screen_rect(o: &Objc, view: c_pointer, window: c_pointer, area: &Area) -> NSRect {60 const scale = o.number(window, &"backingScaleFactor");61 const in_view = NSRect {62 .x = area.x to f64 / scale,63 .y = area.y to f64 / scale,64 .width = area.width to f64 / scale,65 .height = area.height to f64 / scale66 };67 const to_window = o.send_struct<extern (c_pointer, c_pointer, NSRect, c_pointer) -> NSRect>();68 const in_window = to_window(view, o.sel(&"convertRect:toView:"), in_view, 0);69 const to_screen = o.send_struct<extern (c_pointer, c_pointer, NSRect) -> NSRect>();70 return to_screen(window, o.sel(&"convertRectToScreen:"), in_window);71}7273// A point on the screen, in points from its bottom left, in the view, in74// pixels from its top left.75pub fn view_point(o: &Objc, view: c_pointer, window: c_pointer, at: NSPoint) -> Point {76 const from_screen = o.send<extern (c_pointer, c_pointer, NSPoint) -> NSPoint>();77 const in_window = from_screen(window, o.sel(&"convertPointFromScreen:"), at);78 return window_point(&o, view, window, in_window);79}8081// A point in the window, in points from its bottom left, in the view, in82// pixels from its top left.83pub fn window_point(o: &Objc, view: c_pointer, window: c_pointer, at: NSPoint) -> Point {84 const convert = o.send<extern (c_pointer, c_pointer, NSPoint, c_pointer) -> NSPoint>();85 const in_view = convert(view, o.sel(&"convertPoint:fromView:"), at, 0);86 const scale = o.number(window, &"backingScaleFactor");87 return Point { .x = (in_view.x * scale) to f32, .y = (in_view.y * scale) to f32 };88}8990// An NSString's or NSAttributedString's text, as an input method or a91// screen reader passes either.92pub fn plain_text(o: &Objc, text: c_pointer) -> String {93 if (text == 0) {94 return String::empty();95 }96 const attributed = o.class(&"NSAttributedString");97 const is_kind = o.send<extern (c_pointer, c_pointer, c_pointer) -> bool>();98 if (is_kind(text, o.sel(&"isKindOfClass:"), attributed)) {99 return o.text_of(o.call(text, &"string"));100 }101 return o.text_of(text);102}