Skip to content

Instantly share code, notes, and snippets.

@tatut
Created January 9, 2024 19:17
Show Gist options
  • Save tatut/9aee8d527afd03011cd30cbebcb1b84b to your computer and use it in GitHub Desktop.
Save tatut/9aee8d527afd03011cd30cbebcb1b84b to your computer and use it in GitHub Desktop.
WebUI FFI bindings
"create new window instance"
w := WuWindow new.
"open it with some html"
w show: '<html><head><script src="/webui.js"></script><title>WuWindow</title></head><body><button id="klik">click me</button></body></html>'.
"clicking the button in the displayed window does nothing now"
"bind an event... this call succeeds"
b := w bind: 'klik' to: [ :evt | Transcript show: 'clicked me'; cr. ]
" ^ after that call, clicking the button crashes the vm"
FFIStructure subclass: #WuEvent
instanceVariableNames: ''
classVariableNames: 'OFFSET_BIND_ID OFFSET_ELEMENT OFFSET_EVENT_NUMBER OFFSET_EVENT_TYPE OFFSET_WINDOW'
package: 'WebUI'!
!WuEvent methodsFor: 'accessing - structure variables' stamp: 'UFFIGenerator 1/4/2024 13:07'!
element: anObject
"This method was automatically generated"
handle pointerAt: OFFSET_ELEMENT put: anObject getHandle.! !
!WuEvent methodsFor: 'accessing - structure variables' stamp: 'UFFIGenerator 1/4/2024 13:07'!
element
"This method was automatically generated"
^ExternalData fromHandle: (handle pointerAt: OFFSET_ELEMENT) type: ExternalType char asPointerType! !
!WuEvent methodsFor: 'accessing - structure variables' stamp: 'UFFIGenerator 1/4/2024 13:07'!
event_type: anObject
"This method was automatically generated"
^handle platformSizeTAt: OFFSET_EVENT_TYPE put: anObject! !
!WuEvent methodsFor: 'accessing - structure variables' stamp: 'UFFIGenerator 1/4/2024 13:07'!
event_number
"This method was automatically generated"
^handle platformSizeTAt: OFFSET_EVENT_NUMBER! !
!WuEvent methodsFor: 'accessing - structure variables' stamp: 'UFFIGenerator 1/4/2024 13:07'!
window
"This method was automatically generated"
^handle platformSizeTAt: OFFSET_WINDOW! !
!WuEvent methodsFor: 'accessing - structure variables' stamp: 'UFFIGenerator 1/4/2024 13:07'!
bind_id
"This method was automatically generated"
^handle platformSizeTAt: OFFSET_BIND_ID! !
!WuEvent methodsFor: 'accessing - structure variables' stamp: 'UFFIGenerator 1/4/2024 13:07'!
event_type
"This method was automatically generated"
^handle platformSizeTAt: OFFSET_EVENT_TYPE! !
!WuEvent methodsFor: 'accessing - structure variables' stamp: 'UFFIGenerator 1/4/2024 13:07'!
bind_id: anObject
"This method was automatically generated"
^handle platformSizeTAt: OFFSET_BIND_ID put: anObject! !
!WuEvent methodsFor: 'accessing - structure variables' stamp: 'UFFIGenerator 1/4/2024 13:07'!
window: anObject
"This method was automatically generated"
^handle platformSizeTAt: OFFSET_WINDOW put: anObject! !
!WuEvent methodsFor: 'accessing - structure variables' stamp: 'UFFIGenerator 1/4/2024 13:07'!
event_number: anObject
"This method was automatically generated"
^handle platformSizeTAt: OFFSET_EVENT_NUMBER put: anObject! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
WuEvent class
instanceVariableNames: ''!
!WuEvent class methodsFor: 'field definition' stamp: 'TatuTarvainen 1/3/2024 16:01'!
fieldsDesc
^ #(
size_t window;
size_t event_type;
char* element;
size_t event_number;
size_t bind_id;
)
! !
Object subclass: #WuFFI
instanceVariableNames: ''
classVariableNames: ''
package: 'WebUI'!
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
WuFFI class
instanceVariableNames: ''!
!WuFFI class methodsFor: 'as yet unclassified' stamp: 'TatuTarvainen 1/3/2024 10:18'!
show: aWindowRef html: anHtmlString
self ffiCall: #( void webui_show(int aWindowRef, char* anHtmlString) )! !
!WuFFI class methodsFor: 'as yet unclassified' stamp: 'TatuTarvainen 1/8/2024 11:55'!
bind: aWindow element: anElementId to: aBlock
| cb id |
cb := (FFICallback signature: #( void (WuEvent *e) ) block: aBlock).
id := self _bind: aWindow element: anElementId to: cb.
^ id -> cb! !
!WuFFI class methodsFor: 'as yet unclassified' stamp: 'TatuTarvainen 1/3/2024 15:54'!
window: windowRef icon: contents type: mime
self ffiCall: #( void webui_set_icon( int windowRef, char* contents, char* mime) )! !
!WuFFI class methodsFor: 'as yet unclassified' stamp: 'TatuTarvainen 1/8/2024 11:54'!
_bind: aWindow element: anElementId to: aCallback
^ self ffiCall: #( size_t webui_bind(size_t aWindow, const char* anElementId, FFICallback aCallback) ) ! !
!WuFFI class methodsFor: 'instance creation' stamp: 'TatuTarvainen 1/3/2024 10:17'!
newWindow
^ self ffiCall: #( int webui_new_window() )! !
!WuFFI class methodsFor: 'accessing' stamp: 'TatuTarvainen 1/4/2024 13:06'!
ffiLibrary
^ WuFFILibrary uniqueInstance
! !
FFILibrary subclass: #WuFFILibrary
instanceVariableNames: ''
classVariableNames: ''
package: 'WebUI'!
!WuFFILibrary methodsFor: 'as yet unclassified' stamp: 'TatuTarvainen 1/3/2024 10:24'!
macModuleName
^ (FileLocator home / 'software' / 'webui' / 'dist' / 'webui-2.dylib') asFileReference fullName ! !
!WuFFILibrary methodsFor: 'converting' stamp: 'TatuTarvainen 1/4/2024 13:12'!
calloutAPIClass
Transcript show: 'callout api class haettu'; cr.
^ TFCalloutAPI
! !
!WuFFILibrary methodsFor: 'accessing' stamp: 'TatuTarvainen 1/8/2024 08:48'!
runner
Transcript show: 'runner haettu';cr.
"^ TFWorker default"
^ TFSameThreadRunner uniqueInstance
! !
Object subclass: #WuWindow
instanceVariableNames: 'window'
classVariableNames: ''
package: 'WebUI'!
!WuWindow methodsFor: 'as yet unclassified' stamp: 'TatuTarvainen 1/3/2024 15:54'!
icon: svgIconBlock
| svg |
svg := String streamContents: [ :out | svgIconBlock value: (WuHTML on: out) ].
WuFFI window: window icon: svg type: 'image/svg+xml'! !
!WuWindow methodsFor: 'as yet unclassified' stamp: 'TatuTarvainen 1/3/2024 15:54'!
bind: anElementId to: aBlock
WuFFI bind: window element: anElementId to: aBlock! !
!WuWindow methodsFor: 'accessing' stamp: 'TatuTarvainen 1/3/2024 15:54'!
window
^ window
! !
!WuWindow methodsFor: 'showing' stamp: 'TatuTarvainen 1/3/2024 10:27'!
show: htmlStringOrComponent
| html |
html := htmlStringOrComponent asString.
WuFFI show: window html: html.
^ self! !
!WuWindow methodsFor: 'initialization' stamp: 'TatuTarvainen 1/3/2024 10:26'!
initialize
window := WuFFI newWindow.
! !
WuEvent compileFields!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment