diff --git a/OpenBullet/Globals.cs b/OpenBullet/Globals.cs index ad8a0c7..dc25e05 100644 --- a/OpenBullet/Globals.cs +++ b/OpenBullet/Globals.cs @@ -59,9 +59,20 @@ namespace OpenBullet public static ObservableCollection Runners = new ObservableCollection(); // 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; 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() @@ -126,4 +137,35 @@ namespace OpenBullet 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); + } } diff --git a/OpenBullet/Pages/Main/Runner/Runner.xaml.cs b/OpenBullet/Pages/Main/Runner/Runner.xaml.cs index 44f7262..e19961d 100644 --- a/OpenBullet/Pages/Main/Runner/Runner.xaml.cs +++ b/OpenBullet/Pages/Main/Runner/Runner.xaml.cs @@ -67,9 +67,9 @@ namespace OpenBullet } #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) diff --git a/OpenBulletCLI/Program.cs b/OpenBulletCLI/Program.cs index 7086f78..ca5266f 100644 --- a/OpenBulletCLI/Program.cs +++ b/OpenBulletCLI/Program.cs @@ -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 if (!Verbose) return; diff --git a/RuriLib/Runner/IRunnerMessaging.cs b/RuriLib/Runner/IRunnerMessaging.cs index 4777cce..4e1d535 100644 --- a/RuriLib/Runner/IRunnerMessaging.cs +++ b/RuriLib/Runner/IRunnerMessaging.cs @@ -13,7 +13,7 @@ namespace RuriLib.Runner public interface IRunnerMessaging { /// A message has arrived. - event Action MessageArrived; + event Action MessageArrived; /// The status of the Master Worker changed. event Action WorkerStatusChanged; diff --git a/RuriLib/Runner/RunnerViewModel.cs b/RuriLib/Runner/RunnerViewModel.cs index f64daae..ca15ce1 100644 --- a/RuriLib/Runner/RunnerViewModel.cs +++ b/RuriLib/Runner/RunnerViewModel.cs @@ -651,7 +651,7 @@ namespace RuriLib.Runner // If no one else issued the no more proxies warning message, issue it 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; } @@ -927,7 +927,7 @@ namespace RuriLib.Runner /// /// Fired when a new message needs to be logged. /// - public event Action MessageArrived; + public event Action MessageArrived; /// /// Fired when the Master Worker status changed. @@ -959,9 +959,9 @@ namespace RuriLib.Runner /// public event Action 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() @@ -1021,7 +1021,7 @@ namespace RuriLib.Runner { 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); break;