From 428ef01d396b7769b23377deff379cdde2e0932c Mon Sep 17 00:00:00 2001 From: dbuechel Date: Fri, 11 Aug 2017 15:06:52 +0200 Subject: [PATCH] Minor refactoring of taskbar (extracted code for physical pixel calculation and removed scroll wheel preview for ApplicationScrollViewer). --- .../SafeExamBrowser.UserInterface.csproj | 1 + SafeExamBrowser.UserInterface/Taskbar.xaml | 2 +- SafeExamBrowser.UserInterface/Taskbar.xaml.cs | 65 ++----------------- .../Utilities/VisualExtensions.cs | 42 ++++++++++++ 4 files changed, 48 insertions(+), 62 deletions(-) create mode 100644 SafeExamBrowser.UserInterface/Utilities/VisualExtensions.cs diff --git a/SafeExamBrowser.UserInterface/SafeExamBrowser.UserInterface.csproj b/SafeExamBrowser.UserInterface/SafeExamBrowser.UserInterface.csproj index f8a97bc0..795ee7b3 100644 --- a/SafeExamBrowser.UserInterface/SafeExamBrowser.UserInterface.csproj +++ b/SafeExamBrowser.UserInterface/SafeExamBrowser.UserInterface.csproj @@ -112,6 +112,7 @@ + diff --git a/SafeExamBrowser.UserInterface/Taskbar.xaml b/SafeExamBrowser.UserInterface/Taskbar.xaml index 5b35fe25..ded567f0 100644 --- a/SafeExamBrowser.UserInterface/Taskbar.xaml +++ b/SafeExamBrowser.UserInterface/Taskbar.xaml @@ -19,7 +19,7 @@ - + 5 diff --git a/SafeExamBrowser.UserInterface/Taskbar.xaml.cs b/SafeExamBrowser.UserInterface/Taskbar.xaml.cs index cf52f1c5..27fed1c3 100644 --- a/SafeExamBrowser.UserInterface/Taskbar.xaml.cs +++ b/SafeExamBrowser.UserInterface/Taskbar.xaml.cs @@ -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)); - } } } diff --git a/SafeExamBrowser.UserInterface/Utilities/VisualExtensions.cs b/SafeExamBrowser.UserInterface/Utilities/VisualExtensions.cs new file mode 100644 index 00000000..749e76cf --- /dev/null +++ b/SafeExamBrowser.UserInterface/Utilities/VisualExtensions.cs @@ -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 + { + /// + /// 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 + /// + 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)); + } + } +}