Skip to content

Commit

Permalink
types
Browse files Browse the repository at this point in the history
  • Loading branch information
bung87 committed Sep 10, 2024
1 parent 4601912 commit 9da66f9
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 47 deletions.
2 changes: 1 addition & 1 deletion demos/entries/nim2js.nim.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Generated by the Nim Compiler v1.9.5 */
/* Generated by the Nim Compiler v2.1.99 */
var framePtr = null;
var excHandler = 0;
var lastJSError = null;
Expand Down
8 changes: 5 additions & 3 deletions src/crowngui/platforms/macos/app_delegate.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import std/[math]
import objc_runtime
import darwin / [app_kit, foundation, objc/runtime]
import ./types
import crowngui/types
import ./bundle

proc applicationOpenFile(self: ID; cmd: SEL; sender: NSApplication; openFile: NSString): Bool {.cdecl.} =
Expand All @@ -28,7 +28,8 @@ proc on_application_did_finish_launching(delegate: ID; app: ID) {.objcr.}=
# set_up_window()

proc applicationDidFinishLaunching(self: ID; cmd: SEL; notification: ID): void {.cdecl.} =
echo "applicationDidFinishLaunching"
when not defined(release):
echo "applicationDidFinishLaunching"
# var w = getAssociatedObject(self, cast[pointer]($$"webview"))
# var wv = cast[Webview](w)

Expand All @@ -37,7 +38,8 @@ proc applicationDidFinishLaunching(self: ID; cmd: SEL; notification: ID): void {
on_application_did_finish_launching(self, app)

proc applicationWillBecomeActive(self: ID; cmd: SEL; notification: ID): void {.cdecl.} =
echo "applicationWillBecomeActive"
when not defined(release):
echo "applicationWillBecomeActive"

proc registerAppDelegate*(): ObjcClass =
result = allocateClassPair(getClass("NSResponder"), "MyAppDelegate", 0)
Expand Down
2 changes: 1 addition & 1 deletion src/crowngui/platforms/macos/internal_dialogs.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import objc_runtime
import darwin / [app_kit, foundation, objc/runtime, objc/blocks]
import types
import crowngui/types
import ./dialog_types

const WEBVIEW_DIALOG_FLAG_FILE = (0 shl 0)
Expand Down
2 changes: 1 addition & 1 deletion src/crowngui/platforms/macos/message_handler.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import objc_runtime
import darwin / [app_kit, foundation, objc/runtime]
import types
import crowngui/types

Check failure on line 3 in src/crowngui/platforms/macos/message_handler.nim

View workflow job for this annotation

GitHub Actions / build (macOS-12, devel)

cannot open file: crowngui/types

proc webview_external_invoke(self: ID; cmd: SEL; contentController: Id;
message: Id) =
Expand Down
16 changes: 0 additions & 16 deletions src/crowngui/platforms/macos/types.nim
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
import objc_runtime

type
Webview* = ptr WebviewObj
WebviewObj* {.pure.} = object ## WebView Type
url* : string ## Current URL
title* : string ## Window Title
width* : int ## Window Width
height* : int ## Window Height
resizable* : bool ## `true` to Resize the Window, `false` for Fixed size Window
debug* : bool ## Debug is `true` when not build for Release
invokeCb* : pointer ## Callback proc js:window.external.invoke
priv* : WebviewPrivObj
userdata* : pointer
onOpenFile*: OnOpenFile

WebviewPrivObj* = object
pool*: ID
window*: ID
webview*: ID
windowDelegate*: ID

OnOpenFile* = proc (w: Webview; filePath: string; name = ""):bool

11 changes: 7 additions & 4 deletions src/crowngui/platforms/macos/webview.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import strutils, base64
# import strutils, base64
import objc_runtime
import darwin / [app_kit,web_kit, foundation, objc/runtime, core_graphics/cggeometry]
# import menu
Expand All @@ -15,6 +15,7 @@ import ./navigation_delegate
import ./window_delegate
import ./utils
import ./app_utils
import ../../types

{.passl: "-framework Cocoa -framework WebKit".}

Expand Down Expand Up @@ -109,9 +110,11 @@ proc webview_init*(w: Webview): cint {.objcr.} =
[w.priv.webview setUIDelegate: uiDel]
[w.priv.webview setNavigationDelegate: navDel]
let url = $(w.url)
if "data:text/html;charset=utf-8;base64," in url:
let html = base64.decode(url.split(",")[1])
[w.priv.webview loadHTMLString: @html, baseURL: nil]
case w.entryType
of EntryType.html:
# if "data:text/html;charset=utf-8;base64," in url:
# let html = base64.decode(url.split(",")[1])
[w.priv.webview loadHTMLString: @url, baseURL: nil]
else:
var nsURL = [NSURL URLWithString: @url]
[w.priv.webview loadRequest: [NSURLRequest requestWithURL: nsURL]]
Expand Down
2 changes: 1 addition & 1 deletion src/crowngui/platforms/macos/window_delegate.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import objc_runtime
import darwin / [app_kit, objc/runtime]
import ./types
import crowngui/types

proc webview_window_will_close(self: Id; cmd: SEL; notification: Id) =
var w = getAssociatedObject(self, cast[pointer]($$"webview"))
Expand Down
1 change: 1 addition & 0 deletions src/crowngui/platforms/win/webview2.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import crowngui/types

Check failure on line 1 in src/crowngui/platforms/win/webview2.nim

View workflow job for this annotation

GitHub Actions / build (windows-latest, devel)

cannot open file: crowngui/types
import webview2/[types,controllers,context,dialog,com,environment_options,loader]
import winim
import winim/inc/winuser
Expand Down
2 changes: 1 addition & 1 deletion src/crowngui/platforms/win/webview2/context.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import std/[locks, tables]
import winim
import types
import crowngui/types

type
WebviewContextStoreObj = object
Expand Down
16 changes: 1 addition & 15 deletions src/crowngui/platforms/win/webview2/types.nim
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
import winim
import com
# import std/[atomics]
import ../../../types

type
WebView* = ptr WebViewObj
OnOpenFile* = proc (w: Webview; filePath: string; name = ""):bool
WebViewObj* = object
url* : string
title* : string
width* : int
height* : int
resizable*: bool
debug* : bool
invokeCb* : pointer
priv*: WebviewPrivObj
created*: bool
onOpenFile*: OnOpenFile
entryType*: EntryType

WebviewPrivObj* = object
windowHandle*: HWND
view*: ptr ICoreWebView2
Expand Down
22 changes: 21 additions & 1 deletion src/crowngui/types.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
when defined(windows):
import ./platforms/win/webview2/types
elif defined(macosx):
import ./platforms/macos/types

type
WebView* = ptr WebViewObj
WebViewObj* = object
url* : string
title* : string
width* : int
height* : int
resizable*: bool
debug* : bool
invokeCb* : pointer
priv*: WebviewPrivObj

Check failure on line 16 in src/crowngui/types.nim

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, devel)

undeclared identifier: 'WebviewPrivObj'
created*: bool
onOpenFile*: OnOpenFile
entryType*: EntryType
EntryType* = enum
url, file, html, dir
url, file, html, dir
OnOpenFile* = proc (w: Webview; filePath: string; name = ""):bool

9 changes: 6 additions & 3 deletions src/crowngui/webview.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
include js_utils
import tables, strutils, macros, logging, json, os, base64, strformat, std/exitprocs
import ./types

var logger = newRollingFileLogger(expandTilde("~/crowngui.log"))
addHandler(logger)

Expand Down Expand Up @@ -182,7 +183,7 @@ proc run*(w: Webview; quitProc: proc () {.noconv.}; controlCProc: proc () {.noco
system.setControlCHook(controlCProc)
w.run

proc webView(title = ""; url = "";entryType:static[EntryType]; width: Positive = 1000; height: Positive = 700; resizable: static[bool] = true;
proc webView(title = ""; url = "";entryType:EntryType; width: Positive = 1000; height: Positive = 700; resizable: static[bool] = true;
debug: static[bool] = not defined(release); callback: ExternalInvokeCb = nil): Webview {.inline.} =
result = create(WebviewObj)
result.title = title
Expand All @@ -208,9 +209,11 @@ proc newWebView*(path: static[string] = ""; entryType:static[EntryType]; title =
## * `debug` Debug mode, Debug is `true` when not built for Release.

var entry = path
var entryType1 = entryType
when path.endsWith".js" or path.endsWith".nim":
entry = dataUriHtmlHeader "<!DOCTYPE html><html><head><meta content='width=device-width,initial-scale=1' name=viewport></head><body id=body ><div id=ROOT ><div></body></html>" # Copied from Karax
var webview = webView(title, entry, entryType, width, height, resizable, debug, callback)
entry = "<!DOCTYPE html><html><head><meta content='width=device-width,initial-scale=1' name=viewport></head><body id=body ><div id=ROOT ><div></body></html>" # Copied from Karax
entryType1 = EntryType.html
var webview = webView(title, entry, entryType1, width, height, resizable, debug, callback)
when defined(macosx):
let MyAppDelegate = registerAppDelegate()
let WindowController = registerWindowController()
Expand Down

0 comments on commit 9da66f9

Please sign in to comment.