crowngui icon indicating copy to clipboard operation
crowngui copied to clipboard

Using webview2.nim

Open harrier77 opened this issue 1 year ago • 6 comments

I want to use the code in webview2.nim without the crowngui app to learn something about the internals of webview, it seems it should be possible because there is this code at line 250:

when isMainModule:
  SetCurrentProcessExplicitAppUserModelID("webview2 app")
  var v = newWebView()
  assert v.webview_init() == 0

  v.run

but there is no newWebView() proc which can be called. How can I write a newWebView() that starts the app? I tried to add this at line 252: import ../../webview.nim and I can compile, but it fails to run with this error:
ERROR:window_impl.cc(121)] Failed to unregister class Chrome_WidgetWin_0. Error = 0

harrier77 avatar Jun 04 '24 07:06 harrier77

I haven't touched this project for a while, for your fist question, import modules as you need explicitly. the second seems cause by destroy window before exiting webview, that might can be ignored. as error code is 0

bung87 avatar Jun 04 '24 07:06 bung87

Thank you very much for quick answer. I tried with this and I can finally get a webview window:

import webview
var v=newWebView()
discard v.webview_init()
v.run

On the other hand, using assert v.webview_init() == 0 as in the webview2.nim code causes an error like this: Error: unhandled exception: prova.nim(5, 1) v.webview_init() == 0 [AssertionDefect]

harrier77 avatar Jun 04 '24 08:06 harrier77

How can I add a custom button to the webview window I can get with the code above? (It should be useful to have a "home" button with which redirect the browser to a starting page. )

harrier77 avatar Jun 04 '24 09:06 harrier77

that's more specific to windows gui development, webview2 official repo has such a sample, you can check it there.

bung87 avatar Jun 13 '24 07:06 bung87

Thanks for your answer, in the meantime I made some improvement in my skills and actually I can now write some C lines that add a custom toolbar to a webview2 window; after that it is relatively easy to convert C to nim. Here is the code for a toolbar inside your webview2 window in nim:

var tbab1,tbab2,tbab3:TTBADDBITMAP
 type tbbtipo = array[0..3,TBBUTTON]
 var tbb:tbbtipo
 
 var miatoolb=CreateWindowW(
   TOOLBARCLASSNAME, 
   NULL, 
   WS_CHILD or WS_VISIBLE or TBSTYLE_FLAT, 
   0, 0, 0, 0, 
   w.priv.windowHandle, 
   cast[HMENU](NULL),
   g_hInstance,  
   cast[LPVOID](w))

SendMessage(miatoolb, TB_BUTTONSTRUCTSIZE, cast[WPARAM](sizeof(TBBUTTON)), 0)
 SendMessage(miatoolb, TB_SETBITMAPSIZE, cast[WPARAM](0), cast[LPARAM](MAKELONG(32, 32)))
 const
   IDB_BACK = 1003
   IDB_FOR = 1004
   IDB_HOME = 1005
   MIO_COMANDO = 11
 ##Add Bitmaps
 tbab1.hInst  = g_hInstance
 tbab1.nID    = IDB_BACK
 SendMessage(miatoolb, TB_ADDBITMAP, cast[WPARAM](1), cast[LPARAM](&tbab1))
 
 tbab2.hInst  = g_hInstance
 tbab2.nID    = IDB_HOME
 SendMessage(miatoolb, TB_ADDBITMAP, cast[WPARAM](1), cast[LPARAM](&tbab2))

 tbab3.hInst  = g_hInstance
 tbab3.nID    = IDB_FOR
 SendMessage(miatoolb, TB_ADDBITMAP, cast[WPARAM](1), cast[LPARAM](&tbab3));

 #ZeroMemory(cast[PVOID](NULL),sizeof(tbb))
 tbb[0].iBitmap    = 0 ##The index of the bitmap on toolbar bitmap collection
 tbb[0].idCommand  = MIO_COMANDO
 tbb[0].fsState    = TBSTATE_ENABLED
 tbb[0].fsStyle    = TBSTYLE_BUTTON
 
 tbb[1].iBitmap    = 1 
 tbb[1].idCommand  = 0
 tbb[1].fsState    = TBSTATE_ENABLED;
 tbb[1].fsStyle    = TBSTYLE_BUTTON
 ##tbb[1].iString    = SendMessage(hWndToolBar, TB_ADDSTRING, 0, (LPARAM)TEXT(""));

 tbb[2].iBitmap    = 2 
 tbb[2].idCommand  = 0
 tbb[2].fsState    = TBSTATE_ENABLED 
 tbb[2].fsStyle    = TBSTYLE_BUTTON 
 ##tbb[1].iString    = SendMessage(hWndToolBar, TB_ADDSTRING, 0, (LPARAM)TEXT(""));
 
 SendMessage(miatoolb, TB_ADDBUTTONS, 3, cast[LPARAM](&tbb))

harrier77 avatar Jun 13 '24 14:06 harrier77

Hello again, I started a new project and I can not get the return value of a nim proc when I call it from javascript. For example, I can bind the javascript api.carica_dati() to the nim proc in this way:

proc due (webview: Webview; data:string):string =
       return "myvar"

v.bindProcs("api"):
        proc carica_dati(data:string) =  carica_dati(v,data)

and when I call api.carica_dati() inside the webview page I can see that the corresponding nim proc runs. But I need to read the return value of nim proc in javascript and at now I can not get it. Can you give me a simple example of a binded nim proc wich returns a string?

harrier77 avatar Jul 12 '24 12:07 harrier77

it's not supported. I might take a look at https://github.com/webui-dev/nim-webui see how it is implemented.

bung87 avatar Aug 02 '24 07:08 bung87