using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; using System.Windows.Threading; using Titano.App.Library; using Titano.Ui.Visuals; namespace Titano.App.Sections; public partial class LibraryView : UserControl { public LibraryView() { InitializeComponent(); DataContextChanged += OnDataContextChanged; } private LibraryViewModel? Model => DataContext as LibraryViewModel; /// Loads the next page when the grid or the list scrolls within two screens of its end. private void OnScrollChanged(object sender, ScrollChangedEventArgs e) { if (Model is not { } vm || e.ExtentHeight <= 0) return; if (e.VerticalOffset + e.ViewportHeight >= e.ExtentHeight - 2 * Math.Max(e.ViewportHeight, 200)) _ = vm.LoadMoreAsync(); } /// A month of the timeline: the Library narrows to its year and the grid scrolls to it. private void OnSlotActivated(object sender, ChartItemEventArgs e) => Model?.ShowSlot(e.Row, e.Column); /// "Saved views" and "Rate ▾" open their menu under themselves. private void OnOpenMenu(object sender, RoutedEventArgs e) { if (sender is not Button { ContextMenu: { } menu } button) return; menu.DataContext = DataContext; menu.PlacementTarget = button; menu.Placement = PlacementMode.Bottom; menu.IsOpen = true; } private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { if (e.OldValue is LibraryViewModel old) { old.ScrollRequested -= OnScrollRequested; old.PropertyChanged -= OnModelChanged; } if (e.NewValue is LibraryViewModel model) { model.ScrollRequested += OnScrollRequested; model.PropertyChanged += OnModelChanged; } } private void OnScrollRequested(SequenceItem item) => Dispatcher.BeginInvoke(DispatcherPriority.Loaded, () => CardGrid.ScrollIntoView(item)); /// A dialog that opens takes the keyboard: its first field or button. private void OnModelChanged(object? sender, PropertyChangedEventArgs e) { if (e.PropertyName != nameof(LibraryViewModel.Dialog) || Model?.Dialog is null || !IsVisible) return; Dispatcher.BeginInvoke(DispatcherPriority.Input, () => DialogHost.MoveFocus(new TraversalRequest(FocusNavigationDirection.First))); } }