Added Label

This commit is contained in:
Thomas Peterson 2025-10-22 18:24:50 +02:00
parent 949cd47c96
commit 82cfa6c80d
4 changed files with 52 additions and 2 deletions

View File

@ -1,5 +1,7 @@
<?php
use PHPNative\Ui\Widget\Label;
require_once __DIR__ . '/../vendor/autoload.php';
// Check PHP version
@ -10,6 +12,11 @@ if (PHP_VERSION_ID < 80100) {
$app = new \PHPNative\Framework\Application('ToDo Demo', 700, 500);
$container = new \PHPNative\Ui\Widget\Container(style: 'm-10 p-10 rounded-xl bg-lime-200');
$containerMenu = new \PHPNative\Ui\Widget\Container(style: 'bg-lime-700');
$label = new Label(
text: 'Todo App',
style: 'text-xl2 text-red-500',
);
$containerMenu->addComponent($label);
$container->addComponent($containerMenu);
$app->setRoot($container);
$app->run();

View File

@ -8,7 +8,6 @@ class Container
{
public static function layout(Viewport $viewport, \PHPNative\UI\Widget\Container $view, int $index = 0): Viewport
{
var_dump('test');
return $viewport;
}
}

View File

@ -22,7 +22,7 @@ abstract class Component
protected Viewport $viewport;
protected array $computedStyles = [];
private Viewport $contentViewport;
protected Viewport $contentViewport;
public function setViewport(Viewport $viewport): void
{

44
src/Ui/Widget/Label.php Normal file
View File

@ -0,0 +1,44 @@
<?php
namespace PHPNative\Ui\Widget;
use PHPNative\Framework\TextRenderer;
use PHPNative\Tailwind\Style\Text;
use PHPNative\Ui\Component;
class Label extends Component
{
public function __construct(
public string $text = '',
public string $style = '',
) {}
public function setText(string $text): void
{
$this->text = $text;
}
public function renderContent(null|TextRenderer $textRenderer = null): void
{
if (!$this->visible || $textRenderer === null || !$textRenderer->isInitialized()) {
return;
}
// Get text style from computed styles
$textStyle = $this->computedStyles[Text::class] ?? new Text();
// Set text color
$color = $textStyle->color;
$textRenderer->setColor($color->red / 255, $color->green / 255, $color->blue / 255, $color->alpha / 255);
// Calculate text position based on alignment
$x = $this->contentViewport->x;
$y = $this->contentViewport->y;
// Draw the text
$textRenderer->drawText($this->text, (int) $x, (int) $y, $textStyle->size);
// Call parent to render children if any
parent::renderContent($textRenderer);
}
}