public static void ShowView(this IBluescreenViewModel bluescreen)
        {
            var attribute = bluescreen.GetType().GetCustomAttribute <BluescreenViewAttribute>();

            if (attribute is null)
            {
                throw new InvalidOperationException("No BluescreenViewAttribute has been found");
            }
            var type = attribute.WindowType;

            void DispatcherWindowShow()
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    var window = (Window)Activator.CreateInstance(type, bluescreen);
                    window.ShowOnMonitor(Screen.PrimaryScreen);
                    window.Activate();
                    window.Focus();
                    window.Loaded += (sender, _) => (sender as Window)?.Focus();
                    foreach (var otherScreen in Screen.AllScreens.Where(s => !Equals(s, Screen.PrimaryScreen)))
                    {
                        var blackScreenWindow = new BlackWindow(window);
                        blackScreenWindow.ShowOnMonitor(otherScreen);
                    }
                    return(window);
                });
            }

            bluescreen.ExecuteCommand.Execute((Action)DispatcherWindowShow);
        }
Example #2
0
        private void Application_Startup(object sender, EventArgs e)
        {
            void SetTheme() => DarkThemeDictionary.IsEnabled = Settings.Default.IsDarkTheme;

            Settings.Default.Upgrade();
            Settings.Default.PropertyChanged +=
                (_, __) => SetTheme();
            SetTheme();
            DispatcherUnhandledException += (o, eventArgs) =>
            {
                var m = ShowErrorMessage(eventArgs.Exception);
                eventArgs.Handled = m != MessageBoxResult.Cancel || m != MessageBoxResult.No;
            };
            AppDomain.CurrentDomain.UnhandledException +=
                delegate(object o, UnhandledExceptionEventArgs eventArgs)
            {
                ShowErrorMessage(eventArgs.ExceptionObject as Exception);
            };
            var args = Environment.GetCommandLineArgs();

            if (args.Length > 1) // #0 is file path
            {
                bool showHelp = false;
                void AddHelp(OptionSet set)
                {
                    set.Add("h|help", "Show this message and exit", h => showHelp = h != null);
                }

                IBluescreenViewModel data = null;
                Type type       = null;
                var  currentSet = CmdParameterAttribute.GetBaseOptionSet(t => type = t, out var bluescreenOptions);
                AddHelp(currentSet);
                try
                {
                    foreach (var option in bluescreenOptions)
                    {
                        if (args.Contains(option.Value))
                        {
                            type = option.Key;
                            break;
                        }
                    }
                    if (type is null)
                    {
                        showHelp = true;
                        goto showHelp;
                    }
                    data       = (IBluescreenViewModel)Activator.CreateInstance(type);
                    currentSet = CmdParameterAttribute.GetOptionSetFor(type, data);
                    if (showHelp)
                    {
                        goto showHelp;
                    }
                    AddHelp(currentSet);
                    currentSet.Parse(args);
                }
                catch (OptionException ex)
                {
                    if (AttachConsole(AttachParentProcess))
                    {
                        Console.WriteLine("\n");
                        Console.Write("BluescreenSimulator: ");
                        Console.WriteLine(ex.Message);
                        Console.WriteLine("Try `--help' for more information.");
                        Console.WriteLine();
                        FreeConsole();
                    }
                    Shutdown(1);
                    return;
                }
showHelp:
                if (showHelp)
                {
                    ShowHelp(currentSet);
                    return;
                }

                if (data.EnableUnsafe)
                {
                    MessageBox.Show("You are entering Unsafe Mode. Be careful!", "Careful", MessageBoxButton.OK, MessageBoxImage.Warning);
                    RunGui(true);
                }
                else
                {
                    data.ShowView();
                }
            }
            else
            {
                RunGui(false);
            }
        }