-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindow.php
202 lines (180 loc) · 3.71 KB
/
Window.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace Dahl\PhpTerm;
/**
* Wrapper class for handling input and output on a terminal application.
*
* @copyright Copyright (C) 2015 Albert Dahlin
* @author Albert Dahlin <[email protected]>
* @license MIT License <http://opensource.org/licenses/MIT>
*/
class Window
{
/**
* Input object.
*
* @var Keyboard
* @access protected
*/
protected $_input;
/**
* Output class.
*
* @var Terminal
* @access protected
*/
protected $_output;
/**
* Holds the window size.
*
* @var array
* @access protected
*/
protected $_size;
/**
* The elements added to the window.
*
* @var array
* @access protected
*/
protected $_elements = array();
/**
* An element that has focus.
*
* @var mixed
* @access protected
*/
protected $_focus;
/**
* Constructor. Initializes input and output.
*
* @access public
* @return void
*/
public function __construct()
{
$this->_input = Input\Keyboard::getInstance();
$this->_output = new Output\Terminal;
}
/**
* Returns the intput object.
*
* @access public
* @return Keyboard
*/
public function getInput()
{
return $this->_input;
}
/**
* Returns the output object.
*
* @access public
* @return Terminal
*/
public function getOutput()
{
return $this->_output;
}
/**
* Returns the widow size in rows and cols.
*
* @access public
* @return array
*/
public function getSize()
{
if (!$this->_size) {
$this->_size = $this->_output->getSize();
}
return $this->_size;
}
public function getMaxHeight()
{
$size = $this->getSize();
return $size['row'];
}
public function getMaxWidth()
{
$size = $this->getSize();
return $size['col'];
}
/**
* Set focus on an element.
*
* @param Element $element
* @access public
* @return Window
*/
public function setFocus($element)
{
$this->_focus = $element;
return $this;
}
/**
* Render window elements.
*
* @access public
* @return void
*/
public function render($force = false)
{
foreach ($this->_elements as $element) {
$element->render($force);
}
if ($this->_focus) {
$this->_focus->applyFocus();
}
}
/**
* Adds an element to the output and returns it.
*
* @param string $id
* @access public
* @return Element
*/
public function addElement($id, $type = 'Element')
{
if (!isset($this->_elements[$id])) {
$type = __namespace__ . '\\Output\\' . ucfirst($type);
$element = new $type($id);
$this->_elements[$id] = $element;
$element->setParent($this);
return $element;
} else {
throw new Exception("An element with id {$id} already exists");
}
}
/**
* Remove one element by id.
*
* @param string $id
* @access public
* @return void
*/
public function removeElement($id)
{
if (isset($this->_elements[$id])) {
unset($this->_elements[$id]);
}
}
/**
* Remove all elements
*
* @access public
* @return void
*/
public function removeElements()
{
$this->_elements = array();
}
/**
* Returns a new page.
*
* @access public
* @return Page
*/
public function getPage()
{
return new Page($this);
}
}