Fork me on GitHub
#obb
<
2023-10-09
>
grounded_sage11:10:46

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)

borkdude11:10:34

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

borkdude11:10:54

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

grounded_sage11:10:44

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.

borkdude11:10:06

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

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

borkdude11:10:55

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

grounded_sage11:10:26

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.

borkdude11:10:11

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)

borkdude11:10:22

just have to fix the #_ expression

grounded_sage12:10:26

It seems that js/$.NSApp is empty

borkdude12:10:03

it's not empty if you run

(.import js/ObjC "Cocoa")

grounded_sage12:10:34

hmm I have these at the top

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

borkdude12:10:53

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

grounded_sage12:10:42

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

grounded_sage12:10:58

it is definitely to do with keywindow

grounded_sage12:10:32

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

borkdude12:10:05

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

borkdude12:10:19

usually sleeping to solve a timing issue smells like a workaround

borkdude12:10:37

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

grounded_sage12:10:41

thats a good point

grounded_sage12:10:08

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

borkdude12:10:06

(js/delay 2)
seems to do it

borkdude12:10:51

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

grounded_sage12:10:28

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_sage12:10:39

But it looks like we want to use a delegate

grounded_sage13:10:11

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))

borkdude13:10:07

I'm also trying #C03U8L2NXNC now for this:

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

👍 1
borkdude13:10:27

might give better error messages / locations (not sure)

grounded_sage13:10:59

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

grounded_sage18:10:32

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

borkdude18:10:39

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

👍 1
grounded_sage19:10:37

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.