<commit_before>Imports System.Linq
Imports HolzShots.Input
Imports HolzShots.Input.Actions
Imports HolzShots.Interop
Imports HolzShots.My
Imports HolzShots.Composition.Command

Namespace UI.Specialized
    Friend Class MainWindow

        Public Shared ReadOnly Property Instance As MainWindow = New MainWindow()

        Private _forceClose As Boolean = False

        Private _keyboardHook As KeyboardHook
        Private _actionContainer As HolzShotsActionCollection
        Public Shared ReadOnly CommandManager As CommandManager = New CommandManager()


        Private Sub HideForm()
            Opacity = 0
            Visible = False
            ShowInTaskbar = False
            Hide()
        End Sub

#Region "Form Events"

        Private Sub New()
            InitializeComponent()
            TrayIcon.ContextMenu = trayIconMenu
        End Sub

        Private Sub MainWindowFormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
            If _forceClose = False Then
                e.Cancel = True
                HideForm()
            Else
                e.Cancel = False
                TrayIcon.Visible = False
            End If
        End Sub

        Private Sub SettingsUpdated(sender As Object, newSettings As HSSettings)
            _actionContainer?.Dispose()

            If sender IsNot Me Then
                ' If sender is MainWindow, the function was invoke on application startup
                ' We only want to show this message when the user edits this file
                HumanInterop.SettingsUpdated()
            End If

            Dim parsedBindings = newSettings.KeyBindings.Select(AddressOf CommandManager.GetHotkeyActionFromKeyBinding).ToArray()

            _actionContainer = New HolzShotsActionCollection(_keyboardHook, parsedBindings)

            Try
                _actionContainer.Refresh()
            Catch ex As AggregateException
                Dim registrationExceptions = ex.InnerExceptions.OfType(Of HotkeyRegistrationException)
                HumanInterop.ErrorRegisteringHotkeys(registrationExceptions)
            End Try

        End Sub

        Private Shared Sub RegisterCommands()
            ' TODO: This looks like it could be integrated in our plugin system
            CommandManager.RegisterCommand(New SelectAreaCommand())
            CommandManager.RegisterCommand(New FullscreenCommand())
            CommandManager.RegisterCommand(New WindowCommand())
            CommandManager.RegisterCommand(New OpenImagesFolderCommand())
            CommandManager.RegisterCommand(New OpenSettingsJsonCommand())
            CommandManager.RegisterCommand(New UploadImageCommand())
            CommandManager.RegisterCommand(New EditImageCommand())
        End Sub

        Private Async Sub MainWindowLoad(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            HideForm()

            HolzShotsEnvironment.CurrentStartupManager.FixWorkingDirectory()

            Drawing.DpiAwarenessFix.SetDpiAwareness()

            _keyboardHook = KeyboardHookSelector.CreateHookForCurrentPlatform(Me)
            RegisterCommands()

            Await UserSettings.Load(Me)
            SettingsUpdated(Me, UserSettings.Current)
            AddHandler UserSettings.Manager.OnSettingsUpdated, AddressOf SettingsUpdated

            Dim isAutorun = HolzShotsEnvironment.CurrentStartupManager.IsStartedUp
            Dim args = HolzShotsEnvironment.CurrentStartupManager.CommandLineArguments

            Await MyApplication.ProcessCommandLineArguments(args).ConfigureAwait(True)

            Dim saveSettings As Boolean = False
            Dim openSettingsOnFinish As Boolean = False

            If String.IsNullOrWhiteSpace(Global.HolzShots.My.Settings.DefaultImageHoster) Then
                Global.HolzShots.My.Settings.DefaultImageHoster = "DirectUpload.net"
                saveSettings = True
            End If

            If Not isAutorun AndAlso Global.HolzShots.My.Settings.IsFirstRun Then
                Global.HolzShots.My.Settings.IsFirstRun = False
                openSettingsOnFinish = True
                saveSettings = True
            End If

            If saveSettings Then Global.HolzShots.My.Settings.Save()
            If openSettingsOnFinish Then OpenSettings()
        End Sub

#End Region

        Private Sub ExitApplication()
            _forceClose = True
            Try
                Dim forms = Application.OpenForms
                For i As Integer = forms.Count - 1 To 0
                    If forms(i) IsNot Me Then
                        forms(i).Close()
                    End If
                Next
                System.Windows.Forms.Application.Exit()
            Catch ex As Exception
                End
            End Try
        End Sub

#Region "Open Windows"

        Private Shared Sub OpenSettings()
            If Not SettingsWindow.Instance.Visible Then
                SettingsWindow.Instance.ShowDialog() ' Showdialog necessary?
            End If
        End Sub
        Private Shared Sub OpenAbout()
            If Not AboutForm.IsAboutInstanciated Then
                Dim newAboutForm As New AboutForm()
                newAboutForm.Show()
            End If
        End Sub
<commit_msg>Create a defensive copy of all forms<commit_after>Imports System.Linq
Imports HolzShots.Input
Imports HolzShots.Input.Actions
Imports HolzShots.Interop
Imports HolzShots.My
Imports HolzShots.Composition.Command

Namespace UI.Specialized
    Friend Class MainWindow

        Public Shared ReadOnly Property Instance As MainWindow = New MainWindow()

        Private _forceClose As Boolean = False

        Private _keyboardHook As KeyboardHook
        Private _actionContainer As HolzShotsActionCollection
        Public Shared ReadOnly CommandManager As CommandManager = New CommandManager()


        Private Sub HideForm()
            Opacity = 0
            Visible = False
            ShowInTaskbar = False
            Hide()
        End Sub

#Region "Form Events"

        Private Sub New()
            InitializeComponent()
            TrayIcon.ContextMenu = trayIconMenu
        End Sub

        Private Sub MainWindowFormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
            If _forceClose = False Then
                e.Cancel = True
                HideForm()
            Else
                e.Cancel = False
                TrayIcon.Visible = False
            End If
        End Sub

        Private Sub SettingsUpdated(sender As Object, newSettings As HSSettings)
            _actionContainer?.Dispose()

            If sender IsNot Me Then
                ' If sender is MainWindow, the function was invoke on application startup
                ' We only want to show this message when the user edits this file
                HumanInterop.SettingsUpdated()
            End If

            Dim parsedBindings = newSettings.KeyBindings.Select(AddressOf CommandManager.GetHotkeyActionFromKeyBinding).ToArray()

            _actionContainer = New HolzShotsActionCollection(_keyboardHook, parsedBindings)

            Try
                _actionContainer.Refresh()
            Catch ex As AggregateException
                Dim registrationExceptions = ex.InnerExceptions.OfType(Of HotkeyRegistrationException)
                HumanInterop.ErrorRegisteringHotkeys(registrationExceptions)
            End Try

        End Sub

        Private Shared Sub RegisterCommands()
            ' TODO: This looks like it could be integrated in our plugin system
            CommandManager.RegisterCommand(New SelectAreaCommand())
            CommandManager.RegisterCommand(New FullscreenCommand())
            CommandManager.RegisterCommand(New WindowCommand())
            CommandManager.RegisterCommand(New OpenImagesFolderCommand())
            CommandManager.RegisterCommand(New OpenSettingsJsonCommand())
            CommandManager.RegisterCommand(New UploadImageCommand())
            CommandManager.RegisterCommand(New EditImageCommand())
        End Sub

        Private Async Sub MainWindowLoad(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            HideForm()

            HolzShotsEnvironment.CurrentStartupManager.FixWorkingDirectory()

            Drawing.DpiAwarenessFix.SetDpiAwareness()

            _keyboardHook = KeyboardHookSelector.CreateHookForCurrentPlatform(Me)
            RegisterCommands()

            Await UserSettings.Load(Me)
            SettingsUpdated(Me, UserSettings.Current)
            AddHandler UserSettings.Manager.OnSettingsUpdated, AddressOf SettingsUpdated

            Dim isAutorun = HolzShotsEnvironment.CurrentStartupManager.IsStartedUp
            Dim args = HolzShotsEnvironment.CurrentStartupManager.CommandLineArguments

            Await MyApplication.ProcessCommandLineArguments(args).ConfigureAwait(True)

            Dim saveSettings As Boolean = False
            Dim openSettingsOnFinish As Boolean = False

            If String.IsNullOrWhiteSpace(Global.HolzShots.My.Settings.DefaultImageHoster) Then
                Global.HolzShots.My.Settings.DefaultImageHoster = "DirectUpload.net"
                saveSettings = True
            End If

            If Not isAutorun AndAlso Global.HolzShots.My.Settings.IsFirstRun Then
                Global.HolzShots.My.Settings.IsFirstRun = False
                openSettingsOnFinish = True
                saveSettings = True
            End If

            If saveSettings Then Global.HolzShots.My.Settings.Save()
            If openSettingsOnFinish Then OpenSettings()
        End Sub

#End Region

        Private Sub ExitApplication()
            _forceClose = True
            Try
                ' Defensive copy of Application.OpenForm
                Dim forms = New List(Of Form)(Application.OpenForms.Cast(Of Form)())
                For Each f In forms
                    If f IsNot Me Then
                        f.Close()
                    End If
                Next

                System.Windows.Forms.Application.Exit()
            Catch ex As Exception
                End
            End Try
        End Sub

#Region "Open Windows"

        Private Shared Sub OpenSettings()
            If Not SettingsWindow.Instance.Visible Then
                SettingsWindow.Instance.ShowDialog() ' Showdialog necessary?
            End If
        End Sub
        Private Shared Sub OpenAbout()
            If Not AboutForm.IsAboutInstanciated Then
                Dim newAboutForm As New AboutForm()
                newAboutForm.Show()
            End If
        End Sub

{'fraction_non_alphanumeric': 0.046660395108184385, 'fraction_numerical': 0.00037629350893697084, 'mean_word_length': 2.038296656187482, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2546900', 'n_tokens_mistral': 2545, 'n_tokens_neox': 2400, 'n_words': 702}