From 9da66f9e9f353dd0f12f0204dba054fb7c2c630f Mon Sep 17 00:00:00 2001 From: bung87 Date: Tue, 10 Sep 2024 12:35:29 +0800 Subject: [PATCH] types --- demos/entries/nim2js.nim.js | 2 +- src/crowngui/platforms/macos/app_delegate.nim | 8 ++++--- .../platforms/macos/internal_dialogs.nim | 2 +- .../platforms/macos/message_handler.nim | 2 +- src/crowngui/platforms/macos/types.nim | 16 -------------- src/crowngui/platforms/macos/webview.nim | 11 ++++++---- .../platforms/macos/window_delegate.nim | 2 +- src/crowngui/platforms/win/webview2.nim | 1 + .../platforms/win/webview2/context.nim | 2 +- src/crowngui/platforms/win/webview2/types.nim | 16 +------------- src/crowngui/types.nim | 22 ++++++++++++++++++- src/crowngui/webview.nim | 9 +++++--- 12 files changed, 46 insertions(+), 47 deletions(-) diff --git a/demos/entries/nim2js.nim.js b/demos/entries/nim2js.nim.js index 3a92025..837933b 100644 --- a/demos/entries/nim2js.nim.js +++ b/demos/entries/nim2js.nim.js @@ -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; diff --git a/src/crowngui/platforms/macos/app_delegate.nim b/src/crowngui/platforms/macos/app_delegate.nim index 59c27b3..7338aad 100644 --- a/src/crowngui/platforms/macos/app_delegate.nim +++ b/src/crowngui/platforms/macos/app_delegate.nim @@ -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.} = @@ -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) @@ -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) diff --git a/src/crowngui/platforms/macos/internal_dialogs.nim b/src/crowngui/platforms/macos/internal_dialogs.nim index 58aeb3b..16f6549 100644 --- a/src/crowngui/platforms/macos/internal_dialogs.nim +++ b/src/crowngui/platforms/macos/internal_dialogs.nim @@ -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) diff --git a/src/crowngui/platforms/macos/message_handler.nim b/src/crowngui/platforms/macos/message_handler.nim index bff15b2..979498a 100644 --- a/src/crowngui/platforms/macos/message_handler.nim +++ b/src/crowngui/platforms/macos/message_handler.nim @@ -1,6 +1,6 @@ import objc_runtime import darwin / [app_kit, foundation, objc/runtime] -import types +import crowngui/types proc webview_external_invoke(self: ID; cmd: SEL; contentController: Id; message: Id) = diff --git a/src/crowngui/platforms/macos/types.nim b/src/crowngui/platforms/macos/types.nim index 452ab1a..2ac6ae6 100644 --- a/src/crowngui/platforms/macos/types.nim +++ b/src/crowngui/platforms/macos/types.nim @@ -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 - \ No newline at end of file diff --git a/src/crowngui/platforms/macos/webview.nim b/src/crowngui/platforms/macos/webview.nim index 2b8dfde..98859d4 100644 --- a/src/crowngui/platforms/macos/webview.nim +++ b/src/crowngui/platforms/macos/webview.nim @@ -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 @@ -15,6 +15,7 @@ import ./navigation_delegate import ./window_delegate import ./utils import ./app_utils +import ../../types {.passl: "-framework Cocoa -framework WebKit".} @@ -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]] diff --git a/src/crowngui/platforms/macos/window_delegate.nim b/src/crowngui/platforms/macos/window_delegate.nim index d539491..9039d90 100644 --- a/src/crowngui/platforms/macos/window_delegate.nim +++ b/src/crowngui/platforms/macos/window_delegate.nim @@ -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")) diff --git a/src/crowngui/platforms/win/webview2.nim b/src/crowngui/platforms/win/webview2.nim index 25aca83..8ff5607 100644 --- a/src/crowngui/platforms/win/webview2.nim +++ b/src/crowngui/platforms/win/webview2.nim @@ -1,3 +1,4 @@ +import crowngui/types import webview2/[types,controllers,context,dialog,com,environment_options,loader] import winim import winim/inc/winuser diff --git a/src/crowngui/platforms/win/webview2/context.nim b/src/crowngui/platforms/win/webview2/context.nim index 5fa4bc3..0db2b41 100644 --- a/src/crowngui/platforms/win/webview2/context.nim +++ b/src/crowngui/platforms/win/webview2/context.nim @@ -1,6 +1,6 @@ import std/[locks, tables] import winim -import types +import crowngui/types type WebviewContextStoreObj = object diff --git a/src/crowngui/platforms/win/webview2/types.nim b/src/crowngui/platforms/win/webview2/types.nim index 2022c7a..4250ad3 100644 --- a/src/crowngui/platforms/win/webview2/types.nim +++ b/src/crowngui/platforms/win/webview2/types.nim @@ -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 diff --git a/src/crowngui/types.nim b/src/crowngui/types.nim index fe6f70e..07e9784 100644 --- a/src/crowngui/types.nim +++ b/src/crowngui/types.nim @@ -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 + created*: bool + onOpenFile*: OnOpenFile + entryType*: EntryType EntryType* = enum - url, file, html, dir \ No newline at end of file + url, file, html, dir + OnOpenFile* = proc (w: Webview; filePath: string; name = ""):bool + \ No newline at end of file diff --git a/src/crowngui/webview.nim b/src/crowngui/webview.nim index bc47f87..b275fb5 100644 --- a/src/crowngui/webview.nim +++ b/src/crowngui/webview.nim @@ -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) @@ -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 @@ -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 "
" # Copied from Karax - var webview = webView(title, entry, entryType, width, height, resizable, debug, callback) + entry = "
" # 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()