Force rerender on visibility change

This commit is contained in:
2023-08-12 09:09:17 +02:00
parent 1fde0df2d6
commit 16bdc1ed40
20 changed files with 292 additions and 147 deletions

View File

@@ -1,86 +1,32 @@
using TerminalUI.Controls;
using TerminalUI.Models;
namespace TerminalUI;
namespace TerminalUI;
public class EventLoop : IEventLoop
{
private readonly IApplicationContext _applicationContext;
private readonly object _lock = new();
private readonly List<IView> _viewsToRender = new();
private bool _rerenderRequested;
private bool _lastCursorVisible;
private readonly List<Action> _permanentQueue = new();
public EventLoop(IApplicationContext applicationContext)
{
_applicationContext = applicationContext;
}
public void AddToPermanentQueue(Action action) => _permanentQueue.Add(action);
public void Run()
{
_applicationContext.IsRunning = true;
_rerenderRequested = true;
while (_applicationContext.IsRunning)
{
Render();
ProcessQueues();
Thread.Sleep(10);
}
}
public void RequestRerender()
private void ProcessQueues()
{
lock (_lock)
foreach (var action in _permanentQueue)
{
_rerenderRequested = true;
}
}
public void Render()
{
List<IView> viewsToRender;
lock (_lock)
{
if (!_rerenderRequested) return;
_rerenderRequested = false;
viewsToRender = _viewsToRender.ToList();
}
var driver = _applicationContext.ConsoleDriver;
var size = driver.GetWindowSize();
var renderContext = new RenderContext(
driver,
false,
null,
null
);
foreach (var view in viewsToRender)
{
view.Attached = true;
view.GetRequestedSize();
view.Render(renderContext, new Position(0, 0), size);
}
if (_applicationContext.FocusManager.Focused is { } focused)
{
focused.SetCursorPosition(driver);
if (!_lastCursorVisible)
{
driver.SetCursorVisible(true);
_lastCursorVisible = true;
}
}
else if (_lastCursorVisible)
{
driver.SetCursorVisible(false);
_lastCursorVisible = false;
}
}
public void AddViewToRender(IView view)
{
lock (_lock)
{
_viewsToRender.Add(view);
action();
}
}
}