DeclareModule WebBrowser
	Declare Open()
EndDeclareModule

Module WebBrowser
	EnableExplicit

	#Toolbar_Height = 35
	#Window_W       = 800
	#Window_H       = 600

	Global Window.i    = 0
	Global UrlBar.i
	Global WebView.i

	Declare Handler_Resize()
	Declare Handler_Navigate()
	Declare Handler_Close()

	Procedure Open()
		If IsWindow(Window)
			SetActiveWindow(Window)
			ProcedureReturn
		EndIf

		Window = OpenWindow(#PB_Any, 100, 60, #Window_W, #Window_H, "Web Browser",
		                    #PB_Window_TitleBar | #PB_Window_SizeGadget | #PB_Window_SystemMenu)

		UrlBar  = StringGadget(#PB_Any, 4, 5, #Window_W - 8, #Toolbar_Height - 10, "https://lastlife.net")
		WebView = WebGadget(#PB_Any, 0, #Toolbar_Height, #Window_W, #Window_H - #Toolbar_Height, "https://lastlife.net")

		BindGadgetEvent(UrlBar,  @Handler_Navigate())
		BindEvent(#PB_Event_SizeWindow,  @Handler_Resize(), Window)
		BindEvent(#PB_Event_CloseWindow, @Handler_Close(),  Window)

		Desktop::Register("Web Browser", Window, "🌐")
	EndProcedure

	Procedure Handler_Navigate()
; 		Protected URL.s = Trim(GetGadgetText(UrlBar))
; 		If URL = "" : ProcedureReturn : EndIf
; 		If Left(URL, 4) <> "http"
; 			URL = "https://" + URL
; 			SetGadgetText(UrlBar, URL)
; 		EndIf
; 		SetGadgetAttribute(WebView, #PB_Web_URL, URL)
	EndProcedure

	Procedure Handler_Resize()
		Protected W.i = WindowWidth(Window)
		Protected H.i = WindowHeight(Window)
		ResizeGadget(UrlBar,  4, 5, W - 8, #Toolbar_Height - 10)
		ResizeGadget(WebView, 0, #Toolbar_Height, W, H - #Toolbar_Height)
	EndProcedure

	Procedure Handler_Close()
		UnbindGadgetEvent(UrlBar,  @Handler_Navigate())
		UnbindEvent(#PB_Event_SizeWindow,  @Handler_Resize(), Window)
		UnbindEvent(#PB_Event_CloseWindow, @Handler_Close(),  Window)
		Desktop::Unregister(Window)
		Window = 0
	EndProcedure

EndModule
; IDE Options = SpiderBasic 3.10 (Windows - x86)
; CursorPosition = 29
; Folding = --
; iOSAppOrientation = 0
; AndroidAppCode = 0
; AndroidAppOrientation = 0
; EnableXP
; DPIAware
; CompileSourceDirectory