Skip to content

Commit

Permalink
Remove dependency to MahApps.Metro
Browse files Browse the repository at this point in the history
  • Loading branch information
awaescher committed Jul 6, 2023
1 parent 5121148 commit bcfe030
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 21 deletions.
5 changes: 4 additions & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Download and run the executable from the [Releases tab](https://github.com/awaes
- cd into the repository directory
- run `dotnet run --project StageManager`

To quit, find the app's tray icon (Windows might have moved this in the overflow menu) and use its context menu to close the app.
To quit, find the app's tray icon (Windows might move it into the overflow menu) and use its context menu to close the app.

### Requirements
- Windows 10 version 2004 or newer
Expand All @@ -35,6 +35,8 @@ This is an experimental fun project. I don't have any idea whether or not this i
|scene management with drag&drop||
|restore windows on quit/restart||
|auto hide & fly-in scenes for maximized windows||
|full screenshots for windows that were minimized on startup||
|drag windows from other scenes into the current one||
|place screenshots in relative size of the desktop||
|place app icons over 3D window adorners||
|rounded corners for the window thumbnails||
Expand All @@ -45,6 +47,7 @@ This is an experimental fun project. I don't have any idea whether or not this i
|**Product stage**||
|virtual desktop support (pin window)||
|multi-monitor support||
|visual feedback when dragging windows from other scenes||
|feature parity with macOS Stage Manager||
|**Polishing stage**||
|window animations||
Expand Down
3 changes: 1 addition & 2 deletions StageManager/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using AsyncAwaitBestPractices;
using MahApps.Metro.Controls;
using Microsoft.Xaml.Behaviors.Core;
using SharpHook;
using StageManager.Model;
Expand Down Expand Up @@ -64,7 +63,7 @@ protected override void OnInitialized(EventArgs e)
_hook.MouseReleased += OnMouseReleased;
_hook.MouseMoved += _hook_MouseMoved;

Task.Run(() => _hook.Run());
Task.Run(_hook.Run);
}

protected override void OnClosed(EventArgs e)
Expand Down
82 changes: 82 additions & 0 deletions StageManager/Native/VisualHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// This file contains two methods taken from MahApps.Metro to be able to remove the NuGet dependency.

// Taken from
// MahApps.Metro/src/MahApps.Metro/Controls/WinApiHelper.cs
// MahApps.Metro/src/MahApps.Metro/Controls/TreeHelper.cs

// Original license header:
//
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using ControlzEx.Standard;
using System;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;

namespace StageManager.Native
{
public static class VisualHelper
{
/// <summary>
/// Get the working area size of the monitor from where the visual stays.
/// </summary>
/// <param name="visual">The visual element to get the monitor information.</param>
/// <returns>The working area size of the monitor.</returns>
public static Size GetMonitorWorkSize(this Visual visual)
{
if (visual != null)
{
var hwndSource = PresentationSource.FromVisual(visual) as HwndSource;
if (hwndSource != null && !hwndSource.IsDisposed && hwndSource.RootVisual != null && hwndSource.Handle != IntPtr.Zero)
{
IntPtr intPtr = NativeMethods.MonitorFromWindow(hwndSource.Handle, MonitorOptions.MONITOR_DEFAULTTONEAREST);
if (intPtr != IntPtr.Zero)
{
var monitorInfoW = NativeMethods.GetMonitorInfoW(intPtr);
return new Size(monitorInfoW.rcWork.Width, monitorInfoW.rcWork.Height);
}
}
}

return default;
}

/// <summary>
/// This method is an alternative to WPF's <see cref="VisualTreeHelper.GetParent"/> method, which also supports content elements. Keep in mind that for content element, this method falls back to the logical tree of the element!
/// </summary>
/// <param name="child">The item to be processed.</param>
/// <returns>The submitted item's parent, if available. Otherwise null.</returns>
public static DependencyObject? GetParentObject(this DependencyObject? child)
{
if (child is null)
return null;

// handle content elements separately
if (child is ContentElement contentElement)
{
DependencyObject parent = ContentOperations.GetParent(contentElement);
if (parent is not null)
return parent;

return contentElement is FrameworkContentElement fce ? fce.Parent : null;
}

var childParent = VisualTreeHelper.GetParent(child);
if (childParent is not null)
return childParent;

// also try searching for parent in framework elements (such as DockPanel, etc)
if (child is FrameworkElement frameworkElement)
{
DependencyObject parent = frameworkElement.Parent;
if (parent is not null)
return parent;
}

return null;
}
}
}
36 changes: 18 additions & 18 deletions StageManager/StageManager.csproj
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon>StageManager.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon>StageManager.ico</ApplicationIcon>
</PropertyGroup>

<ItemGroup>
<Resource Include="StageManager.ico" />
</ItemGroup>
<ItemGroup>
<Resource Include="StageManager.ico" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AsyncAwaitBestPractices" Version="6.0.6" />
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="1.1.0" />
<PackageReference Include="MahApps.Metro" Version="2.4.10" />
<PackageReference Include="SharpHook" Version="4.2.1" />
<PackageReference Include="WpfScreenHelper" Version="2.1.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AsyncAwaitBestPractices" Version="6.0.6" />
<PackageReference Include="ControlzEx" Version="4.4.0" />
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="1.1.0" />
<PackageReference Include="SharpHook" Version="4.2.1" />
<PackageReference Include="WpfScreenHelper" Version="2.1.0" />
</ItemGroup>

</Project>

0 comments on commit bcfe030

Please sign in to comment.