Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nomerge] Cg context type #451

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
run: /usr/bin/clang++ info.mm -framework Cocoa -o test && ./test
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ matrix:
install:
- rustup target add $TARGET
script:
- cargo build --all-targets --verbose --target $TARGET
- cargo test --verbose --target $TARGET -- --nocapture
- /usr/bin/clang++ info.mm -framework Cocoa -o test && ./test
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Targets macOS 10.7 by default.
To enable features added in macOS 10.8, set Cargo feature `mac_os_10_8_features`. To have both 10.8 features and 10.7 compatibility, also set `mac_os_10_7_support`. Setting both requires weak linkage, which is a nightly-only feature as of Rust 1.19.

For more experimental but more complete, generated bindings take a look at https://github.com/michaelwu/RustKit.
Other alternatives are https://github.com/nvzqz/fruity and https://gitlab.com/objrs/objrs
92 changes: 92 additions & 0 deletions info.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* clang++ main.mm -framework Cocoa -o test && ./test
**/

#import <Cocoa/Cocoa.h>

@interface TestView: NSView
{
}

@end

@implementation TestView

- (id)initWithFrame:(NSRect)aFrame
{
if (self = [super initWithFrame:aFrame]) {
}
return self;
}
extern "C" {
typedef enum {
kCGContextTypeUnknown,
kCGContextTypePDF,
kCGContextTypePostScript,
kCGContextTypeWindow,
kCGContextTypeBitmap,
kCGContextTypeGL,
kCGContextTypeDisplayList,
kCGContextTypeKSeparation,
kCGContextTypeIOSurface,
kCGContextTypeCount
} CGContextType;


CGContextType CGContextGetType(CGContextRef);
}
- (void)drawRect:(NSRect)aRect
{
CGContextRef cg = NSGraphicsContext.currentContext.CGContext;
CFShow(cg);
printf("CGContextType: %d\n", CGContextGetType(cg));
CGColorSpaceRef color = CGBitmapContextGetColorSpace(cg);
CFShow(color);
exit(1);
for (int y = 0; y<20; y++) {
for (int x = 0; x<20; x++) {
CGContextSetRGBFillColor(cg, 0.2, 0.6, 1.0, 0.9);
CGContextFillEllipseInRect(cg, CGRectMake(50+x*50, 30+30*y, 200, 130));
}
}
}

@end

@interface TerminateOnClose : NSObject<NSWindowDelegate>
@end

@implementation TerminateOnClose
- (void)windowWillClose:(NSNotification*)notification
{
[NSApp terminate:self];
}
@end

int
main (int argc, char **argv)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];

NSRect contentRect = NSMakeRect(400, 300, 300, 200);
NSWindow* window = [[NSWindow alloc] initWithContentRect:contentRect
styleMask:NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:NO];
window.contentView.wantsLayer = YES;
NSView* view = [[TestView alloc] initWithFrame:NSMakeRect(0, 0, contentRect.size.width, contentRect.size.height)];

[window setContentView:view];
[window setDelegate:[[TerminateOnClose alloc] autorelease]];
[NSApp activateIgnoringOtherApps:YES];
[window makeKeyAndOrderFront:window];

[NSApp run];

[pool release];

return 0;
}