The WebView class represents a webview in the Boson application. It provides a way to manage html content, including JavaScript scripts, styles, functions, and more.
Main WebView
The Application::$webview property provides convenient access to the WebView instance of the main window.
$app = new Boson\Application();
// Access the main WebView
$webview = $app->webview;
URL Navigation
The WebView::$url property allows you to load custom html content from any address (including real ones from internet).
$webview->url = 'https://bosonphp.com';
When setting the URL, webview attempts to load data from the specified source, but the address may change based on the behavior of the target page (for example, triggering scripts or redirects), so the result displays the real address, and not the one that was set.
$app = new Boson\Application();
$app->webview->url = 'https://github.com/BosonPHP';
// After set the new URL, the navigation has
// not yet taken place:
//
// string("URL: about:blank\n")
//
echo 'URL: ' . $app->webview->url . "\n";
$app->on(function (WebViewNavigated $e) use ($app): void {
// The navigation occurs later, for this you
// should subscribe to the required event:
//
// string("URL: https://github.com/BosonPHP\n")
//
echo 'URL: ' . $app->webview->url . "\n";
});
HTML Content
The WebView::$html property allows you to load custom html content without navigation to any address.
$webview->html = '<button>Do Not Click Me!</button>';
State
The WebView::$state property provides access to the current state of the webview.
// Check if the webview is loading
if ($webview->state === Boson\WebView\State::Loading) {
echo "WebView is currently loading content\n";
}
This property will contain one of the possible values of Boson\WebView\State enum.
enum State
{
/**
* Navigation to a new URL.
*/
case Navigating;
/**
* Data is being loaded from the specified URL.
*/
case Loading;
/**
* Readiness for work with document.
*/
case Ready;
}