Skip to content

Commit

Permalink
Account for differing Stopwatch.Frequency.
Browse files Browse the repository at this point in the history
The timer on .NET 6 on Linux is much more precise, apparently.
  • Loading branch information
madewokherd committed Feb 22, 2023
1 parent 68930d4 commit 351fcf1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 37 deletions.
4 changes: 2 additions & 2 deletions xalia/Ui/SendScroll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public SendScroll(UiDomElement element, WindowingSystem windowing) : base(elemen

private static readonly double xscale = 100.0 / 6 / 32767;
private static readonly double yscale = 100.0 / 6 / 32767;
private static readonly long delay_ticks = 10000000 / 120;
private static readonly long delay_ticks = Stopwatch.Frequency / 120;

public WindowingSystem Windowing { get; }

Expand Down Expand Up @@ -51,7 +51,7 @@ public override async Task ProcessInputQueue(InputQueue queue)
var elapsed_ticks = stopwatch.ElapsedTicks - last_repeat;
if (elapsed_ticks < delay_ticks)
{
await Task.WhenAny(queue.WaitForInput(), Task.Delay(new TimeSpan(delay_ticks - elapsed_ticks)));
await Task.WhenAny(queue.WaitForInput(), Task.Delay(TimeSpan.FromSeconds((delay_ticks - elapsed_ticks) / (double)Stopwatch.Frequency)));
continue;
}
long num_steps = elapsed_ticks / delay_ticks;
Expand Down
4 changes: 2 additions & 2 deletions xalia/UiDom/UiDomAdjustScrollbars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public UiDomAdjustScrollbars(UiDomElement hscroll, UiDomElement vscroll)

private static readonly double xscale = 1.0 / 3 / 32767;
private static readonly double yscale = 1.0 / 3 / 32767;
private static readonly long delay_ticks = 10000000 / 120;
private static readonly long delay_ticks = Stopwatch.Frequency / 120;

public override bool Equals(object obj)
{
Expand Down Expand Up @@ -88,7 +88,7 @@ public override async Task ProcessInputQueue(InputQueue queue)
var elapsed_ticks = stopwatch.ElapsedTicks - last_repeat;
if (elapsed_ticks < delay_ticks)
{
await Task.WhenAny(queue.WaitForInput(), Task.Delay(new TimeSpan(delay_ticks - elapsed_ticks)));
await Task.WhenAny(queue.WaitForInput(), Task.Delay(TimeSpan.FromSeconds((delay_ticks - elapsed_ticks) / (double)Stopwatch.Frequency)));
continue;
}
long num_steps = elapsed_ticks / delay_ticks;
Expand Down
51 changes: 20 additions & 31 deletions xalia/UiDom/UiDomRepeatAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Xalia.UiDom
{
public class UiDomRepeatAction : UiDomRoutine
{
public UiDomRepeatAction(UiDomValue context, UiDomRoot root, GudlExpression action_expression, TimeSpan initial_delay, TimeSpan repeat_delay)
public UiDomRepeatAction(UiDomValue context, UiDomRoot root, GudlExpression action_expression, long initial_delay, long repeat_delay)
{
Context = context;
Root = root;
Expand All @@ -21,8 +21,8 @@ public UiDomRepeatAction(UiDomValue context, UiDomRoot root, GudlExpression acti
public UiDomValue Context { get; }
public UiDomRoot Root { get; }
public GudlExpression ActionExpression { get; }
public TimeSpan InitialDelay { get; }
public TimeSpan RepeatDelay { get; }
public long InitialDelay { get; }
public long RepeatDelay { get; }

public static UiDomValue GetMethod()
{
Expand All @@ -40,50 +40,37 @@ private static UiDomValue ApplyFn(UiDomMethod method, UiDomValue context, GudlEx
return UiDomUndefined.Instance;

UiDomValue initial_delay = context.Evaluate(arglist[1], root, depends_on);
TimeSpan initial_delay_ts;
long initial_delay_ticks;

if (initial_delay is UiDomInt ii)
if (initial_delay.TryToDouble(out double id))
{
if (ii.Value <= 0)
if (id <= 0)
return action;
initial_delay_ts = new TimeSpan((long)(ii.Value * 10000));
}
else if (initial_delay is UiDomDouble id)
{
if (id.Value <= 0)
return action;
initial_delay_ts = new TimeSpan((long)(id.Value * 10000));
initial_delay_ticks = (long)(id * Stopwatch.Frequency / 1000);
}
else
return action;

TimeSpan repeat_delay_ts;
long repeat_delay_ticks;

if (arglist.Length >= 3)
{
UiDomValue repeat_delay = context.Evaluate(arglist[2], root, depends_on);

if (repeat_delay is UiDomInt ri)
{
if (ri.Value <= 0)
repeat_delay_ts = initial_delay_ts;
else
repeat_delay_ts = new TimeSpan((long)(ri.Value * 10000));
}
else if (repeat_delay is UiDomDouble rd)
if (repeat_delay.TryToDouble(out double rd))
{
if (rd.Value <= 0)
repeat_delay_ts = initial_delay_ts;
if (rd <= 0)
repeat_delay_ticks = initial_delay_ticks;
else
repeat_delay_ts = new TimeSpan((long)(rd.Value * 10000));
repeat_delay_ticks = (long)(rd * Stopwatch.Frequency / 1000);
}
else
repeat_delay_ts = initial_delay_ts;
repeat_delay_ticks = initial_delay_ticks;
}
else
repeat_delay_ts = initial_delay_ts;
repeat_delay_ticks = initial_delay_ticks;

return new UiDomRepeatAction(context, root, action_expr, initial_delay_ts, repeat_delay_ts);
return new UiDomRepeatAction(context, root, action_expr, initial_delay_ticks, repeat_delay_ticks);
}

public override bool Equals(object obj)
Expand Down Expand Up @@ -149,7 +136,9 @@ public override async Task ProcessInputQueue(InputQueue queue)
prev_state = state;
state = await queue.Dequeue();
if (!(inner_queue is null))
{
inner_queue.Enqueue(state);
}
if (state.Kind == InputStateKind.Disconnected)
{
break;
Expand All @@ -163,7 +152,7 @@ public override async Task ProcessInputQueue(InputQueue queue)
}
if (stopwatch.IsRunning && state.Pressed)
{
long remaining_delay = (is_initial ? InitialDelay : RepeatDelay).Ticks -
long remaining_delay = (is_initial ? InitialDelay : RepeatDelay) -
stopwatch.ElapsedTicks;
if (remaining_delay <= 0)
{
Expand All @@ -177,7 +166,7 @@ public override async Task ProcessInputQueue(InputQueue queue)
}
continue;
}
await Task.WhenAny(queue.WaitForInput(), watcher.WaitChanged(), Task.Delay(new TimeSpan(remaining_delay)));
await Task.WhenAny(queue.WaitForInput(), watcher.WaitChanged(), Task.Delay(TimeSpan.FromSeconds(remaining_delay / (double)Stopwatch.Frequency)));
continue;
}
await Task.WhenAny(queue.WaitForInput(), watcher.WaitChanged());
Expand All @@ -187,7 +176,7 @@ public override async Task ProcessInputQueue(InputQueue queue)

public override string ToString()
{
return $"{Context}.(repeat_action({ActionExpression}, {InitialDelay.Ticks / 10000.0}, {RepeatDelay.Ticks / 10000.0}))";
return $"{Context}.(repeat_action({ActionExpression}, {InitialDelay / (double)Stopwatch.Frequency * 1000}, {RepeatDelay / (double)Stopwatch.Frequency * 1000}))";
}
}
}
4 changes: 2 additions & 2 deletions xalia/Uia/UiaAdjustScrollContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public UiaAdjustScrollContainer(UiaElement element) : base(element, "uia_adjust_

private static readonly double xscale = 100.0 / 6 / 32767;
private static readonly double yscale = 100.0 / 6 / 32767;
private static readonly long delay_ticks = 10000000 / 120;
private static readonly long delay_ticks = Stopwatch.Frequency / 120;

public new UiaElement Element { get; }

Expand Down Expand Up @@ -50,7 +50,7 @@ public override async Task ProcessInputQueue(InputQueue queue)
var elapsed_ticks = stopwatch.ElapsedTicks - last_repeat;
if (elapsed_ticks < delay_ticks)
{
await Task.WhenAny(queue.WaitForInput(), Task.Delay(new TimeSpan(delay_ticks - elapsed_ticks)));
await Task.WhenAny(queue.WaitForInput(), Task.Delay(TimeSpan.FromSeconds((delay_ticks - elapsed_ticks) / (double)Stopwatch.Frequency)));
continue;
}
long num_steps = elapsed_ticks / delay_ticks;
Expand Down

0 comments on commit 351fcf1

Please sign in to comment.