Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
843 lines (658 sloc) 24.7 KB
library com.livecode.extensions.devore.nswindow
use com.livecode.foreign
use com.livecode.objc
use com.livecode.library.scriptitems
metadata title is "NSWindow"
metadata author is "Trevor DeVore"
metadata version is "0.1.0"
private foreign handler ObjC_NSApplicationSharedApplication() returns ObjcId binds to "objc:NSApplication.+sharedApplication"
private foreign handler ObjC_NSAppWindowWithWindowNumber(in pObj as ObjcId, in pWindowNumber as CLong) returns ObjcId binds to "objc:NSApplication.-windowWithWindowNumber:"
private foreign handler ObjC_NSWindowGetStyleMask(in pObj as ObjcId) returns CLong binds to "objc:NSWindow.-styleMask"
private foreign handler ObjC_NSWindowSetStyleMask(in pObj as ObjcId, in pOptions as CInt) returns nothing binds to "objc:NSWindow.-setStyleMask:"
private foreign handler ObjC_NSWindowToggleFullScreen(in pObj as ObjcId, in pSender as optional ObjcId) returns nothing binds to "objc:NSWindow.-toggleFullScreen:"
private foreign handler ObjC_NSWindowSetCollectionBehavior(in pObj as ObjcId, in pOptions as CInt) returns nothing binds to "objc:NSWindow.-setCollectionBehavior:"
private foreign handler ObjC_NSWindowGetCollectionBehavior(in pObj as ObjcId) returns CInt binds to "objc:NSWindow.-collectionBehavior"
private foreign handler ObjC_NSWindowSetIgnoresMouseEvents(in pObj as ObjcId, in pBool as CBool) returns nothing binds to "objc:NSWindow.-setIgnoresMouseEvents:"
private foreign handler ObjC_NSWindowGetIgnoresMouseEvents(in pObj as ObjcId) returns CBool binds to "objc:NSWindow.-ignoresMouseEvents"
private foreign handler ObjC_NSWindowSetLevel(in pObj as ObjcId, in pLevel as CInt) returns nothing binds to "objc:NSWindow.-setLevel:"
private foreign handler ObjC_NSWindowGetLevel(in pObj as ObjcId) returns CInt binds to "objc:NSWindow.-level"
private foreign handler ObjC_NSWindowMakeKeyAndOrderFront(in pObj as ObjcId, in Sender as optional ObjcId) returns nothing binds to "objc:NSWindow.-makeKeyAndOrderFront:"
private foreign handler ObjC_NSWindowOrderFront(in pObj as ObjcId, in Sender as optional ObjcId) returns nothing binds to "objc:NSWindow.-orderFront:"
private foreign handler ObjC_NSWindowGetRepresentedFilename(in pObj as ObjcId) returns ObjcId binds to "objc:NSWindow.-representedFilename"
private foreign handler ObjC_NSWindowSetRepresentedFilename(in pObj as ObjcId, in pFilename as ObjcId) returns nothing binds to "objc:NSWindow.-setRepresentedFilename:"
private foreign handler CGWindowListCopyWindowInfo(in pListOptions as CUint, in pRelativeToWindow as CUint) \
returns ObjcRetainedId \
binds to "c:CoreGraphics.framework>CGWindowListCopyWindowInfo"
private constant kCGNullWindowId is 0
private handler _GetNSWindow(in pWindowId as Integer) returns optional ObjcId
variable tNSApp as ObjcObject
variable tWindow as optional ObjcObject
unsafe
put ObjC_NSApplicationSharedApplication() into tNSApp
put ObjC_NSAppWindowWithWindowNumber(tNSApp, pWindowId) into tWindow
end unsafe
return tWindow
end handler
/**
Description:
https://developer.apple.com/documentation/appkit/nswindowstylemask?language=objc
*/
private handler _GetStyles() returns Array
return {"borderless": 0, \
"titled": 1 shifted left by 0 bitwise, \
"closable": 1 shifted left by 1 bitwise, \
"miniaturizable": 1 shifted left by 2 bitwise, \
"resizable": 1 shifted left by 3 bitwise, \
"textured background": 1 shifted left by 8 bitwise, \
"unified title and toolbar": 1 shifted left by 12 bitwise, \
"full screen": 1 shifted left by 14 bitwise, \
"full size content view": 1 shifted left by 15 bitwise, \
"utility window": 1 shifted left by 4 bitwise, \
"doc modal window": 1 shifted left by 6 bitwise, \
"nonactivating modal": 1 shifted left by 7 bitwise, \
"HUD window": 1 shifted left by 13 bitwise \
}
end handler
/**
Description:
https://developer.apple.com/documentation/appkit/nswindowcollectionbehavior?language=objc
*/
private handler _GetCollectionBehaviors() returns Array
return {"default": 0, \
"can join all spaces": 1 shifted left by 0 bitwise, \
"move to active space": 1 shifted left by 1 bitwise, \
"managed": 1 shifted left by 2 bitwise, \
"transient": 1 shifted left by 3 bitwise, \
"stationary": 1 shifted left by 4 bitwise, \
"participates in cycle": 1 shifted left by 5 bitwise, \
"ignores cycle": 1 shifted left by 6 bitwise, \
"full screen primary": 1 shifted left by 7 bitwise, \
"full screen auxiliary": 1 shifted left by 8 bitwise, \
"full screen allows tiling": 1 shifted left by 11 bitwise, \
"full screen disallows tiling": 1 shifted left by 12 bitwise, \
"full screen none": 1 shifted left by 9 bitwise \
}
end handler
/**
Description:
https://developer.apple.com/documentation/appkit/nswindowlevel?language=objc
Integer values come from CoreGraphics -> CGWindowLevel.h
*/
private handler _GetLevels() returns Array
return {"floating": 3, \
"main menu": 24, \
"panel": 8, \
"normal": 0, \
"pop-up menu": 101, \
"screen saver": 1000, \
"status": 25, \
"submenu": 3, \
"torn off": 3 \
}
end handler
/**
Description:
https://developer.apple.com/documentation/coregraphics/cgwindowlistoption?language=objc
*/
private handler _GetCGWindowListOptions() returns Array
return {"all": 0, \
"on screen only": 1 shifted left by 0 bitwise, \
"on screen above window": 1 shifted left by 1 bitwise, \
"on screen below window": 1 shifted left by 2 bitwise, \
"including window": 1 shifted left by 3 bitwise, \
"exclude desktop elements": 1 shifted left by 4 bitwise \
}
end handler
/**
Summary: Returns the list of available styles for a window.
Description:
See the following url for a description of the options:
https://developer.apple.com/documentation/appkit/nswindowstylemask?language=objc
* HUD window
* borderless
* closable
* doc modal window
* full screen
* full size content view
* miniaturizable
* nonactivating modal
* resizable
* textured background
* titled
* unified title and toolbar
* utility window
Returns: Comma-delimited list of styles.
*/
public handler NSWindowStyleMasks() returns String
variable tKeys as List
variable tOptions as String
put the keys of _GetStyles() into tKeys
sort tKeys in ascending text order
combine tKeys with "," into tOptions
return tOptions
end handler
/**
Summary: Returns the list of available collection behaviors for a window.
Description:
See the following url for a description of the options:
https://developer.apple.com/documentation/appkit/nswindowcollectionbehavior?language=objc
* default
* can join all spaces
* move to active space
* managed
* transient
* stationary
* participates in cycle
* ignores cycle
* full screen primary
* full screen auxiliary
* full screen allows tiling
* full screen disallows tiling
* full screen none
Returns: Comma-delimited list of collection behaviors.
*/
public handler NSWindowCollectionBehaviors() returns String
variable tKeys as List
variable tOptions as String
put the keys of _GetLevels() into tKeys
sort tKeys in ascending text order
combine tKeys with "," into tOptions
return tOptions
end handler
/**
Summary: Returns the list of available levels for a window.
Description:
See the following url for a description of the options:
https://developer.apple.com/documentation/appkit/nswindowlevel?language=objc
* floating
* main menu
* panel
* normal
* pop-up menu
* screen saver
* torn off
Returns: Comma-delimited list of levels.
*/
public handler NSWindowLevels() returns String
variable tKeys as List
variable tOptions as String
put the keys of _GetLevels() into tKeys
sort tKeys in ascending text order
combine tKeys with "," into tOptions
return tOptions
end handler
/**
Summary: Gets the styles for a window.
Parameters:
pWindowId: The `windowId` property of a stack.
Description:
See `NSWindowStyleMasks()` for a list the available collection behaviors.
Returns: Comma-delimited list of styles
*/
public handler NSWindowGetStyleMask(in pWindowId as Integer) returns String
variable tWindow as optional ObjcObject
variable tAllStyles as Array
variable tWindowStyles as Integer
variable tOptions as List
variable tOption as String
variable tOptionsStr as String
put _GetNSWindow(pWindowId) into tWindow
put _GetStyles() into tAllStyles
if tWindow is nothing then
log "window not found"
else
unsafe
put ObjC_NSWindowGetStyleMask(tWindow) into tWindowStyles
end unsafe
if tWindowStyles is 0 then
push "borderless" onto tOptions
else
repeat for each key tOption in tAllStyles
if tWindowStyles bitwise and tAllStyles[tOption] > 0 then
push tOption onto tOptions
end if
end repeat
end if
sort tOptions in ascending text order
combine tOptions with "," into tOptionsStr
end if
return tOptionsStr
end handler
/**
Summary: Gets the styles for a window.
Parameters:
pWindowId: The `windowId` property of a stack.
pStyles: Comma-delimited list of styles.
Description:
See `NSWindowStyleMasks()` for a list the available collection behaviors.
Returns: Comma-delimited list of collection behaviors
*/
public handler NSWindowSetStyleMask(in pWindowId as Integer, in pStyles as String) returns nothing
variable tWindow as optional ObjcObject
variable tStylesList as List
variable tStyle as String
variable tStyles as Integer
variable tAllStyles as Array
split pStyles by "," into tStylesList
put _GetNSWindow(pWindowId) into tWindow
put _GetStyles() into tAllStyles
repeat for each element tStyle in tStylesList
if tAllStyles[tStyle] is 0 then
put 0 into tStyles
exit repeat
else
put tStyles bitwise or tAllStyles[tStyle] into tStyles
end if
end repeat
unsafe
ObjC_NSWindowSetStyleMask(tWindow, tStyles)
end unsafe
end handler
/**
Summary: Returns whether or not a window has the specified list of styles.
Parameters:
pWindowId: The `windowId` property of a stack.
pStyles: Comma-delimited list of styles.
Description:
See `NSWindowStyleMasks()` for a list the available styles.
Returns: Boolean
*/
public handler NSWindowHasStyleMasks(in pWindowId as Integer, in pStyleOptions as String) returns Boolean
variable tWindow as optional ObjcObject
variable tStyleMask as optional Integer
variable tStyleMasks as Array
variable tOptionsToCheck as List
variable tIsMatch as Boolean
split pStyleOptions by "," into tOptionsToCheck
put _GetNSWindow(pWindowId) into tWindow
unsafe
if tWindow is nothing then
log "window not found"
else
put ObjC_NSWindowGetStyleMask(tWindow) into tStyleMask
end if
end unsafe
if tStyleMask is not nothing then
variable tMask as String
put _GetStyles() into tStyleMasks
repeat for each element tMask in tOptionsToCheck
if tStyleMasks[tMask] is 0 then
put tStyleMask is 0 into tIsMatch
exit repeat
else
put tStyleMask bitwise and tStyleMasks[tMask] > 0 into tIsMatch
if not tIsMatch then
exit repeat
end if
end if
end repeat
end if
return tIsMatch
end handler
/**
Summary: Toggles a window back and forth between the full screen state.
Parameters:
pWindowId: The `windowId` property of a stack
Returns: nothing
*/
public handler NSWindowToggleIsFullScreen(in pWindowId as Integer)
variable tWindow as optional ObjcObject
unsafe
put _GetNSWindow(pWindowId) into tWindow
if tWindow is nothing then
log "window not found"
else
ObjC_NSWindowToggleFullScreen(tWindow, nothing)
end if
end unsafe
end handler
/**
Summary: Sets the collection behaviors for a window.
Parameters:
pWindowId: The `windowId` property of a stack.
pBehaviors: Comma-delimited list of collection behaviors.
Description:
See `NSWindowCollectionBehaviors()` for a list the available collection behaviors.
Returns: nothing
*/
public handler NSWindowSetCollectionBehavior(in pWindowId as Integer, in pBehaviors as String)
variable tWindow as optional ObjcObject
variable tBehaviorsList as List
variable tBehavior as String
variable tBehaviors as Integer
variable tCollectionBehaviors as Array
split pBehaviors by "," into tBehaviorsList
put _GetNSWindow(pWindowId) into tWindow
put _GetCollectionBehaviors() into tCollectionBehaviors
repeat for each element tBehavior in tBehaviorsList
if tCollectionBehaviors[tBehavior] is 0 then
put 0 into tBehaviors
exit repeat
else
put tBehaviors bitwise or tCollectionBehaviors[tBehavior] into tBehaviors
end if
end repeat
unsafe
ObjC_NSWindowSetCollectionBehavior(tWindow, tBehaviors)
end unsafe
end handler
/**
Summary: Gets the collection behaviors for a window.
Parameters:
pWindowId: The `windowId` property of a stack.
Description:
See `NSWindowCollectionBehaviors()` for a list the available collection behaviors.
Returns: Comma-delimited list of collection behaviors
*/
public handler NSWindowGetCollectionBehavior(in pWindowId as Integer) returns String
variable tWindow as optional ObjcObject
variable tCollectionBehaviors as Array
variable tBehaviors as Integer
variable tOptions as List
variable tOption as String
variable tOptionsStr as String
put _GetNSWindow(pWindowId) into tWindow
put _GetCollectionBehaviors() into tCollectionBehaviors
if tWindow is nothing then
log "window not found"
else
unsafe
put ObjC_NSWindowGetCollectionBehavior(tWindow) into tBehaviors
end unsafe
if tBehaviors is 0 then
push "default" onto tOptions
else
repeat for each key tOption in tCollectionBehaviors
if tBehaviors bitwise and tCollectionBehaviors[tOption] > 0 then
push tOption onto tOptions
end if
end repeat
end if
sort tOptions in ascending text order
combine tOptions with "," into tOptionsStr
end if
return tOptionsStr
end handler
/**
Summary: Set to true to tell the window to ignore mouse events.
Parameters:
pWindowId: The `windowId` property of a stack.
pIgnoreEvents: true or false.
Description:
When set to true this property will ignore all mouse events.
Returns: nothing
*/
public handler NSWindowSetIgnoresMouseEvents(in pWindowId as Integer, in pIgnoreEvents as Boolean)
variable tWindow as optional ObjcObject
put _GetNSWindow(pWindowId) into tWindow
unsafe
if tWindow is nothing then
log "window not found"
else
ObjC_NSWindowSetIgnoresMouseEvents(tWindow, pIgnoreEvents)
end if
end unsafe
end handler
/**
Summary: Returns the ignore mouse events setting for the window.
Parameters:
pWindowId: The `windowId` property of a stack.
Returns: Boolean
*/
public handler NSWindowGetIgnoresMouseEvents(in pWindowId as Integer) returns Boolean
variable tWindow as optional ObjcObject
variable tBoolean as Boolean
put _GetNSWindow(pWindowId) into tWindow
if tWindow is nothing then
log "window not found"
else
unsafe
put ObjC_NSWindowGetIgnoresMouseEvents(tWindow) into tBoolean
end unsafe
end if
return tBoolean
end handler
/**
Summary: Moves the window to the front of the screen list, within its level, and shows it.
Parameters:
pWindowId: The `windowId` property of a stack.
Returns: nothing
*/
public handler NSWindowMakeKeyAndOrderFront(in pWindowId as Integer)
variable tWindow as optional ObjcObject
put _GetNSWindow(pWindowId) into tWindow
unsafe
if tWindow is nothing then
log "window not found"
else
ObjC_NSWindowMakeKeyAndOrderFront(tWindow, nothing)
end if
end unsafe
end handler
/**
Summary: Moves the window to the front of the screen list, within its level.
Parameters:
pWindowId: The `windowId` property of a stack.
Returns: nothing
*/
public handler NSWindowOrderFront(in pWindowId as Integer)
variable tWindow as optional ObjcObject
put _GetNSWindow(pWindowId) into tWindow
unsafe
if tWindow is nothing then
log "window not found"
else
ObjC_NSWindowOrderFront(tWindow, nothing)
end if
end unsafe
end handler
/**
Summary: Returns the filename associated with a window.
Parameters:
pWindowId: The `windowId` property of a stack.
Returns: String
*/
public handler NSWindowGetRepresentedFilename(in pWindowId as Integer) returns String
variable tWindow as optional ObjcObject
variable tFilename as optional ObjcObject
put _GetNSWindow(pWindowId) into tWindow
unsafe
put ObjC_NSWindowGetRepresentedFilename(tWindow) into tFilename
end unsafe
if tFilename is not nothing then
return StringFromNSString(tFilename)
else
return ""
end if
end handler
/**
Summary: Sets the filename associated with a window.
Parameters:
pWindowId: The `windowId` property of a stack.
pFilename:
Description:
Setting the filename of a window will display the icon of that filename in the title
bar. Command + clicking on the title bar title will display the path to the file in a
menu.
Returns: nothing
*/
public handler NSWindowSetRepresentedFilename(in pWindowId as Integer, in pFilename as String)
variable tWindow as optional ObjcObject
put _GetNSWindow(pWindowId) into tWindow
unsafe
ObjC_NSWindowSetRepresentedFilename(tWindow, StringToNSString(pFilename))
end unsafe
end handler
/**
Summary: Sets the level of a window.
Parameters:
pWindowId: The `windowId` property of a stack.
pLevel: The level description (e.g. "screen saver").
Description:
See `NSWindowLevels()` for list of levels.
Returns: nothing
*/
public handler NSWindowSetLevel(in pWindowId as Integer, in pLevel as String)
variable tWindow as optional ObjcObject
variable tLevel as Integer
variable tError as String
put _GetNSWindow(pWindowId) into tWindow
if tWindow is nothing then
log "window not found"
else
put _GetWindowLevelFromString(pLevel, tLevel) into tError
if tError is not empty then
log tError
else
unsafe
ObjC_NSWindowSetLevel(tWindow, tLevel)
end unsafe
end if
end if
end handler
/**
Summary: Returns the level of a window.
Parameters:
pWindowId: The `windowId` property of a stack.
Description:
See `NSWindowLevels()` for list of levels.
Returns: String representing the window level (e.g. "screen saver").
*/
public handler NSWindowGetLevel(in pWindowId as Integer) returns String
variable tWindow as optional ObjcObject
variable tLevel as Integer
put _GetNSWindow(pWindowId) into tWindow
if tWindow is nothing then
log "window not found"
else
unsafe
put ObjC_NSWindowGetLevel(tWindow) into tLevel
end unsafe
return _GetWindowLevelStringFromInteger(tLevel)
end if
return ""
end handler
/**
Summary: Returns the integer window level represented by a string.
*/
private handler _GetWindowLevelFromString(in pLevel as String, out pOutLevel as Integer) returns String
variable tLevels as Array
variable tKey as String
put _GetLevels() into tLevels
repeat for each key tKey in tLevels
if tKey is pLevel then
put tLevels[tKey] into pOutLevel
return ""
end if
end repeat
return "window level not found:" && pLevel
end handler
/**
Summary: Returns the string that represents an integer window level.
*/
private handler _GetWindowLevelStringFromInteger(in pLevel as Integer) returns String
variable tLevels as Array
variable tKey as String
put _GetLevels() into tLevels
repeat for each key tKey in tLevels
if tLevels[tKey] is pLevel then
return tKey
end if
end repeat
return ""
end handler
/**
Summary:
Parameters:
pFilename:
Returns: NSDictionary
*/
public handler NSWindowGetInfoForOpenWindows(in pWindowIdsToIgnore as String) returns List
variable tWindowListOptions as Array
variable tWindowListOption as Integer
variable tWindowArray as ObjcObject
variable tWindowsIdsToIgnore as List
variable kCGWindowSharingNone as Number
put 0 into kCGWindowSharingNone
put parseItemsAsNumberList(pWindowIdsToIgnore, nothing, nothing) into tWindowsIdsToIgnore
put _GetCGWindowListOptions() into tWindowListOptions
-- put tWindowListOption bitwise or tWindowListOptions["on screen only"] into tWindowListOption
-- put tWindowListOption bitwise or tWindowListOptions["exclude desktop elements"] into tWindowListOption
unsafe
put CGWindowListCopyWindowInfo(tWindowListOption, kCGNullWindowId) into tWindowArray
end unsafe
variable tWindows as List
variable tWindow as Array
variable tEntry as Array
variable tWindowList as List
variable tRect as Array
variable tString as String
put ListFromNSArray(tWindowArray) into tWindows
repeat for each element tWindow in tWindows
/*
{kCGWindowAlpha: 1.000000, kCGWindowIsOnscreen: true, kCGWindowStoreType: 2, kCGWindowOwnerName: "Window Server",
kCGWindowNumber: 2, kCGWindowSharingState: 1, kCGWindowLayer: -2147483626, kCGWindowName: "Desktop", kCGWindowMemoryUsage: 5328,
kCGWindowBounds: {X: 0.000000, Height: 1440.000000, Y: 0.000000, Width: 2560.000000},
kCGWindowOwnerPID: 198}
*/
-- log "window:"
-- repeat for each key tKey in tWindow
-- log [" " & tKey & ": ", tWindow[tKey]]
-- end repeat
if "kCGWindowOwnerName" is among the keys of tWindow and tWindow["kCGWindowOwnerName"] is "Dock" then
next repeat
end if
if "kCGWindowAlpha" is among the keys of tWindow and tWindow["kCGWindowAlpha"] is 0 then
next repeat
end if
if "kCGWindowSharingState" is among the keys of tWindow and tWindow["kCGWindowSharingState"] is kCGWindowSharingNone then
-- next repeat
end if
put the empty array into tEntry
if "kCGWindowNumber" is among the keys of tWindow then
if tWindowsIdsToIgnore contains [tWindow["kCGWindowNumber"]] then
next repeat
end if
put tWindow["kCGWindowNumber"] into tEntry["id"]
end if
if "kCGWindowIsOnscreen" is among the keys of tWindow then
put tWindow["kCGWindowIsOnscreen"] into tEntry["is onscreen"]
else
put false into tEntry["is onscreen"]
end if
if "kCGWindowOwnerPID" is among the keys of tWindow then
put tWindow["kCGWindowOwnerPID"] into tEntry["owner pid"]
end if
if "kCGWindowAlpha" is among the keys of tWindow then
put tWindow["kCGWindowAlpha"] into tEntry["alpha"]
end if
if "kCGWindowOwnerName" is among the keys of tWindow then
put tWindow["kCGWindowOwnerName"] into tEntry["owner"]
end if
if "kCGWindowSharingState" is among the keys of tWindow then
put tWindow["kCGWindowSharingState"] into tEntry["sharing state"]
end if
if "kCGWindowName" is among the keys of tWindow then
put tWindow["kCGWindowName"] into tEntry["name"]
else if "owner" is among the keys of tEntry and tEntry["owner"] is not "SystemUIServer" then
put tEntry["owner"] into tEntry["name"]
end if
if "kCGWindowLayer" is among the keys of tWindow then
put tWindow["kCGWindowLayer"] into tEntry["layer"]
end if
if "kCGWindowBounds" is among the keys of tWindow then
put tWindow["kCGWindowBounds"] into tRect
format tRect["X"] as string into tString
put tString & "," into tEntry["rect"]
format tRect["Y"] as string into tString
put tString & "," after tEntry["rect"]
format tRect["X"] + tRect["Width"] as string into tString
put tString & "," after tEntry["rect"]
format tRect["Y"] + tRect["Height"] as string into tString
put tString after tEntry["rect"]
end if
push tEntry onto tWindowList
end repeat
return tWindowList
end handler
end library