-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathViewController.swift
117 lines (99 loc) · 4.22 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// ViewController.swift
// SwiftTerm
//
// Created by Miguel de Icaza on 3/19/19.
// Copyright © 2019 Miguel de Icaza. All rights reserved.
//
import UIKit
import SwiftTerm
class ViewController: UIViewController {
var tv: TerminalView!
var transparent: Bool = false
var useAutoLayout: Bool {
get { true }
}
func makeFrame (keyboardDelta: CGFloat, _ fn: String = #function, _ ln: Int = #line) -> CGRect
{
if useAutoLayout {
return CGRect.zero
} else {
return CGRect (x: view.safeAreaInsets.left,
y: view.safeAreaInsets.top,
width: view.frame.width - view.safeAreaInsets.left - view.safeAreaInsets.right,
height: view.frame.height - view.safeAreaInsets.top - keyboardDelta)
}
}
func setupKeyboardMonitor ()
{
if #available(iOS 15.0, *), useAutoLayout {
tv.translatesAutoresizingMaskIntoConstraints = false
tv.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
tv.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
tv.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
tv.keyboardLayoutGuide.topAnchor.constraint(equalTo: tv.bottomAnchor).isActive = true
} else {
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillShow),
name: UIWindow.keyboardWillShowNotification,
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillHide),
name: UIWindow.keyboardWillHideNotification,
object: nil)
}
}
var keyboardDelta: CGFloat = 0
@objc private func keyboardWillShow(_ notification: NSNotification) {
guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardScreenEndFrame = keyboardValue.cgRectValue
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
keyboardDelta = keyboardViewEndFrame.height
tv.frame = makeFrame(keyboardDelta: keyboardViewEndFrame.height)
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
tv.frame = CGRect (origin: tv.frame.origin, size: size)
}
@objc private func keyboardWillHide(_ notification: NSNotification) {
//let key = UIResponder.keyboardFrameBeginUserInfoKey
keyboardDelta = 0
tv.frame = makeFrame(keyboardDelta: 0)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tv = SshTerminalView(frame: makeFrame (keyboardDelta: 0))
if transparent {
let x = UIImage (contentsOfFile: "/tmp/Lucia.png")!.cgImage
//let x = UIImage (systemName: "star")!.cgImage
let layer = CALayer()
tv.isOpaque = false
tv.backgroundColor = UIColor.clear
tv.nativeBackgroundColor = UIColor.clear
layer.contents = x
layer.frame = tv.bounds
view.layer.addSublayer(layer)
}
view.addSubview(tv)
tv.getTerminal().backgroundColor = SwiftTerm.Color.init(red: 0x8080, green: 0, blue: 0)
setupKeyboardMonitor()
tv.becomeFirstResponder()
self.tv.feed(text: "Welcome to SwiftTerm - connecting to my localhost\r\n\n")
var text = UITextField(frame: CGRect (x: 0, y: 100, width: 300, height: 20))
view.addSubview(text)
text.backgroundColor = UIColor.white
text.text = "HELLO WORLD"
text.font = UIFont(name: "Courier", size: 30)
}
override func viewWillLayoutSubviews() {
if useAutoLayout, #available(iOS 15.0, *) {
} else {
tv.frame = makeFrame (keyboardDelta: keyboardDelta)
}
if transparent {
tv.backgroundColor = UIColor.clear
}
}
}