2017-07-05 17:21:52 +02:00
|
|
|
|
/*
|
2023-03-08 00:30:20 +01:00
|
|
|
|
* Copyright (c) 2023 ETH Zürich, Educational Development and Technology (LET)
|
2017-07-05 17:21:52 +02:00
|
|
|
|
*
|
|
|
|
|
* 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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-11-24 08:42:07 +01:00
|
|
|
|
using System;
|
2018-02-20 15:15:26 +01:00
|
|
|
|
using System.ComponentModel;
|
2017-07-05 17:21:52 +02:00
|
|
|
|
using System.Windows;
|
2022-05-02 16:33:35 +02:00
|
|
|
|
using SafeExamBrowser.Browser.Contracts.Events;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.I18n.Contracts;
|
|
|
|
|
using SafeExamBrowser.Logging.Contracts;
|
|
|
|
|
using SafeExamBrowser.UserInterface.Contracts.Shell;
|
|
|
|
|
using SafeExamBrowser.UserInterface.Contracts.Shell.Events;
|
2019-05-08 09:56:34 +02:00
|
|
|
|
using SafeExamBrowser.UserInterface.Shared.Utilities;
|
2017-07-05 17:21:52 +02:00
|
|
|
|
|
2020-03-16 18:29:06 +01:00
|
|
|
|
namespace SafeExamBrowser.UserInterface.Desktop.Windows
|
2017-07-05 17:21:52 +02:00
|
|
|
|
{
|
2020-03-17 11:07:40 +01:00
|
|
|
|
internal partial class Taskbar : Window, ITaskbar
|
2017-07-05 17:21:52 +02:00
|
|
|
|
{
|
2022-05-06 15:52:37 +02:00
|
|
|
|
private readonly ILogger logger;
|
|
|
|
|
|
2018-06-21 08:54:43 +02:00
|
|
|
|
private bool allowClose;
|
2021-11-24 08:42:07 +01:00
|
|
|
|
private bool isQuitButtonFocusedAtKeyDown;
|
|
|
|
|
private bool isFirstChildFocusedAtKeyDown;
|
2017-08-11 08:28:17 +02:00
|
|
|
|
|
2019-01-11 08:25:40 +01:00
|
|
|
|
public bool ShowClock
|
|
|
|
|
{
|
|
|
|
|
set { Dispatcher.Invoke(() => Clock.Visibility = value ? Visibility.Visible : Visibility.Collapsed); }
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 12:37:54 +02:00
|
|
|
|
public bool ShowQuitButton
|
|
|
|
|
{
|
|
|
|
|
set { Dispatcher.Invoke(() => QuitButton.Visibility = value ? Visibility.Visible : Visibility.Collapsed); }
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 08:42:07 +01:00
|
|
|
|
public event LoseFocusRequestedEventHandler LoseFocusRequested;
|
2018-02-20 15:15:26 +01:00
|
|
|
|
public event QuitButtonClickedEventHandler QuitButtonClicked;
|
|
|
|
|
|
2020-03-17 11:07:40 +01:00
|
|
|
|
internal Taskbar(ILogger logger)
|
2017-07-05 17:21:52 +02:00
|
|
|
|
{
|
2017-08-22 09:37:17 +02:00
|
|
|
|
this.logger = logger;
|
|
|
|
|
|
2019-03-15 11:38:59 +01:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
InitializeTaskbar();
|
2017-07-05 17:21:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 08:45:37 +01:00
|
|
|
|
public void AddApplicationControl(IApplicationControl control, bool atFirstPosition = false)
|
2017-07-11 15:29:29 +02:00
|
|
|
|
{
|
2019-03-08 11:43:52 +01:00
|
|
|
|
if (control is UIElement uiElement)
|
2017-07-11 15:29:29 +02:00
|
|
|
|
{
|
2019-11-06 08:45:37 +01:00
|
|
|
|
if (atFirstPosition)
|
|
|
|
|
{
|
|
|
|
|
ApplicationStackPanel.Children.Insert(0, uiElement);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ApplicationStackPanel.Children.Add(uiElement);
|
|
|
|
|
}
|
2017-07-11 15:29:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-08 11:43:52 +01:00
|
|
|
|
public void AddNotificationControl(INotificationControl control)
|
2017-07-17 16:59:50 +02:00
|
|
|
|
{
|
2019-03-08 11:43:52 +01:00
|
|
|
|
if (control is UIElement uiElement)
|
2017-07-17 16:59:50 +02:00
|
|
|
|
{
|
2018-02-19 14:36:00 +01:00
|
|
|
|
NotificationStackPanel.Children.Add(uiElement);
|
2017-07-17 16:59:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 15:30:31 +02:00
|
|
|
|
public void AddSystemControl(ISystemControl control)
|
|
|
|
|
{
|
2018-02-19 14:36:00 +01:00
|
|
|
|
if (control is UIElement uiElement)
|
2017-08-15 15:30:31 +02:00
|
|
|
|
{
|
2018-02-19 14:36:00 +01:00
|
|
|
|
SystemControlStackPanel.Children.Add(uiElement);
|
2017-08-15 15:30:31 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 15:15:26 +01:00
|
|
|
|
public new void Close()
|
|
|
|
|
{
|
|
|
|
|
Dispatcher.Invoke(base.Close);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 15:52:37 +02:00
|
|
|
|
public void Focus(bool forward)
|
|
|
|
|
{
|
|
|
|
|
Dispatcher.BeginInvoke((Action) (() =>
|
|
|
|
|
{
|
|
|
|
|
Activate();
|
|
|
|
|
|
2022-10-27 10:27:55 +02:00
|
|
|
|
if (forward && ApplicationStackPanel.Children.Count > 0)
|
2022-05-06 15:52:37 +02:00
|
|
|
|
{
|
2022-10-27 10:27:55 +02:00
|
|
|
|
SetFocusWithin(ApplicationStackPanel.Children[0]);
|
2022-05-06 15:52:37 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
QuitButton.Focus();
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 14:16:47 +02:00
|
|
|
|
public int GetAbsoluteHeight()
|
2017-07-05 17:21:52 +02:00
|
|
|
|
{
|
2017-07-26 14:36:20 +02:00
|
|
|
|
return Dispatcher.Invoke(() =>
|
2017-07-20 14:16:47 +02:00
|
|
|
|
{
|
2017-08-22 10:29:00 +02:00
|
|
|
|
var height = (int) this.TransformToPhysical(Width, Height).Y;
|
2017-07-26 14:36:20 +02:00
|
|
|
|
|
2019-03-19 16:09:07 +01:00
|
|
|
|
logger.Debug($"Calculated physical taskbar height is {height}px.");
|
|
|
|
|
|
|
|
|
|
return height;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetRelativeHeight()
|
|
|
|
|
{
|
|
|
|
|
return Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
var height = (int) Height;
|
|
|
|
|
|
|
|
|
|
logger.Debug($"Logical taskbar height is {height}px.");
|
2017-07-20 14:16:47 +02:00
|
|
|
|
|
2017-08-22 10:29:00 +02:00
|
|
|
|
return height;
|
2017-07-26 14:36:20 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void InitializeBounds()
|
|
|
|
|
{
|
|
|
|
|
Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
2019-03-19 16:09:07 +01:00
|
|
|
|
Width = SystemParameters.PrimaryScreenWidth;
|
|
|
|
|
Left = 0;
|
|
|
|
|
Top = SystemParameters.PrimaryScreenHeight - Height;
|
2017-08-11 08:28:17 +02:00
|
|
|
|
|
2017-08-22 10:29:00 +02:00
|
|
|
|
var position = this.TransformToPhysical(Left, Top);
|
|
|
|
|
var size = this.TransformToPhysical(Width, Height);
|
2017-08-11 08:28:17 +02:00
|
|
|
|
|
2019-03-19 16:09:07 +01:00
|
|
|
|
logger.Debug($"Set taskbar bounds to {Width}x{Height} at ({Left}/{Top}), in physical pixels: {size.X}x{size.Y} at ({position.X}/{position.Y}).");
|
2017-07-26 14:36:20 +02:00
|
|
|
|
});
|
2017-07-05 17:21:52 +02:00
|
|
|
|
}
|
2017-08-17 12:17:58 +02:00
|
|
|
|
|
2022-05-06 15:52:37 +02:00
|
|
|
|
private void InitializeTaskbar()
|
|
|
|
|
{
|
|
|
|
|
Closing += Taskbar_Closing;
|
|
|
|
|
Loaded += (o, args) => InitializeBounds();
|
|
|
|
|
QuitButton.Clicked += QuitButton_Clicked;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-15 11:38:59 +01:00
|
|
|
|
public void InitializeText(IText text)
|
|
|
|
|
{
|
|
|
|
|
Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
2022-02-11 10:54:37 +01:00
|
|
|
|
var txt = text.Get(TextKey.Shell_QuitButton);
|
|
|
|
|
QuitButton.ToolTip = txt;
|
|
|
|
|
QuitButton.SetValue(System.Windows.Automation.AutomationProperties.NameProperty, txt);
|
2019-03-15 11:38:59 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 15:52:37 +02:00
|
|
|
|
public void Register(ITaskbarActivator activator)
|
|
|
|
|
{
|
|
|
|
|
activator.Activated += Activator_Activated;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 16:10:00 +01:00
|
|
|
|
public new void Show()
|
|
|
|
|
{
|
|
|
|
|
Dispatcher.Invoke(base.Show);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 15:52:37 +02:00
|
|
|
|
private void Activator_Activated()
|
|
|
|
|
{
|
|
|
|
|
(this as ITaskbar).Focus(true);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 08:54:43 +02:00
|
|
|
|
private void QuitButton_Clicked(CancelEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
QuitButtonClicked?.Invoke(args);
|
|
|
|
|
allowClose = !args.Cancel;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 15:15:26 +01:00
|
|
|
|
private void Taskbar_Closing(object sender, CancelEventArgs e)
|
2017-08-17 12:17:58 +02:00
|
|
|
|
{
|
2019-09-04 12:07:32 +02:00
|
|
|
|
if (allowClose)
|
2018-06-21 08:54:43 +02:00
|
|
|
|
{
|
2019-09-04 12:07:32 +02:00
|
|
|
|
foreach (var child in SystemControlStackPanel.Children)
|
2017-08-22 10:29:00 +02:00
|
|
|
|
{
|
2019-09-04 12:07:32 +02:00
|
|
|
|
if (child is ISystemControl systemControl)
|
|
|
|
|
{
|
|
|
|
|
systemControl.Close();
|
|
|
|
|
}
|
2017-08-22 10:29:00 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-04 12:07:32 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
}
|
2017-08-17 12:17:58 +02:00
|
|
|
|
}
|
2019-03-15 11:38:59 +01:00
|
|
|
|
|
2021-11-24 08:42:07 +01:00
|
|
|
|
private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
|
|
|
|
|
{
|
2022-05-06 15:52:37 +02:00
|
|
|
|
isQuitButtonFocusedAtKeyDown = QuitButton.IsKeyboardFocusWithin;
|
2023-03-27 16:55:17 +02:00
|
|
|
|
isFirstChildFocusedAtKeyDown = ApplicationStackPanel.Children.Count > 0 && ApplicationStackPanel.Children[0].IsKeyboardFocusWithin;
|
2021-11-24 08:42:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Key == System.Windows.Input.Key.Tab)
|
|
|
|
|
{
|
|
|
|
|
var shift = System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.LeftShift);
|
2023-03-27 16:55:17 +02:00
|
|
|
|
var hasFocus = ApplicationStackPanel.Children.Count > 0 && ApplicationStackPanel.Children[0].IsKeyboardFocusWithin;
|
|
|
|
|
|
|
|
|
|
if (!shift && hasFocus && isQuitButtonFocusedAtKeyDown)
|
2021-11-24 08:42:07 +01:00
|
|
|
|
{
|
2022-05-06 15:52:37 +02:00
|
|
|
|
LoseFocusRequested?.Invoke(true);
|
2021-11-24 08:42:07 +01:00
|
|
|
|
e.Handled = true;
|
|
|
|
|
}
|
2022-05-06 15:52:37 +02:00
|
|
|
|
else if (shift && QuitButton.IsKeyboardFocusWithin && isFirstChildFocusedAtKeyDown)
|
2021-11-24 08:42:07 +01:00
|
|
|
|
{
|
2022-05-06 15:52:37 +02:00
|
|
|
|
LoseFocusRequested?.Invoke(false);
|
2021-11-24 08:42:07 +01:00
|
|
|
|
e.Handled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 15:52:37 +02:00
|
|
|
|
isQuitButtonFocusedAtKeyDown = false;
|
|
|
|
|
isFirstChildFocusedAtKeyDown = false;
|
2021-11-24 08:42:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool SetFocusWithin(UIElement uIElement)
|
|
|
|
|
{
|
|
|
|
|
if (uIElement.Focusable)
|
|
|
|
|
{
|
|
|
|
|
uIElement.Focus();
|
2022-05-06 15:52:37 +02:00
|
|
|
|
|
2021-11-24 08:42:07 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (uIElement is System.Windows.Controls.Panel)
|
|
|
|
|
{
|
|
|
|
|
var panel = uIElement as System.Windows.Controls.Panel;
|
2022-05-06 15:52:37 +02:00
|
|
|
|
|
2021-11-24 08:42:07 +01:00
|
|
|
|
for (var i = 0; i < panel.Children.Count; i++)
|
|
|
|
|
{
|
2022-05-06 15:52:37 +02:00
|
|
|
|
if (SetFocusWithin(panel.Children[i]))
|
2021-11-24 08:42:07 +01:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else if (uIElement is System.Windows.Controls.ContentControl)
|
|
|
|
|
{
|
|
|
|
|
var control = uIElement as System.Windows.Controls.ContentControl;
|
|
|
|
|
var content = control.Content as UIElement;
|
2022-05-06 15:52:37 +02:00
|
|
|
|
|
2021-11-24 08:42:07 +01:00
|
|
|
|
if (content != null)
|
|
|
|
|
{
|
2022-05-06 15:52:37 +02:00
|
|
|
|
return SetFocusWithin(content);
|
2021-11-24 08:42:07 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-07-05 17:21:52 +02:00
|
|
|
|
}
|
|
|
|
|
}
|