2017-09-13 08:33:12 +02:00
|
|
|
|
/*
|
2019-01-09 11:25:21 +01:00
|
|
|
|
* Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET)
|
2017-09-13 08:33:12 +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/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using SafeExamBrowser.Contracts.I18n;
|
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
|
|
|
|
using SafeExamBrowser.Contracts.SystemComponents;
|
2019-03-06 16:10:00 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface.Shell;
|
2017-09-13 08:33:12 +02:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.SystemComponents
|
|
|
|
|
{
|
|
|
|
|
public class KeyboardLayout : ISystemComponent<ISystemKeyboardLayoutControl>
|
|
|
|
|
{
|
|
|
|
|
private IList<KeyboardLayoutDefinition> layouts = new List<KeyboardLayoutDefinition>();
|
|
|
|
|
private ILogger logger;
|
|
|
|
|
private InputLanguage originalLanguage;
|
2019-03-08 15:56:38 +01:00
|
|
|
|
private IList<ISystemKeyboardLayoutControl> controls;
|
2017-09-13 08:33:12 +02:00
|
|
|
|
private IText text;
|
|
|
|
|
|
|
|
|
|
public KeyboardLayout(ILogger logger, IText text)
|
|
|
|
|
{
|
2019-03-08 15:56:38 +01:00
|
|
|
|
this.controls = new List<ISystemKeyboardLayoutControl>();
|
2017-09-13 08:33:12 +02:00
|
|
|
|
this.logger = logger;
|
|
|
|
|
this.text = text;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-08 15:56:38 +01:00
|
|
|
|
public void Initialize()
|
2017-09-13 08:33:12 +02:00
|
|
|
|
{
|
|
|
|
|
originalLanguage = InputLanguage.CurrentInputLanguage;
|
|
|
|
|
logger.Info($"Saved current keyboard layout {ToString(originalLanguage)}.");
|
|
|
|
|
|
|
|
|
|
foreach (InputLanguage language in InputLanguage.InstalledInputLanguages)
|
|
|
|
|
{
|
|
|
|
|
var layout = new KeyboardLayoutDefinition
|
|
|
|
|
{
|
|
|
|
|
CultureCode = language.Culture.ThreeLetterISOLanguageName.ToUpper(),
|
|
|
|
|
IsCurrent = originalLanguage.Equals(language),
|
|
|
|
|
Language = language,
|
|
|
|
|
Name = language.LayoutName
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
layouts.Add(layout);
|
2019-03-08 15:56:38 +01:00
|
|
|
|
logger.Info($"Detected keyboard layout {ToString(language)}.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Register(ISystemKeyboardLayoutControl control)
|
|
|
|
|
{
|
|
|
|
|
control.LayoutSelected += Control_LayoutSelected;
|
|
|
|
|
control.SetTooltip(text.Get(TextKey.SystemControl_KeyboardLayoutTooltip));
|
2017-09-13 08:33:12 +02:00
|
|
|
|
|
2019-03-08 15:56:38 +01:00
|
|
|
|
foreach (var layout in layouts)
|
|
|
|
|
{
|
|
|
|
|
control.Add(layout);
|
2017-09-13 08:33:12 +02:00
|
|
|
|
}
|
2019-03-08 15:56:38 +01:00
|
|
|
|
|
|
|
|
|
controls.Add(control);
|
2017-09-13 08:33:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Terminate()
|
|
|
|
|
{
|
|
|
|
|
if (originalLanguage != null)
|
|
|
|
|
{
|
|
|
|
|
InputLanguage.CurrentInputLanguage = originalLanguage;
|
|
|
|
|
logger.Info($"Restored original keyboard layout {ToString(originalLanguage)}.");
|
|
|
|
|
}
|
2019-03-08 15:56:38 +01:00
|
|
|
|
|
|
|
|
|
foreach (var control in controls)
|
|
|
|
|
{
|
|
|
|
|
control.Close();
|
|
|
|
|
}
|
2017-09-13 08:33:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Control_LayoutSelected(IKeyboardLayout layout)
|
|
|
|
|
{
|
|
|
|
|
var language = layouts.First(l => l.Id == layout.Id).Language;
|
|
|
|
|
|
|
|
|
|
InputLanguage.CurrentInputLanguage = language;
|
|
|
|
|
|
|
|
|
|
foreach (var l in layouts)
|
|
|
|
|
{
|
|
|
|
|
l.IsCurrent = l.Id == layout.Id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Info($"Changed keyboard layout to {ToString(language)}.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string ToString(InputLanguage language)
|
|
|
|
|
{
|
|
|
|
|
return $"'{language.LayoutName}' ({language.Culture.ThreeLetterISOLanguageName.ToUpper()})";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|