obb

grounded_sage 2023-10-09T11:49:46.362819Z

Is it possible to launch a webview with obb? I have a Swift executable to do it but it would be nice to do it all in Clojure. This is what I have atm.

#!/usr/bin/env obb

(.import js/ObjC "Cocoa")
(.import js/ObjC "WebKit")

(doto (js/$.NSWindow.alloc.initWithContentRectStyleMaskBackingDefer
       (js/$.NSMakeRect 0 0 800 600)
       (bit-or js/$.NSTitledWindowMask
               js/$.NSClosableWindowMask
               js/$.NSResizableWindowMask
               js/$.NSMiniaturizableWindowMask)
       js/$.NSBackingStoreBuffered
       false)
  (.setTitle "Web View Test")
  (.makeKeyAndOrderFront nil))

(let [webView (js/$.WKWebView.alloc.initWithFrame
               (js/$.NSMakeRect 0 0 800 600))]
  (.addSubview (.contentView (.keyWindow js/$.NSApp)) webView)
  (.loadRequest webView (.NSURLRequest.requestWithURL (.NSURL.URLWithString ""))))


(doto js/$.NSApplication.sharedApplication
  (.setActivationPolicy js/$.NSApplicationActivationPolicyRegular)
  (.activateIgnoringOtherApps true))

(.run js/$.NSApp true)

borkdude 2023-10-09T11:51:34.538919Z

I'm not an expert with the underlying platform but hopefully someone else will be able to help you. Perhaps @zane

borkdude 2023-10-09T11:51:54.536219Z

Anything you can do in JS should be able to work in obb as well

grounded_sage 2023-10-09T11:52:44.540739Z

I haven't tried this in JS yet. I just found Obb last night and am exploring using it to handle the components of my app.

borkdude 2023-10-09T11:53:06.466789Z

This seems to get "object is not a function":

(.addSubview (.contentView (.keyWindow js/$.NSApp)) webView)

borkdude 2023-10-09T11:53:55.891389Z

minimal repro: (.keyWindow js/$.NSApp)

grounded_sage 2023-10-09T11:54:26.694529Z

Cool. yea I have been scoping the issue. I was just asking to see if it's known to work. I am assuming that it will.

borkdude 2023-10-09T11:56:11.481849Z

This works:

(.import js/ObjC "Cocoa")
(.import js/ObjC "WebKit")

(doto (js/$.NSWindow.alloc.initWithContentRectStyleMaskBackingDefer
       (js/$.NSMakeRect 0 0 800 600)
       (bit-or js/$.NSTitledWindowMask
               js/$.NSClosableWindowMask
               js/$.NSResizableWindowMask
               js/$.NSMiniaturizableWindowMask)
       js/$.NSBackingStoreBuffered
       false)
  (.setTitle "Web View Test")
  (.makeKeyAndOrderFront nil))

(let [webView (js/$.WKWebView.alloc.initWithFrame
               (js/$.NSMakeRect 0 0 800 600))]
  #_(.addSubview (.contentView (.keyWindow js/$.NSApp)) webView)
  (.loadRequest webView (js/$.NSURLRequest.requestWithURL (js/$.NSURL.URLWithString ""))))


(doto js/$.NSApplication.sharedApplication
  (.setActivationPolicy js/$.NSApplicationActivationPolicyRegular)
  (.activateIgnoringOtherApps true))

(.run js/$.NSApp true)

borkdude 2023-10-09T11:56:22.711909Z

just have to fix the #_ expression

borkdude 2023-10-09T12:03:54.936149Z

I don't see a contentView thing on KeyWindow https://developer.apple.com/documentation/appkit/nsapplication/1428406-keywindow

grounded_sage 2023-10-09T12:04:26.399929Z

It seems that js/$.NSApp is empty

borkdude 2023-10-09T12:05:03.062209Z

it's not empty if you run

(.import js/ObjC "Cocoa")

grounded_sage 2023-10-09T12:05:34.668309Z

hmm I have these at the top

(.import js/ObjC "Cocoa")
(.import js/ObjC "WebKit")

borkdude 2023-10-09T12:05:53.899779Z

try (js/console.log js/$.NSApp)

grounded_sage 2023-10-09T12:06:42.999469Z

oh I was using println . noted use js/console.log

grounded_sage 2023-10-09T12:08:58.418229Z

it is definitely to do with keywindow

grounded_sage 2023-10-09T12:20:32.373389Z

It might be a timing issue. How do you sleep in obb?

borkdude 2023-10-09T12:21:05.610939Z

a better question is how do you sleep in osascript, obb is just a layer on top of that

borkdude 2023-10-09T12:21:19.621699Z

usually sleeping to solve a timing issue smells like a workaround

borkdude 2023-10-09T12:21:37.383079Z

if you can sleep at all, usually JavaScript doesn't allow this synchronously

grounded_sage 2023-10-09T12:21:41.353999Z

thats a good point

grounded_sage 2023-10-09T12:22:08.603929Z

I think there is a way to check the window is loaded. I just wanted to do this as a quick test.

borkdude 2023-10-09T12:23:06.242689Z

(js/delay 2)
seems to do it

borkdude 2023-10-09T12:26:51.845669Z

the delay may prevent the window from loading, so I don't know if this helps ;)

grounded_sage 2023-10-09T12:27:54.221219Z

haha

grounded_sage 2023-10-09T12:44:28.621239Z

Well this doesn't break

#!/usr/bin/env obb

(.import js/ObjC "Cocoa")
(.import js/ObjC "WebKit")

(def window (let [newWindow (js/$.NSWindow.alloc.initWithContentRectStyleMaskBackingDefer
                             (js/$.NSMakeRect 0 0 800 600)
                             (bit-or js/$.NSTitledWindowMask
                                     js/$.NSClosableWindowMask
                                     js/$.NSResizableWindowMask
                                     js/$.NSMiniaturizableWindowMask)
                             js/$.NSBackingStoreBuffered
                             false)]
              
              (.setTitle newWindow "Web View Test")
              (.makeKeyAndOrderFront newWindow nil)
              newWindow
              #_(js/delay 2)
              #_(.addSubview (.contentView newWindow) webView)
              #_(.loadRequest webView (js/$.NSURLRequest.requestWithURL (js/$.NSURL.URLWithString "")))))



(let [webview (js/$.WKWebView.alloc.initWithFrame
               (js/$.NSMakeRect 0 0 800 600))]
  (js/console.log "window" (.-contentView window))
  (js/delay 2)
  (.addSubview (.-contentView window) webview))

window

(doto js/$.NSApplication.sharedApplication
  (.setActivationPolicy js/$.NSApplicationActivationPolicyRegular)
  (.activateIgnoringOtherApps true))

(.run js/$.NSApp true)

grounded_sage 2023-10-09T12:44:39.114789Z

But it looks like we want to use a delegate

grounded_sage 2023-10-09T13:02:11.647619Z

I tried creating a delegate and adding it as a method. But it doesn't seem possible to use addMethod. I also tried it as (js/$.addMethod delegate "windowDidLoad:" #(window-did-load))

borkdude 2023-10-09T13:08:07.404879Z

I'm also trying #squint now for this:

npx squint webview.cljs && esbuild webview.mjs --bundle --outfile=webview.min.mjs && osascript -l JavaScript webview.min.mjs

👍 1
borkdude 2023-10-09T13:08:27.818379Z

might give better error messages / locations (not sure)

grounded_sage 2023-10-09T13:53:59.909629Z

if this is figured out and I can package it then I might be able to go without needing Swift which would be nice.

borkdude 2023-10-09T14:03:58.665579Z

right

grounded_sage 2023-10-09T18:24:32.314789Z

What would be the most elegant way to handle if possible using ZeroMQ in obb?

borkdude 2023-10-09T18:25:39.787759Z

the underlying platform is called osascript, so you might just want to search for that

👍 1
grounded_sage 2023-10-09T19:58:37.486529Z

I am unsure whether what I am doing can be obtained. But I got very close! I shifted to launching the executable built with Swift. But then the application would hang. It might be because Webkit wants to run on the main thread.