using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using Titano.Ui.Controls; namespace Titano.Ui.Visuals; /// /// A tooltip (same style as every other) that opens at once and follows the mouse to read out the value under /// the cursor. While it shows, the owner's own tooltip (which describes the action) is suppressed. The text reads in /// the UI language's direction unless the caller pins a numeric readout left to right. /// public sealed class HoverReadout(FrameworkElement owner) { private ToolTip? _tip; private TextBlock? _text; private object? _savedEnabled; public bool IsOpen => _tip?.IsOpen == true; /// /// Shows next to (owner coordinates); empty text hides the /// readout. defaults to the UI language's. /// public void Show(string? text, Point position, FlowDirection? direction = null) { if (string.IsNullOrEmpty(text)) { Hide(); return; } if (_tip is null) { // The tip keeps the owner's direction so its placement offsets stay the owner's; only the text inside reads // right to left. The content is explicit, so the tooltip style's text settings are repeated here. _text = new TextBlock { TextWrapping = TextWrapping.Wrap, LineHeight = 16 }; _tip = new ToolTip { Placement = PlacementMode.Relative, PlacementTarget = owner, IsHitTestVisible = false, Focusable = false, FlowDirection = owner.FlowDirection, Content = _text, }; } _text!.FlowDirection = direction ?? Bidi.UiDirection; if (_text.Text != text) _text.Text = text; // Below-right of the cursor so the popup never sits under the mouse (which would end the hover). _tip.HorizontalOffset = position.X + 12; _tip.VerticalOffset = position.Y + 18; if (_tip.IsOpen) return; _savedEnabled = owner.ReadLocalValue(ToolTipService.IsEnabledProperty); ToolTipService.SetIsEnabled(owner, false); _tip.IsOpen = true; } public void Hide() { if (_tip is not { IsOpen: true }) return; _tip.IsOpen = false; if (_savedEnabled is null || _savedEnabled == DependencyProperty.UnsetValue) owner.ClearValue(ToolTipService.IsEnabledProperty); else owner.SetValue(ToolTipService.IsEnabledProperty, _savedEnabled); _savedEnabled = null; } }