diff --git a/src/windy/platforms/macos/macdefs.nim b/src/windy/platforms/macos/macdefs.nim index 7d939ac..0493956 100644 --- a/src/windy/platforms/macos/macdefs.nim +++ b/src/windy/platforms/macos/macdefs.nim @@ -30,6 +30,7 @@ type NSTrackingAreaOptions* = uint NSStringEncoding* = uint NSStringEncodingConversionOptions* = uint + NSBitmapImageFileType* = uint NSRect* = CGRect NSPoint* = CGPoint @@ -61,6 +62,8 @@ type NSCursor* = distinct NSObject NSTextInputContext* = distinct NSObject NSTextInputClient* = distinct int + NSBitmapImageRep* = distinct NSObject + NSDictionary* = distinct NSObject const NSNotFound* = int.high @@ -100,10 +103,12 @@ const NSTrackingInVisibleRect* = 0x200.NSTrackingAreaOptions NSTrackingEnabledDuringMouseDrag* = 0x400.NSTrackingAreaOptions NSUTF32StringEncoding* = 0x8c000100.NSStringEncoding + NSBitmapImageFileTypePNG* = 4.NSBitmapImageFileType var NSApp* {.importc.}: NSApplication NSPasteboardTypeString* {.importc.}: NSPasteboardType + NSPasteboardTypeTIFF* {.importc.}: NSPasteboardType NSDefaultRunLoopMode* {.importc.}: NSRunLoopMode objc: @@ -132,6 +137,7 @@ objc: proc keyCode*(self: NSEvent): uint16 proc dataWithBytes*(class: typedesc[NSData], _: pointer, length: int): NSData proc length*(self: NSData): uint + proc bytes*(self: NSData): pointer proc length*(self: NSString): uint proc array*(class: typedesc[NSArray]): NSArray proc count*(self: NSArray): uint @@ -144,6 +150,7 @@ objc: proc generalPasteboard*(class: typedesc[NSPasteboard]): NSPasteboard proc types*(self: NSPasteboard): NSArray proc stringForType*(self: NSPasteboard, _: NSPasteboardType): NSString + proc dataForType*(self: NSPasteboard, _: NSPasteboardType): NSData proc clearContents*(self: NSPasteboard) proc setString*(self: NSPasteboard, _: NSString, forType: NSPasteboardType) proc processInfo*(class: typedesc[NSProcessInfo]): NSProcessInfo @@ -177,7 +184,7 @@ objc: _: NSString, action: SEL, keyEquivalent: NSString - ) + ): NSMenuItem proc setSubmenu*(self: NSMenuItem, _: NSMenu) proc initWithContentRect*( self: NSWindow, @@ -185,7 +192,7 @@ objc: styleMask: NSWindowStyleMask, backing: NSBackingStoreType, defer_mangle: bool - ) + ): NSWindow proc orderFront*(self: NSWindow, _: ID) proc orderOut*(self: NSWindow, _: ID) proc setTitle*(self: NSWindow, _: NSString) @@ -221,12 +228,12 @@ objc: proc initWithAttributes*( self: NSOpenGLPixelFormat, _: ptr NSOpenGLPixelFormatAttribute - ) + ): NSOpenGLPixelFormat proc initWithFrame*( self: NSOpenGLView, _: NSRect, pixelFormat: NSOpenGLPixelFormat - ) + ): NSOpenGLView proc setWantsBestResolutionOpenGLSurface*( self: NSOpenGLView, _: bool @@ -245,14 +252,20 @@ objc: options: NSTrackingAreaOptions, owner: ID, userInfo: ID - ) - proc initWithData*(self: NSImage, _: NSData) - proc initWithImage*(self: NSCursor, _: NSImage, hotSpot: NSPoint) + ): NSTrackingArea + proc initWithData*(self: NSImage, _: NSData): NSImage + proc initWithImage*(self: NSCursor, _: NSImage, hotSpot: NSPoint): NSCursor proc discardMarkedText*(self: NSTextInputContext) proc handleEvent*(self: NSTextInputContext, _: NSEvent): bool proc deactivate*(self: NSTextInputContext) proc activate*(self: NSTextInputContext) proc insertText*(self: NSTextInputClient, _: ID, replacementRange: NSRange) + proc initWithData*(self: NSBitmapImageRep, _: NSData): NSBitmapImageRep + proc representationUsingType*( + self: NSBitmapImageRep, + _: NSBitmapImageFileType, + properties: NSDictionary + ): NSData {.push inline.} diff --git a/src/windy/platforms/macos/platform.nim b/src/windy/platforms/macos/platform.nim index cfec19f..f7d2725 100644 --- a/src/windy/platforms/macos/platform.nim +++ b/src/windy/platforms/macos/platform.nim @@ -233,12 +233,11 @@ proc createMenuBar() = appMenu = NSMenu.new() processName = NSProcessInfo.processinfo.processName quitTitle = @("Quit " & $processName) - quitMenuitem = NSMenuItem.alloc() - quitMenuitem.initWithTitle( - quitTitle, - s"terminate:", - @"q" - ) + quitMenuitem = NSMenuItem.alloc().initWithTitle( + quitTitle, + s"terminate:", + @"q" + ) appMenu.addItem(quitMenuItem) appMenuItem.setSubmenu(appMenu) @@ -349,8 +348,7 @@ proc updateTrackingAreas(self: ID, cmd: SEL): ID {.cdecl.} = NSTrackingInVisibleRect or NSTrackingAssumeInside - window.trackingArea = NSTrackingArea.alloc() - window.trackingArea.initWithRect( + window.trackingArea = NSTrackingArea.alloc().initWithRect( NSMakeRect(0, 0, 0, 0), options, self, @@ -629,17 +627,15 @@ proc resetCursorRects(self: ID, cmd: SEL): ID {.cdecl.} = else: let encodedPng = window.state.cursor.image.encodePng() - image = NSImage.alloc() - cursor = NSCursor.alloc() + image = NSImage.alloc().initWithData(NSData.dataWithBytes( + encodedPng[0].unsafeAddr, + encodedPng.len + )) hotspot = NSMakePoint( window.state.cursor.hotspot.x.float, window.state.cursor.hotspot.y.float ) - image.initWithData(NSData.dataWithBytes( - encodedPng[0].unsafeAddr, - encodedPng.len - )) - cursor.initWithImage(image, hotspot) + cursor = NSCursor.alloc().initWithImage(image, hotspot) self.NSView.addCursorRect(self.NSView.bounds, cursor) proc init() = @@ -766,8 +762,7 @@ proc newWindow*( init() autoreleasepool: - result.inner = WindyWindow.alloc().NSWindow - result.inner.initWithContentRect( + result.inner = WindyWindow.alloc().NSWindow.initWithContentRect( NSMakeRect(0, 0, 400, 400), decoratedResizableWindowMask, NSBackingStoreBuffered, @@ -775,7 +770,6 @@ proc newWindow*( ) let - pixelFormat = NSOpenGLPixelFormat.alloc() pixelFormatAttribs = [ NSOpenGLPFADoubleBuffer, NSOpenGLPFASampleBuffers, if msaa != msaaDisabled: 1 else: 0, @@ -797,10 +791,11 @@ proc newWindow*( ), 0 ] - pixelFormat.initWithAttributes(pixelFormatAttribs[0].unsafeAddr) + pixelFormat = NSOpenGLPixelFormat.alloc().initWithAttributes( + pixelFormatAttribs[0].unsafeAddr + ) - let openglView = WindyView.alloc().NSOpenGLView - openglView.initWithFrame( + let openglView = WindyView.alloc().NSOpenGLView.initWithFrame( result.inner.contentView.frame, pixelFormat ) @@ -876,6 +871,63 @@ proc buttonReleased*(window: Window): ButtonView = proc buttonToggle*(window: Window): ButtonView = window.state.buttonToggle.ButtonView +proc getClipboardContentKinds*(): set[ClipboardContentKind] = + init() + autoreleasepool: + let + pboard = NSPasteboard.generalPasteboard + types = pboard.types + + if types.int == 0: + return + + for i in 0 ..< types.count: + echo $types[i.int].NSString + + if types.containsObject(NSPasteboardTypeString.ID): + result.incl TextContent + if types.containsObject(NSPasteboardTypeTIFF.ID): + result.incl ImageContent + +proc getClipboardImage*(): Image = + init() + + autoreleasepool: + let + pboard = NSPasteboard.generalPasteboard + types = pboard.types + + if types.int == 0: + return + + if not types.containsObject(NSPasteboardTypeTIFF.ID): + return + + let data = pboard.dataForType(NSPasteboardTypeTIFF) + if data.int == 0: + return + + let bitmap = NSBitmapImageRep.alloc().initWithData(data) + if bitmap.int == 0: + return + + let pngData = bitmap.representationUsingType( + NSBitmapImageFileTypePNG, + 0.NSDictionary + ) + if pngData.int == 0: + return + + # Pixie needs a decodePng for ptr + len, copy into string for now + + var copiedData = newString(pngData.length) + copyMem(copiedData[0].addr, pngData.bytes, pngData.length) + + try: + result = decodePng(copiedData) + except: + return + proc getClipboardString*(): string = init() autoreleasepool: