Files
pokerfreak/winui/Seiten/Bausteine.cs
T
mmintelandClaude Opus 5.5 9fc61a2cec WinUI: Fehler in der Oberflaeche beenden das Cockpit nicht mehr (Handled, Klick-Abfang, absturz.txt anhaengen)
30.11. 10:10 stuerzte das Cockpit bei einem Klick ab (0x800F1000, Microsoft.UI.Xaml FailFast) und riss den
Echtgeld-Laeufer mit (Job KILL_ON_JOB_CLOSE): Tische ohne Kanal, Zuege bis 29 s spaet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 10:23:47 +01:00

391 lines
15 KiB
C#

using Microsoft.UI.Xaml;
using Pokerfreak.Cockpit.Kern;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Shapes;
using Windows.Foundation;
namespace Pokerfreak.Cockpit.Seiten;
/// <summary>Kleine Merkzettel des Fensters (gewaehlte Reiterteile, Filter): eine Zeile je Schluessel in
/// %APPDATA%\Pokerfreak\cockpit-winui.txt - wie `@AppStorage` der Mac-App.</summary>
public static class Merker
{
private static string Datei => System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Pokerfreak", "cockpit-winui.txt");
public static string? Lesen(string k)
{
try
{
foreach (var z in File.ReadAllLines(Datei))
if (z.StartsWith(k + "=")) return z[(k.Length + 1)..];
}
catch { }
return null;
}
/// <summary>Den Grund eines Fehlers neben die Merkdatei haengen (absturz.txt; nie ueberschreiben, ab 1 MB als .alt beiseite).</summary>
public static void Absturz(Exception e, bool behandelt = false)
{
try
{
var pfad = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Datei)!, "absturz.txt");
Directory.CreateDirectory(System.IO.Path.GetDirectoryName(pfad)!);
if (File.Exists(pfad) && new FileInfo(pfad).Length > 1_000_000) File.Move(pfad, pfad + ".alt", true);
File.AppendAllText(pfad, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}{(behandelt ? " (abgefangen, Cockpit laeuft weiter)" : "")}\n{e}\n\n");
}
catch { }
}
public static void Schreiben(string k, string v)
{
try
{
var zeilen = File.Exists(Datei) ? File.ReadAllLines(Datei).Where(z => !z.StartsWith(k + "=")).ToList() : new List<string>();
zeilen.Add($"{k}={v}");
Directory.CreateDirectory(System.IO.Path.GetDirectoryName(Datei)!);
File.WriteAllLines(Datei, zeilen);
}
catch { }
}
}
/// <summary>
/// Segmentierte Wahl wie `.pickerStyle(.segmented)` im Stil der Mac-App: eine Kapsel aus Weiss 6 %,
/// das gewaehlte Teil gefuellt. Merkt sich die Wahl unter `schluessel` (falls gegeben).
/// </summary>
public sealed class Segmente : UserControl
{
private readonly Border[] _teile, _striche;
private readonly TextBlock[] _texte;
private readonly string? _schluessel;
private readonly string _ton;
private int _index;
public event Action<int>? Gewaehlt;
public Segmente(string[] namen, string? schluessel = null, int vorgabe = 0, string ton = "blau", double hoehe = 28)
{
_schluessel = schluessel;
_ton = ton;
_index = vorgabe;
if (schluessel != null && int.TryParse(Merker.Lesen(schluessel), out var g) && g >= 0 && g < namen.Length) _index = g;
var raster = new Grid { ColumnSpacing = 2 };
_teile = new Border[namen.Length];
_striche = new Border[namen.Length];
_texte = new TextBlock[namen.Length];
for (var i = 0; i < namen.Length; i++)
{
raster.ColumnDefinitions.Add(new ColumnDefinition());
_texte[i] = Ui.Text(namen[i], 12);
_texte[i].HorizontalAlignment = HorizontalAlignment.Center;
_texte[i].VerticalAlignment = VerticalAlignment.Center;
_striche[i] = Strich();
var inhalt = new Grid();
inhalt.Children.Add(_texte[i]);
inhalt.Children.Add(_striche[i]);
_teile[i] = new Border { CornerRadius = new CornerRadius(4), Height = hoehe, Child = inhalt, BorderThickness = new Thickness(1) };
var n = i;
raster.Children.Add(_teile[i].Tippen(() => Waehlen(n, true), 4).Spalte(i));
}
Content = Kasten(raster);
Zeigen();
}
internal static Border Strich() => new()
{
Width = 16, Height = 3, CornerRadius = new CornerRadius(2),
HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Bottom,
};
internal static Border Kasten(UIElement raster) => new()
{
Background = Ui.Weiss(0.04),
BorderBrush = Ui.Weiss(0.07),
BorderThickness = new Thickness(1),
CornerRadius = new CornerRadius(6),
Padding = new Thickness(2),
Child = raster,
};
public int Index
{
get => _index;
set { if (value != _index) { _index = value; Zeigen(); } }
}
private void Waehlen(int i, bool melden)
{
_index = i;
if (_schluessel != null) Merker.Schreiben(_schluessel, i.ToString());
Zeigen();
if (melden) Gewaehlt?.Invoke(i);
}
private void Zeigen()
{
for (var i = 0; i < _teile.Length; i++)
{
var an = i == _index;
_teile[i].Background = an ? Ui.Weiss(0.10) : null;
_teile[i].BorderBrush = an ? Ui.Weiss(0.08) : null;
_striche[i].Background = an ? Ui.Pinsel(_ton) : null;
_texte[i].Foreground = an ? Ui.Pinsel(null) : Ui.Zweit;
}
}
}
/// <summary>Ein Menue wie SwiftUI `Menu(titel)`: kleine Kapsel mit Pfeil, "alle" setzt zurueck.</summary>
public sealed class Auswahl : UserControl
{
private readonly string _titel;
private readonly DropDownButton _knopf = new()
{
FontSize = 12,
Padding = new Thickness(10, 3, 8, 4),
CornerRadius = new CornerRadius(4),
};
private string _abdruck = "";
public string? Wahl { get; private set; }
public event Action? Geaendert;
public Auswahl(string titel)
{
_titel = titel;
Content = _knopf;
Werte(Array.Empty<string>());
}
public void Setzen(string? w)
{
Wahl = w;
_knopf.Content = w ?? _titel;
_knopf.Foreground = w == null ? Ui.Zweit : Ui.Akzent;
}
public void Werte(IEnumerable<string> werte)
{
var liste = werte.ToList();
var abdruck = string.Join("|", liste);
if (abdruck == _abdruck && _knopf.Flyout != null) return;
_abdruck = abdruck;
var menue = new MenuFlyout();
var alle = new MenuFlyoutItem { Text = "alle" };
alle.Click += (_, _) => { Setzen(null); Geaendert?.Invoke(); };
menue.Items.Add(alle);
foreach (var w in liste)
{
var e = new MenuFlyoutItem { Text = w };
e.Click += (_, _) => { Setzen(w); Geaendert?.Invoke(); };
menue.Items.Add(e);
}
_knopf.Flyout = menue;
_knopf.IsEnabled = liste.Count > 0;
Setzen(Wahl);
}
}
/// <summary>Bausteine der Auswertungsseiten (Bausteine.swift `Kennzahl`, `Leer`).</summary>
public static class Teile
{
/// <summary>Eine Kennzahl: Titel klein, Wert gross, auf heller Flaeche.</summary>
public static Border Kennzahl(string titel, string wert, string? ton = null)
{
var s = Ui.Spalte(2, Ui.Winzig(titel), Ui.Gross(wert, 18, ton));
return Ui.Flaeche(s, 12, 0.06, 10).Mit(b => b.HorizontalAlignment = HorizontalAlignment.Stretch);
}
/// <summary>Platzhalter mittig: Symbol, Titel, Satz.</summary>
public static StackPanel Leer(string titel, string text, string glyph = "")
{
var s = Ui.Spalte(6,
Ui.Zeichen(glyph, 28, "grau").Mit(z => z.HorizontalAlignment = HorizontalAlignment.Center),
Ui.Text(titel, 15, fett: true).Mit(t => t.HorizontalAlignment = HorizontalAlignment.Center),
Ui.Klein(text, true).Mit(t => { t.TextAlignment = TextAlignment.Center; t.HorizontalAlignment = HorizontalAlignment.Center; }));
s.HorizontalAlignment = HorizontalAlignment.Center;
s.VerticalAlignment = VerticalAlignment.Center;
s.Margin = new Thickness(24, 40, 24, 40);
s.MaxWidth = 320;
return s;
}
/// <summary>Ergebnisfarbe wie `Farben.ergebnis`: gruen &gt; 0, rot &lt; 0, sonst Text.</summary>
public static string? Ton(double? x) => x is double v ? (v > 0 ? "gruen" : v < 0 ? "rot" : null) : null;
/// <summary>Kleine Textknopf-Kapsel (`.buttonStyle(.borderless)` bzw. `.borderedProminent`).</summary>
public static Tippflaeche Knopf(string text, Action tun, string? ton = null, bool voll = false)
{
var b = new Border
{
CornerRadius = new CornerRadius(4),
Padding = new Thickness(11, 4, 11, 5),
BorderBrush = Ui.Weiss(0.08),
BorderThickness = new Thickness(1),
Background = voll ? Ui.Pinsel(ton ?? "blau") : Ui.Weiss(0.06),
Child = Ui.Text(text, 12, fett: voll, pinsel: voll ? Ui.Schwarz(1) : ton != null ? Ui.Pinsel(ton) : Ui.Akzent),
};
return b.Tippen(tun, 4);
}
}
/// <summary>
/// Ein Diagramm auf einer Leinwand, das bei jeder Groessenaenderung neu zeichnet. Die Zeichenfunktion
/// bekommt Leinwand, Breite und Hoehe; die Helfer unten zeichnen Linien, Saeulen und Punkte wie Swift Charts
/// in schlicht: leise Gitterlinien, Achsenbeschriftung klein in .tertiary.
/// </summary>
public sealed class Diagramm : Grid
{
private readonly Canvas _leinwand = new();
private readonly Canvas _zeiger = new() { IsHitTestVisible = false };
private Action<Canvas, double, double>? _zeichnen;
private Action<Canvas, double, double, Point>? _zeigen;
public Diagramm(double hoehe = 220)
{
Height = hoehe;
Children.Add(_leinwand);
Children.Add(_zeiger);
SizeChanged += (_, _) => Neu();
}
public Action<Canvas, double, double>? Zeichnen
{
set { _zeichnen = value; Neu(); }
}
/// <summary>Fadenkreuz/Wertfenster: zeichnet beim Zeigen auf eine eigene, leichte Ebene ueber dem Diagramm.</summary>
public Action<Canvas, double, double, Point>? Zeiger
{
set
{
if (_zeigen == null && value != null)
{
Background = new SolidColorBrush(Microsoft.UI.Colors.Transparent);
PointerMoved += (_, e) =>
{
_zeiger.Children.Clear();
if (_zeigen != null && ActualWidth > 20) _zeigen(_zeiger, ActualWidth, ActualHeight, e.GetCurrentPoint(this).Position);
};
PointerExited += (_, _) => _zeiger.Children.Clear();
}
_zeigen = value;
}
}
private void Neu()
{
_leinwand.Children.Clear();
_zeiger.Children.Clear();
if (_zeichnen != null && ActualWidth > 20) _zeichnen(_leinwand, ActualWidth, ActualHeight);
}
public const double Links = 40, Unten = 18;
/// <summary>Achse y mit 4 Gitterlinien von lo bis hi; gibt die Abbildung Wert -> y zurueck.</summary>
public static Func<double, double> AchseY(Canvas c, double b, double h, double lo, double hi, string einheit = "")
{
if (hi - lo < 1e-9) { lo -= 1; hi += 1; }
var oben = 6.0;
double Y(double v) => oben + (h - Unten - oben) * (1 - (v - lo) / (hi - lo));
foreach (var v in RundeWerte(lo, hi))
{
var y = Y(v);
c.Children.Add(new Line { X1 = Links, X2 = b, Y1 = y, Y2 = y, Stroke = Ui.Weiss(0.07), StrokeThickness = 1 });
var t = Ui.Text(Format.Kurzzahl(v) + einheit, 10, pinsel: Ui.Dritt);
t.Width = Links - 6;
t.TextAlignment = TextAlignment.Right;
Canvas.SetLeft(t, 0);
Canvas.SetTop(t, y - 7);
c.Children.Add(t);
}
if (lo < 0 && hi > 0)
c.Children.Add(new Line { X1 = Links, X2 = b, Y1 = Y(0), Y2 = Y(0), Stroke = Ui.Weiss(0.25), StrokeThickness = 1 });
return Y;
}
/// <summary>Gitterwerte auf runden Schritten (1, 2, 2,5, 5 x 10^n), etwa vier bis sechs im Bereich.</summary>
public static IEnumerable<double> RundeWerte(double lo, double hi)
{
var roh = (hi - lo) / 4;
var zehn = Math.Pow(10, Math.Floor(Math.Log10(roh)));
var schritt = new[] { 1, 2, 2.5, 5, 10 }.Select(f => f * zehn).First(x => x >= roh * 0.999);
for (var v = Math.Ceiling(lo / schritt - 1e-9) * schritt; v <= hi + schritt * 1e-9; v += schritt)
yield return Math.Abs(v) < schritt * 1e-9 ? 0 : v;
}
public static void Beschriftung(Canvas c, string text, double x, double y, double breite = 60)
{
var t = Ui.Text(text, 10, pinsel: Ui.Dritt);
t.Width = breite;
t.TextAlignment = TextAlignment.Center;
Canvas.SetLeft(t, x - breite / 2);
Canvas.SetTop(t, y);
c.Children.Add(t);
}
/// <summary>Linie mit Flaeche darunter (LineMark + AreaMark in Tint 12 %).</summary>
public static void Linie(Canvas c, double b, double h, IList<(double X, double Y)> punkte, string ton = "blau",
Func<double, string>? xText = null, bool flaeche = true, double? lo = null, double? hi = null)
{
if (punkte.Count == 0) return;
var ylo = lo ?? Math.Min(0, punkte.Min(p => p.Y));
var yhi = hi ?? Math.Max(0, punkte.Max(p => p.Y));
var Y = AchseY(c, b, h, ylo, yhi);
double x0 = punkte[0].X, x1 = punkte[^1].X;
if (x1 - x0 < 1e-9) x1 = x0 + 1;
double X(double v) => Links + 4 + (b - Links - 8) * (v - x0) / (x1 - x0);
var linie = new Polyline { Stroke = Ui.Pinsel(ton), StrokeThickness = 2, StrokeLineJoin = PenLineJoin.Round };
foreach (var p in punkte) linie.Points.Add(new Point(X(p.X), Y(p.Y)));
if (flaeche)
{
var f = new Polygon { Fill = Ui.Pinsel(ton, 0.12) };
var basis = Y(Math.Max(ylo, Math.Min(yhi, 0)));
f.Points.Add(new Point(X(punkte[0].X), basis));
foreach (var p in punkte) f.Points.Add(new Point(X(p.X), Y(p.Y)));
f.Points.Add(new Point(X(punkte[^1].X), basis));
c.Children.Add(f);
}
c.Children.Add(linie);
if (punkte.Count <= 40)
foreach (var p in punkte)
{
var e = new Ellipse { Width = 5, Height = 5, Fill = Ui.Pinsel(ton) };
Canvas.SetLeft(e, X(p.X) - 2.5);
Canvas.SetTop(e, Y(p.Y) - 2.5);
c.Children.Add(e);
}
if (xText != null)
{
Beschriftung(c, xText(x0), Links + 30, h - Unten + 3);
Beschriftung(c, xText(x1), b - 30, h - Unten + 3);
}
}
/// <summary>Saeulen (BarMark): Werte mit Farbe und Beschriftung unten.</summary>
public static void Saeulen(Canvas c, double b, double h, IList<(string Name, double Wert, string Ton)> saeulen,
int beschriftungJede = 1)
{
if (saeulen.Count == 0) return;
var Y = AchseY(c, b, h, 0, Math.Max(1, saeulen.Max(s => s.Wert)));
var breite = (b - Links - 4) / saeulen.Count;
for (var i = 0; i < saeulen.Count; i++)
{
var (name, wert, ton) = saeulen[i];
var x = Links + 4 + i * breite;
var r = new Rectangle
{
Width = Math.Max(1, breite - 3),
Height = Math.Max(0, Y(0) - Y(wert)),
Fill = Ui.Pinsel(ton, 0.7),
RadiusX = 2,
RadiusY = 2,
};
Canvas.SetLeft(r, x + 1);
Canvas.SetTop(r, Y(wert));
c.Children.Add(r);
if (i % beschriftungJede == 0) Beschriftung(c, name, x + breite / 2, h - Unten + 3, Math.Max(breite * beschriftungJede, 30));
}
}
}