Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

Change browser size

celitojr
Posts: 10
Joined: Fri May 06, 2022 2:05 pm

Change browser size

Post by celitojr »

Hi. I'm having trouble generating two images of different sizes using the same browser.

I won't include all the code I'm using because it's quite long. I'll try to summarize it, but if it's not clear, please let me know and I'll try to explain it better.

I'm using the uCEFBrowserThread unit that was in one of the demos available to do the following:
- Instantiate a TCEFBrowserThread object;
- Set the desired size;
- Navigate to the desired route;
- Wait a while and generate a snapshot (correct dimensions);
- Set a new size;
- Navigate to another route;
- Wait a while longer and generate a new snapshot (wrong dimensions, he's using the previous size);

To change the size, I simply change the size of the bitmap:
fBrowserBitmap.SetSize(aWidth, aHeight);

This works for the first image but not for the second. I mean, it does change the size, but later, when navigating to the second route, it ends up returning to the initial size.

From what I've seen, on the second route, a CEF_PENDINGRESIZE message is triggered, which ends up returning to the first size. I think something is missing. I mean, it's not enough to change the size of the bitmap, but I don't know what it is.

I tried using the fBrowser.WasResized method after fBrowserBitmap.SetSize and, apparently, it works, but I admit that I didn't feel confident in doing it. Can you guide me?
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Change browser size

Post by salvadordf »

Hi,

TCEFBrowserThread is very similar to the SimpleOSRBrowser demo.

Setting the fBrowserBitmap size and then calling TCEFBrowserThread.Resize should be enough to resize the browser internally.
celitojr
Posts: 10
Joined: Fri May 06, 2022 2:05 pm

Re: Change browser size

Post by celitojr »

Thank you. I'll try it.
celitojr
Posts: 10
Joined: Fri May 06, 2022 2:05 pm

Re: Change browser size

Post by celitojr »

Thanks again for the answer. Unfortunately, it didn't help me. But at least now I have some more information.

When I read your answer, I didn't realize that I had already tested this possibility (Resize).

In fact, the SimpleOSRBrowser demo is very similar, but I noticed a difference that could be crucial.

In the demo, TBufferPanel is used. In this class, there is a BufferIsResized method that is used in the Resize of that example.

In my project, I use the TCEFBrowserThread class that uses the TCEFBrowserBitmap class, which also has a BufferIsResized method that is used in the Resize.

However, in a brief analysis, it seems to me that the TCEFBrowserBitmap.BufferIsResized method will never return false. See, for example, this snippet:

Code: Select all

Result := (Width = Width) and (Height = Height)
This means that it also never calls fBrowser.WasResized. In the demo fBrowser.WasResized is called.
Can you guide me with this?
celitojr
Posts: 10
Joined: Fri May 06, 2022 2:05 pm

Re: Change browser size

Post by celitojr »

I simulated the problem I'm facing by slightly modifying the ConsoleBrowser2 demo.
This demo uses the uCEFBrowserThread class I mentioned.

What I did:
- In the uCEFBrowserThread unit I added the following method:

Code: Select all

procedure TCEFBrowserThread.SetDimensions(w, h: integer);
begin
    FBrowserBitmap.SetSize(w,h);
    Resize; // I used Resize as suggested
end;
- In the uEncapsulatedBrowser unit I added the following procedure in order to generate several snapshots:

Code: Select all

  procedure ResetMinAppEvent;
  begin
      MainAppEvent.ResetEvent;
      EncapsulatedBrowser.LoadURL('https://www.google.com');
  end;
- This procedure is called in dpr:

Code: Select all

  CreateGlobalCEFApp;
  if WaitForMainAppEvent then
    WriteResult;
  ResetMinAppEvent;
  if WaitForMainAppEvent then
    WriteResult;
  ResetMinAppEvent;
  if WaitForMainAppEvent then
    WriteResult;
- I also changed the initialization size to 250x250:

Code: Select all

constructor TEncapsulatedBrowser.Create;
begin
  inherited Create;
  FThread        := nil;
  FWidth         := 250;
  FHeight        := 250;
  ...
end;
- And I modified the LoadURL method to double the size with each execution:

Code: Select all

procedure TEncapsulatedBrowser.LoadURL(const aURL : ustring);
begin
  if (FThread = nil) then
    ...
   else
    begin
        FWidth := 2 * FWidth;
        FHeight := 2 * FHeight;
        FThread.SetDimensions(FWidth, FHeight);
        FThread.LoadUrl(aURL);
    end;
end;
- Finally, I removed the use of the second exe because I don't use this model in my project. I think, in any case, that this doesn't imply what I'm trying to demonstrate.

Code: Select all

// GlobalCEFApp.BrowserSubprocessPath      := 'ConsoleBrowser2_sp.exe';
You can see that all snapshots are generated with the initial size (250x250).

I noticed that there was a change in the uCEFBrowserThread unit in this demo. Previously it used uCEFBufferPanel.TBufferPanel. Now it uses uCEFBrowserBitmap.TCEFBrowserBitmap.

If I use the version that used TBufferPanel, it works. If I use the newer version, which uses TCEFBrowserBitmap, it doesn't work.

As I said in the previous comment, I think there is a problem with TCEFBrowserBitmap that causes fBrowser.WasResized to never be called.

I could use TBufferPanel and ignore this problem, but it seems more appropriate to avoid using a window control in a windowless application.

Additional information: in my first comment I said that calling fBrowser.WasResized seemed to solve the problem, but after several tests I realized that it doesn't always work. So I still need help finding the solution.

Can anyone help me?
dilfich
Posts: 368
Joined: Thu Nov 30, 2017 1:17 am

Re: Change browser size

Post by dilfich »

OSRBrowser

GetScreenInfo - transmits the size of the monitor (desktop)
GetViewRect - transmits the size of the browser

You can change it on the go, when the page loads again, the site will receive new permission data.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Change browser size

Post by salvadordf »

Consider using two instances of the ConsoleBrowser2 demo, each with different initial browser sizes.

Make sure they use different GlobalCEFApp.RootCache and GlobalCEFApp.Cache values.
celitojr
Posts: 10
Joined: Fri May 06, 2022 2:05 pm

Re: Change browser size

Post by celitojr »

dilfich wrote: Wed Nov 27, 2024 4:42 am OSRBrowser

GetScreenInfo - transmits the size of the monitor (desktop)
GetViewRect - transmits the size of the browser

You can change it on the go, when the page loads again, the site will receive new permission data.
I'm not sure if I understand the idea but I adapted my previous example (using the ConsoleBrowser2 demo). Tell me if it's this:

- I changed the TCEFBrowserThread.SetDimensions method:

Code: Select all

procedure TCEFBrowserThread.SetDimensions(w, h: integer);
begin
    FBrowserSize.cx        := w;
    FBrowserSize.cy        := h;
    FBrowserBitmap.SetSize(w,h);
end;
- I changed the TCEFBrowserThread.Browser_OnGetViewRect method:

Code: Select all

procedure TCEFBrowserThread.Browser_OnGetViewRect(Sender: TObject; const browser: ICefBrowser; var rect: TCefRect);
begin
  rect.x      := 0;
  rect.y      := 0;
//  rect.width  := DeviceToLogical(FBrowserBitmap.Width,  FScreenScale);
  rect.width  := DeviceToLogical(FBrowserSize.cx,  FScreenScale);
//  rect.height := DeviceToLogical(FBrowserBitmap.Height, FScreenScale);
  rect.height := DeviceToLogical(FBrowserSize.cy, FScreenScale);
end;
- I changed the TCEFBrowserThread.Browser_OnGetViewRect method:

Code: Select all

procedure TCEFBrowserThread.Browser_OnGetScreenInfo(Sender: TObject; const browser: ICefBrowser; var screenInfo: TCefScreenInfo; out Result: Boolean);
var
  TempRect : TCEFRect;
begin
  TempRect.x      := 0;
  TempRect.y      := 0;
//  TempRect.width  := DeviceToLogical(FBrowserBitmap.Width,  FScreenScale);
  TempRect.width  := DeviceToLogical(FBrowserSize.cx,  FScreenScale);
//  TempRect.height := DeviceToLogical(FBrowserBitmap.Height, FScreenScale);
  TempRect.height := DeviceToLogical(FBrowserSize.cy, FScreenScale);
  screenInfo.device_scale_factor := FScreenScale;
  screenInfo.depth               := 0;
  screenInfo.depth_per_component := 0;
  screenInfo.is_monochrome       := Ord(False);
  screenInfo.rect                := TempRect;
  screenInfo.available_rect      := TempRect;
  Result := True;
end;
Is that what you were trying to say or did I misunderstand?
celitojr
Posts: 10
Joined: Fri May 06, 2022 2:05 pm

Re: Change browser size

Post by celitojr »

salvadordf wrote: Wed Nov 27, 2024 2:33 pm Consider using two instances of the ConsoleBrowser2 demo, each with different initial browser sizes.

Make sure they use different GlobalCEFApp.RootCache and GlobalCEFApp.Cache values.
I'm using this demo just to illustrate the problem I'm having in my application. I use the same browser to generate several images of various sizes. I do this to avoid the overhead of creating/destroying the browser.

I could create/destroy the browsers and generate the images. This works. They are all generated with the correct size. But I'm trying to avoid this and that's why I created a small pool of browsers. When I did this, I came across the problems I described here. So this suggestion, although effective, does not meet my needs. The help I need is to adjust the sizes of the images generated using the same browser.

Anyway, don't you think there is a problem with the TCEFBrowserBitmap.BufferIsResized method that I mentioned? It is comparing the same properties. It will always result in a true value.

Code: Select all

Result := (Width = Width) and (Height = Height)
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Change browser size

Post by salvadordf »

I just uploaded a new version of CEF4Delphi with some fixes to the ConsoleBrowser2 demo.

See that ConsoleBrowser2 has some disabled code that it's used to test the new UpdateBrowserSize function :
https://github.com/salvadordf/CEF4Delphi/blob/6badc8f3bd460f06bae7f6a32f69ba6d58bfb8b0/demos/Delphi_VCL/ConsoleBrowser2/uEncapsulatedBrowser.pas#L200
Post Reply