timers, part 1

This commit is contained in:
Vadim Lopatin 2015-01-28 13:46:54 +03:00
parent 9510ec4c93
commit b1a9a950c8
4 changed files with 53 additions and 2 deletions

View File

@ -66,7 +66,7 @@
<debuglevel>0</debuglevel>
<debugids>DebugFocus</debugids>
<versionlevel>0</versionlevel>
<versionids>EmbedStandardResources Unicode</versionids>
<versionids>EmbedStandardResources Unicode USE_SDL USE_FREETYPE</versionids>
<dump_source>0</dump_source>
<mapverbosity>0</mapverbosity>
<createImplib>1</createImplib>

View File

@ -51,7 +51,7 @@ enum LogLevel : int {
}
/// Returns timestamp in milliseconds since 1970 UTC similar to Java System.currentTimeMillis()
long currentTimeMillis() {
@property long currentTimeMillis() {
return std.datetime.Clock.currStdTime / 10000;
}

View File

@ -86,6 +86,51 @@ class EventList {
}
}
class TimerInfo {
static ulong nextId;
this(Widget targetWidget, long intervalMillis) {
_id = ++nextId;
assert(intervalMillis >= 0 && intervalMillis < 7*24*60*60*1000L);
_targetWidget = targetWidget;
_interval = intervalMillis;
_nextTimestamp = currentTimeMillis + _interval;
}
/// cancel timer
void cancel() {
_targetWidget = null;
}
/// cancel timer
void notify() {
if (_targetWidget) {
_nextTimestamp = currentTimeMillis + _interval;
if (!_targetWidget.onTimer(_id)) {
_targetWidget = null;
}
}
}
/// unique Id of timer
@property ulong id() { return _id; }
/// timer interval, milliseconds
@property long interval() { return _interval; }
/// next timestamp to invoke timer at, as per currentTimeMillis()
@property long nextTimestamp() { return _nextTimestamp; }
/// widget to route timer event to
@property Widget targetWidget() { return _targetWidget; }
protected ulong _id;
protected long _interval;
protected long _nextTimestamp;
protected Widget _targetWidget;
}
class TimerQueue {
protected TimerInfo[] _queue;
void add(TimerInfo event) {
int len = cast(int)_queue.length;
}
}
/**
* Window abstraction layer. Widgets can be shown only inside window.
*

View File

@ -923,6 +923,12 @@ class Widget {
return res;
}
/// handle timer; return true to repeat timer event after next interval, false cancel timer
bool onTimer(ulong id) {
// override to do something useful
return false;
}
/// map key to action
Action findKeyAction(uint keyCode, uint flags) {
Action action = _acceleratorMap.findByKey(keyCode, flags);