using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace ClassNotify.Client.Services { public class DisplayProfile { public double HeaderHeight { get; set; } = 88; public double FooterHeight { get; set; } = 116; public double ButtonHeight { get; set; } = 54; public double ButtonWidth { get; set; } = 340; public double IconFontSize { get; set; } = 42; public double TitleFontSize { get; set; } = 32; public double ButtonFontSize { get; set; } = 24; public double CountdownFontSize { get; set; } = 16; public Thickness BodyMargin { get; set; } = new Thickness(48, 16, 48, 16); public Thickness MessageMargin { get; set; } = new Thickness(16, 8, 16, 8); public static DisplayProfile GetProfile(double screenWidth, double screenHeight) { var profile = new DisplayProfile(); // 4K Ultra-Large Educational Screens (3840x2160 or larger) if (screenHeight >= 1800) { profile.HeaderHeight = 140; profile.FooterHeight = 180; profile.ButtonHeight = 84; profile.ButtonWidth = 520; profile.IconFontSize = 64; profile.TitleFontSize = 48; profile.ButtonFontSize = 36; profile.CountdownFontSize = 24; profile.BodyMargin = new Thickness(80, 24, 80, 24); profile.MessageMargin = new Thickness(32, 16, 32, 16); } // 1080p Standard Classroom Displays (1920x1080) else if (screenHeight >= 1000) { profile.HeaderHeight = 96; profile.FooterHeight = 120; profile.ButtonHeight = 56; profile.ButtonWidth = 360; profile.IconFontSize = 44; profile.TitleFontSize = 34; profile.ButtonFontSize = 26; profile.CountdownFontSize = 18; profile.BodyMargin = new Thickness(48, 16, 48, 16); profile.MessageMargin = new Thickness(24, 12, 24, 12); } // 768p / 800p Compact Displays (1280x800, 1366x768) else { profile.HeaderHeight = 80; profile.FooterHeight = 106; profile.ButtonHeight = 50; profile.ButtonWidth = 300; profile.IconFontSize = 36; profile.TitleFontSize = 28; profile.ButtonFontSize = 22; profile.CountdownFontSize = 15; profile.BodyMargin = new Thickness(32, 10, 32, 10); profile.MessageMargin = new Thickness(16, 6, 16, 6); } return profile; } } public class TypographyResult { public double FontSize { get; set; } public double LineHeight { get; set; } public bool NeedsAutoScroll { get; set; } public double ContentHeight { get; set; } public double ViewportHeight { get; set; } } public static class NotificationLayoutEngine { public const double MinFontSize = 28.0; /// /// Precision WPF Typography Area-Fill Measurement Engine (v2): /// Enforces Dual Width + Height constraints to expand font size dynamically, /// filling 60%~78% of the available body canvas for maximum readability from the back of the classroom. /// public static TypographyResult CalculateOptimalTypography( string message, double availWidth, double availHeight, FontFamily fontFamily, FontWeight fontWeight, double windowHeight) { if (string.IsNullOrWhiteSpace(message)) { return new TypographyResult { FontSize = 64, LineHeight = 84, NeedsAutoScroll = false }; } // Absolute NaN & non-positive defense (Prevents UIElement.Measure NaN InvalidOperationException) if (double.IsNaN(availWidth) || availWidth <= 50) availWidth = 1200; if (double.IsNaN(availHeight) || availHeight <= 50) availHeight = 600; if (double.IsNaN(windowHeight) || windowHeight <= 50) windowHeight = 800; int charCount = message.Trim().Length; // Step 1: Liberated font size ceilings per character tier (P79 Architecture Guidance) double maxCandidate; if (charCount <= 8) { // Tier 1 (1~8 chars: "下楼集合", "紧急避险"): Grand, authoritative title presence maxCandidate = Math.Min(180, Math.Round(availHeight * 0.28)); } else if (charCount <= 25) { // Tier 2 (9~25 chars: e.g. "请保持教室内安静自习,班干部负责维护纪律。"): 2~3 strong lines maxCandidate = Math.Min(105, Math.Round(availHeight * 0.17)); } else if (charCount <= 60) { // Tier 3 (26~60 chars): Generous 3~4 line coverage maxCandidate = Math.Min(74, Math.Round(availHeight * 0.12)); } else if (charCount <= 120) { // Tier 4 (61~120 chars): Compact multi-line reading maxCandidate = Math.Min(48, Math.Round(availHeight * 0.08)); } else { // Tier 5 (>120 chars): High-density announcement with fallback auto-scroll maxCandidate = Math.Min(36, Math.Round(availHeight * 0.06)); } // Ensure effective available width leaves safety padding double effectiveAvailWidth = Math.Max(320, availWidth - 32); // Safe height target: 76% of available body height (leaves comfortable top/bottom buffer) double safeTargetHeight = Math.Max(120, availHeight * 0.76); double bestSize = MinFontSize; double bestMeasuredHeight = 0; // High-precision downward search with dual (Width + Height) constraints for (double testSize = maxCandidate; testSize >= MinFontSize; testSize -= 2.0) { double testLineHeight = Math.Round(testSize * 1.30); var measureBlock = new TextBlock { Text = message, FontFamily = fontFamily, FontWeight = fontWeight, FontSize = testSize, LineHeight = testLineHeight, TextWrapping = TextWrapping.Wrap, TextAlignment = TextAlignment.Center }; measureBlock.Measure(new Size(effectiveAvailWidth, double.PositiveInfinity)); double measuredHeight = measureBlock.DesiredSize.Height; double measuredWidth = measureBlock.DesiredSize.Width; // Dual Constraints: Both width and height must fit safely if (measuredHeight <= safeTargetHeight && measuredWidth <= effectiveAvailWidth) { bestSize = testSize; bestMeasuredHeight = measuredHeight; break; } } // Fallback check if even MinFontSize exceeds total available height bool needsScroll = false; if (bestSize <= MinFontSize) { var minMeasure = new TextBlock { Text = message, FontFamily = fontFamily, FontWeight = fontWeight, FontSize = MinFontSize, LineHeight = Math.Round(MinFontSize * 1.28), TextWrapping = TextWrapping.Wrap, TextAlignment = TextAlignment.Center }; minMeasure.Measure(new Size(effectiveAvailWidth, double.PositiveInfinity)); bestMeasuredHeight = minMeasure.DesiredSize.Height; if (bestMeasuredHeight > (availHeight - 20)) { needsScroll = true; } } return new TypographyResult { FontSize = bestSize, LineHeight = Math.Round(bestSize * 1.30), NeedsAutoScroll = needsScroll, ContentHeight = bestMeasuredHeight, ViewportHeight = availHeight }; } } }