| 1 | #nullable enable |
| 2 | |
| 3 | using Avalonia; |
| 4 | using Avalonia.Controls; |
| 5 | using Avalonia.Media; |
| 6 | using Avalonia.VisualTree; |
| 7 | using CascadeIDE.Models; |
| 8 | |
| 9 | namespace CascadeIDE.Views.Chat; |
| 10 | |
| 11 | /// <summary>Применяет <c>[fonts.intercom]</c> к Avalonia-элементам MFD Chat (заголовки, spine, уточнения).</summary> |
| 12 | internal static class ChatPanelTypographyApplier |
| 13 | { |
| 14 | public const string TitleClass = "chatPanelTitle"; |
| 15 | public const string SubtitleClass = "chatPanelSubtitle"; |
| 16 | public const string LabelClass = "chatPanelLabel"; |
| 17 | public const string BodyClass = "chatPanelBody"; |
| 18 | public const string InputClass = "chatPanelInput"; |
| 19 | |
| 20 | public static void Apply(Control root, IntercomFontsSettings fonts) |
| 21 | { |
| 22 | var family = new FontFamily(fonts.ResolveProseFamily()); |
| 23 | var titlePt = fonts.ResolvePanelTitlePt(); |
| 24 | var subtitlePt = fonts.ResolvePanelSubtitlePt(); |
| 25 | var labelPt = fonts.ResolvePanelLabelPt(); |
| 26 | var bodyPt = fonts.ResolvePanelBodyPt(); |
| 27 | var inputPt = fonts.ResolvePanelInputPt(); |
| 28 | |
| 29 | foreach (var control in root.GetVisualDescendants().OfType<Control>()) |
| 30 | { |
| 31 | if (control is TextBlock tb) |
| 32 | ApplyTextBlock(tb, family, titlePt, subtitlePt, labelPt, bodyPt); |
| 33 | else if (control is TextBox box) |
| 34 | ApplyTextBox(box, family, inputPt); |
| 35 | else if (control is ComboBox combo && control.Classes.Contains(InputClass)) |
| 36 | ApplyComboBox(combo, family, inputPt); |
| 37 | else if (control is Expander expander && control.Classes.Contains(LabelClass)) |
| 38 | { |
| 39 | expander.FontFamily = family; |
| 40 | expander.FontSize = labelPt; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | private static void ApplyTextBlock( |
| 46 | TextBlock tb, |
| 47 | FontFamily family, |
| 48 | float titlePt, |
| 49 | float subtitlePt, |
| 50 | float labelPt, |
| 51 | float bodyPt) |
| 52 | { |
| 53 | tb.FontFamily = family; |
| 54 | if (tb.Classes.Contains(TitleClass)) |
| 55 | tb.FontSize = titlePt; |
| 56 | else if (tb.Classes.Contains(SubtitleClass)) |
| 57 | tb.FontSize = subtitlePt; |
| 58 | else if (tb.Classes.Contains(LabelClass)) |
| 59 | tb.FontSize = labelPt; |
| 60 | else if (tb.Classes.Contains(BodyClass)) |
| 61 | tb.FontSize = bodyPt; |
| 62 | } |
| 63 | |
| 64 | private static void ApplyTextBox(TextBox box, FontFamily family, float inputPt) |
| 65 | { |
| 66 | if (!box.Classes.Contains(InputClass)) |
| 67 | return; |
| 68 | |
| 69 | box.FontFamily = family; |
| 70 | box.FontSize = inputPt; |
| 71 | var scale = inputPt / 12f; |
| 72 | if (box.MinHeight > 0 && box.MinHeight < 200) |
| 73 | box.MinHeight = Math.Max(box.MinHeight, 32f * scale); |
| 74 | } |
| 75 | |
| 76 | private static void ApplyComboBox(ComboBox combo, FontFamily family, float inputPt) |
| 77 | { |
| 78 | combo.FontFamily = family; |
| 79 | combo.FontSize = inputPt; |
| 80 | } |
| 81 | } |
| 82 | |