microsoft-visualbasic-runtime/ApplicationServices/Terminal/InteractiveIODevice/TerminalExtensions.vb

132 lines
4.3 KiB
VB.net
Raw Normal View History

2020-03-28 15:44:37 +08:00
#Region "Microsoft.VisualBasic::09223a62d016cb1d5b3fdd044f2d19ab, Microsoft.VisualBasic.Core\ApplicationServices\Terminal\InteractiveIODevice\TerminalExtensions.vb"
2018-08-02 20:14:48 +08:00
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
' /********************************************************************************/
' Summaries:
' Module TerminalEvents
'
'
' Delegate Sub
'
' Properties: ConsoleHandleInvalid, CurrentSize
'
' Sub: doEvents
2018-08-02 20:14:48 +08:00
'
'
'
' /********************************************************************************/
#End Region
Imports System.Drawing
Imports System.Threading
Imports Microsoft.VisualBasic.Language
2020-03-20 21:21:55 +08:00
Namespace ApplicationServices.Terminal
2018-08-02 20:14:48 +08:00
''' <summary>
''' 这个终端事件会依赖于<see cref="App.Running"/>属性值来自动退出的
''' </summary>
2018-08-02 20:14:48 +08:00
Public Module TerminalEvents
Public Delegate Sub ResizeEventHandle(size As Size, oldSize As Size)
Dim resizeHandles As New List(Of ResizeEventHandle)
Dim oldSize As Size
Dim eventThread As Thread
2018-08-02 20:14:48 +08:00
Public ReadOnly Property CurrentSize As Size
Get
Return oldSize
2018-08-02 20:14:48 +08:00
End Get
End Property
Private Sub doEvents()
2018-08-02 20:14:48 +08:00
Do While App.Running
If Console.WindowHeight <> oldSize.Height Then
2018-08-02 20:14:48 +08:00
RaiseEvent Resize()
ElseIf Console.WindowWidth <> oldSize.Width Then
2018-08-02 20:14:48 +08:00
RaiseEvent Resize()
End If
Thread.Sleep(10)
Loop
End Sub
Public ReadOnly Property ConsoleHandleInvalid As Boolean = False
''' <summary>
''' Terminal resize event for [<see cref="Console.WindowWidth"/>, <see cref="Console.WindowHeight"/>]
''' </summary>
Public Custom Event Resize As ResizeEventHandle
AddHandler(value As ResizeEventHandle)
If resizeHandles.IndexOf(value) = -1 Then
resizeHandles += value
2018-08-02 20:14:48 +08:00
End If
If eventThread Is Nothing Then
2018-08-02 20:14:48 +08:00
Try
oldSize = New Size(Console.WindowWidth, Console.WindowHeight)
eventThread = New Thread(AddressOf doEvents)
eventThread.Start()
2018-08-02 20:14:48 +08:00
Catch ex As Exception ' 可能是WindowsForm应用则在这里就忽略掉这个错误了
Call App.LogException(ex)
_ConsoleHandleInvalid = True
End Try
End If
End AddHandler
RemoveHandler(value As ResizeEventHandle)
If resizeHandles.IndexOf(value) > -1 Then
resizeHandles.Remove(value)
2018-08-02 20:14:48 +08:00
End If
If resizeHandles.Count = 0 Then
eventThread.Abort()
eventThread = Nothing
2018-08-02 20:14:48 +08:00
End If
End RemoveHandler
RaiseEvent()
Dim [new] As New Size(Console.WindowWidth, Console.WindowHeight)
If [new].Height >= Console.BufferHeight Then
[new] = New Size([new].Width, Console.BufferHeight - 1)
End If
For Each h As ResizeEventHandle In resizeHandles
Call h([new], oldSize)
2018-08-02 20:14:48 +08:00
Next
oldSize = [new]
2018-08-02 20:14:48 +08:00
End RaiseEvent
End Event
End Module
End Namespace