From 53c013810f2d0956b987e32ee937009c2d5337d7 Mon Sep 17 00:00:00 2001 From: XanatosX <10531466+XanatosX@users.noreply.github.com> Date: Tue, 21 Feb 2023 20:40:06 +0100 Subject: [PATCH] Fix issue #68 Block minimize while a modal is already open --- src/ModularToolManager/App.axaml.cs | 6 ----- .../Models/Messages/ModalWindowOpened.cs | 17 +++++++++++++ .../ViewModels/AppViewModel.cs | 25 +++++++++++++++++-- .../Views/ModalWindow.axaml.cs | 14 +++++++++++ 4 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 src/ModularToolManager/Models/Messages/ModalWindowOpened.cs diff --git a/src/ModularToolManager/App.axaml.cs b/src/ModularToolManager/App.axaml.cs index 46915a1a..fa73e331 100644 --- a/src/ModularToolManager/App.axaml.cs +++ b/src/ModularToolManager/App.axaml.cs @@ -5,23 +5,17 @@ using Avalonia.Markup.Xaml; using CommunityToolkit.Mvvm.Messaging; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using ModularToolManager.DependencyInjection; using ModularToolManager.Models.Messages; -using ModularToolManager.Services.IO; using ModularToolManager.Services.Settings; -using ModularToolManager.Services.Ui; using ModularToolManager.ViewModels; using ModularToolManager.Views; using ModularToolManagerModel.Services.IO; using ModularToolManagerModel.Services.Language; -using ModularToolManagerModel.Services.Logging; using Serilog; using System; -using System.Collections.Generic; using System.Globalization; using System.IO; -using System.Linq; namespace ModularToolManager; diff --git a/src/ModularToolManager/Models/Messages/ModalWindowOpened.cs b/src/ModularToolManager/Models/Messages/ModalWindowOpened.cs new file mode 100644 index 00000000..43591bb1 --- /dev/null +++ b/src/ModularToolManager/Models/Messages/ModalWindowOpened.cs @@ -0,0 +1,17 @@ +using CommunityToolkit.Mvvm.Messaging.Messages; + +namespace ModularToolManager.Models.Messages; + +/// +/// Message if a modal window was opened or closed +/// +internal class ModalWindowOpened : ValueChangedMessage +{ + /// + /// Create a new instance of this message + /// + /// Bool if the modal window was opened (true) or closed (false) + public ModalWindowOpened(bool isOpen) : base(isOpen) + { + } +} diff --git a/src/ModularToolManager/ViewModels/AppViewModel.cs b/src/ModularToolManager/ViewModels/AppViewModel.cs index f14411de..db14154d 100644 --- a/src/ModularToolManager/ViewModels/AppViewModel.cs +++ b/src/ModularToolManager/ViewModels/AppViewModel.cs @@ -1,7 +1,9 @@ using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; +using Microsoft.Extensions.Logging; using ModularToolManager.Models.Messages; +using Serilog; using System.Windows.Input; namespace ModularToolManager.ViewModels; @@ -21,18 +23,37 @@ public class AppViewModel : ObservableObject /// public ICommand ShowApplicationCommand { get; } + /// + /// The current number of modal windows which are open + /// + private int numberOfOpenModalWindows; + + /// + /// Logger to use for the app view + /// + private readonly ILogger logger; + /// /// Create a new instance of this class /// - public AppViewModel() + public AppViewModel(ILogger logger) { + numberOfOpenModalWindows = 0; + + WeakReferenceMessenger.Default.Register(this, (_, message) => numberOfOpenModalWindows += message.Value ? 1 : -1); + ShowApplicationCommand = new RelayCommand(() => { + if (numberOfOpenModalWindows > 0) + { + logger.LogWarning($"Tried to minimize app while {numberOfOpenModalWindows} where opend"); + return; + } var response = WeakReferenceMessenger.Default.Send(new RequestApplicationVisiblity()); bool toggleMode = response.HasReceivedResponse ? response.Response : false; WeakReferenceMessenger.Default.Send(new ToggleApplicationVisibilityMessage(toggleMode)); - }); ExitApplicationCommand = new RelayCommand(() => WeakReferenceMessenger.Default.Send(new CloseApplicationMessage())); + this.logger = logger; } } diff --git a/src/ModularToolManager/Views/ModalWindow.axaml.cs b/src/ModularToolManager/Views/ModalWindow.axaml.cs index 784f2b87..6b7d9857 100644 --- a/src/ModularToolManager/Views/ModalWindow.axaml.cs +++ b/src/ModularToolManager/Views/ModalWindow.axaml.cs @@ -4,6 +4,8 @@ using CommunityToolkit.Mvvm.Messaging; using ModularToolManager.Models.Messages; using ModularToolManager.ViewModels; +using System; +using System.ComponentModel; namespace ModularToolManager.Views; @@ -32,4 +34,16 @@ private void InitializeComponent() { AvaloniaXamlLoader.Load(this); } + + protected override void OnOpened(EventArgs e) + { + WeakReferenceMessenger.Default.Send(new ModalWindowOpened(true)); + base.OnOpened(e); + } + + protected override void OnClosing(CancelEventArgs e) + { + WeakReferenceMessenger.Default.Send(new ModalWindowOpened(false)); + base.OnClosing(e); + } }