Skip to content

Commit

Permalink
Fix issue with application not starting minimized
Browse files Browse the repository at this point in the history
Rework window position on screen
Add strategy to show window in bottom right corner
Add structure to position the window baesed on a strategy
  • Loading branch information
XanatosX committed Aug 2, 2023
1 parent c455185 commit 7394c90
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public static IServiceCollection AddViewModels(this IServiceCollection collectio
/// <returns>The extended collection</returns>
public static IServiceCollection AddViews(this IServiceCollection collection)
{
return collection.AddTransient(resolver => new MainWindow(resolver.GetRequiredService<IWindowManagementService>(), resolver.GetRequiredService<ISettingsService>())
return collection.AddTransient(resolver => new MainWindow(
resolver.GetRequiredService<IWindowManagementService>(),
resolver.GetRequiredService<ISettingsService>(),
resolver.GetRequiredService<IWindowPositionFactory>())
{
DataContext = resolver?.GetService<MainWindowViewModel>(),
})
Expand Down Expand Up @@ -96,6 +99,7 @@ public static IServiceCollection AddServices(this IServiceCollection collection)
.AddSingleton<ISettingsService, SerializedSettingsService>()
.AddSingleton<PluginSettingViewModelService>()
.AddSingleton<IThemeService, AvaloniaThemeService>()
.AddSingleton<IWindowPositionFactory, DefaultWindowPositionStrategyFactory>()
.AddTransient<IImageService, ImageService>()
.AddTransient<ResourceReaderService>()
.AddTransient<GetApplicationInformationService>()
Expand Down
12 changes: 12 additions & 0 deletions src/ModularToolManager/Enums/WindowPositionEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ModularToolManager.Enums;

/// <summary>
/// Enum for the correct position of the window
/// </summary>
public enum WindowPositionEnum
{
TopLeft,
TopRight,
BottomLeft,
BottomRight,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using ModularToolManager.Enums;
using ModularToolManager.Strategies.WindowPosition;

namespace ModularToolManager.Services.Ui;

/// <summary>
/// Default factory for getting the window position strategy
/// </summary>
public class DefaultWindowPositionStrategyFactory : IWindowPositionFactory
{
/// <inheritdoc/>
public IWindowPositionStrategy? GetWindowPositionStrategy(WindowPositionEnum positionEnum)
{
return positionEnum switch
{
WindowPositionEnum.BottomRight => new BottomRightStrategy(),
_ => new BottomRightStrategy()
};
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia.Controls;
using ModularToolManager.Enums;
using ModularToolManager.Models;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -26,7 +27,7 @@ public interface IWindowManagementService
/// <summary>
/// Get the main window of the application
/// </summary>
/// <returns></returns>
/// <returns>The main window or nothing if no main window was found</returns>
Window? GetMainWindow();

/// <summary>
Expand Down
17 changes: 17 additions & 0 deletions src/ModularToolManager/Services/Ui/IWindowPositionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using ModularToolManager.Enums;
using ModularToolManager.Strategies.WindowPosition;

namespace ModularToolManager.Services.Ui;

/// <summary>
/// Factory to get a matching strategy for a requested window position
/// </summary>
public interface IWindowPositionFactory
{
/// <summary>
/// Get the strategy for the given window
/// </summary>
/// <param name="positionEnum">The position enum to use</param>
/// <returns>The strategy to position the window on the given position</returns>
IWindowPositionStrategy? GetWindowPositionStrategy(WindowPositionEnum positionEnum);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Platform;

namespace ModularToolManager.Strategies.WindowPosition;

/// <summary>
/// Position a Window in the bottom right of a given screen
/// </summary>
public class BottomRightStrategy : IWindowPositionStrategy
{
/// <inheritdoc/>
public void PositionWindow(Window window, Screen? screen)
{
if (screen is null)
{
return;
}
PixelRect workingArea = screen.WorkingArea;
double newXPos = workingArea.X + workingArea.Width - window.Width;
double newYPos = workingArea.Y + workingArea.Height - window.Height;
window.Position = new PixelPoint((int)newXPos, (int)newYPos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Avalonia.Controls;
using Avalonia.Platform;

namespace ModularToolManager.Strategies.WindowPosition;

/// <summary>
/// Strategy to position a window on the screen
/// </summary>
public interface IWindowPositionStrategy
{
/// <summary>
/// Position the given window on the screen
/// </summary>
/// <param name="window">The window which should be moved</param>
/// <param name="screen">The screen to position the window on</param>
void PositionWindow(Window window, Screen? screen);
}
37 changes: 23 additions & 14 deletions src/ModularToolManager/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
using Avalonia.Media;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using ModularToolManager.Enums;
using ModularToolManager.Models;
using ModularToolManager.Models.Messages;
using ModularToolManager.Services.Settings;
using ModularToolManager.Services.Ui;
using System;
using System.Linq;

namespace ModularToolManager.Views;

Expand All @@ -27,19 +29,24 @@ public partial class MainWindow : Window, IDisposable
private readonly IWindowManagementService? modalService;

/// <summary>
/// Is this the first render of the application
/// Is this the first time the window was shown
/// </summary>
private bool firstRender;
private bool firstTimeShown;

/// <summary>
/// The settings service to use
/// </summary>
private readonly ISettingsService? settingsService;

/// <summary>
/// The factory to get the window position strategy to use
/// </summary>
private readonly IWindowPositionFactory? windowPositionFactory;

/// <summary>
/// Create a new empty instance of this class
/// </summary>
public MainWindow() : this(null, null)
public MainWindow() : this(null, null, null)
{

}
Expand All @@ -49,13 +56,13 @@ public MainWindow() : this(null, null)
/// </summary>
/// <param name="modalService">The modal service to use</param>
/// <param name="settingsService">The settings service to use</param>
public MainWindow(IWindowManagementService? modalService, ISettingsService? settingsService)
public MainWindow(IWindowManagementService? modalService, ISettingsService? settingsService, IWindowPositionFactory? windowPositionFactory)
{

this.modalService = modalService;
this.settingsService = settingsService;

firstRender = true;
this.windowPositionFactory = windowPositionFactory;
firstTimeShown = true;
InitializeComponent();

WeakReferenceMessenger.Default.Register<CloseApplicationMessage>(this, (_, _) =>
Expand Down Expand Up @@ -99,28 +106,30 @@ public MainWindow(IWindowManagementService? modalService, ISettingsService? sett
public override void Render(DrawingContext context)
{
base.Render(context);
if (firstRender)
PositionWindow();
}

/// <inheritdoc/>
public override void Show()
{
base.Show();
if (firstTimeShown)
{
if (settingsService?.GetApplicationSettings().StartMinimized ?? false)
{
var mainWindow = modalService?.GetMainWindow();
mainWindow?.Hide();
}
}

PositionWindow();
firstRender = false;
firstTimeShown = false;
}

/// <summary>
/// Method to position a given window in the bottom right corner based on the window height
/// </summary>
private void PositionWindow()
{
PixelRect workingArea = Screens.Primary.WorkingArea;
double newXPos = workingArea.X + workingArea.Width - Width;
double newYPos = workingArea.Y + workingArea.Height - Height;
Position = new PixelPoint((int)newXPos, (int)newYPos);
windowPositionFactory?.GetWindowPositionStrategy(WindowPositionEnum.BottomRight)?.PositionWindow(this, Screens.Primary);
}

/// <inheritdoc/>
Expand Down

0 comments on commit 7394c90

Please sign in to comment.