diff --git a/SafeExamBrowser.Configuration/ConfigurationData/DataMapper.Browser.cs b/SafeExamBrowser.Configuration/ConfigurationData/DataMapper.Browser.cs index c5c543bb..a96f4999 100644 --- a/SafeExamBrowser.Configuration/ConfigurationData/DataMapper.Browser.cs +++ b/SafeExamBrowser.Configuration/ConfigurationData/DataMapper.Browser.cs @@ -197,5 +197,111 @@ namespace SafeExamBrowser.Configuration.ConfigurationData } } } + + private void MapWindowHeightAdditionalWindow(AppSettings settings, object value) + { + if (value is string raw) + { + if (raw.EndsWith("%") && int.TryParse(raw.Replace("%", string.Empty), out var relativeHeight)) + { + settings.Browser.AdditionalWindow.RelativeHeight = relativeHeight; + } + else if (int.TryParse(raw, out var absoluteHeight)) + { + settings.Browser.AdditionalWindow.AbsoluteHeight = absoluteHeight; + } + } + } + + private void MapWindowHeightMainWindow(AppSettings settings, object value) + { + if (value is string raw) + { + if (raw.EndsWith("%") && int.TryParse(raw.Replace("%", string.Empty), out var relativeHeight)) + { + settings.Browser.MainWindow.RelativeHeight = relativeHeight; + } + else if (int.TryParse(raw, out var absoluteHeight)) + { + settings.Browser.MainWindow.AbsoluteHeight = absoluteHeight; + } + } + } + + private void MapWindowPositionAdditionalWindow(AppSettings settings, object value) + { + const int LEFT = 0; + const int CENTER = 1; + const int RIGHT = 2; + + if (value is int position) + { + switch (position) + { + case LEFT: + settings.Browser.AdditionalWindow.Position = BrowserWindowPosition.Left; + break; + case CENTER: + settings.Browser.AdditionalWindow.Position = BrowserWindowPosition.Center; + break; + case RIGHT: + settings.Browser.AdditionalWindow.Position = BrowserWindowPosition.Right; + break; + } + } + } + + private void MapWindowPositionMainWindow(AppSettings settings, object value) + { + const int LEFT = 0; + const int CENTER = 1; + const int RIGHT = 2; + + if (value is int position) + { + switch (position) + { + case LEFT: + settings.Browser.MainWindow.Position = BrowserWindowPosition.Left; + break; + case CENTER: + settings.Browser.MainWindow.Position = BrowserWindowPosition.Center; + break; + case RIGHT: + settings.Browser.MainWindow.Position = BrowserWindowPosition.Right; + break; + } + } + } + + private void MapWindowWidthAdditionalWindow(AppSettings settings, object value) + { + if (value is string raw) + { + if (raw.EndsWith("%") && int.TryParse(raw.Replace("%", string.Empty), out var relativeWidth)) + { + settings.Browser.AdditionalWindow.RelativeWidth = relativeWidth; + } + else if (int.TryParse(raw, out var absoluteWidth)) + { + settings.Browser.AdditionalWindow.AbsoluteWidth = absoluteWidth; + } + } + } + + private void MapWindowWidthMainWindow(AppSettings settings, object value) + { + if (value is string raw) + { + if (raw.EndsWith("%") && int.TryParse(raw.Replace("%", string.Empty), out var relativeWidth)) + { + settings.Browser.MainWindow.RelativeWidth = relativeWidth; + } + else if (int.TryParse(raw, out var absoluteWidth)) + { + settings.Browser.MainWindow.AbsoluteWidth = absoluteWidth; + } + } + } } } diff --git a/SafeExamBrowser.Configuration/ConfigurationData/DataMapper.cs b/SafeExamBrowser.Configuration/ConfigurationData/DataMapper.cs index 3b6884ee..7dd85246 100644 --- a/SafeExamBrowser.Configuration/ConfigurationData/DataMapper.cs +++ b/SafeExamBrowser.Configuration/ConfigurationData/DataMapper.cs @@ -81,9 +81,6 @@ namespace SafeExamBrowser.Configuration.ConfigurationData case Keys.Browser.AllowPopups: MapAllowPopups(settings, value); break; - case Keys.Browser.MainWindowMode: - MapMainWindowMode(settings, value); - break; case Keys.Browser.AdditionalWindow.AllowAddressBar: MapAllowAddressBarAdditionalWindow(settings, value); break; @@ -96,6 +93,15 @@ namespace SafeExamBrowser.Configuration.ConfigurationData case Keys.Browser.AdditionalWindow.ShowReloadWarning: MapShowReloadWarningAdditionalWindow(settings, value); break; + case Keys.Browser.AdditionalWindow.WindowHeight: + MapWindowHeightAdditionalWindow(settings, value); + break; + case Keys.Browser.AdditionalWindow.WindowPosition: + MapWindowPositionAdditionalWindow(settings, value); + break; + case Keys.Browser.AdditionalWindow.WindowWidth: + MapWindowWidthAdditionalWindow(settings, value); + break; case Keys.Browser.Filter.UrlFilterRules: MapUrlFilterRules(settings, value); break; @@ -111,6 +117,18 @@ namespace SafeExamBrowser.Configuration.ConfigurationData case Keys.Browser.MainWindow.ShowReloadWarning: MapShowReloadWarning(settings, value); break; + case Keys.Browser.MainWindow.WindowHeight: + MapWindowHeightMainWindow(settings, value); + break; + case Keys.Browser.MainWindow.WindowMode: + MapMainWindowMode(settings, value); + break; + case Keys.Browser.MainWindow.WindowPosition: + MapWindowPositionMainWindow(settings, value); + break; + case Keys.Browser.MainWindow.WindowWidth: + MapWindowWidthMainWindow(settings, value); + break; } } diff --git a/SafeExamBrowser.Configuration/ConfigurationData/Keys.cs b/SafeExamBrowser.Configuration/ConfigurationData/Keys.cs index 6dcdb5c9..5ba013de 100644 --- a/SafeExamBrowser.Configuration/ConfigurationData/Keys.cs +++ b/SafeExamBrowser.Configuration/ConfigurationData/Keys.cs @@ -52,10 +52,20 @@ namespace SafeExamBrowser.Configuration.ConfigurationData internal const string AllowPopups = "blockPopUpWindows"; internal const string CustomUserAgentDesktop = "browserUserAgentWinDesktopModeCustom"; internal const string CustomUserAgentMobile = "browserUserAgentWinTouchModeCustom"; - internal const string MainWindowMode = "browserViewMode"; internal const string UserAgentModeDesktop = "browserUserAgentWinDesktopMode"; internal const string UserAgentModeMobile = "browserUserAgentWinTouchMode"; + internal static class AdditionalWindow + { + internal const string AllowAddressBar = "newBrowserWindowAllowAddressBar"; + internal const string AllowNavigation = "newBrowserWindowNavigation"; + internal const string AllowReload = "newBrowserWindowAllowReload"; + internal const string ShowReloadWarning = "newBrowserWindowShowReloadWarning"; + internal const string WindowHeight = "newBrowserWindowByLinkHeight"; + internal const string WindowWidth = "newBrowserWindowByLinkWidth"; + internal const string WindowPosition = "newBrowserWindowByLinkPositioning"; + } + internal static class Filter { internal const string EnableContentRequestFilter = "URLFilterEnableContentFilter"; @@ -73,14 +83,10 @@ namespace SafeExamBrowser.Configuration.ConfigurationData internal const string AllowNavigation = "allowBrowsingBackForward"; internal const string AllowReload = "browserWindowAllowReload"; internal const string ShowReloadWarning = "showReloadWarning"; - } - - internal static class AdditionalWindow - { - internal const string AllowAddressBar = "newBrowserWindowAllowAddressBar"; - internal const string AllowNavigation = "newBrowserWindowNavigation"; - internal const string AllowReload = "newBrowserWindowAllowReload"; - internal const string ShowReloadWarning = "newBrowserWindowShowReloadWarning"; + internal const string WindowHeight = "mainBrowserWindowHeight"; + internal const string WindowMode = "browserViewMode"; + internal const string WindowWidth = "mainBrowserWindowWidth"; + internal const string WindowPosition = "mainBrowserWindowPositioning"; } } diff --git a/SafeExamBrowser.Settings/Browser/BrowserWindowPosition.cs b/SafeExamBrowser.Settings/Browser/BrowserWindowPosition.cs new file mode 100644 index 00000000..f1dfce02 --- /dev/null +++ b/SafeExamBrowser.Settings/Browser/BrowserWindowPosition.cs @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2019 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/. + */ + +namespace SafeExamBrowser.Settings.Browser +{ + /// + /// Defines the initial position of a browser window. + /// + public enum BrowserWindowPosition + { + Left, + Center, + Right + } +} diff --git a/SafeExamBrowser.Settings/Browser/BrowserWindowSettings.cs b/SafeExamBrowser.Settings/Browser/BrowserWindowSettings.cs index 62d327ed..c3d8f754 100644 --- a/SafeExamBrowser.Settings/Browser/BrowserWindowSettings.cs +++ b/SafeExamBrowser.Settings/Browser/BrowserWindowSettings.cs @@ -16,6 +16,16 @@ namespace SafeExamBrowser.Settings.Browser [Serializable] public class BrowserWindowSettings { + /// + /// Optionally defines the height of the browser window in physical pixels. + /// + public int? AbsoluteHeight { get; set; } + + /// + /// Optionally defines the width of the browser window in physical pixels. + /// + public int? AbsoluteWidth { get; set; } + /// /// Determines whether the user will be allowed to change the URL in the address bar. /// @@ -46,6 +56,21 @@ namespace SafeExamBrowser.Settings.Browser /// public bool FullScreenMode { get; set; } + /// + /// Determines the initial position of the browser window (if it is not maximized). + /// + public BrowserWindowPosition Position { get; set; } + + /// + /// Optionally defines the height of the browser window as percentage of the working area height. + /// + public int? RelativeHeight { get; set; } + + /// + /// Optionally defines the width of the browser window as percentage of the working area width. + /// + public int? RelativeWidth { get; set; } + /// /// Determines whether the user will need to confirm every reload attempt. /// diff --git a/SafeExamBrowser.Settings/SafeExamBrowser.Settings.csproj b/SafeExamBrowser.Settings/SafeExamBrowser.Settings.csproj index e9ccacdb..0e1102c7 100644 --- a/SafeExamBrowser.Settings/SafeExamBrowser.Settings.csproj +++ b/SafeExamBrowser.Settings/SafeExamBrowser.Settings.csproj @@ -59,6 +59,7 @@ + diff --git a/SafeExamBrowser.UserInterface.Desktop/BrowserWindow.xaml.cs b/SafeExamBrowser.UserInterface.Desktop/BrowserWindow.xaml.cs index 27ad8055..5fe6fceb 100644 --- a/SafeExamBrowser.UserInterface.Desktop/BrowserWindow.xaml.cs +++ b/SafeExamBrowser.UserInterface.Desktop/BrowserWindow.xaml.cs @@ -261,28 +261,65 @@ namespace SafeExamBrowser.UserInterface.Desktop private void InitializeBounds() { - if (isMainWindow) + if (isMainWindow && WindowSettings.FullScreenMode) { - if (WindowSettings.FullScreenMode) - { - Top = 0; - Left = 0; - Height = SystemParameters.WorkArea.Height; - Width = SystemParameters.WorkArea.Width; - ResizeMode = ResizeMode.NoResize; - WindowStyle = WindowStyle.None; - } - else - { - WindowState = WindowState.Maximized; - } + Top = 0; + Left = 0; + Height = SystemParameters.WorkArea.Height; + Width = SystemParameters.WorkArea.Width; + ResizeMode = ResizeMode.NoResize; + WindowStyle = WindowStyle.None; + } + else if (WindowSettings.RelativeHeight == 100 && WindowSettings.RelativeWidth == 100) + { + WindowState = WindowState.Maximized; } else { - Top = 0; - Left = SystemParameters.WorkArea.Width / 2; - Height = SystemParameters.WorkArea.Height; - Width = SystemParameters.WorkArea.Width / 2; + if (WindowSettings.RelativeHeight > 0) + { + Height = SystemParameters.WorkArea.Height * WindowSettings.RelativeHeight.Value / 100; + Top = (SystemParameters.WorkArea.Height / 2) - (Height / 2); + } + else if (WindowSettings.AbsoluteHeight > 0) + { + Height = this.TransformFromPhysical(0, WindowSettings.AbsoluteHeight.Value).Y; + Top = (SystemParameters.WorkArea.Height / 2) - (Height / 2); + } + + if (WindowSettings.RelativeWidth > 0) + { + Width = SystemParameters.WorkArea.Width * WindowSettings.RelativeWidth.Value / 100; + } + else if (WindowSettings.AbsoluteWidth > 0) + { + Width = this.TransformFromPhysical(WindowSettings.AbsoluteWidth.Value, 0).X; + } + + if (Height > SystemParameters.WorkArea.Height) + { + Top = 0; + Height = SystemParameters.WorkArea.Height; + } + + if (Width > SystemParameters.WorkArea.Width) + { + Left = 0; + Width = SystemParameters.WorkArea.Width; + } + + switch (WindowSettings.Position) + { + case BrowserWindowPosition.Left: + Left = 0; + break; + case BrowserWindowPosition.Center: + Left = (SystemParameters.WorkArea.Width / 2) - (Width / 2); + break; + case BrowserWindowPosition.Right: + Left = SystemParameters.WorkArea.Width - Width; + break; + } } } diff --git a/SafeExamBrowser.UserInterface.Mobile/BrowserWindow.xaml.cs b/SafeExamBrowser.UserInterface.Mobile/BrowserWindow.xaml.cs index f0ac98b9..19b5b41b 100644 --- a/SafeExamBrowser.UserInterface.Mobile/BrowserWindow.xaml.cs +++ b/SafeExamBrowser.UserInterface.Mobile/BrowserWindow.xaml.cs @@ -270,28 +270,65 @@ namespace SafeExamBrowser.UserInterface.Mobile private void InitializeBounds() { - if (isMainWindow) + if (isMainWindow && WindowSettings.FullScreenMode) { - if (WindowSettings.FullScreenMode) - { - Top = 0; - Left = 0; - Height = SystemParameters.WorkArea.Height; - Width = SystemParameters.WorkArea.Width; - ResizeMode = ResizeMode.NoResize; - WindowStyle = WindowStyle.None; - } - else - { - WindowState = WindowState.Maximized; - } + Top = 0; + Left = 0; + Height = SystemParameters.WorkArea.Height; + Width = SystemParameters.WorkArea.Width; + ResizeMode = ResizeMode.NoResize; + WindowStyle = WindowStyle.None; + } + else if (WindowSettings.RelativeHeight == 100 && WindowSettings.RelativeWidth == 100) + { + WindowState = WindowState.Maximized; } else { - Top = 0; - Left = SystemParameters.WorkArea.Width / 2; - Height = SystemParameters.WorkArea.Height; - Width = SystemParameters.WorkArea.Width / 2; + if (WindowSettings.RelativeHeight > 0) + { + Height = SystemParameters.WorkArea.Height * WindowSettings.RelativeHeight.Value / 100; + Top = (SystemParameters.WorkArea.Height / 2) - (Height / 2); + } + else if (WindowSettings.AbsoluteHeight > 0) + { + Height = this.TransformFromPhysical(0, WindowSettings.AbsoluteHeight.Value).Y; + Top = (SystemParameters.WorkArea.Height / 2) - (Height / 2); + } + + if (WindowSettings.RelativeWidth > 0) + { + Width = SystemParameters.WorkArea.Width * WindowSettings.RelativeWidth.Value / 100; + } + else if (WindowSettings.AbsoluteWidth > 0) + { + Width = this.TransformFromPhysical(WindowSettings.AbsoluteWidth.Value, 0).X; + } + + if (Height > SystemParameters.WorkArea.Height) + { + Top = 0; + Height = SystemParameters.WorkArea.Height; + } + + if (Width > SystemParameters.WorkArea.Width) + { + Left = 0; + Width = SystemParameters.WorkArea.Width; + } + + switch (WindowSettings.Position) + { + case BrowserWindowPosition.Left: + Left = 0; + break; + case BrowserWindowPosition.Center: + Left = (SystemParameters.WorkArea.Width / 2) - (Width / 2); + break; + case BrowserWindowPosition.Right: + Left = SystemParameters.WorkArea.Width - Width; + break; + } } }