Minor refactoring of taskbar (extracted code for physical pixel calculation and removed scroll wheel preview for ApplicationScrollViewer).

This commit is contained in:
dbuechel 2017-08-11 15:06:52 +02:00
parent 0dcbe9e5b6
commit 428ef01d39
4 changed files with 48 additions and 62 deletions

View file

@ -112,6 +112,7 @@
</Compile>
<Compile Include="UserInterfaceFactory.cs" />
<Compile Include="Utilities\IconResourceLoader.cs" />
<Compile Include="Utilities\VisualExtensions.cs" />
<Compile Include="ViewModels\DateTimeViewModel.cs" />
<Compile Include="ViewModels\LogViewModel.cs" />
<Compile Include="ViewModels\SplashScreenViewModel.cs" />

View file

@ -19,7 +19,7 @@
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<ScrollViewer Grid.Column="0" x:Name="ApplicationScrollViewer" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto" PreviewMouseWheel="ApplicationScrollViewer_PreviewMouseWheel">
<ScrollViewer Grid.Column="0" x:Name="ApplicationScrollViewer" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto">
<ScrollViewer.Resources>
<s:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarHeightKey}">5</s:Double>
</ScrollViewer.Resources>

View file

@ -7,11 +7,9 @@
*/
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.UserInterface.Utilities;
namespace SafeExamBrowser.UserInterface
{
@ -48,7 +46,7 @@ namespace SafeExamBrowser.UserInterface
{
return Dispatcher.Invoke(() =>
{
var height = (int) TransformToPhysical(Width, Height).Y;
var height = (int) this.TransformToPhysical(Width, Height).Y;
logger.Info($"Calculated physical taskbar height is {height}px.");
@ -64,66 +62,11 @@ namespace SafeExamBrowser.UserInterface
Left = SystemParameters.WorkArea.Right - Width;
Top = SystemParameters.WorkArea.Bottom;
var position = TransformToPhysical(Left, Top);
var size = TransformToPhysical(Width, Height);
var position = this.TransformToPhysical(Left, Top);
var size = this.TransformToPhysical(Width, Height);
logger.Info($"Set taskbar bounds to {Width}x{Height} at ({Left}/{Top}), in physical pixels: {size.X}x{size.Y} at ({position.X}/{position.Y}).");
});
}
private void ApplicationScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
var scrollAmount = 20;
if (ApplicationScrollViewer.IsMouseOver)
{
if (e.Delta < 0)
{
if (ApplicationScrollViewer.HorizontalOffset + scrollAmount > 0)
{
ApplicationScrollViewer.ScrollToHorizontalOffset(ApplicationScrollViewer.HorizontalOffset + scrollAmount);
}
else
{
ApplicationScrollViewer.ScrollToLeftEnd();
}
}
else
{
if (ApplicationScrollViewer.ExtentWidth > ApplicationScrollViewer.HorizontalOffset - scrollAmount)
{
ApplicationScrollViewer.ScrollToHorizontalOffset(ApplicationScrollViewer.HorizontalOffset - scrollAmount);
}
else
{
ApplicationScrollViewer.ScrollToRightEnd();
}
}
}
}
private Vector TransformToPhysical(double x, double y)
{
// WPF works with device-independent pixels. The following code is required
// to transform those values to their absolute, device-specific pixel value.
// Source: https://stackoverflow.com/questions/3286175/how-do-i-convert-a-wpf-size-to-physical-pixels
Matrix transformToDevice;
var source = PresentationSource.FromVisual(this);
if (source != null)
{
transformToDevice = source.CompositionTarget.TransformToDevice;
}
else
{
using (var newSource = new HwndSource(new HwndSourceParameters()))
{
transformToDevice = newSource.CompositionTarget.TransformToDevice;
}
}
return transformToDevice.Transform(new Vector(x, y));
}
}
}

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2017 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
namespace SafeExamBrowser.UserInterface.Utilities
{
internal static class VisualExtensions
{
/// <summary>
/// WPF works with device-independent pixels. This method is required to
/// transform such values to their absolute, device-specific pixel value.
/// Source: https://stackoverflow.com/questions/3286175/how-do-i-convert-a-wpf-size-to-physical-pixels
/// </summary>
internal static Vector TransformToPhysical(this Visual visual, double x, double y)
{
Matrix transformToDevice;
var source = PresentationSource.FromVisual(visual);
if (source != null)
{
transformToDevice = source.CompositionTarget.TransformToDevice;
}
else
{
using (var newSource = new HwndSource(new HwndSourceParameters()))
{
transformToDevice = newSource.CompositionTarget.TransformToDevice;
}
}
return transformToDevice.Transform(new Vector(x, y));
}
}
}