-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tools.swift
90 lines (86 loc) · 3.09 KB
/
Tools.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
import SwiftUI
import Combine
struct Tools: View {
@Binding var text: String
@Binding var url: URL?
@State private var editing = ""
@State private var hide = true
@State private var tabsY = CGFloat()
@State private var menuY = CGFloat()
@State private var bottom = CGFloat()
@State private var barWidth = CGFloat()
@State private var subs = Set<AnyCancellable>()
@State private var options = false
var body: some View {
VStack {
Spacer()
ZStack {
HStack {
Spacer()
Bar(text: $editing, width: barWidth, action: commit)
Spacer()
}
HStack {
Spacer()
ZStack {
Blob.Icon(icon: "square.on.square", action: show)
.padding()
.offset(y: tabsY)
Blob.Icon(icon: "line.horizontal.3") {
self.show()
self.options = true
}.padding()
.offset(y: menuY)
.sheet(isPresented: $options) {
Options(url: self.$url, visible: self.$options)
}
Blob.Icon(icon: hide ? "magnifyingglass" : "multiply", action: show)
.padding()
}
if hide {
Spacer()
}
}
}.offset(y: bottom)
}.onAppear {
NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification).sink { notification in
withAnimation {
self.bottom = -(notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
}
}.store(in: &self.subs)
NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification).sink { notification in
withAnimation {
self.bottom = 0
}
}.store(in: &self.subs)
}
}
private func show() {
if hide {
withAnimation(Animation.linear(duration: 0.2)) {
hide = false
barWidth = 200
}
withAnimation(Animation.easeOut(duration: 0.2).delay(0.1)) {
menuY = -75
}
withAnimation(Animation.easeOut(duration: 0.3).delay(0.1)) {
tabsY = -150
}
} else {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
withAnimation(Animation.easeOut(duration: 0.2)) {
tabsY = 0
menuY = 0
}
withAnimation(Animation.linear(duration: 0.2).delay(0.1)) {
barWidth = 0
hide = true
}
}
}
private func commit() {
show()
text = editing
}
}