123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- using InABox.Clients;
- using InABox.Core;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Windows;
- using System.Windows.Controls;
- using System.IO;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using InABox.WPF;
- namespace InABox.Wpf;
- public enum MessageWindowButtonPosition
- {
- Left,
- Right
- }
- public enum MessageWindowResult
- {
- None,
- OK,
- Cancel,
- Other
- }
- public class MessageWindowButton : INotifyPropertyChanged
- {
- public delegate void MessageWindowButtonDelegate(MessageWindow window, MessageWindowButton button);
- public MessageWindowButtonPosition Position { get; set; }
- private string _content;
- public string Content
- {
- get => _content;
- [MemberNotNull(nameof(_content))]
- set
- {
- _content = value;
- OnPropertyChanged();
- }
- }
- public MessageWindowButtonDelegate Action { get; set; }
- public MessageWindowButton(string content, MessageWindowButtonDelegate action, MessageWindowButtonPosition position)
- {
- Content = content;
- Action = action;
- Position = position;
- }
- public event PropertyChangedEventHandler? PropertyChanged;
- protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- /// <summary>
- /// Interaction logic for MessageWindow.xaml
- /// </summary>
- public partial class MessageWindow : Window, INotifyPropertyChanged
- {
- public ObservableCollection<MessageWindowButton> Buttons { get; private set; } = new();
- public IEnumerable<MessageWindowButton> LeftButtons => Buttons.Where(x => x.Position == MessageWindowButtonPosition.Left);
- public IEnumerable<MessageWindowButton> RightButtons => Buttons.Where(x => x.Position == MessageWindowButtonPosition.Right);
- private string _message = "";
- public string Message
- {
- get => _message;
- set
- {
- _message = value;
- OnPropertyChanged();
- }
- }
- public ImageSource? _image = null;
- public ImageSource? Image
- {
- get => _image;
- set
- {
- _image = value;
- OnPropertyChanged();
- }
- }
- private string _details = "";
- public string Details
- {
- get => _details;
- set
- {
- _details = value;
- OnPropertyChanged();
- }
- }
- public static readonly DependencyProperty ShowDetailsProperty = DependencyProperty.Register(nameof(ShowDetails), typeof(bool), typeof(MessageWindow));
- public bool ShowDetails
- {
- get => (bool)GetValue(ShowDetailsProperty);
- set => SetValue(ShowDetailsProperty, value);
- }
- public MessageWindowResult Result { get; set; } = MessageWindowResult.None;
- public object? OtherResult { get; set; }
- public MessageWindow()
- {
- InitializeComponent();
- Buttons.CollectionChanged += Buttons_CollectionChanged;
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- if (sender is not Button button || button.Tag is not MessageWindowButton winButton) return;
- winButton.Action(this, winButton);
- }
- private void Buttons_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
- {
- OnPropertyChanged(nameof(LeftButtons));
- OnPropertyChanged(nameof(RightButtons));
- }
- public void AddButton(MessageWindowButton button)
- {
- Buttons.Add(button);
- }
- public void AddOKButton(string content = "OK")
- {
- Buttons.Add(new MessageWindowButton(content, OKButton_Click, MessageWindowButtonPosition.Right));
- }
- public void AddCancelButton(string content = "Cancel")
- {
- Buttons.Add(new MessageWindowButton(content, CancelButton_Click, MessageWindowButtonPosition.Right));
- }
- private void CancelButton_Click(MessageWindow window, MessageWindowButton button)
- {
- Result = MessageWindowResult.Cancel;
- Close();
- }
- private void OKButton_Click(MessageWindow window, MessageWindowButton button)
- {
- Result = MessageWindowResult.OK;
- Close();
- }
- public event PropertyChangedEventHandler? PropertyChanged;
- protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- #region Static Constructors
- public static readonly BitmapImage _warning = InABox.Wpf.Resources.warning.AsBitmapImage();
- public static void ShowMessage(string message, string title, ImageSource? image = null)
- {
- var window = new MessageWindow
- {
- Message = message,
- Title = title,
- Image = image
- };
- window.AddOKButton();
- window.ShowDialog();
- }
- public static bool ShowOKCancel(string message, string title, ImageSource? image = null)
- {
- var window = new MessageWindow
- {
- Message = message,
- Title = title,
- Image = image
- };
- window.AddOKButton();
- window.AddCancelButton();
- window.ShowDialog();
- return window.Result == MessageWindowResult.OK;
- }
- public static bool ShowYesNo(string message, string title, ImageSource? image = null)
- {
- var window = new MessageWindow
- {
- Message = message,
- Title = title,
- Image = image
- };
- window.AddOKButton("Yes");
- window.AddCancelButton("No");
- window.ShowDialog();
- return window.Result == MessageWindowResult.OK;
- }
- /// <summary>
- /// Display a message box for an exception, giving options to view the logs.
- /// </summary>
- /// <param name="message">The message to display. Set to <see langword="null"/> to default to the exception message.</param>
- /// <param name="exception"></param>
- /// <param name="title"></param>
- /// <param name="shouldLog">If <see langword="true"/>, also logs the exception.</param>
- public static void ShowError(string? message, Exception exception, string title = "Error", bool shouldLog = true, ImageSource? image = null)
- {
- if (shouldLog)
- {
- CoreUtils.LogException(ClientFactory.UserID, exception);
- }
- var window = new MessageWindow
- {
- Message = message ?? exception.Message,
- Title = title,
- Details = CoreUtils.FormatException(exception),
- Image = image ?? _warning
- };
- window.AddButton(new MessageWindowButton("Show Logs", ShowLogs_Click, MessageWindowButtonPosition.Left));
- var showDetailsButton = new MessageWindowButton("Show Details", (win, button) =>
- {
- win.ShowDetails = !win.ShowDetails;
- button.Content = win.ShowDetails
- ? "Hide Details"
- : "Show Details";
- }, MessageWindowButtonPosition.Left);
- window.AddButton(showDetailsButton);
- window.AddOKButton();
- window.ShowDialog();
- }
- private static void ShowLogs_Click(MessageWindow window, MessageWindowButton button)
- {
- var console = new MessageWindowConsole("Logs", Path.Combine(CoreUtils.GetPath(), string.Format("{0:yyyy-MM-dd}.log", DateTime.Today)));
- console.ShowDialog();
- }
- #endregion
- }
- public class MessageWindowConsole : Console.Console
- {
- public string FileName { get; set; }
- public MessageWindowConsole(string description, string file) : base(description)
- {
- FileName = file;
- ConsoleControl.AllowLoadLogButton = false;
- }
- protected override void OnLoaded()
- {
- base.OnLoaded();
- if (File.Exists(FileName))
- {
- var lines = File.ReadLines(FileName);
- ConsoleControl.LoadLogEntries(lines);
- }
- }
- protected override string GetLogDirectory()
- {
- return CoreUtils.GetPath();
- }
- }
|