bung87 / crowngui

Web Technologies based Crossplatform GUI Framework, WebView in Nim
https://bung87.github.io/crowngui/
MIT License
25 stars 3 forks source link

Using webview2.nim #6

Open harrier77 opened 2 weeks ago

harrier77 commented 2 weeks ago

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

bung87 commented 2 weeks ago

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

harrier77 commented 2 weeks ago

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 commented 2 weeks ago

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

bung87 commented 5 days ago

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

harrier77 commented 4 days ago

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