Webview2 control - Chromium browser for Purebasic (Windows) [Jan 1, 2024]

Everything else that doesn't fall into one of the other PB categories.
User avatar
Derren
Enthusiast
Enthusiast
Posts: 313
Joined: Sat Jul 23, 2011 1:13 am
Location: Germany

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Post by Derren »

Thanks for the reply. What a shame :(
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Post by Justin »

Updated to SDK 1.0.1020.30 / 1.0.1056-prerelease.

API Release notes
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Post by ChrisR »

Thanks Justin for sharing your great work on WebView2
It opens great perspectives by being able to insert Web components for building modern apps.
I'll have to find some time to play with it.
vmars316
Enthusiast
Enthusiast
Posts: 464
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Post by vmars316 »

Justin ,
webView2 for .pb , Awesome .
I wrote a pbBrowser in PB , but it could handle IE-11 , and MANY sites won't Allow for IE-11 .
I started making a KidSafe Browser , a year or so ago , first in VisualSudio in VB , then C# ,
but ran into limitations . So I switched to Electron , but ran into probs with links opening default Browser .
Then I got sick and had to abandon all .
But now I am ready to try again .
I would be happy to share my 'Electron code' if anyone is interested , it might be helpful .

Are there any simple working PB examples available ?
Thanks
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
User avatar
Kiffi
Addict
Addict
Posts: 1353
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Post by Kiffi »

Good news: Evergreen WebView2 Runtime will be able to get installed without elevation.

More Informations: https://github.com/MicrosoftEdge/WebVie ... /issues/37
Hygge
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Post by Justin »

Hi vmars316,
the examples i included cover various things like limiting the pages you can visit, there is also a basic browser in the Ohm folder, not sure if is this what you are looking for.
vmars316
Enthusiast
Enthusiast
Posts: 464
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Post by vmars316 »

Justin :
Justin wrote: Wed Dec 15, 2021 10:07 am Hi vmars316,
the examples i included cover various things like limiting the pages you can visit, there is also a basic browser in the Ohm folder, not sure if is this what you are looking for.


Awesome
Where can I down all this ?
I have already installed >
Latest Microsoft VisualStudio
MS Edge
and MS Edge Webview2 Runtime

Can PB find what it needs to run WebView2 ?


Thank you...
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Post by Justin »

Just download the code, this is the direct link of the zipped files from github:
https://github.com/omegakode/PBWebview2 ... master.zip

Unzip it and in the examples folder load any of the .pb files like 'basic_browser_async.pb' for example in the PB Ide and if you have the webview runtime installed it should work.
vmars316
Enthusiast
Enthusiast
Posts: 464
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Post by vmars316 »

Thanks Justin !
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Post by PureLust »

Hi Justin,

I just had a quick look into your Webview2 Control and .... wow, great work and very usefull .... THANKS A LOT !!! :D

But I have a little question:

I want to open several instances of a website at the same time (in different tabs / containers), but with different cookie-settings.

Is there any way to open a Webview2 Control, where I can define the cookie-directory it uses or to pass specific cookies through?

Thanks again for your great work. Image

PureLust
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
vmars316
Enthusiast
Enthusiast
Posts: 464
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Post by vmars316 »

I am excited about you your Webview2 Control .
But I need a guide to compile it .
There so many files , I am in overwhelm .
Is there one yet .
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Post by Justin »

Hi PureLust,

to use different cookie settings you have to use CreateCoreWebView2EnvironmentWithOptions() to set the userDataFolder, all webview instances using that WebView2Environment will use that datafolder, so for each webview instance with a different data folder you need to create a new WebView2Environment instance with that data folder..
https://docs.microsoft.com/en-us/micros ... ithoptions

vmars316,
to test it simply load any example in the examples folder into the PB IDE and run it, assuming you have the webview runtime installed it should work.
vmars316
Enthusiast
Enthusiast
Posts: 464
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Post by vmars316 »

WOW , that is very encouraging ,
Thanks justin
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Post by PureLust »

Justin wrote: Tue Jan 18, 2022 7:25 pm Hi PureLust,

to use different cookie settings you have to use CreateCoreWebView2EnvironmentWithOptions() to set the userDataFolder, all webview instances using that WebView2Environment will use that datafolder, so for each webview instance with a different data folder you need to create a new WebView2Environment instance with that data folder..
https://docs.microsoft.com/en-us/micros ... ithoptions
Yeah ... that seems to do the trick .... thanks a lot. Image

But ... I have another question:

I wanted to create multiple WebView2 Controls (in different Panels within a PanelGadget), but struggled doing it.
Looking at your examples, you use only one set of environment (Structure APP_TAG), so no multi-Gadget usage there (right?).

I've tried to create an Array (List().APP_TAG) but don't know how to refer to different environments when I call one of your several wv-Subroutines.

Is there any way (maybe within you WV2_EVENT_HANDLER-Structure) to identify different sessions?
Or if MultiControl support is already included, maybe you can show a quick example?

Thanks a lot for WebView2 (its really awesome) and your kind support,
PureLust.
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: Webview2 control - Chromium browser for Purebasic (Windows)

Post by Justin »

Hi,

With the last parameter of wv2_EventHandler_New() you can pass a context value.

See example.
Use Ctrl + T for new browser, Ctrl + W to close the current browser.

You will have to set a different folder name in each CreateCoreWebView2EnvironmentWithOptions() call, i didn't.

Code: Select all

;multi_tab.pb

;Multi tab browser
;New environment for each browser.
;Use Ctrl + T new browser, Ctrl + w closes the current browser

IncludeFile "..\PBWebView2.pb"

EnableExplicit

;- CMD Enum
Enumeration
	#CMD_NEW_TAB
	#CMD_CLOSE_TAB
EndEnumeration

;- BROWSER
Structure BROWSER
	wvEnvironment.ICoreWebView2Environment
	wvController.ICoreWebView2Controller
	wvCore.ICoreWebView2
	
	*eventNavigationCompleted.WV2_EVENT_HANDLER

	container.i
	id.l
EndStructure

;- APP_TAG
Structure APP_TAG
	window.i
	panel.i
	*evAccelKeyPressed.WV2_EVENT_HANDLER
	browserID.l
EndStructure
Global.APP_TAG app

;- DECLARES
Declare main()

Declare window_Close()
Declare window_Resize()
Declare window_ProcessEvents(ev.l)
Declare window_new_tab()
Declare window_close_tab(index.l)

Declare wvEnvironment_Created(*this.WV2_EVENT_HANDLER, result.l, environment.ICoreWebView2Environment)	
Declare wvController_Created(*this.WV2_EVENT_HANDLER, result.l=0, controller.ICoreWebView2Controller=0)
Declare wv_NavigationCompleted(*this.WV2_EVENT_HANDLER, sender.ICoreWebView2, args.ICoreWebView2NavigationCompletedEventArgs)
Declare wv_NavigationStarting(*this.WV2_EVENT_HANDLER, sender.ICoreWebView2, args.ICoreWebView2NavigationStartingEventArgs)
Declare wv_WebResourceRequested(*this.WV2_EVENT_HANDLER, sender.ICoreWebView2, args.ICoreWebView2WebResourceRequestedEventArgs)	


;-
;Resizes the webview to the dimensions of ite container gadget
Procedure browser_resize(*browser.BROWSER)
	Protected.RECT wvBounds

	If *browser\wvController
		;Resize container
		ResizeGadget(*browser\container, #PB_Ignore, #PB_Ignore, GetGadgetAttribute(app\panel, #PB_Panel_ItemWidth), 
		GetGadgetAttribute(app\panel, #PB_Panel_ItemHeight))
		
		;Resize webview
		GetClientRect_(GadgetID(*browser\container), @wvBounds)
		wv2_Controller_put_Bounds(*browser\wvController, @wvBounds)
	EndIf 
EndProcedure

Procedure browser_free(*browser.BROWSER)
	If *browser\wvCore : *browser\wvCore\Release() : *browser\wvCore = 0 : EndIf
	If *browser\wvController : *browser\wvController\Release() : *browser\wvController = 0 : EndIf
	If *browser\wvEnvironment : *browser\wvEnvironment\Release() : *browser\wvEnvironment = 0 : EndIf
	FreeMemory(*browser)
EndProcedure

;-
Procedure wvEnvironment_Created(*this.WV2_EVENT_HANDLER, result.l, environment.ICoreWebView2Environment)
	Protected.BROWSER *browser
	
	If result = #S_OK
		*browser = *this\context
		
		environment\QueryInterface(?IID_ICoreWebView2Environment, @*browser\wvEnvironment)
		*browser\wvEnvironment\CreateCoreWebView2Controller(GadgetID(*browser\container), 
			wv2_EventHandler_New(?IID_ICoreWebView2CreateCoreWebView2ControllerCompletedHandler, @wvController_Created(), *browser))

		wv2_EventHandler_Release(*this)

	Else
		MessageRequester("Error", "Failed to create WebView2Environment.")
		End 
	EndIf 
EndProcedure

Procedure wvController_Created(*this.WV2_EVENT_HANDLER, result.l=0, controller.ICoreWebView2Controller=0)
	Protected.BROWSER *browser

	If result = #S_OK
		*browser = *this\context

		controller\QueryInterface(?IID_ICoreWebView2Controller, @*browser\wvController)
		
		*browser\wvController\get_CoreWebView2(@*browser\wvCore)
	
		;Setup events
		;Global event for all browsers
		*browser\wvController\add_AcceleratorKeyPressed(app\evAccelKeyPressed, #Null)

		;Browser specific event
		*browser\eventNavigationCompleted = wv2_EventHandler_New(?IID_ICoreWebView2NavigationCompletedEventHandler, @wv_NavigationCompleted(), *browser)
		*browser\wvCore\add_NavigationCompleted(*browser\eventNavigationCompleted, #Null)

		browser_resize(*browser)
		*browser\wvCore\Navigate("https://duckduckgo.com")
		
		wv2_EventHandler_Release(*this)

	Else
		MessageRequester("Error", "Failed to create WebView2Controller.")
		End
	EndIf 
EndProcedure

Procedure wvController_AccelKeyPressed(*this.WV2_EVENT_HANDLER, sender.ICoreWebView2Controller, args.ICoreWebView2AcceleratorKeyPressedEventArgs)
	Protected.l keyEventKind, key, handled
			
	;Disable Ctrl + T , Ctrl + w so it can be handled by the main window.
	
	handled = #False 
	args\get_KeyEventKind(@keyEventKind)
	If keyEventKind = #COREWEBVIEW2_KEY_EVENT_KIND_KEY_DOWN Or keyEventKind = #COREWEBVIEW2_KEY_EVENT_KIND_SYSTEM_KEY_DOWN
		args\get_VirtualKey(@key)
		
		If key_IsDown(#VK_CONTROL)
			Select key
				Case #VK_T
					window_new_tab()
					handled = #True
					
				Case #VK_W
					window_close_tab(GetGadgetState(app\panel))
					handled = #True
			EndSelect
		EndIf 
	EndIf 
	
	args\put_Handled(handled)
EndProcedure 

Procedure wv_NavigationCompleted(*this.WV2_EVENT_HANDLER, sender.ICoreWebView2, args.ICoreWebView2NavigationCompletedEventArgs)
	Protected.BROWSER *browser

	*browser = *this\context
	
	Debug "Event NavigationCompleted ID: " + Str(*browser\id)
	
EndProcedure

;-
Procedure panel_on_change()
	Protected.BROWSER *browser

	*browser = GetGadgetItemData(app\panel, GetGadgetState(app\panel))
	If *browser : browser_resize(*browser) :EndIf	
EndProcedure

;-
Procedure window_on_move_moving()
	Protected.BROWSER *browser

	*browser = GetGadgetItemData(app\panel, GetGadgetState(app\panel))
	If *browser And *browser\wvController
		wv2_Controller_On_WM_MOVE_MOVING(*browser\wvController)
	EndIf 
EndProcedure

Procedure window_Proc(hwnd.i, msg.l, wparam.i, lparam.i)
	Select msg
		Case #WM_MOVE, #WM_MOVING : window_on_move_moving()
	EndSelect
	
	ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

Procedure window_new_tab()
	Protected.BROWSER *browser
	Protected.l currTab
	Protected.WV2_EVENT_HANDLER *wvEv
		
	;New browser
	*browser = AllocateMemory(SizeOf(BROWSER))
	app\browserID + 1
	*browser\id = app\browserID
	
	OpenGadgetList(app\panel)
	AddGadgetItem(app\panel, -1, "New tab " + Str(*browser\id))
	CloseGadgetList() ;Panel
	currTab = CountGadgetItems(app\panel) - 1
	SetGadgetItemData(app\panel, currTab, *browser)
	
	OpenGadgetList(app\panel, currTab)
	
	*browser\container = ContainerGadget(#PB_Any, 0, 0, GetGadgetAttribute(app\panel, #PB_Panel_ItemWidth), 
		GetGadgetAttribute(app\panel, #PB_Panel_ItemHeight))
	CloseGadgetList() ;Container
		
	CreateCoreWebView2EnvironmentWithOptions("", "", #Null, 
		wv2_EventHandler_New(?IID_ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler, @wvEnvironment_Created(), *browser))

	SetGadgetState(app\panel, currTab)

	CloseGadgetList() ;Tab
EndProcedure

Procedure window_close_tab(index.l)
	Protected.BROWSER *browser
	
	If index <> -1
		*browser = GetGadgetItemData(app\panel, index)
		RemoveGadgetItem(app\panel, index)
		If *browser : browser_free(*browser) : EndIf
		
		;Resize the next current browser
		*browser = GetGadgetItemData(app\panel, GetGadgetState(app\panel))
		If *browser : browser_resize(*browser) : EndIf 
	EndIf 
EndProcedure

Procedure window_Close()
	Protected.l iTab
	
	;Free all browsers
	For iTab = 0 To CountGadgetItems(app\panel)
		window_close_tab(iTab)
	Next
	
	;Global event
	If app\evAccelKeyPressed
		wv2_EventHandler_Release(app\evAccelKeyPressed)
		app\evAccelKeyPressed = 0
	EndIf 
	
	ProcedureReturn #True ;Exit message loop.
EndProcedure

Procedure window_Resize()
	Protected.RECT wvBounds
	Protected.BROWSER *browser
	
	;Panel
	ResizeGadget(app\panel, #PB_Ignore, #PB_Ignore, WindowWidth(app\window), WindowHeight(app\window))
		
	*browser = GetGadgetItemData(app\panel, GetGadgetState(app\panel))
	If *browser : browser_resize(*browser) : EndIf 
EndProcedure

Procedure window_ProcessEvents(ev.l)
	Select ev
		Case #PB_Event_Gadget
			Select EventType()
				Case #PB_EventType_Change
					Select EventGadget()
						Case app\panel : panel_on_change()
					EndSelect
			EndSelect
			
		Case #PB_Event_Menu
			Select EventMenu()
				Case #CMD_NEW_TAB : window_new_tab()
					
				Case #CMD_CLOSE_TAB : window_close_tab(GetGadgetState(app\panel))
			EndSelect
			
		Case #PB_Event_CloseWindow : ProcedureReturn window_Close()
	EndSelect
	
	ProcedureReturn #False 
EndProcedure

;-
Procedure main()	
	If wv2_GetBrowserVersion("") = ""
		MessageRequester("Error", "MS Edge not found, install MS Edge runtime.")
		End 
	EndIf
	
	;Global event handler for all browsers, to disable Ctrl W Ctrl T processing
	app\evAccelKeyPressed = wv2_EventHandler_New(?IID_ICoreWebView2AcceleratorKeyPressedEventHandler, @wvController_AccelKeyPressed())

	app\window = OpenWindow(#PB_Any, 10, 10, 600, 400, "Multi Tab - Use: Ctrl + T New Tab / Ctrl + W Close Tab", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget)
	SetWindowCallback(@window_Proc(), app\window)
	app\panel = PanelGadget(#PB_Any, 0, 0, WindowWidth(app\window), WindowHeight(app\window))
	CloseGadgetList()
	BindEvent(#PB_Event_SizeWindow, @window_Resize())
	
	AddKeyboardShortcut(app\window, #PB_Shortcut_Control | #PB_Shortcut_T, #CMD_NEW_TAB)
	AddKeyboardShortcut(app\window, #PB_Shortcut_Control | #PB_Shortcut_W, #CMD_CLOSE_TAB)

	Repeat
	Until window_ProcessEvents(WaitWindowEvent()) = #True 
EndProcedure

main()
Post Reply