| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- using System.Runtime.InteropServices;
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Controls.ApplicationLifetimes;
- using Avalonia.Input;
- using Avalonia.Media;
- using PicView.Avalonia.Input;
- using PicView.Avalonia.UI;
- using PicView.Avalonia.ViewModels;
- using PicView.Avalonia.WindowBehavior;
- using PicView.Core.Config;
- using PicView.Core.Keybindings;
- using PicView.Core.Sizing;
- using PicView.Core.ViewModels;
- using R3;
- // ReSharper disable CompareOfFloatsByEqualityOperator
- namespace PicView.Avalonia.Views;
- public partial class SettingsView : UserControl
- {
- #region Fields
- private static CompositeDisposable? _marginSubscription;
- private readonly Stack<TabItem?> _backStack = new();
- private readonly Stack<TabItem?> _forwardStack = new();
- private TabItem? _currentTab;
- #endregion
- #region Constructor
- public SettingsView()
- {
- InitializeComponent();
- if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
- {
- FileAssociationsTabItem.IsEnabled = false;
- }
- Loaded += OnLoaded;
- }
- #endregion
- #region Properties
- private MainViewModel? ViewModel => DataContext as MainViewModel;
- #endregion
- #region Initialization
- private void OnLoaded(object? sender, EventArgs e)
- {
- if (DataContext is not MainViewModel)
- {
- return;
- }
- InitializeViewModel();
- SetupUI();
- AttachEventHandlers();
- LoadInitialSettings();
- }
- private void InitializeViewModel()
- {
- if (ViewModel is not { } vm)
- {
- return;
- }
- var settingsVm = vm.SettingsViewModel;
- Task.Run(() =>
- {
- settingsVm.InitializeNavigation(GoBack, GoForward);
- settingsVm.SubscriptionSettingsUpdate();
- });
- SubscribeToChanges(vm, settingsVm);
- }
- private static void SubscribeToChanges(MainViewModel vm, SettingsViewModel settingsVm)
- {
- _marginSubscription = new CompositeDisposable();
- Observable.EveryValueChanged(settingsVm.WindowMargin, x => x.CurrentValue, UIHelper.GetFrameProvider)
- .Skip(1)
- .Subscribe(x =>
- {
- Settings.WindowProperties.Margin = x;
- WindowResizing.SetSize(vm.PicViewer.PixelWidth.CurrentValue, vm.PicViewer.PixelHeight.CurrentValue, 0,
- 0, vm.GlobalSettings.RotationAngle.CurrentValue, vm);
- WindowFunctions.CenterWindowOnScreen();
- }).AddTo(_marginSubscription);
- }
- private void LoadInitialSettings()
- {
- Task.Run(() =>
- {
- if (string.IsNullOrWhiteSpace(SettingsConfiguration.CurrentUserSettingsPath))
- {
- _ = SaveSettingsAsync();
- }
- if (string.IsNullOrWhiteSpace(KeybindingFunctions.CurrentKeybindingsPath))
- {
- _ = KeybindingManager.UpdateKeyBindingsFile();
- }
- });
- }
- #endregion
- #region UI Setup and Event Handlers
- private void SetupUI()
- {
- Height = MainTabControl.MaxHeight = ScreenHelper.GetWindowMaxHeight() - SizeDefaults.TopBorderHeight;
- if (!Settings.Theme.Dark)
- {
- MainTabControl.Background = Brushes.Transparent;
- }
- }
- private void AttachEventHandlers()
- {
- MainTabControl.SelectionChanged += OnTabSelectionChanged;
- PointerPressed += OnPointerPressed;
- }
- protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
- {
- base.OnDetachedFromVisualTree(e);
- Disposable.Dispose(_marginSubscription, ViewModel.SettingsViewModel);
- ViewModel.SettingsViewModel = null;
- }
- #endregion
- #region Navigation
- private void OnTabSelectionChanged(object? sender, SelectionChangedEventArgs e)
- {
- if (MainTabControl.SelectedItem is TabItem selectedTab && _currentTab != selectedTab)
- {
- HandleTabSelection(selectedTab);
- }
- }
- private void HandleTabSelection(TabItem selectedTab)
- {
- if (_currentTab != null)
- {
- _backStack.Push(_currentTab);
- _forwardStack.Clear(); // Clear forward history
- }
- _currentTab = selectedTab;
- SelectTab(_currentTab);
- UpdateNavigationButtons();
- }
- public void GoBack()
- {
- if (!_backStack.TryPop(out var previousTab))
- {
- return;
- }
- _forwardStack.Push(_currentTab);
- _currentTab = previousTab;
- SelectTab(_currentTab);
- UpdateNavigationButtons();
- }
- public void GoForward()
- {
- if (!_forwardStack.TryPop(out var nextTab))
- {
- return;
- }
- _backStack.Push(_currentTab);
- _currentTab = nextTab;
- SelectTab(_currentTab);
- UpdateNavigationButtons();
- }
- private void SelectTab(TabItem? tab)
- {
- MainTabControl.SelectedItem = tab;
- }
- private void UpdateNavigationButtons()
- {
- if (ViewModel?.SettingsViewModel is not { } svm)
- {
- return;
- }
- svm.IsBackButtonEnabled.Value = _backStack.Count > 0;
- svm.IsForwardButtonEnabled.Value = _forwardStack.Count > 0;
- }
- #endregion
- #region Input Handlers
- private void OnPointerPressed(object? sender, PointerPressedEventArgs e)
- {
- if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
- {
- return;
- }
- var topLevel = TopLevel.GetTopLevel(desktop.MainWindow);
- var properties = e.GetCurrentPoint(topLevel).Properties;
- if (properties.IsXButton1Pressed)
- {
- GoBack();
- }
- if (properties.IsXButton2Pressed)
- {
- GoForward();
- }
- if (properties.IsRightButtonPressed)
- {
- ContextMenu.Open();
- }
- }
- #endregion
- }
|