mirror of https://github.com/buggins/dlangui.git
fix timers support; add timer example
This commit is contained in:
parent
1377556d92
commit
e63a9b0b2a
|
@ -45,6 +45,42 @@ Widget createAboutWidget()
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class TimerTest : HorizontalLayout {
|
||||||
|
ulong timerId;
|
||||||
|
TextWidget _counter;
|
||||||
|
int _value;
|
||||||
|
Button _start;
|
||||||
|
Button _stop;
|
||||||
|
override bool onTimer(ulong id) {
|
||||||
|
_value++;
|
||||||
|
_counter.text = to!dstring(_value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
this() {
|
||||||
|
addChild(new TextWidget(null, "Timer test."d));
|
||||||
|
_counter = new TextWidget(null, "0"d);
|
||||||
|
_counter.fontSize(32);
|
||||||
|
_start = new Button(null, "Start timer"d);
|
||||||
|
_stop = new Button(null, "Stop timer"d);
|
||||||
|
_stop.enabled = false;
|
||||||
|
_start.onClickListener = delegate(Widget src) {
|
||||||
|
_start.enabled = false;
|
||||||
|
_stop.enabled = true;
|
||||||
|
timerId = setTimer(1000);
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
_stop.onClickListener = delegate(Widget src) {
|
||||||
|
_start.enabled = true;
|
||||||
|
_stop.enabled = false;
|
||||||
|
cancelTimer(timerId);
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
addChild(_start);
|
||||||
|
addChild(_stop);
|
||||||
|
addChild(_counter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class AnimatedDrawable : Drawable {
|
class AnimatedDrawable : Drawable {
|
||||||
DrawableRef background;
|
DrawableRef background;
|
||||||
this() {
|
this() {
|
||||||
|
@ -375,6 +411,7 @@ extern (C) int UIAppMain(string[] args) {
|
||||||
layout.addChild((new TextWidget()).textColor(0x00802000).text("Text widget 0"));
|
layout.addChild((new TextWidget()).textColor(0x00802000).text("Text widget 0"));
|
||||||
layout.addChild((new TextWidget()).textColor(0x40FF4000).text("Text widget"));
|
layout.addChild((new TextWidget()).textColor(0x40FF4000).text("Text widget"));
|
||||||
layout.addChild((new Button("BTN1")).textResource("EXIT")); //.textColor(0x40FF4000)
|
layout.addChild((new Button("BTN1")).textResource("EXIT")); //.textColor(0x40FF4000)
|
||||||
|
layout.addChild(new TimerTest());
|
||||||
|
|
||||||
static if (true) {
|
static if (true) {
|
||||||
|
|
||||||
|
|
|
@ -891,7 +891,9 @@ class Window {
|
||||||
}
|
}
|
||||||
/// cancel timer
|
/// cancel timer
|
||||||
void cancelTimer(ulong timerId) {
|
void cancelTimer(ulong timerId) {
|
||||||
for (size_t i = _queue.length - 1; i >= 0; i--) {
|
if (!_queue.length)
|
||||||
|
return;
|
||||||
|
for (int i = cast(int)_queue.length - 1; i >= 0; i--) {
|
||||||
if (_queue[i].id == timerId) {
|
if (_queue[i].id == timerId) {
|
||||||
_queue[i].cancel();
|
_queue[i].cancel();
|
||||||
break;
|
break;
|
||||||
|
@ -908,9 +910,11 @@ class Window {
|
||||||
return delta;
|
return delta;
|
||||||
}
|
}
|
||||||
private void cleanup() {
|
private void cleanup() {
|
||||||
|
if (!_queue.length)
|
||||||
|
return;
|
||||||
sort(_queue);
|
sort(_queue);
|
||||||
size_t newsize = 0;
|
size_t newsize = _queue.length;
|
||||||
for (size_t i = _queue.length - 1; i >= 0; i--) {
|
for (int i = cast(int)_queue.length - 1; i >= 0; i--) {
|
||||||
if (!_queue[i].valid) {
|
if (!_queue[i].valid) {
|
||||||
newsize = i;
|
newsize = i;
|
||||||
}
|
}
|
||||||
|
@ -919,6 +923,8 @@ class Window {
|
||||||
_queue.length = newsize;
|
_queue.length = newsize;
|
||||||
}
|
}
|
||||||
private TimerInfo[] expired() {
|
private TimerInfo[] expired() {
|
||||||
|
if (!_queue.length)
|
||||||
|
return null;
|
||||||
long ts = currentTimeMillis;
|
long ts = currentTimeMillis;
|
||||||
TimerInfo[] res;
|
TimerInfo[] res;
|
||||||
for (int i = 0; i < _queue.length; i++) {
|
for (int i = 0; i < _queue.length; i++) {
|
||||||
|
|
Loading…
Reference in New Issue