31 lines
1.3 KiB
C#
31 lines
1.3 KiB
C#
|
|
using System.Globalization;
|
||
|
|
using System.Windows.Data;
|
||
|
|
|
||
|
|
namespace TeamsISO.App.Converters;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Converts a display name to up to two uppercase initials for an avatar bubble.
|
||
|
|
/// "Brendon Power" → "BP". "(Local)" → "L". Falls back to "·" for empty inputs.
|
||
|
|
/// </summary>
|
||
|
|
public sealed class InitialsConverter : IValueConverter
|
||
|
|
{
|
||
|
|
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||
|
|
{
|
||
|
|
var s = value as string;
|
||
|
|
if (string.IsNullOrWhiteSpace(s)) return "·";
|
||
|
|
|
||
|
|
// Strip surrounding parens / punctuation that would otherwise become
|
||
|
|
// useless initials (e.g. "(Local)" should yield "L", not "(").
|
||
|
|
var cleaned = new string(s.Where(c => char.IsLetterOrDigit(c) || char.IsWhiteSpace(c)).ToArray()).Trim();
|
||
|
|
if (cleaned.Length == 0) return "·";
|
||
|
|
|
||
|
|
var parts = cleaned.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||
|
|
if (parts.Length == 0) return "·";
|
||
|
|
if (parts.Length == 1) return char.ToUpperInvariant(parts[0][0]).ToString();
|
||
|
|
return $"{char.ToUpperInvariant(parts[0][0])}{char.ToUpperInvariant(parts[^1][0])}";
|
||
|
|
}
|
||
|
|
|
||
|
|
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||
|
|
=> throw new NotSupportedException();
|
||
|
|
}
|