sdl3/examples/advanced_multi_window_example.php
2025-10-23 10:40:07 +02:00

190 lines
5.1 KiB
PHP

<?php
require_once __DIR__ . '/../vendor/autoload.php';
use PHPNative\Framework\Application;
use PHPNative\Ui\Widget\Button;
use PHPNative\Ui\Widget\Container;
use PHPNative\Ui\Widget\Label;
// Create application
$app = new Application();
// Create main window
$mainWindow = $app->createWindow('Advanced Multi-Window Demo', 700, 500, 100, 100);
$windowCounter = 1;
// Main UI
$mainContainer = new Container('flex flex-col p-6 gap-4 bg-gray-50');
$title = new Label(
text: 'Advanced Multi-Window Demo',
style: 'text-2xl text-gray-900'
);
$subtitle = new Label(
text: 'Windows mit asynchronen Operationen',
style: 'text-base text-gray-600'
);
// Button to create window with async data loading
$createAsyncWindowButton = new Button(
text: 'Window mit Async-Datenladung',
style: 'bg-purple-500 hover:bg-purple-700 text-white p-4 rounded-lg'
);
$createAsyncWindowButton->setOnClickAsync(
onClickAsync: function() use (&$windowCounter) {
// Simulate loading data in background
sleep(1);
return [
'windowId' => $windowCounter++,
'data' => 'Daten erfolgreich geladen!',
'timestamp' => date('H:i:s'),
'items' => ['Item 1', 'Item 2', 'Item 3']
];
},
onComplete: function($result) use ($app) {
// Create window with loaded data
$windowId = $result['windowId'];
$newWindow = $app->createWindow(
"Data Window #$windowId",
500,
350,
150 + ($windowId * 30),
150 + ($windowId * 30)
);
$container = new Container('flex flex-col p-6 gap-3 bg-green-50');
$titleLabel = new Label(
text: "Data Window #$windowId",
style: 'text-xl text-gray-900'
);
$dataLabel = new Label(
text: $result['data'],
style: 'text-base text-green-700 p-2 bg-white rounded'
);
$timeLabel = new Label(
text: "Geladen um: " . $result['timestamp'],
style: 'text-sm text-gray-600'
);
$itemsLabel = new Label(
text: "Items:\n" . implode("\n", $result['items']),
style: 'text-sm text-gray-700 p-2 bg-white rounded'
);
$closeButton = new Button(
text: 'Schließen',
style: 'bg-red-500 hover:bg-red-700 text-white p-3 rounded'
);
$closeButton->setOnClick(function() use ($newWindow) {
$newWindow->close();
});
$container->addComponent($titleLabel);
$container->addComponent($dataLabel);
$container->addComponent($timeLabel);
$container->addComponent($itemsLabel);
$container->addComponent($closeButton);
$newWindow->setRoot($container);
},
onError: function($error) {
error_log("Error creating window: " . $error->getMessage());
}
);
// Button to create multiple windows at once
$createMultipleButton = new Button(
text: '3 Windows auf einmal erstellen',
style: 'bg-orange-500 hover:bg-orange-700 text-white p-4 rounded-lg'
);
$createMultipleButton->setOnClick(function() use ($app, &$windowCounter) {
for ($i = 0; $i < 3; $i++) {
$currentId = $windowCounter++;
$offset = $currentId * 40;
$newWindow = $app->createWindow(
"Batch Window #$currentId",
400,
250,
200 + $offset,
200 + ($i * 50)
);
$container = new Container('flex flex-col p-4 gap-2 bg-yellow-50');
$label = new Label(
text: "Batch Window #$currentId",
style: 'text-lg text-gray-900'
);
$info = new Label(
text: "Teil einer Batch-Erstellung ($i/3)",
style: 'text-sm text-gray-600'
);
$closeButton = new Button(
text: 'X',
style: 'bg-red-500 hover:bg-red-700 text-white p-2 rounded'
);
$closeButton->setOnClick(function() use ($newWindow) {
$newWindow->close();
});
$container->addComponent($label);
$container->addComponent($info);
$container->addComponent($closeButton);
$newWindow->setRoot($container);
}
});
// Window count label
$windowCountLabel = new Label(
text: 'Offene Windows: 1',
style: 'text-sm text-blue-600 p-2 bg-white rounded'
);
// Update window count periodically
$mainContainer->addComponent($title);
$mainContainer->addComponent($subtitle);
$mainContainer->addComponent($windowCountLabel);
$mainContainer->addComponent($createAsyncWindowButton);
$mainContainer->addComponent($createMultipleButton);
// Info
$info = new Label(
text: 'Die UI bleibt responsive während des Ladens!',
style: 'text-xs text-gray-500 italic mt-4'
);
$mainContainer->addComponent($info);
// Quit button
$quitButton = new Button(
text: 'Beenden',
style: 'bg-red-600 hover:bg-red-800 text-white p-4 rounded-lg'
);
$quitButton->setOnClick(function() use ($app) {
$app->quit();
});
$mainContainer->addComponent($quitButton);
$mainWindow->setRoot($mainContainer);
// Run application
$app->run();