Files
elwig/Elwig/Windows/ContextWindow.cs
Lorenz Stechauner f96ebdcf60
All checks were successful
Test / Run tests (push) Successful in 2m2s
DeliveryAdminWindow: Fix creation of new deliveries
2026-04-02 20:58:01 +02:00

70 lines
2.2 KiB
C#

using Elwig.Helpers;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace Elwig.Windows {
public abstract class ContextWindow : Window {
private bool _lockContext = false;
protected bool LockContext {
get => _lockContext;
set {
_lockContext = value;
if (!_lockContext && _renewPending) {
Dispatcher.BeginInvoke(async () => await EnsureContextRenewed());
}
}
}
private bool _renewPending = false;
private readonly RoutedCommand CtrlR = new("CtrlR", typeof(ContextWindow), [new KeyGesture(Key.R, ModifierKeys.Control)]);
private readonly RoutedCommand F5 = new("F5", typeof(ContextWindow), [new KeyGesture(Key.F5)]);
public ContextWindow() : base() {
CommandBindings.Add(new CommandBinding(CtrlR, ForceContextReload));
CommandBindings.Add(new CommandBinding(F5, ForceContextReload));
Loaded += OnLoaded;
}
public async void ForceContextReload(object sender, EventArgs evt) {
await ForceContextReload();
}
public async Task ForceContextReload() {
HintContextChange();
await TryContextReload();
}
public void HintContextChange() {
_renewPending = true;
}
public async Task TryContextReload() {
if (LockContext) return;
await EnsureContextRenewed();
}
protected async void OnLoaded(object? sender, RoutedEventArgs? evt) {
Mouse.OverrideCursor = Cursors.AppStarting;
using var ctx = new AppDbContext();
await OnRenewContext(ctx);
await OnInit(ctx);
Mouse.OverrideCursor = null;
}
protected async Task EnsureContextRenewed() {
if (!_renewPending) return;
_renewPending = false;
using var ctx = new AppDbContext();
await OnRenewContext(ctx);
}
virtual protected async Task OnInit(AppDbContext ctx) { }
abstract protected Task OnRenewContext(AppDbContext ctx);
}
}