Skip to content

Commit c7d6c56

Browse files
Refactor Canvas class to enhance option handling and improve structure
1 parent 8e5aa4f commit c7d6c56

File tree

1 file changed

+65
-3
lines changed

1 file changed

+65
-3
lines changed

src/Widget/Canvas.php

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace PhpGui\Widget;
34

45
/**
@@ -7,8 +8,69 @@
78
*
89
* @package PhpGui\Widget
910
*/
10-
class Canvas extends AbstractWidget {
11-
protected function create(): void {
12-
$this->tcl->evalTcl("canvas .{$this->parentId}.{$this->id}");
11+
class Canvas extends AbstractWidget
12+
{
13+
public function __construct(string $parentId, array $options = [])
14+
{
15+
parent::__construct($parentId, $options);
16+
$this->create();
17+
}
18+
19+
protected function create(): void
20+
{
21+
$extra = $this->getOptionString();
22+
$this->tcl->evalTcl("canvas .{$this->parentId}.{$this->id} {$extra}");
23+
}
24+
25+
protected function getOptionString(): string
26+
{
27+
$opts = "";
28+
foreach ($this->options as $key => $value) {
29+
$opts .= " -$key \"$value\"";
30+
}
31+
return $opts;
32+
}
33+
34+
public function drawLine(int $x1, int $y1, int $x2, int $y2, array $options = []): string
35+
{
36+
$optStr = $this->formatOptions($options);
37+
return (string) $this->tcl->evalTcl(".{$this->parentId}.{$this->id} create line $x1 $y1 $x2 $y2 $optStr");
38+
}
39+
40+
public function drawRectangle(int $x1, int $y1, int $x2, int $y2, array $options = []): string
41+
{
42+
$optStr = $this->formatOptions($options);
43+
return (string) $this->tcl->evalTcl(".{$this->parentId}.{$this->id} create rectangle $x1 $y1 $x2 $y2 $optStr");
44+
}
45+
46+
public function drawOval(int $x1, int $y1, int $x2, int $y2, array $options = []): string
47+
{
48+
$optStr = $this->formatOptions($options);
49+
return (string) $this->tcl->evalTcl(".{$this->parentId}.{$this->id} create oval $x1 $y1 $x2 $y2 $optStr");
50+
}
51+
52+
public function drawText(int $x, int $y, string $text, array $options = []): string
53+
{
54+
$optStr = $this->formatOptions($options);
55+
return (string) $this->tcl->evalTcl(".{$this->parentId}.{$this->id} create text $x $y -text {$text} $optStr");
56+
}
57+
58+
public function delete(string $itemId): void
59+
{
60+
$this->tcl->evalTcl(".{$this->parentId}.{$this->id} delete $itemId");
61+
}
62+
63+
public function clear(): void
64+
{
65+
$this->tcl->evalTcl(".{$this->parentId}.{$this->id} delete all");
66+
}
67+
68+
protected function formatOptions(array $options): string
69+
{
70+
$result = [];
71+
foreach ($options as $key => $value) {
72+
$result[] = "-$key {$value}";
73+
}
74+
return implode(' ', $result);
1375
}
1476
}

0 commit comments

Comments
 (0)