using System.Windows; using Livia.Properties; using Livia.Utility.DependencyInjection; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Livia.Views.Controls; /// /// Interaction logic for MaterialTitleBar.xaml /// public partial class MaterialTitleBar { public Visibility MinimizeButtonVisibility { get; set; } = Visibility.Visible; public Visibility MaximizeButtonVisibility { get; set; } = Visibility.Visible; public static readonly DependencyProperty WindowIsMaximizedProperty = DependencyProperty.Register( nameof(WindowIsMaximized), typeof(bool), typeof(MaterialTitleBar) ); public bool WindowIsMaximized { get => (bool)GetValue(WindowIsMaximizedProperty); set => SetValue(WindowIsMaximizedProperty, value); } private readonly ILogger _logger; public MaterialTitleBar() { InitializeComponent(); DataContext = this; _logger = ActivatorUtilities.GetServiceOrCreateInstance(ServiceProviderFactory.ServiceProvider); } private void MinimizeButtonClick(object sender, RoutedEventArgs e) { _logger.LogInformation("Minimizing window"); Window? window = Window.GetWindow(this); if (window == null) return; window.WindowState = WindowState.Minimized; } private void MaximizeButtonClick(object sender, RoutedEventArgs e) { _logger.LogInformation("Maximize button clicked"); Window? window = Window.GetWindow(this); if (window == null) return; if (WindowIsMaximized) { RestoreWindow(window); } else { MaximizeWindow(window); } } private void MaximizeWindow(Window window) { _logger.LogInformation("Maximizing window"); WindowIsMaximized = true; //store location if it is main window if (window is MainWindow) { ViewSettings.Default.MainWindowIsMaximized = true; ViewSettings.Default.MainWindowRestoreHeight = window.Height; ViewSettings.Default.MainWindowRestoreWidth = window.Width; ViewSettings.Default.MainWindowRestoreTop = window.Top; ViewSettings.Default.MainWindowRestoreLeft = window.Left; } window.Width = SystemParameters.WorkArea.Width; window.Height = SystemParameters.WorkArea.Height; window.Left = 0; window.Top = 0; } private void RestoreWindow(Window window) { _logger.LogInformation("Restoring window"); WindowIsMaximized = false; window.WindowState = WindowState.Normal; //restore location if it is main window if (window is MainWindow) { ViewSettings.Default.MainWindowIsMaximized = false; window.Width = ViewSettings.Default.MainWindowRestoreWidth; window.Height = ViewSettings.Default.MainWindowRestoreHeight; window.Left = ViewSettings.Default.MainWindowRestoreLeft; window.Top = ViewSettings.Default.MainWindowRestoreTop; } else { window.Width = window.DesiredSize.Width; window.Height = window.DesiredSize.Height; window.Left = (SystemParameters.WorkArea.Width - window.DesiredSize.Width) / 2; window.Top = (SystemParameters.WorkArea.Height - window.DesiredSize.Height) / 2; } } public void TryManuallyExitMaximizedMode(Window window, SizeChangedEventArgs e) { if (e.PreviousSize != SystemParameters.WorkArea.Size) return; WindowIsMaximized = false; if (window is MainWindow) { ViewSettings.Default.MainWindowIsMaximized = false; } } private void CloseButtonClick(object sender, RoutedEventArgs e) { _logger.LogInformation("Closing {current}", Window.GetWindow(this)); Window.GetWindow(this)?.Close(); } }