写了一个 Lemon 语言到 Objective-C 的绑定,可以用 Lemon 语言写 Mac 和

2020-04-14 23:42发布

下面的代码示例

import 'lobjc.so' as objc;

var NSObject = objc.NSClassFromString('NSObject');
var NSColor = objc.NSClassFromString('NSColor');
var NSWindow = objc.NSClassFromString('NSWindow');
var NSWindowController = objc.NSClassFromString('NSWindowController');
var NSApplication = objc.NSClassFromString('NSApplication');

@objc.protocol('NSWindowDelegate')
class WindowDelegate(NSObject) {
        def windowDidResize_(var n) {
        }
}

class WindowController(NSWindowController) {
}

@objc.protocol('NSApplicationDelegate')
class AppDelegate(NSObject) {
        def applicationDidFinishLaunching_(var aNotification) {
                self.window = NSWindow(
                        contentRect = objc.CGRectMake(10, 10, 800, 600),
                        styleMask = objc.NSWindowStyleMaskMiniaturizable |
                                        objc.NSWindowStyleMaskTitled |
                                        objc.NSWindowStyleMaskClosable |
                                        objc.NSWindowStyleMaskResizable,
                        backing = objc.NSBackingStoreBuffered,
                        defer = false);

                self.window.setTitle_("Hello,World!");
                self.windowDelegate = WindowDelegate();
                self.window.setDelegate_(self.windowDelegate);
                self.window.setBackgroundColor_(NSColor.whiteColor());
                self.windowController = WindowController(window=self.window);
                self.windowController.showWindow_(self);

                self.windowController.showWindow_(self.windowController);
        }

        def applicationShouldTerminateAfterLastWindowClosed_(var app) {
                return true;
        }
}

var delegate = AppDelegate();
var app = NSApplication.sharedApplication();
app.setDelegate_(delegate);
app.run();

更多例子可以看代码里的 example 目录。目前只有 2000 多行代码,欢迎大家帮忙测试和改进。


登录 后发表评论
1条评论
齐丽娟
1楼 · 2020-04-14 23:50

[db:评论]