|
| 1 | +import "@nativescript/macos-node-api"; |
| 2 | + |
| 3 | +const adapter = await navigator.gpu.requestAdapter(); |
| 4 | +const device = await adapter!.requestDevice(); |
| 5 | + |
| 6 | +@NativeClass |
| 7 | +export class ApplicationDelegate |
| 8 | + extends NSObject |
| 9 | + implements NSApplicationDelegate |
| 10 | +{ |
| 11 | + static ObjCProtocols = [NSApplicationDelegate]; |
| 12 | + |
| 13 | + running = true; |
| 14 | + window: Window | null = null; |
| 15 | + |
| 16 | + applicationDidFinishLaunching(_notification: NSNotification) { |
| 17 | + this.window = Window.new(); |
| 18 | + } |
| 19 | + |
| 20 | + applicationWillTerminate(_notification: NSNotification) { |
| 21 | + this.running = false; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +@NativeClass |
| 26 | +export class Window extends NSWindow implements NSWindowDelegate { |
| 27 | + static ObjCProtocols = [NSWindowDelegate]; |
| 28 | + |
| 29 | + surface!: Deno.UnsafeWindowSurface; |
| 30 | + context!: GPUCanvasContext; |
| 31 | + renderPipeline!: GPURenderPipeline; |
| 32 | + |
| 33 | + init() { |
| 34 | + const menu = NSMenu.new(); |
| 35 | + NSApp.mainMenu = menu; |
| 36 | + |
| 37 | + const appMenuItem = NSMenuItem.new(); |
| 38 | + menu.addItem(appMenuItem); |
| 39 | + |
| 40 | + const appMenu = NSMenu.new(); |
| 41 | + appMenuItem.submenu = appMenu; |
| 42 | + |
| 43 | + appMenu.addItemWithTitleActionKeyEquivalent("Quit", "terminate:", "q"); |
| 44 | + |
| 45 | + super.initWithContentRectStyleMaskBackingDefer( |
| 46 | + { origin: { x: 0, y: 0 }, size: { width: 500, height: 500 } }, |
| 47 | + NSWindowStyleMask.Titled | |
| 48 | + NSWindowStyleMask.Closable | |
| 49 | + NSWindowStyleMask.Miniaturizable | |
| 50 | + NSWindowStyleMask.Resizable, |
| 51 | + 2, |
| 52 | + false |
| 53 | + ); |
| 54 | + |
| 55 | + this.title = "NativeScript for macOS"; |
| 56 | + this.delegate = this; |
| 57 | + |
| 58 | + this.isReleasedWhenClosed = false; |
| 59 | + |
| 60 | + this.surface = new Deno.UnsafeWindowSurface( |
| 61 | + "cocoa", |
| 62 | + Deno.UnsafePointer.create(BigInt(interop.handleof(this).toNumber())), |
| 63 | + Deno.UnsafePointer.create( |
| 64 | + BigInt(interop.handleof(this.contentView).toNumber()) |
| 65 | + ) |
| 66 | + ); |
| 67 | + |
| 68 | + const { width, height } = this.convertRectToBacking(this.frame).size; |
| 69 | + |
| 70 | + this.context = this.surface.getContext("webgpu"); |
| 71 | + |
| 72 | + this.context.configure({ |
| 73 | + device, |
| 74 | + format: "bgra8unorm", |
| 75 | + width, |
| 76 | + height, |
| 77 | + }); |
| 78 | + |
| 79 | + const shaderCode = ` |
| 80 | +@vertex |
| 81 | +fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> @builtin(position) vec4<f32> { |
| 82 | + let x = f32(i32(in_vertex_index) - 1); |
| 83 | + let y = f32(i32(in_vertex_index & 1u) * 2 - 1); |
| 84 | + return vec4<f32>(x, y, 0.0, 1.0); |
| 85 | +} |
| 86 | +
|
| 87 | +@fragment |
| 88 | +fn fs_main() -> @location(0) vec4<f32> { |
| 89 | + return vec4<f32>(1.0, 0.0, 0.0, 1.0); |
| 90 | +} |
| 91 | +`; |
| 92 | + |
| 93 | + const shaderModule = device.createShaderModule({ |
| 94 | + code: shaderCode, |
| 95 | + }); |
| 96 | + |
| 97 | + const pipelineLayout = device.createPipelineLayout({ |
| 98 | + bindGroupLayouts: [], |
| 99 | + }); |
| 100 | + |
| 101 | + this.renderPipeline = device.createRenderPipeline({ |
| 102 | + layout: pipelineLayout, |
| 103 | + vertex: { |
| 104 | + module: shaderModule, |
| 105 | + entryPoint: "vs_main", |
| 106 | + }, |
| 107 | + fragment: { |
| 108 | + module: shaderModule, |
| 109 | + entryPoint: "fs_main", |
| 110 | + targets: [ |
| 111 | + { |
| 112 | + format: "bgra8unorm", |
| 113 | + }, |
| 114 | + ], |
| 115 | + }, |
| 116 | + }); |
| 117 | + |
| 118 | + NSApp.activateIgnoringOtherApps(true); |
| 119 | + |
| 120 | + this.makeKeyAndOrderFront(NSApp); |
| 121 | + |
| 122 | + this.center(); |
| 123 | + |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + render() { |
| 128 | + const encoder = device.createCommandEncoder(); |
| 129 | + const texture = this.context.getCurrentTexture().createView(); |
| 130 | + const renderPass = encoder.beginRenderPass({ |
| 131 | + colorAttachments: [ |
| 132 | + { |
| 133 | + view: texture, |
| 134 | + storeOp: "store", |
| 135 | + loadOp: "clear", |
| 136 | + clearValue: { r: 0, g: 1, b: 0, a: 1.0 }, |
| 137 | + }, |
| 138 | + ], |
| 139 | + }); |
| 140 | + renderPass.setPipeline(this.renderPipeline); |
| 141 | + renderPass.draw(3, 1); |
| 142 | + renderPass.end(); |
| 143 | + device.queue.submit([encoder.finish()]); |
| 144 | + this.surface.present(); |
| 145 | + } |
| 146 | + |
| 147 | + windowWillClose(_notification: NSNotification) { |
| 148 | + NSApp.terminate(this); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +const NSApp = NSApplication.sharedApplication; |
| 153 | + |
| 154 | +NSApp.setActivationPolicy(NSApplicationActivationPolicy.Regular); |
| 155 | + |
| 156 | +const delegate = ApplicationDelegate.new(); |
| 157 | + |
| 158 | +NSApp.delegate = delegate; |
| 159 | + |
| 160 | +NSApp.finishLaunching(); |
| 161 | + |
| 162 | +while (true) { |
| 163 | + const event = NSApp.nextEventMatchingMaskUntilDateInModeDequeue( |
| 164 | + NSEventMask.Any, |
| 165 | + null, |
| 166 | + "kCFRunLoopDefaultMode", |
| 167 | + true |
| 168 | + ); |
| 169 | + |
| 170 | + if (event != null) { |
| 171 | + NSApp.sendEvent(event); |
| 172 | + } |
| 173 | + |
| 174 | + if (!delegate.running) { |
| 175 | + break; |
| 176 | + } |
| 177 | + |
| 178 | + delegate.window?.render(); |
| 179 | +} |
0 commit comments