Merge branch 'master' into idisposable

This commit is contained in:
Charlie Kindel
2020-06-23 06:57:15 -07:00
5 changed files with 36 additions and 43 deletions

View File

@@ -168,45 +168,38 @@ namespace Terminal.Gui {
bool IMainLoopDriver.EventsPending (bool wait)
{
int pollTimeout = 0;
int n;
if (CkeckTimeout (wait, ref pollTimeout))
if (CheckTimers (wait, out var pollTimeout)) {
return true;
}
UpdatePollMap ();
while (true) {
n = poll (pollmap, (uint)pollmap.Length, 0);
if (pollmap != null) {
break;
}
if (mainLoop.idleHandlers.Count > 0 || CkeckTimeout (wait, ref pollTimeout)) {
return true;
}
}
int ic;
lock (mainLoop.idleHandlers)
ic = mainLoop.idleHandlers.Count;
return n > 0 || mainLoop.timeouts.Count > 0 && ((mainLoop.timeouts.Keys [0] - DateTime.UtcNow.Ticks) < 0) || ic > 0;
var n = poll (pollmap, (uint)pollmap.Length, pollTimeout);
return n > 0 || CheckTimers (wait, out pollTimeout);
}
bool CkeckTimeout (bool wait, ref int pollTimeout)
bool CheckTimers (bool wait, out int pollTimeout)
{
long now = DateTime.UtcNow.Ticks;
if (mainLoop.timeouts.Count > 0) {
pollTimeout = (int)((mainLoop.timeouts.Keys [0] - now) / TimeSpan.TicksPerMillisecond);
if (pollTimeout < 0)
if (pollTimeout < 0) {
return true;
}
} else
pollTimeout = -1;
if (!wait)
pollTimeout = 0;
return false;
int ic;
lock (mainLoop.idleHandlers) {
ic = mainLoop.idleHandlers.Count;
}
return ic > 0;
}
void IMainLoopDriver.MainIteration ()

View File

@@ -615,24 +615,16 @@ namespace Terminal.Gui {
bool IMainLoopDriver.EventsPending (bool wait)
{
int waitTimeout = 0;
if (CkeckTimeout (wait, ref waitTimeout))
if (CheckTimers (wait, out var waitTimeout)) {
return true;
}
result = null;
waitForProbe.Set ();
try {
while (result == null) {
if (!tokenSource.IsCancellationRequested)
eventReady.Wait (0, tokenSource.Token);
if (result != null) {
break;
}
if (mainLoop.idleHandlers.Count > 0 || CkeckTimeout (wait, ref waitTimeout)) {
return true;
}
if (!tokenSource.IsCancellationRequested) {
eventReady.Wait (waitTimeout, tokenSource.Token);
}
} catch (OperationCanceledException) {
return true;
@@ -640,15 +632,16 @@ namespace Terminal.Gui {
eventReady.Reset ();
}
if (!tokenSource.IsCancellationRequested)
return result != null;
if (!tokenSource.IsCancellationRequested) {
return result != null || CheckTimers (wait, out waitTimeout);
}
tokenSource.Dispose ();
tokenSource = new CancellationTokenSource ();
return true;
}
bool CkeckTimeout (bool wait, ref int waitTimeout)
bool CheckTimers (bool wait, out int waitTimeout)
{
long now = DateTime.UtcNow.Ticks;
@@ -663,7 +656,12 @@ namespace Terminal.Gui {
if (!wait)
waitTimeout = 0;
return false;
int ic;
lock (mainLoop.idleHandlers) {
ic = mainLoop.idleHandlers.Count;
}
return ic > 0;
}
Action<KeyEvent> keyHandler;

View File

@@ -95,9 +95,11 @@ namespace Terminal.Gui {
/// <param name="idleHandler">Token that can be used to remove the idle handler with <see cref="RemoveIdle(Func{bool})"/> .</param>
public Func<bool> AddIdle (Func<bool> idleHandler)
{
lock (idleHandlers)
lock (idleHandlers) {
idleHandlers.Add (idleHandler);
}
Driver.Wakeup ();
return idleHandler;
}

View File

@@ -285,7 +285,7 @@ namespace Terminal.Gui {
for (int row = 0; row < f.Height; row++, item++) {
bool isSelected = item == selected;
var newcolor = focused ? (isSelected ? ColorScheme.Focus : ColorScheme.Normal) : ColorScheme.Normal;
var newcolor = focused ? (isSelected ? ColorScheme.Focus : ColorScheme.Normal) : (isSelected ? ColorScheme.HotNormal : ColorScheme.Normal);
if (newcolor != current) {
Driver.SetAttribute (newcolor);
current = newcolor;

View File

@@ -47,8 +47,8 @@ namespace UICatalog {
var lbl = new Label (1, 1, "Tick every (ms):");
LeftFrame.Add (lbl);
Speed = new TextField ("") {
X = Pos.Right (lbl) + 1,
Y = Pos.Y (lbl),
X = Pos.X (lbl),
Y = Pos.Bottom (lbl),
Width = 7,
};
LeftFrame.Add (Speed);
@@ -134,9 +134,9 @@ namespace UICatalog {
}
private Timer _systemTimer = null;
private uint _systemTimerTick = 1000; // ms
private uint _systemTimerTick = 100; // ms
private object _mainLoopTimeout = null;
private uint _mainLooopTimeoutTick = 1000; // ms
private uint _mainLooopTimeoutTick = 100; // ms
public override void Setup ()
{
// Demo #1 - Use System.Timer (and threading)