mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
[stdlib] macOS - Add objc and core foundation types and enums. (#2572)
* Add CGFloat, CGPoint, CGSize, CGRect * Add WindowCollectionBehavior, WindowLevel, and WindowTabbingMode * Change EventMask to ulong to match the objc unsigned long long * Change int types to NS(U)Integer types to match objc * Add core foundation tests * Add objc tests * Add darwin conditional to the test files * Change enums to const inline to better match the NSEvent.h api. Update the EventMask helper function to match the NSEvent.h api: event_type_from -> event_mask_from_type. * Update the release notes * Deprecate original objc enums and replace with const inline enums backed with NS numerical types. Rename the new objc enums with an NS prefix. Update unit tests to account for new NS prefixed enums. Add states item length constants to core_foundation. Status item lengths don't really belong in either file, but as they are dependant on CGFloat it made sense to move them to the same module. Update release notes. * Some tweaks --------- Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
This commit is contained in:
76
test/unit/stdlib/os/macos/core_foundation.c3
Normal file
76
test/unit/stdlib/os/macos/core_foundation.c3
Normal file
@@ -0,0 +1,76 @@
|
||||
module std::os::macos::cf @test @if(env::DARWIN);
|
||||
|
||||
fn void test_cgfloat_type()
|
||||
{
|
||||
if (env::ARCH_64_BIT)
|
||||
{
|
||||
assert(CGFloat.sizeof == double.sizeof);
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(CGFloat.sizeof == float.sizeof);
|
||||
}
|
||||
}
|
||||
|
||||
fn void test_cgpoint()
|
||||
{
|
||||
CGPoint point = { .x = 10.0, .y = 20.0 };
|
||||
|
||||
assert(point.x == 10.0);
|
||||
assert(point.y == 20.0);
|
||||
|
||||
CGPoint point2 = { .x = point.x + 5.0, .y = point.y - 10.0 };
|
||||
assert(point2.x == 15.0);
|
||||
assert(point2.y == 10.0);
|
||||
}
|
||||
|
||||
fn void test_cgsize()
|
||||
{
|
||||
CGSize size = { .width = 100.0, .height = 200.0 };
|
||||
|
||||
assert(size.width == 100.0);
|
||||
assert(size.height == 200.0);
|
||||
|
||||
assert(@typeid(size.width) == CGFloat.typeid);
|
||||
assert(@typeid(size.height) == CGFloat.typeid);
|
||||
|
||||
CGSize negSize = { .width = -50.0, .height = -75.0 };
|
||||
assert(negSize.width == -50.0);
|
||||
assert(negSize.height == -75.0);
|
||||
}
|
||||
|
||||
fn void test_cgrect()
|
||||
{
|
||||
CGPoint origin = { .x = 10.0, .y = 20.0 };
|
||||
CGSize size = { .width = 100.0, .height = 200.0 };
|
||||
CGRect rect = { .origin = origin, .size = size };
|
||||
|
||||
assert(rect.origin.x == 10.0);
|
||||
assert(rect.origin.y == 20.0);
|
||||
assert(rect.size.width == 100.0);
|
||||
assert(rect.size.height == 200.0);
|
||||
|
||||
CGRect rect2 = {
|
||||
.origin = { .x = 0.0, .y = 0.0 },
|
||||
.size = { .width = 50.0, .height = 75.0 }
|
||||
};
|
||||
assert(rect2.origin.x == 0.0);
|
||||
assert(rect2.size.width == 50.0);
|
||||
}
|
||||
|
||||
|
||||
fn void test_geometry_struct_sizes()
|
||||
{
|
||||
if (env::ARCH_64_BIT)
|
||||
{
|
||||
assert(CGPoint.sizeof == 16); // 2 * double (8 bytes each)
|
||||
assert(CGSize.sizeof == 16); // 2 * double
|
||||
assert(CGRect.sizeof == 32); // CGPoint + CGSize
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(CGPoint.sizeof == 8); // 2 * float (4 bytes each)
|
||||
assert(CGSize.sizeof == 8); // 2 * float
|
||||
assert(CGRect.sizeof == 16); // CGPoint + CGSize
|
||||
}
|
||||
}
|
||||
188
test/unit/stdlib/os/macos/objc.c3
Normal file
188
test/unit/stdlib/os/macos/objc.c3
Normal file
@@ -0,0 +1,188 @@
|
||||
module std::os::macos::objc @test @if(env::DARWIN);
|
||||
|
||||
fn void test_application_activation_policy()
|
||||
{
|
||||
NSApplicationActivationPolicy regular = NSApplicationActivationPolicy.REGULAR;
|
||||
NSApplicationActivationPolicy accessory = NSApplicationActivationPolicy.ACCESSORY;
|
||||
NSApplicationActivationPolicy prohibited = NSApplicationActivationPolicy.PROHIBITED;
|
||||
|
||||
assert(regular == 0);
|
||||
assert(accessory == 1);
|
||||
assert(prohibited == 2);
|
||||
}
|
||||
|
||||
fn void test_backing_store_type()
|
||||
{
|
||||
NSBackingStoreType retained = NSBackingStoreType.RETAINED;
|
||||
NSBackingStoreType nonretained = NSBackingStoreType.NONRETAINED;
|
||||
NSBackingStoreType buffered = NSBackingStoreType.BUFFERED;
|
||||
|
||||
assert(retained == 0);
|
||||
assert(nonretained == 1);
|
||||
assert(buffered == 2);
|
||||
}
|
||||
|
||||
fn void test_event_type()
|
||||
{
|
||||
NSEventType keyDown = NSEventType.KEY_DOWN;
|
||||
NSEventType keyUp = NSEventType.KEY_UP;
|
||||
NSEventType mouseDown = NSEventType.LEFT_MOUSE_DOWN;
|
||||
NSEventType smartMagnify = NSEventType.SMART_MAGNIFY;
|
||||
NSEventType directTouch = NSEventType.DIRECT_TOUCH;
|
||||
|
||||
assert(keyDown == 10);
|
||||
assert(keyUp == 11);
|
||||
assert(mouseDown == 1);
|
||||
assert(smartMagnify == 32);
|
||||
assert(directTouch == 37);
|
||||
|
||||
NSUInteger raw = 10;
|
||||
NSEventType converted = (NSEventType)raw;
|
||||
assert(converted == NSEventType.KEY_DOWN);
|
||||
}
|
||||
|
||||
fn void test_event_mask_from_type()
|
||||
{
|
||||
NSEventMask maskMouseDown = ns::event_mask_from_type(NSEventType.LEFT_MOUSE_DOWN);
|
||||
NSEventMask maskKeyDown = ns::event_mask_from_type(NSEventType.KEY_DOWN);
|
||||
NSEventMask maskKeyUp = ns::event_mask_from_type(NSEventType.KEY_UP);
|
||||
NSEventMask maskSmartMagnify = ns::event_mask_from_type(NSEventType.SMART_MAGNIFY);
|
||||
NSEventMask maskDirectTouch = ns::event_mask_from_type(NSEventType.DIRECT_TOUCH);
|
||||
|
||||
assert(maskMouseDown == NSEventMask.LEFT_MOUSE_DOWN);
|
||||
assert(maskKeyDown == NSEventMask.KEY_DOWN);
|
||||
assert(maskKeyUp == NSEventMask.KEY_UP);
|
||||
assert(maskSmartMagnify == NSEventMask.SMART_MAGNIFY);
|
||||
assert(maskDirectTouch == NSEventMask.DIRECT_TOUCH);
|
||||
|
||||
assert(maskMouseDown == (1ul << 1));
|
||||
assert(maskKeyDown == (1ul << 10));
|
||||
assert(maskKeyUp == (1ul << 11));
|
||||
assert(maskSmartMagnify == (1ul << 32));
|
||||
assert(maskDirectTouch == (1ul << 37));
|
||||
}
|
||||
|
||||
fn void test_event_mask()
|
||||
{
|
||||
NSEventMask keyDownMask = NSEventMask.KEY_DOWN;
|
||||
NSEventMask keyUpMask = NSEventMask.KEY_UP;
|
||||
NSEventMask smartMagnifyMask = NSEventMask.SMART_MAGNIFY;
|
||||
NSEventMask directTouchMask = NSEventMask.DIRECT_TOUCH;
|
||||
NSEventMask anyMask = NSEventMask.ANY;
|
||||
|
||||
assert(keyDownMask == (1ul << 10));
|
||||
assert(keyUpMask == (1ul << 11));
|
||||
assert(smartMagnifyMask == (1ul << 32));
|
||||
assert(directTouchMask == (1ul << 37));
|
||||
assert(anyMask == ulong.max);
|
||||
|
||||
NSEventMask combined = NSEventMask.KEY_DOWN | NSEventMask.FLAGS_CHANGED;
|
||||
assert(combined == ((1ul << 10) | (1ul << 12)));
|
||||
}
|
||||
|
||||
|
||||
fn void test_status_item_length()
|
||||
{
|
||||
assert(NSStatusItemLength.VARIABLE == -1.0);
|
||||
assert(NSStatusItemLength.SQUARE == -2.0);
|
||||
}
|
||||
|
||||
fn void test_event_modifier_flags()
|
||||
{
|
||||
NSEventModifierFlags shift = NSEventModifierFlags.SHIFT;
|
||||
NSEventModifierFlags command = NSEventModifierFlags.COMMAND;
|
||||
NSEventModifierFlags option = NSEventModifierFlags.OPTION;
|
||||
NSEventModifierFlags control = NSEventModifierFlags.CONTROL;
|
||||
|
||||
assert(shift == (1 << 17));
|
||||
assert(command == (1 << 20));
|
||||
assert(option == (1 << 19));
|
||||
assert(control == (1 << 18));
|
||||
|
||||
NSUInteger cmdShift = (NSUInteger)command | (NSUInteger)shift;
|
||||
assert(cmdShift == ((1 << 20) | (1 << 17)));
|
||||
}
|
||||
|
||||
fn void test_window_style_mask()
|
||||
{
|
||||
NSWindowStyleMask titled = NSWindowStyleMask.TITLED;
|
||||
NSWindowStyleMask closable = NSWindowStyleMask.CLOSABLE;
|
||||
NSWindowStyleMask resizable = NSWindowStyleMask.RESIZABLE;
|
||||
NSWindowStyleMask borderless = NSWindowStyleMask.BORDERLESS;
|
||||
|
||||
assert(titled == (1 << 0));
|
||||
assert(closable == (1 << 1));
|
||||
assert(resizable == (1 << 3));
|
||||
assert(borderless == 0);
|
||||
|
||||
NSWindowStyleMask style = titled | closable | resizable;
|
||||
assert(style == ((1 << 0) | (1 << 1) | (1 << 3)));
|
||||
}
|
||||
|
||||
fn void test_window_collection_behavior()
|
||||
{
|
||||
NSWindowCollectionBehavior def = NSWindowCollectionBehavior.DEFAULT;
|
||||
NSWindowCollectionBehavior canJoin = NSWindowCollectionBehavior.CAN_JOIN_ALL_SPACES;
|
||||
NSWindowCollectionBehavior managed = NSWindowCollectionBehavior.MANAGED;
|
||||
NSWindowCollectionBehavior fullscreen = NSWindowCollectionBehavior.FULL_SCREEN_PRIMARY;
|
||||
NSWindowCollectionBehavior primary = NSWindowCollectionBehavior.PRIMARY;
|
||||
|
||||
assert(def == 0);
|
||||
assert(canJoin == (1 << 0));
|
||||
assert(managed == (1 << 2));
|
||||
assert(fullscreen == (1 << 7));
|
||||
assert(primary == (1 << 16));
|
||||
|
||||
NSWindowCollectionBehavior combined = managed | fullscreen;
|
||||
assert(combined == ((1 << 2) | (1 << 7)));
|
||||
}
|
||||
|
||||
fn void test_window_level()
|
||||
{
|
||||
NSWindowLevel normal = NSWindowLevel.NORMAL;
|
||||
NSWindowLevel floating = NSWindowLevel.FLOATING;
|
||||
NSWindowLevel modalPanel = NSWindowLevel.MODAL_PANEL;
|
||||
NSWindowLevel screenSaver = NSWindowLevel.SCREEN_SAVER;
|
||||
|
||||
assert(normal == 0);
|
||||
assert(floating == 3);
|
||||
assert(modalPanel == 8);
|
||||
assert(screenSaver == 1000);
|
||||
}
|
||||
|
||||
fn void test_window_tabbing_mode()
|
||||
{
|
||||
NSWindowTabbingMode automatic = NSWindowTabbingMode.AUTOMATIC;
|
||||
NSWindowTabbingMode preferred = NSWindowTabbingMode.PREFERRED;
|
||||
NSWindowTabbingMode disallowed = NSWindowTabbingMode.DISALLOWED;
|
||||
|
||||
assert(automatic == 0);
|
||||
assert(preferred == 1);
|
||||
assert(disallowed == 2);
|
||||
}
|
||||
|
||||
fn void test_nsuinteger_nsinteger_architecture()
|
||||
{
|
||||
if (env::ARCH_64_BIT)
|
||||
{
|
||||
assert(NSUInteger.sizeof == ulong.sizeof);
|
||||
assert(NSInteger.sizeof == long.sizeof);
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(NSUInteger.sizeof == uint.sizeof);
|
||||
assert(NSInteger.sizeof == int.sizeof);
|
||||
}
|
||||
}
|
||||
|
||||
// EventMask requires 64-bit values even on 32-bit systems
|
||||
fn void test_event_mask_64bit_values()
|
||||
{
|
||||
NSEventMask smartMagnify = NSEventMask.SMART_MAGNIFY;
|
||||
NSEventMask directTouch = NSEventMask.DIRECT_TOUCH;
|
||||
|
||||
assert(smartMagnify == (1ul << 32));
|
||||
assert(directTouch == (1ul << 37));
|
||||
|
||||
assert(NSEventMask.sizeof == ulong.sizeof);
|
||||
}
|
||||
Reference in New Issue
Block a user