Restore SEBPatch
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (c) 2024 ETH Zürich, IT Services
|
||||
*
|
||||
* 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;
|
||||
using System.ComponentModel;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace SafeExamBrowser.UserInterface.Mobile.ViewModels
|
||||
{
|
||||
internal class DateTimeViewModel : INotifyPropertyChanged
|
||||
{
|
||||
private DispatcherTimer timer;
|
||||
private readonly bool showSeconds;
|
||||
|
||||
public string Date { get; private set; }
|
||||
public string Time { get; private set; }
|
||||
public string ToolTip { get; private set; }
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
public DateTimeViewModel(bool showSeconds)
|
||||
{
|
||||
this.showSeconds = showSeconds;
|
||||
this.timer = new DispatcherTimer();
|
||||
this.timer.Interval = TimeSpan.FromMilliseconds(250);
|
||||
this.timer.Tick += Timer_Tick;
|
||||
this.timer.Start();
|
||||
}
|
||||
|
||||
private void Timer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
var date = DateTime.Now;
|
||||
|
||||
Date = date.ToShortDateString();
|
||||
Time = showSeconds ? date.ToLongTimeString() : date.ToShortTimeString();
|
||||
ToolTip = $"{date.ToLongDateString()} {date.ToLongTimeString()}";
|
||||
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Time)));
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Date)));
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ToolTip)));
|
||||
}
|
||||
}
|
||||
}
|
109
SafeExamBrowser.UserInterface.Mobile/ViewModels/LogViewModel.cs
Normal file
109
SafeExamBrowser.UserInterface.Mobile/ViewModels/LogViewModel.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright (c) 2024 ETH Zürich, IT Services
|
||||
*
|
||||
* 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;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Media;
|
||||
using SafeExamBrowser.I18n.Contracts;
|
||||
using SafeExamBrowser.Logging.Contracts;
|
||||
using SafeExamBrowser.Settings.Logging;
|
||||
|
||||
namespace SafeExamBrowser.UserInterface.Mobile.ViewModels
|
||||
{
|
||||
internal class LogViewModel : ILogObserver
|
||||
{
|
||||
private IText text;
|
||||
private ScrollViewer scrollViewer;
|
||||
private TextBlock textBlock;
|
||||
|
||||
public bool AutoScroll { get; set; } = true;
|
||||
public string AutoScrollTitle => text.Get(TextKey.LogWindow_AutoScroll);
|
||||
public string TopmostTitle => text.Get(TextKey.LogWindow_AlwaysOnTop);
|
||||
public string WindowTitle => text.Get(TextKey.LogWindow_Title);
|
||||
|
||||
public LogViewModel(IText text, ScrollViewer scrollViewer, TextBlock textBlock)
|
||||
{
|
||||
this.text = text;
|
||||
this.scrollViewer = scrollViewer;
|
||||
this.textBlock = textBlock;
|
||||
}
|
||||
|
||||
public void Notify(ILogContent content)
|
||||
{
|
||||
switch (content)
|
||||
{
|
||||
case ILogText text:
|
||||
AppendLogText(text);
|
||||
break;
|
||||
case ILogMessage message:
|
||||
AppendLogMessage(message);
|
||||
break;
|
||||
default:
|
||||
throw new NotImplementedException($"The log window is not yet implemented for log content of type {content.GetType()}!");
|
||||
}
|
||||
}
|
||||
|
||||
private void AppendLogText(ILogText logText)
|
||||
{
|
||||
textBlock.Dispatcher.InvokeAsync(() =>
|
||||
{
|
||||
var isHeader = logText.Text.StartsWith("/* ");
|
||||
var isComment = logText.Text.StartsWith("# ");
|
||||
var brush = isHeader || isComment ? Brushes.LimeGreen : textBlock.Foreground;
|
||||
var fontWeight = isHeader ? FontWeights.Bold : FontWeights.Normal;
|
||||
|
||||
textBlock.Inlines.Add(new Run($"{logText.Text}{Environment.NewLine}") { FontWeight = fontWeight, Foreground = brush });
|
||||
ScrollToEnd();
|
||||
});
|
||||
}
|
||||
|
||||
private void AppendLogMessage(ILogMessage message)
|
||||
{
|
||||
textBlock.Dispatcher.InvokeAsync(() =>
|
||||
{
|
||||
var date = message.DateTime.ToString("HH:mm:ss.fff");
|
||||
var severity = message.Severity.ToString().ToUpper();
|
||||
var threadId = message.ThreadInfo.Id < 10 ? $"0{message.ThreadInfo.Id}" : message.ThreadInfo.Id.ToString();
|
||||
var threadName = message.ThreadInfo.HasName ? ": " + message.ThreadInfo.Name : string.Empty;
|
||||
var threadInfo = $"[{threadId}{threadName}]";
|
||||
|
||||
var infoRun = new Run($"{date} {threadInfo} - ") { Foreground = Brushes.DarkGray };
|
||||
var messageRun = new Run($"{severity}: {message.Message}{Environment.NewLine}") { Foreground = GetBrushFor(message.Severity) };
|
||||
|
||||
textBlock.Inlines.Add(infoRun);
|
||||
textBlock.Inlines.Add(messageRun);
|
||||
ScrollToEnd();
|
||||
});
|
||||
}
|
||||
|
||||
private Brush GetBrushFor(LogLevel severity)
|
||||
{
|
||||
switch (severity)
|
||||
{
|
||||
case LogLevel.Debug:
|
||||
return Brushes.DarkGray;
|
||||
case LogLevel.Error:
|
||||
return Brushes.Red;
|
||||
case LogLevel.Warning:
|
||||
return Brushes.Orange;
|
||||
default:
|
||||
return Brushes.White;
|
||||
}
|
||||
}
|
||||
|
||||
private void ScrollToEnd()
|
||||
{
|
||||
if (AutoScroll)
|
||||
{
|
||||
scrollViewer.ScrollToEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright (c) 2024 ETH Zürich, IT Services
|
||||
*
|
||||
* 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.ComponentModel;
|
||||
using System.Timers;
|
||||
|
||||
namespace SafeExamBrowser.UserInterface.Mobile.ViewModels
|
||||
{
|
||||
internal class ProgressIndicatorViewModel : INotifyPropertyChanged
|
||||
{
|
||||
private readonly object @lock = new object();
|
||||
|
||||
private Timer busyTimer;
|
||||
private int currentProgress;
|
||||
private bool isIndeterminate;
|
||||
private int maxProgress;
|
||||
private string status;
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
public bool BusyIndication
|
||||
{
|
||||
set
|
||||
{
|
||||
HandleBusyIndication(value);
|
||||
}
|
||||
}
|
||||
|
||||
public int CurrentProgress
|
||||
{
|
||||
get
|
||||
{
|
||||
return currentProgress;
|
||||
}
|
||||
set
|
||||
{
|
||||
currentProgress = value;
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentProgress)));
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsIndeterminate
|
||||
{
|
||||
get
|
||||
{
|
||||
return isIndeterminate;
|
||||
}
|
||||
set
|
||||
{
|
||||
isIndeterminate = value;
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsIndeterminate)));
|
||||
}
|
||||
}
|
||||
|
||||
public int MaxProgress
|
||||
{
|
||||
get
|
||||
{
|
||||
return maxProgress;
|
||||
}
|
||||
set
|
||||
{
|
||||
maxProgress = value;
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MaxProgress)));
|
||||
}
|
||||
}
|
||||
|
||||
public string Status
|
||||
{
|
||||
get
|
||||
{
|
||||
return status;
|
||||
}
|
||||
set
|
||||
{
|
||||
status = value;
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Status)));
|
||||
}
|
||||
}
|
||||
|
||||
protected void OnPropertyChanged(string propertyName)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
|
||||
private void HandleBusyIndication(bool start)
|
||||
{
|
||||
lock (@lock)
|
||||
{
|
||||
if (busyTimer != null)
|
||||
{
|
||||
busyTimer.Elapsed -= BusyTimer_Elapsed;
|
||||
busyTimer.Stop();
|
||||
busyTimer.Close();
|
||||
}
|
||||
|
||||
if (start)
|
||||
{
|
||||
busyTimer = new Timer
|
||||
{
|
||||
AutoReset = true,
|
||||
Interval = 1500,
|
||||
};
|
||||
busyTimer.Elapsed += BusyTimer_Elapsed;
|
||||
busyTimer.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void BusyTimer_Elapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
var next = Status ?? string.Empty;
|
||||
|
||||
if (next.EndsWith("..."))
|
||||
{
|
||||
next = Status.Substring(0, Status.Length - 3);
|
||||
}
|
||||
else
|
||||
{
|
||||
next += ".";
|
||||
}
|
||||
|
||||
Status = next;
|
||||
busyTimer.Interval = 750;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (c) 2024 ETH Zürich, IT Services
|
||||
*
|
||||
* 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;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Media;
|
||||
using SafeExamBrowser.Logging.Contracts;
|
||||
using SafeExamBrowser.Settings.Logging;
|
||||
|
||||
namespace SafeExamBrowser.UserInterface.Mobile.ViewModels
|
||||
{
|
||||
internal class RuntimeWindowViewModel : ProgressIndicatorViewModel
|
||||
{
|
||||
private Visibility progressBarVisibility;
|
||||
private readonly TextBlock textBlock;
|
||||
|
||||
public Visibility ProgressBarVisibility
|
||||
{
|
||||
get
|
||||
{
|
||||
return progressBarVisibility;
|
||||
}
|
||||
set
|
||||
{
|
||||
progressBarVisibility = value;
|
||||
OnPropertyChanged(nameof(ProgressBarVisibility));
|
||||
}
|
||||
}
|
||||
|
||||
public RuntimeWindowViewModel(TextBlock textBlock)
|
||||
{
|
||||
this.textBlock = textBlock;
|
||||
}
|
||||
|
||||
public void Notify(ILogContent content)
|
||||
{
|
||||
switch (content)
|
||||
{
|
||||
case ILogText text:
|
||||
AppendLogText(text);
|
||||
break;
|
||||
case ILogMessage message:
|
||||
AppendLogMessage(message);
|
||||
break;
|
||||
default:
|
||||
throw new NotImplementedException($"The runtime window is not yet implemented for log content of type {content.GetType()}!");
|
||||
}
|
||||
}
|
||||
|
||||
private void AppendLogMessage(ILogMessage message)
|
||||
{
|
||||
var time = message.DateTime.ToString("HH:mm:ss.fff");
|
||||
var severity = message.Severity.ToString().ToUpper();
|
||||
|
||||
var infoRun = new Run($"{time} - ") { Foreground = Brushes.Gray };
|
||||
var messageRun = new Run($"{severity}: {message.Message}{Environment.NewLine}") { Foreground = GetBrushFor(message.Severity) };
|
||||
|
||||
textBlock.Inlines.Add(infoRun);
|
||||
textBlock.Inlines.Add(messageRun);
|
||||
}
|
||||
|
||||
private void AppendLogText(ILogText text)
|
||||
{
|
||||
textBlock.Inlines.Add(new Run($"{text.Text}{Environment.NewLine}"));
|
||||
}
|
||||
|
||||
private Brush GetBrushFor(LogLevel severity)
|
||||
{
|
||||
switch (severity)
|
||||
{
|
||||
case LogLevel.Debug:
|
||||
return Brushes.Gray;
|
||||
case LogLevel.Error:
|
||||
return Brushes.Red;
|
||||
case LogLevel.Warning:
|
||||
return Brushes.Orange;
|
||||
default:
|
||||
return Brushes.Black;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user