Skip to content

Commit

Permalink
Merge pull request #71 from XanatosX/bugfix/68-modal-windows-not-open…
Browse files Browse the repository at this point in the history
…able

Fix modal window not reopnable after minimizing app via tray
  • Loading branch information
XanatosX authored Feb 21, 2023
2 parents 5d3cf39 + 53c0138 commit d5a3f32
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
6 changes: 0 additions & 6 deletions src/ModularToolManager/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
17 changes: 17 additions & 0 deletions src/ModularToolManager/Models/Messages/ModalWindowOpened.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using CommunityToolkit.Mvvm.Messaging.Messages;

namespace ModularToolManager.Models.Messages;

/// <summary>
/// Message if a modal window was opened or closed
/// </summary>
internal class ModalWindowOpened : ValueChangedMessage<bool>
{
/// <summary>
/// Create a new instance of this message
/// </summary>
/// <param name="isOpen">Bool if the modal window was opened (true) or closed (false)</param>
public ModalWindowOpened(bool isOpen) : base(isOpen)
{
}
}
25 changes: 23 additions & 2 deletions src/ModularToolManager/ViewModels/AppViewModel.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,18 +23,37 @@ public class AppViewModel : ObservableObject
/// </summary>
public ICommand ShowApplicationCommand { get; }

/// <summary>
/// The current number of modal windows which are open
/// </summary>
private int numberOfOpenModalWindows;

/// <summary>
/// Logger to use for the app view
/// </summary>
private readonly ILogger<AppViewModel> logger;

/// <summary>
/// Create a new instance of this class
/// </summary>
public AppViewModel()
public AppViewModel(ILogger<AppViewModel> logger)
{
numberOfOpenModalWindows = 0;

WeakReferenceMessenger.Default.Register<ModalWindowOpened>(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;
}
}
14 changes: 14 additions & 0 deletions src/ModularToolManager/Views/ModalWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using CommunityToolkit.Mvvm.Messaging;
using ModularToolManager.Models.Messages;
using ModularToolManager.ViewModels;
using System;
using System.ComponentModel;

namespace ModularToolManager.Views;

Expand Down Expand Up @@ -32,4 +34,16 @@ private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}

protected override void OnOpened(EventArgs e)
{
WeakReferenceMessenger.Default.Send<ModalWindowOpened>(new ModalWindowOpened(true));
base.OnOpened(e);
}

protected override void OnClosing(CancelEventArgs e)
{
WeakReferenceMessenger.Default.Send<ModalWindowOpened>(new ModalWindowOpened(false));
base.OnClosing(e);
}
}

0 comments on commit d5a3f32

Please sign in to comment.