Added auto closing message boxes for proxy warnings.

This commit is contained in:
ruri
2019-06-20 16:50:26 +02:00
parent 5cdbe0ceb0
commit 97b55c0f4e
5 changed files with 59 additions and 17 deletions
+50 -8
View File
@@ -59,9 +59,20 @@ namespace OpenBullet
public static ObservableCollection<RunnerViewModel> Runners = new ObservableCollection<RunnerViewModel>(); public static ObservableCollection<RunnerViewModel> Runners = new ObservableCollection<RunnerViewModel>();
// Logging Methods // Logging Methods
public static void Log(Components component, LogLevel level, string message, bool prompt = false) public static void Log(Components component, LogLevel level, string message, bool prompt = false, int timeout = 0)
{ {
if (prompt) MessageBox.Show(message, level.ToString()); if (prompt)
{
if (timeout == 0)
{
MessageBox.Show(message, level.ToString());
}
else
{
AutoClosingMessageBox.Show(message, level.ToString(), timeout * 1000);
}
}
if (!CanLog()) return; if (!CanLog()) return;
App.Current.Dispatcher.Invoke(new Action(() => App.Current.Dispatcher.Invoke(new Action(() =>
{ {
@@ -71,19 +82,19 @@ namespace OpenBullet
})); }));
} }
public static void LogInfo(Components component, string message, bool prompt = false) public static void LogInfo(Components component, string message, bool prompt = false, int timeout = 0)
{ {
Log(component, LogLevel.Info, message, prompt); Log(component, LogLevel.Info, message, prompt, timeout);
} }
public static void LogWarning(Components component, string message, bool prompt = false) public static void LogWarning(Components component, string message, bool prompt = false, int timeout = 0)
{ {
Log(component, LogLevel.Warning, message, prompt); Log(component, LogLevel.Warning, message, prompt, timeout);
} }
public static void LogError(Components component, string message, bool prompt = false) public static void LogError(Components component, string message, bool prompt = false, int timeout = 0)
{ {
Log(component, LogLevel.Error, message, prompt); Log(component, LogLevel.Error, message, prompt, timeout);
} }
private static bool CanLog() private static bool CanLog()
@@ -126,4 +137,35 @@ namespace OpenBullet
catch { return (SolidColorBrush)App.Current.Resources["ForegroundMain"]; } catch { return (SolidColorBrush)App.Current.Resources["ForegroundMain"]; }
} }
} }
// Source: https://stackoverflow.com/questions/14522540/close-a-messagebox-after-several-seconds
public class AutoClosingMessageBox
{
System.Threading.Timer _timeoutTimer;
string _caption;
AutoClosingMessageBox(string text, string caption, int timeout)
{
_caption = caption;
_timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
null, timeout, System.Threading.Timeout.Infinite);
using (_timeoutTimer)
MessageBox.Show(text, caption);
}
public static void Show(string text, string caption, int timeout)
{
new AutoClosingMessageBox(text, caption, timeout);
}
void OnTimerElapsed(object state)
{
IntPtr mbWnd = FindWindow("#32770", _caption); // lpClassName is #32770 for MessageBox
if (mbWnd != IntPtr.Zero)
SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
_timeoutTimer.Dispose();
}
const int WM_CLOSE = 0x0010;
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
}
} }
+2 -2
View File
@@ -67,9 +67,9 @@ namespace OpenBullet
} }
#region Events #region Events
private void LogRunnerData(IRunnerMessaging sender, LogLevel level, string message, bool prompt) private void LogRunnerData(IRunnerMessaging sender, LogLevel level, string message, bool prompt, int timeout)
{ {
Globals.Log(Components.Runner, level, message, prompt); Globals.Log(Components.Runner, level, message, prompt, timeout);
} }
private void LogWorkerStatus(IRunnerMessaging sender) private void LogWorkerStatus(IRunnerMessaging sender)
+1 -1
View File
@@ -145,7 +145,7 @@ namespace OpenBulletCLI
} }
private static void MessageArrived(IRunnerMessaging obj, LogLevel level, string message, bool prompt) private static void MessageArrived(IRunnerMessaging obj, LogLevel level, string message, bool prompt, int timeout)
{ {
// Do not print anything if no verbose argument was declared // Do not print anything if no verbose argument was declared
if (!Verbose) return; if (!Verbose) return;
+1 -1
View File
@@ -13,7 +13,7 @@ namespace RuriLib.Runner
public interface IRunnerMessaging public interface IRunnerMessaging
{ {
/// <summary>A message has arrived.</summary> /// <summary>A message has arrived.</summary>
event Action<IRunnerMessaging, LogLevel, string, bool> MessageArrived; event Action<IRunnerMessaging, LogLevel, string, bool, int> MessageArrived;
/// <summary>The status of the Master Worker changed.</summary> /// <summary>The status of the Master Worker changed.</summary>
event Action<IRunnerMessaging> WorkerStatusChanged; event Action<IRunnerMessaging> WorkerStatusChanged;
+5 -5
View File
@@ -651,7 +651,7 @@ namespace RuriLib.Runner
// If no one else issued the no more proxies warning message, issue it // If no one else issued the no more proxies warning message, issue it
if (!NoProxyWarningSent) if (!NoProxyWarningSent)
{ {
RaiseMessageArrived(LogLevel.Error, "No more proxies and no Unban All option selected OR the config has a max proxy use! Aborting", true); RaiseMessageArrived(LogLevel.Error, "No more proxies and no Unban All option selected OR the config has a max proxy use! Aborting", true, 2);
NoProxyWarningSent = true; NoProxyWarningSent = true;
} }
@@ -927,7 +927,7 @@ namespace RuriLib.Runner
/// <summary> /// <summary>
/// Fired when a new message needs to be logged. /// Fired when a new message needs to be logged.
/// </summary> /// </summary>
public event Action<IRunnerMessaging, LogLevel, string, bool> MessageArrived; public event Action<IRunnerMessaging, LogLevel, string, bool, int> MessageArrived;
/// <summary> /// <summary>
/// Fired when the Master Worker status changed. /// Fired when the Master Worker status changed.
@@ -959,9 +959,9 @@ namespace RuriLib.Runner
/// </summary> /// </summary>
public event Action<IRunnerMessaging> AskCustomInputs; public event Action<IRunnerMessaging> AskCustomInputs;
private void RaiseMessageArrived(LogLevel level, string message, bool prompt) private void RaiseMessageArrived(LogLevel level, string message, bool prompt = false, int timeout = 0)
{ {
MessageArrived?.Invoke(this, level, message, prompt); MessageArrived?.Invoke(this, level, message, prompt, timeout);
} }
private void RaiseWorkerStatusChanged() private void RaiseWorkerStatusChanged()
@@ -1021,7 +1021,7 @@ namespace RuriLib.Runner
{ {
proxies.AddRange(GetProxiesFromRemoteSource(s.Url, s.Type, s.Pattern, s.Output)); proxies.AddRange(GetProxiesFromRemoteSource(s.Url, s.Type, s.Pattern, s.Output));
} }
catch (Exception ex) { RaiseMessageArrived(LogLevel.Error, $"Could not contact the reload API - {ex.Message}", true); } catch (Exception ex) { RaiseMessageArrived(LogLevel.Error, $"Could not contact the reload API - {ex.Message}", true, 5); }
}); });
ProxyPool = new ProxyPool(proxies, Settings.Proxies.ShuffleOnStart); ProxyPool = new ProxyPool(proxies, Settings.Proxies.ShuffleOnStart);
break; break;