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

mac, win32 floating window #77

Merged
merged 5 commits into from
Mar 14, 2022
Merged
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
5 changes: 5 additions & 0 deletions src/windy/platforms/macos/macdefs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type
NSStringEncoding* = uint
NSStringEncodingConversionOptions* = uint
NSBitmapImageFileType* = uint
NSWindowLevel* = int

NSRect* = CGRect
NSPoint* = CGPoint
Expand Down Expand Up @@ -104,6 +105,8 @@ const
NSTrackingEnabledDuringMouseDrag* = 0x400.NSTrackingAreaOptions
NSUTF32StringEncoding* = 0x8c000100.NSStringEncoding
NSBitmapImageFileTypePNG* = 4.NSBitmapImageFileType
NSNormalWindowLevel* = 0.NSWindowLevel
NSFloatingWindowLevel* = 3.NSWindowLevel

var
NSApp* {.importc.}: NSApplication
Expand Down Expand Up @@ -218,6 +221,8 @@ objc:
proc toggleFullscreen*(self: NSWindow, _: ID)
proc invalidateCursorRectsForView*(self: NSWindow, _: NSView)
proc mouseLocationOutsideOfEventStream*(self: NSWindow): NSPoint
proc level*(self: NSWindow): NSWindowLevel
proc setLevel*(self: NSWindow, _: NSWindowLevel)
proc convertRectToBacking*(self: NSView, _: NSRect): NSRect
proc window*(self: NSView): NSWindow
proc bounds*(self: NSView): NSRect
Expand Down
14 changes: 14 additions & 0 deletions src/windy/platforms/macos/platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ proc style*(window: Window): WindowStyle =
proc fullscreen*(window: Window): bool =
(window.inner.styleMask and NSWindowStyleMaskFullScreen) != 0

proc floating*(window: Window): bool =
window.inner.level == NSFloatingWindowLevel

proc contentScale*(window: Window): float32 =
autoreleasepool:
let
Expand Down Expand Up @@ -151,6 +154,17 @@ proc `fullscreen=`*(window: Window, fullscreen: bool) =
autoreleasepool:
window.inner.toggleFullscreen(0.ID)

proc `floating=`*(window: Window, floating: bool) =
if window.floating == floating:
return
autoreleasepool:
let level =
if floating:
NSFloatingWindowLevel
else:
NSNormalWindowLevel
window.inner.setLevel(level)

proc `size=`*(window: Window, size: IVec2) =
autoreleasepool:
let virtualSize = (size.vec2 / window.contentScale)
Expand Down
22 changes: 21 additions & 1 deletion src/windy/platforms/win32/platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type
state: WindowState
trackMouseEventRegistered: bool
exitFullscreenInfo: ExitFullscreenInfo
isFloating: bool

hWnd: HWND
hdc: HDC
Expand Down Expand Up @@ -328,6 +329,9 @@ proc style*(window: Window): WindowStyle =
proc fullscreen*(window: Window): bool =
window.exitFullscreenInfo != nil

proc floating*(window: Window): bool =
window.isFloating

proc contentScale*(window: Window): float32 =
let dpi = GetDpiForWindow(window.hWnd)
result = dpi.float32 / defaultScreenDpi
Expand Down Expand Up @@ -432,7 +436,7 @@ proc `fullscreen=`*(window: Window, fullscreen: bool) =
let mi = window.monitorInfo
discard SetWindowPos(
window.hWnd,
HWND_TOPMOST,
HWND_TOP,
mi.rcMonitor.left,
mi.rcMonitor.top,
mi.rcMonitor.right - mi.rcMonitor.left,
Expand Down Expand Up @@ -470,6 +474,22 @@ proc `fullscreen=`*(window: Window, fullscreen: bool) =
if maximized:
discard SendMessageW(window.hWnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0)

proc `floating=`*(window: Window, floating: bool) =
if window.floating == floating:
return

window.isFloating = floating

discard SetWindowPos(
window.hWnd,
if floating: HWND_TOPMOST else: HWND_NOTOPMOST,
0,
0,
0,
0,
SWP_NOMOVE or SWP_NOSIZE or SWP_NOACTIVATE
)

proc `size=`*(window: Window, size: IVec2) =
if window.fullscreen:
return
Expand Down