Skip to content

Commit

Permalink
Fixed DPI scaling issue.
Browse files Browse the repository at this point in the history
Bumped rev.
  • Loading branch information
GoldenTao committed Aug 1, 2015
1 parent 8532adf commit 8f1ec7f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
19 changes: 19 additions & 0 deletions EarTrumpet/Extensions/WindowExtensions.cs
@@ -1,6 +1,7 @@
using EarTrumpet.Services;
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace EarTrumpet.Extensions
Expand Down Expand Up @@ -92,5 +93,23 @@ public static void ShowwithAnimation(this Window window)
}
_windowVisible = true;
}

public static Matrix CalculateDpiFactors(this Window window)
{
var mainWindowPresentationSource = PresentationSource.FromVisual(window);
return mainWindowPresentationSource == null ? new Matrix() { M11 = 1, M22 = 1} : mainWindowPresentationSource.CompositionTarget.TransformToDevice;
}

public static double DpiHeightFactor(this Window window)
{
var m = CalculateDpiFactors(window);
return m.M22;
}

public static double DpiWidthFactor(this Window window)
{
var m = CalculateDpiFactors(window);
return m.M11;
}
}
}
17 changes: 14 additions & 3 deletions EarTrumpet/MainWindow.xaml.cs
Expand Up @@ -24,12 +24,23 @@ public MainWindow()

DataContext = _viewModel;

CreateAndHideWindow();

// Move keyboard focus to the first element. Disabled this since it is ugly but not sure invisible
// visuals are preferrable.
// Activated += (s,e) => MoveFocus(new TraversalRequest(FocusNavigationDirection.First));

SourceInitialized += (s, e) => UpdateTheme();
}

private void CreateAndHideWindow()
{
// Ensure the Win32 and WPF windows are created to fix first show issues with DPI Scaling
Opacity = 0;
Show();
Hide();
Opacity = 1;
}

void TrayIcon_Invoked()
{
Expand Down Expand Up @@ -156,9 +167,9 @@ private void UpdateWindowPosition()
Height = LayoutRoot.DesiredSize.Height;

var taskbarScreenWorkArea = TaskbarService.TaskbarScreen.WorkingArea;
var taskbarPosition = TaskbarService.TaskbarPosition;
Left = (taskbarPosition == TaskbarPosition.Left) ? taskbarScreenWorkArea.Left : taskbarScreenWorkArea.Right - Width;
Top = (taskbarPosition == TaskbarPosition.Top) ? taskbarScreenWorkArea.Top : taskbarScreenWorkArea.Bottom - Height;
var taskbarPosition = TaskbarService.TaskbarPosition;
Left = (taskbarPosition == TaskbarPosition.Left) ? (taskbarScreenWorkArea.Left / this.DpiWidthFactor()) : (taskbarScreenWorkArea.Right / this.DpiWidthFactor()) - Width;
Top = (taskbarPosition == TaskbarPosition.Top) ? (taskbarScreenWorkArea.Top / this.DpiHeightFactor()) : (taskbarScreenWorkArea.Bottom / this.DpiHeightFactor()) - Height;
}
}
}
4 changes: 2 additions & 2 deletions EarTrumpet/Properties/AssemblyInfo.cs
Expand Up @@ -10,7 +10,7 @@
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyVersion("1.1.1.0")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: ComVisible(false)]
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

0 comments on commit 8f1ec7f

Please sign in to comment.