1 #Region "Microsoft.VisualBasic::14341d17843f00756c425a6687e34f05, Microsoft.VisualBasic.Core\ApplicationServices\Terminal\InteractiveIODevice\TerminalExtensions.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module TerminalEvents
35     
36     
37     '         Delegate Sub
38     
39     '             Properties: ConsoleHandleInvalid, CurrentSize
40     
41     '             Sub: __detects
42     
43     
44     
45     ' /********************************************************************************/
46
47 #End Region
48
49 Imports System.Drawing
50 Imports System.Threading
51 Imports Microsoft.VisualBasic.Language
52
53 Namespace Terminal
54
55     Public Module TerminalEvents
56
57         Public Delegate Sub ResizeEventHandle(size As Size, oldSize As Size)
58
59         Dim __resizeHandles As New List(Of ResizeEventHandle)
60         Dim _old As Size
61         Dim _eventThread As Thread
62
63         Public ReadOnly Property CurrentSize As Size
64             Get
65                 Return _old
66             End Get
67         End Property
68
69         Private Sub __detects()
70             Do While App.Running
71                 If Console.WindowHeight <> _old.Height Then
72                     RaiseEvent Resize()
73                 ElseIf Console.WindowWidth <> _old.Width Then
74                     RaiseEvent Resize()
75                 End If
76
77                 Thread.Sleep(10)
78             Loop
79         End Sub
80
81         Public ReadOnly Property ConsoleHandleInvalid As Boolean = False
82
83         ''' <summary>
84         ''' Terminal resize event for [<see cref="Console.WindowWidth"/>, <see cref="Console.WindowHeight"/>]
85         ''' </summary>
86         Public Custom Event Resize As ResizeEventHandle
87             AddHandler(value As ResizeEventHandle)
88                 If __resizeHandles.IndexOf(value) = -1 Then
89                     __resizeHandles += value
90                 End If
91
92                 If _eventThread Is Nothing Then
93                     Try
94                         _old = New Size(Console.WindowWidth, Console.WindowHeight)
95                         _eventThread = New Thread(AddressOf __detects)
96                         _eventThread.Start()
97                     Catch ex As Exception  ' 可能是WindowsForm应用,则在这里就忽略掉这个错误了
98                         Call App.LogException(ex)
99                         _ConsoleHandleInvalid = True
100                     End Try
101                 End If
102             End AddHandler
103             RemoveHandler(value As ResizeEventHandle)
104                 If __resizeHandles.IndexOf(value) > -1 Then
105                     __resizeHandles.Remove(value)
106                 End If
107
108                 If __resizeHandles.Count = 0 Then
109                     _eventThread.Abort()
110                     _eventThread = Nothing
111                 End If
112             End RemoveHandler
113             RaiseEvent()
114                 Dim [new] As New Size(Console.WindowWidth, Console.WindowHeight)
115
116                 For Each h As ResizeEventHandle In __resizeHandles
117                     Call h([new], _old)
118                 Next
119
120                 _old = [new]
121             End RaiseEvent
122         End Event
123     End Module
124 End Namespace