fpwebview icon indicating copy to clipboard operation
fpwebview copied to clipboard

Suggestion for 2 updates

Open Air16 opened this issue 2 years ago • 4 comments

Great work, thanks !

Looking at the reference c header file https://github.com/webview/webview/blob/master/webview.h, and after testing (Windows only but it should be the same on Linux & MacOSX), let me suggest two updates :

  1. It's possible to add a useful declaration in webview.pas : procedure webview_set_html(w: PWebView; const html: PAnsiChar); cdecl; external webview_lib;

  2. in lclembed example, there is an improvement in CreateWebView and DestroyWebView calls, because the c reference header file indicates that webview_run() is a message loop running until it's terminated by a call to webview_terminate(). So webview_run() is a blocking call. It is required in a pascal command line program but not in a LCL GUI application.

This leads to the following changes in lclembed GUI application :

CreateWebView :

  if wvHandle <> nil then
    begin
      webview_bind(wvHandle, PAnsiChar('HostSayHello'), @SayHello, nil);
      webview_navigate(wvHandle, PAnsiChar('http://localhost:8000/'));
//      webview_run(wvHandle); //        <-- no need to call this in a GUI application
    end
  else
    // Use GUI dialog, not writeln.
    writeln('Failed to create webview object.');

Then to destroy the webview we only need to call webview_destroy() :

procedure TForm1.OnClickExitProgram(Sender: TObject);
begin
  isExiting := true;
//  if wvHandle <> nil then
//    webview_terminate(wvHandle); //     <-- not needed in a GUI application
// Sleep(500);                            <-- not needed too, i suppose (don't know why it's here)
    fphttpapp.Application.Terminate;
 if wvHandle <> nil then
   webview_destroy(wvHandle); 
 wvHandle := nil; 
  WebPanel.Hide;
//  Halt;           <-- not needed, the application will close gracefully because it's not blocked anymore by webview_run()
end;end;

Air16 avatar Apr 04 '23 17:04 Air16

without webview_run() we lost functionality of bind functions, promise will not return result becouse void run() { MSG msg; BOOL res; while ((res = GetMessage(&msg, nullptr, 0, 0)) != -1) { if (msg.hwnd) { TranslateMessage(&msg); DispatchMessage(&msg); continue; } if (msg.message == WM_APP) { auto f = (dispatch_fn_t *)(msg.lParam); (*f)(); delete f; } else if (msg.message == WM_QUIT) { return; } } }

commanderz avatar Apr 19 '23 14:04 commanderz

If you need to use bind function there are two ways :

  1. call webview_run() in an asynchronous call so that the main GUI thread is not frozen (QueueAsyncCall or PostMessage), and call webview_terminate() in the Form.OnClose() event

  2. create a TThread and put every webview_xxx call inside

I've built a TControl descendant which works great in the main GUI thread with every features, I'll publish it soon.

Air16 avatar Apr 19 '23 14:04 Air16

I've built a TControl descendant which works great in the main GUI thread with every features, I'll publish it soon.

thank you, looking forward to it ^_^

commanderz avatar Apr 20 '23 14:04 commanderz

1. It's possible to add a useful declaration in `webview.pas` :
   `procedure webview_set_html(w: PWebView; const html: PAnsiChar); cdecl; external webview_lib; `

I've added this in the WIP branch, which I should be publishing soon.

2. in `lclembed` example, there is an improvement in CreateWebView and DestroyWebView calls, because the c reference header file indicates that `webview_run()` is a message loop running until it's terminated by a call to `webview_terminate()`. So `webview_run()` is a blocking call. It is required in a pascal command line program but not in a LCL GUI application.

This leads to the following changes in lclembed GUI application :

Thanks, I'll test the changes.

I've built a TControl descendant which works great in the main GUI thread with every features, I'll publish it soon.

Looking forward to it.

PierceNg avatar May 14 '23 06:05 PierceNg