Window Class
use Kingbes\PebView\Window;
Methods
Create a Window Object
Constructor
new WindowCreates a window object. Parametersbool$debugWhether to enable debug mode. Default: false ReturnsWindowReturns a window object.
Usage:
$win = new Window(true);
Destroy Window
Public Method
destroyDestroys the window. Parameters- None Returns
voidNo return value.
Usage:
// Must be called after the run() method.
$win->destroy();
Run Window
Public Method
runRuns the window. Parameters- None Returns
WindowReturns the window object.
Usage:
// Must be called after the setHtml() or navigate() method.
$win->run();
Terminate Window
Public Method
terminateTerminates the window. Parameters- None Returns
voidNo return value.
Usage:
// Used to close the application.
$win->terminate();
Dispatch Function
Schedule the functions to be called on the thread that has a running/event loop. This feature can be used, for example, to interact with libraries or native objects.
Public Method
dispatchDispatches the function to be called on the thread that has a running/event loop. Parameterscallable$callbackThe function to be called. ReturnsWindowReturns the window object.
Usage:
// Used to interact with libraries or native objects.
$win->dispatch(function($win, $arg) {
// Interact with libraries or native objects.
// $win is the window object.
// $arg is the argument passed to the dispatch() method.
});
Set Window Icon
Windows requires the ICO format; Linux requires the PNG format; MacOs requires the ICO format. Usually, only the Windows system will work, while Linux and MacOs do not have this effect.
Public Method
setIconSets the window icon. Parametersstring$pathThe path to the icon file. ReturnsWindowReturns the window object.
Usage:
// Must be called before the run() method.
$win->setIcon("path/to/icon.ico");
Set Window Title
Public Method
setTitleSets the window title. Parametersstring$titleThe window title name. ReturnsWindowReturns the window object.
Usage:
// Must be called before the run() method.
$win->setTitle("PebView");
Set Window Size
Public Method
setSizeSets the window size. Parametersint$widthThe window width.int$heightThe window height.WindowHint$hintThe window hint. Default: WindowHint::None ReturnsWindowReturns the window object.
Usage:
// Must be called before the run() method.
$win->setSize(800, 600, WindowHint::None);
Initialize JavaScript
Will load the js code before the window.onload event.
Public Method
initInitializes the JavaScript. Parametersstring$jsThe JavaScript code to be initialized. ReturnsWindowReturns the window object.
Usage:
// Must be called before the run() method.
$win->init("console.log('hello PebView!');");
Evaluate JavaScript
Public Method
evalEvaluates the JavaScript code. Parametersstring$jsThe JavaScript code to be evaluated. ReturnsWindowReturns the window object.
Usage:
$win->eval("console.log('hello PebView!');");
Set Window HTML Content
Public Method
setHtmlSets the window HTML content. Parametersstring$htmlThe HTML content to be set. ReturnsWindowReturns the window object.
Usage:
// Must be called before the run() method.
$win->setHtml("<h1>hello PebView!</h1>");
Navigate Window to Specified URL
Public Method
navigateNavigates the window to the specified URL. Parametersstring$urlThe URL to navigate to. ReturnsWindowReturns the window object.
Usage:
// The and methods must be called before the run() method. And only one of the two can be selected.
$win->navigate("https://www.baidu.com");
Bind JS Function
Public function
bindBinds a JS function. Parametersstring$nameThe function name.callable$callbackThe function to call. ReturnsWindowReturns the window object.
用法:
$win->bind("hello", function(...$params) {
// $win is the window object
// $params are the parameters passed to the hello() method
// Use params[0],params[1], ... to access the parameters
// Example: params[0] is the first parameter, params[1] is the second, etc.
return $params[0] . " " . $params[1];
});
Unbind JS Function
Public function
unbindUnbinds a JS function. Parametersstring$nameThe function name. ReturnsWindowReturns the window object.
Usage:
$win->unbind("hello");
Set Window Close Event
Public function
setCloseCallbackSets the window close event callback. Parameterscallable$callbackThe function to call. ReturnsWindowReturns the window object.
用法:
$win->setCloseCallback(function($win) {
// $win is the window object
// Perform some operations when closing the window
return true; // Return true to allow closing the window
return false; // Return false to prevent closing the window
});
Show Window
Public function
showShows the window. Parameters- None Returns
WindowReturns the window object.
Usage:
$win->show();
Hide Window
Public function
hideHides the window. Parameters- None Returns
WindowReturns the window object.
Usage:
$win->hide();
Create Tray
Windows requires the ICO format; Linux requires the PNG format; MacOs requires the ICO format. Usually, only the Windows system will work, while Linux and MacOs do not have this effect.
Public function
trayCreates the tray icon. Parametersstring$pathThe path to the icon file. ReturnsWindowReturns the window object.
Usage:
// Must be called before the run() method.
$win->tray("path/to/icon.ico");
Tray Menu
Public function
trayMenuCreates the tray menu. Parametersarray$menuThe menu array. ReturnsWindowReturns the window object.
Usage:
// Must be called before the run() method.
$win->trayMenu([
"text" => "menu1", // Menu name
"disabled" => 0, // Whether clickable 0 clickable
"cb" => function($win) { // The function to call when the menu is clicked
// $win is the window object
// You can perform some operations when the menu is clicked
},
"text" => "menu2",
"cb" => function($win) {
// $win is the window object
// You can perform some operations when the menu is clicked
},
...
]);