Platforms to show: All Mac Windows Linux Cross-Platform
/Mac64bit/WebKit2/DesktopWebView
Required plugins for this example: MBS MacBase Plugin, MBS Mac64bit Plugin, MBS Main Plugin, MBS MacCocoa Plugin, MBS MacControls Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Mac64bit/WebKit2/DesktopWebView
This example is the version from Thu, 25th Jan 2023.
Project "DesktopWebView.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits DesktopWindow
Control browser Inherits DesktopWKWebViewControlMBS
ControlInstance browser Inherits DesktopWKWebViewControlMBS
EventHandler Sub JavaScriptEvaluated(JavaScript as String, Result as Variant, Error as NSErrorMBS, Tag as String)
List.AddRow CurrentMethodName
List.AddRow "JavaScript", JavaScript
if Error = nil then
List.AddRow "Error", "none"
else
List.AddRow "Error", Error.LocalizedDescription
end if
List.AddRow "Tag", Tag
List.AddRow ""
List.ScrollPosition = List.RowCount
MsgBox "JavaScript result: "+result.StringValue
End EventHandler
EventHandler Sub Opening()
Dim f As FolderItem = FindFile("test.html")
Dim b As BinaryStream = BinaryStream.Open(f)
Dim s As String = b.Read(B.Length, encodings.UTF8)
Me.LoadHTML s
End EventHandler
EventHandler Sub decidePolicyForNavigationAction(navigationAction as WKNavigationActionMBS, decisionHandler as WKPolicyForNavigationActionDecisionHandlerMBS)
List.AddRow CurrentMethodName
List.AddRow "URL", navigationAction.request.URL
List.AddRow "NavigationType", navigationAction.navigationType.ToString
List.AddRow "modifierFlags", navigationAction.modifierFlags.ToString
List.AddRow "buttonNumber", navigationAction.buttonNumber.ToString
List.AddRow "newWindow", navigationAction.newWindow.ToString
List.AddRow "shouldPerformDownload", navigationAction.shouldPerformDownload.ToString
List.AddRow ""
List.ScrollPosition = List.RowCount
decisionHandler.Allow
End EventHandler
EventHandler Sub didCloseContextualMenu(menu as NSMenuMBS, NSEvent as NSEventMBS)
List.AddRow CurrentMethodName
End EventHandler
EventHandler Sub didCommitNavigation(navigation as WKNavigationMBS)
List.AddRow CurrentMethodName
End EventHandler
EventHandler Sub didFailNavigation(navigation as WKNavigationMBS, Error as NSErrorMBS)
List.AddRow CurrentMethodName
if error <> nil then
List.AddRow "error", Error.LocalizedDescription
end if
End EventHandler
EventHandler Sub didFailProvisionalNavigation(navigation as WKNavigationMBS, Error as NSErrorMBS)
List.AddRow CurrentMethodName
if error <> nil then
List.AddRow "error", Error.LocalizedDescription
end if
End EventHandler
EventHandler Sub didFinishNavigation(navigation as WKNavigationMBS)
List.AddRow CurrentMethodName
End EventHandler
EventHandler Sub didReceiveScriptMessage(Body as Variant, name as String)
MsgBox name+EndOfLine+body.StringValue
End EventHandler
EventHandler Sub didReceiveServerRedirectForProvisionalNavigation(navigation as WKNavigationMBS)
List.AddRow CurrentMethodName
End EventHandler
EventHandler Sub didStartProvisionalNavigation(navigation as WKNavigationMBS)
List.AddRow CurrentMethodName
End EventHandler
EventHandler Sub runJavaScriptAlertPanel(initiatedByFrame as WKFrameInfoMBS, message as String)
List.AddRow CurrentMethodName
List.AddRow "Message", Message
List.AddRow ""
List.ScrollPosition = List.RowCount
MsgBox message
End EventHandler
EventHandler Function runJavaScriptConfirmPanel(initiatedByFrame as WKFrameInfoMBS, message as String) As boolean
List.AddRow CurrentMethodName
List.AddRow "Message", Message
List.AddRow ""
List.ScrollPosition = List.RowCount
Dim d as New MessageDialog //declare the MessageDialog object
d.icon=MessageDialog.GraphicCaution //display warning icon
d.ActionButton.Caption="Confirm"
d.CancelButton.Visible=True //show the Cancel button
d.Message=message
Dim b as MessageDialogButton = d.ShowModal //display the dialog
Select Case b //determine which button was pressed.
Case d.ActionButton
//user pressed Confirm
Return true
End select
End EventHandler
EventHandler Function runJavaScriptTextInputPanel(initiatedByFrame as WKFrameInfoMBS, prompt as String, defaultText as String) As String
List.AddRow CurrentMethodName
List.AddRow "prompt", prompt
List.AddRow "defaultText", defaultText
List.AddRow ""
List.ScrollPosition = List.RowCount
dim r as string
dim d as new InputDialog
d.Message.Text = prompt
d.InputField.Text = defaultText
d.ShowModal
if d.ok then
r = d.InputField.Text
end if
d.close
Return r
End EventHandler
EventHandler Sub willShowContextualMenu(menu as NSMenuMBS, NSEvent as NSEventMBS)
List.AddRow CurrentMethodName
// remove all
'menu.removeAllItems
// subclass to catch event (or use addHandler)
Dim m As New MyNSMenuItemMBS
m.CreateMenuItem
m.Title = "Hello World"
// no subclass for separator needed
Dim s As New NSMenuItemMBS
s.CreateSeparator
// add our items
menu.addItem s
menu.addItem m
// make sure Xojo objects stay alive for some time
LastContextMenuItem = m
LastContextMenu = menu
End EventHandler
End Control
Control List Inherits DesktopListBox
ControlInstance List Inherits DesktopListBox
End Control
Control TryButton Inherits DesktopBevelButton
ControlInstance TryButton Inherits DesktopBevelButton
EventHandler Sub Pressed()
browser.EvaluateJavaScript("window.webkit.messageHandlers.test.postMessage('Hello');")
End EventHandler
End Control
EventHandler Sub Opening()
#if Target64Bit then
Title = "WebKit 2 with 64-bit"
#else
Title = "WebKit 1 with 32-bit"
#endif
#if Target64Bit then
browser.addScriptMessageHandler "test"
#endif
End EventHandler
Function FindFile(name as string) As FolderItem
// Look for file in parent folders from executable on
dim parent as FolderItem = app.ExecutableFile.Parent
while parent<>Nil
dim file as FolderItem = parent.Child(name)
if file<>Nil and file.Exists then
Return file
end if
parent = parent.Parent
wend
End Function
Property LastContextMenu As NSMenuMBS
Property LastContextMenuItem As MyNSMenuItemMBS
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class InputDialog Inherits DesktopWindow
Control Message Inherits DesktopLabel
ControlInstance Message Inherits DesktopLabel
End Control
Control InputField Inherits DesktopTextArea
ControlInstance InputField Inherits DesktopTextArea
End Control
Control CancelButton Inherits DesktopButton
ControlInstance CancelButton Inherits DesktopButton
EventHandler Sub Pressed()
ok = false
hide
End EventHandler
End Control
Control OKButton Inherits DesktopButton
ControlInstance OKButton Inherits DesktopButton
EventHandler Sub Pressed()
ok = true
hide
End EventHandler
End Control
Property ok As Boolean
End Class
Class MyNSMenuItemMBS Inherits NSMenuItemMBS
EventHandler Sub Action()
MainWindow.List.AddRow CurrentMethodName
MsgBox "Context menu entry clicked."
End EventHandler
End Class
End Project
The items on this page are in the following plugins: MBS Mac64bit Plugin.