namespace VideoBrowser.ViewModels { using Dragablz; using System; using System.Linq; using System.Windows; using System.Windows.Input; using VideoBrowser.Common; using VideoBrowser.Controls.CefSharpBrowser; using VideoBrowser.Controls.CefSharpBrowser.ViewModels; using VideoBrowser.Core; using VideoBrowser.Models; using VideoBrowser.Views; /// /// Defines the . /// public class MainWindowViewModel : NotifyPropertyChanged { #region Constructors /// /// Initializes a new instance of the class. /// /// The globalData. /// The globalBrowserData. public MainWindowViewModel(GlobalBrowserData globalBrowserData) { this.ClosingCommand = new RelayCommand(this.OnClosing); this.LoadedCommand = new RelayCommand(this.OnLoaded); this.PressEscCommand = new RelayCommand(this.OnPressEsc); this.PressF2Command = new RelayCommand(this.OnPressF2); this.About = new AboutViewModel(); var downloadItemModels = globalBrowserData.DownloadItemModels; this.DownloadQueueViewModel = new DownloadQueueViewModel(downloadItemModels) { ShowMessageAsync = this.ShowMessageAsync }; this.DownloadFlyoutViewModel = new DownloadFlyoutViewModel(downloadItemModels) { ShowDownloadTabAction = this.ShowDownloadTabAction }; this.WebBrowserTabControlViewModel = new WebBrowserTabControlViewModel(globalBrowserData) { CreateBrowserFunc = this.CreateBrowser }; this.Initialize(); } #endregion Constructors #region Properties /// /// Gets the About. /// public AboutViewModel About { get; } /// /// Gets the CefWindowData. /// public CefWindowData CefWindowData => this.WebBrowserTabControlViewModel.CefWindowData; /// /// Gets the ClosingCommand. /// public RelayCommand ClosingCommand { get; } /// /// Gets the DownloadFlyoutViewModel. /// public DownloadFlyoutViewModel DownloadFlyoutViewModel { get; } /// /// Gets the DownloadQueueViewModel. /// public DownloadQueueViewModel DownloadQueueViewModel { get; } /// /// Gets the GlobalBrowserData. /// public GlobalBrowserData GlobalBrowserData => this.WebBrowserTabControlViewModel.GlobalBrowserData; /// /// Gets the LoadedCommand. /// public ICommand LoadedCommand { get; } /// /// Gets the PressEscCommand. /// public ICommand PressEscCommand { get; } /// /// Gets the PressF2Command. /// public ICommand PressF2Command { get; } /// /// Gets the Settings. /// public SettingsViewModel Settings => this.GlobalBrowserData.Settings; /// /// Gets the Title. /// public string Title => $"{AppEnvironment.Name}"; /// /// Gets the WebBrowserTabControlViewModel. /// public WebBrowserTabControlViewModel WebBrowserTabControlViewModel { get; } /// /// Gets the DownloadAction. /// private Action DownloadAction => this.DownloadQueueViewModel.Download; #endregion Properties #region Methods /// /// The CreateBrowser. /// /// The . internal TabItem CreateBrowser() { var model = new WebBrowserHeaderedItemViewModel(this.GlobalBrowserData, this.CefWindowData, this.DownloadAction); return model; } /// /// The Dispose. /// private void Dispose() { this.WebBrowserTabControlViewModel.Dispose(); var viewModels = this.GlobalBrowserData.WindowViewModels; if (viewModels.Contains(this)) { viewModels.Remove(this); } if (!viewModels.Any()) { this.GlobalBrowserData.Dispose(); } } /// /// The Initialize. /// private void Initialize() { var args = AppEnvironment.Arguments; var url = Properties.Settings.Default.LastUrl; if (args != null && args.Any()) { url = args.First(); } if (this.WebBrowserTabControlViewModel.TabItems.Any()) { var browser = this.WebBrowserTabControlViewModel.TabItems.First(); if (browser is WebBrowserHeaderedItemViewModel model) { model.VideoBrowserViewModel.NavigateUrl = url; } } } /// /// The OnClosing. /// /// The obj. private void OnClosing(object obj) { var (_, _, commandParameter) = (ValueTuple)obj; var window = (Window)commandParameter; var settings = Properties.Settings.Default; settings.WindowPosition = new Point(window.Left, window.Top); settings.WindowWidth = window.ActualWidth; settings.WindowHeight = window.Height; settings.WindowState = window.WindowState; settings.Save(); DownloadQueueHandler.Stop(); this.Dispose(); } /// /// The OnLoaded. /// /// The obj. private void OnLoaded(object obj) { var (_, _, commandParameter) = (ValueTuple)obj; var settings = Properties.Settings.Default; var window = (MainWindow)commandParameter; window.Left = settings.WindowPosition.X; window.Top = settings.WindowPosition.Y; window.Width = settings.WindowWidth; window.Height = settings.WindowHeight; window.WindowState = settings.WindowState; this.CefWindowData.MainWindow = window; DownloadQueueHandler.LimitDownloads = settings.ShowMaxSimDownloads; DownloadQueueHandler.StartWatching(settings.MaxSimDownloads); } /// /// The OnPressEsc. /// /// The obj. private void OnPressEsc(object obj) { // Leave full screen. if (this.CefWindowData.IsFullScreen) { this.CefWindowData.IsFullScreenCommand.Execute(false); } } /// /// The OnPressF2. /// /// The obj. private void OnPressF2(object obj) { var window = this.CefWindowData.MainWindow; window.Width = 1022; window.Height = 900; } /// /// The ShowDownloadTabAction. /// private void ShowDownloadTabAction() { var button = this.GlobalBrowserData.GetAddInButton(typeof(DownloadQueueButton)); button?.Command.Execute(this.WebBrowserTabControlViewModel); } /// /// The ShowMessageAsync. /// /// The title. /// The message. private void ShowMessageAsync(string title, string message) { this.CefWindowData.ShowMessageAsync(title, message); } #endregion Methods } }