This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
UpTool2/UpToolEto/UpToolEto/Controls/AppControlButton.cs

65 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Eto.Drawing;
using Eto.Forms;
using UpToolLib.v2;
using UpToolLib.v2.TaskQueue;
namespace UpToolEto.Controls
{
public abstract class AppControlButton : Button
{
public abstract void ReloadState();
public abstract void SetApp(App app);
public abstract void Clear();
}
public class AppControlButton<T> : AppControlButton where T : KnownAppTask
{
private readonly IList<AppTask> _tasks;
private readonly Func<App, bool> _enabledCheck;
private readonly Color _defaultColor;
private App _app;
public AppControlButton(string text, Func<App, Action?, AppTask> factory, IList<AppTask> tasks, Action reloadState, Func<App, bool> enabledCheck)
{
_tasks = tasks;
_enabledCheck = enabledCheck;
_defaultColor = BackgroundColor;
Text = text;
Click += (_, _) =>
{
bool found = false;
for (var i = tasks.ToArray().Length - 1; i >= 0; i--)
{
if (tasks[i] is T t && t.App == _app)
{
found = true;
tasks.RemoveAt(i);
}
}
if (!found)
tasks.Add(factory(_app, ReloadState));
reloadState();
};
}
public override void ReloadState()
{
Enabled = _enabledCheck(_app);
BackgroundColor = _tasks.Any(s => s is T t && t.App == _app) ? Colors.Green : _defaultColor;
}
public override void SetApp(App app)
{
_app = app;
ReloadState();
}
public override void Clear()
{
Enabled = false;
}
}
}