Skip to content

Commit

Permalink
Update LCManager.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
duraidabdul committed Sep 5, 2021
1 parent 3c2683c commit d5a06c0
Showing 1 changed file with 53 additions and 22 deletions.
75 changes: 53 additions & 22 deletions Sources/LocalConsole/LCManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// Copyright © 2021 Duraid Abdul. All rights reserved.
//

//#if canImport(UIKit)

import UIKit
import SwiftUI

Expand Down Expand Up @@ -62,10 +60,11 @@ public class LCManager: NSObject, UIGestureRecognizerDelegate {

lumaView.translatesAutoresizingMaskIntoConstraints = false

lumaWidthAnchor = lumaView.widthAnchor.constraint(equalTo: consoleView.widthAnchor)
lumaHeightAnchor = lumaView.heightAnchor.constraint(equalToConstant: consoleView.frame.size.height)

NSLayoutConstraint.activate([
lumaView.widthAnchor.constraint(equalTo: consoleView.widthAnchor),
lumaWidthAnchor,
lumaHeightAnchor,
lumaView.centerXAnchor.constraint(equalTo: consoleView.centerXAnchor),
lumaView.centerYAnchor.constraint(equalTo: consoleView.centerYAnchor)
Expand All @@ -74,6 +73,32 @@ public class LCManager: NSObject, UIGestureRecognizerDelegate {
return lumaView
}()

lazy var hideButton: UIButton = {
let button = UIButton()

button.addAction(UIAction(handler: { [self] _ in
UIViewPropertyAnimator(duration: 0.5, dampingRatio: 1) {
consoleView.center = nearestTargetTo(consoleView.center, possibleTargets: possibleEndpoints.dropLast())
}.startAnimation()
grabberMode = false
}), for: .touchUpInside)

consoleView.addSubview(button)

button.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
button.widthAnchor.constraint(equalTo: consoleView.widthAnchor),
button.heightAnchor.constraint(equalTo: consoleView.heightAnchor),
button.centerXAnchor.constraint(equalTo: consoleView.centerXAnchor),
button.centerYAnchor.constraint(equalTo: consoleView.centerYAnchor)
])

button.isHidden = true

return button
}()

/// The fixed size of the console view.
lazy var consoleSize = defaultConsoleSize {
didSet {
Expand Down Expand Up @@ -157,10 +182,10 @@ public class LCManager: NSObject, UIGestureRecognizerDelegate {

// Left edge hiding endpoints.
if consoleView.center.y < (UIScreen.portraitSize.height - (temporaryKeyboardHeightValueTracker ?? 0)) / 2 {
endpoints.append(CGPoint(x: -consoleSize.width / 2 + 10,
endpoints.append(CGPoint(x: -consoleSize.width / 2 + 28,
y: endpoints[0].y))
} else {
endpoints.append(CGPoint(x: -consoleSize.width / 2 + 10,
endpoints.append(CGPoint(x: -consoleSize.width / 2 + 28,
y: endpoints[1].y))
}
} else if consoleView.frame.maxX >= UIScreen.portraitSize.width {
Expand All @@ -170,10 +195,10 @@ public class LCManager: NSObject, UIGestureRecognizerDelegate {

// Right edge hiding endpoints.
if consoleView.center.y < (UIScreen.portraitSize.height - (temporaryKeyboardHeightValueTracker ?? 0)) / 2 {
endpoints.append(CGPoint(x: UIScreen.portraitSize.width + consoleSize.width / 2 - 10,
endpoints.append(CGPoint(x: UIScreen.portraitSize.width + consoleSize.width / 2 - 28,
y: endpoints[0].y))
} else {
endpoints.append(CGPoint(x: UIScreen.portraitSize.width + consoleSize.width / 2 - 10,
endpoints.append(CGPoint(x: UIScreen.portraitSize.width + consoleSize.width / 2 - 28,
y: endpoints[1].y))
}
}
Expand Down Expand Up @@ -300,6 +325,8 @@ public class LCManager: NSObject, UIGestureRecognizerDelegate {
menuButton.showsMenuAsPrimaryAction = true
consoleView.addSubview(menuButton)

let _ = hideButton

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
}
Expand All @@ -319,6 +346,11 @@ public class LCManager: NSObject, UIGestureRecognizerDelegate {
if consoleView.center.x < 0 || consoleView.center.x > UIScreen.portraitSize.width {
grabberMode = true
scrollLocked = !grabberMode

consoleView.layer.removeAllAnimations()
lumaView.layer.removeAllAnimations()
menuButton.layer.removeAllAnimations()
consoleTextView.layer.removeAllAnimations()
}
}

Expand Down Expand Up @@ -415,11 +447,9 @@ public class LCManager: NSObject, UIGestureRecognizerDelegate {

if grabberMode {

if oldValue == false {
lumaView.layer.cornerRadius = consoleView.layer.cornerRadius
lumaHeightAnchor.constant = consoleView.frame.size.height
consoleView.layoutIfNeeded()
}
lumaView.layer.cornerRadius = consoleView.layer.cornerRadius
lumaHeightAnchor.constant = consoleView.frame.size.height
consoleView.layoutIfNeeded()

UIViewPropertyAnimator(duration: 0.3, dampingRatio: 1) { [self] in
consoleTextView.alpha = 0
Expand All @@ -429,19 +459,22 @@ public class LCManager: NSObject, UIGestureRecognizerDelegate {

UIViewPropertyAnimator(duration: 0.5, dampingRatio: 1) { [self] in
lumaView.foregroundView.alpha = 0

}.startAnimation()

lumaWidthAnchor.constant = -34
lumaHeightAnchor.constant = 96
UIViewPropertyAnimator(duration: 0.4, dampingRatio: 1) { [self] in
lumaView.layer.cornerRadius = 8
consoleView.layoutIfNeeded()
}.startAnimation(afterDelay: 0.06)

consoleTextView.isUserInteractionEnabled = false
hideButton.isHidden = false

} else {

lumaHeightAnchor.constant = consoleView.frame.size.height
lumaWidthAnchor.constant = 0
UIViewPropertyAnimator(duration: 0.4, dampingRatio: 1) { [self] in
consoleView.layoutIfNeeded()
lumaView.layer.cornerRadius = consoleView.layer.cornerRadius
Expand All @@ -458,6 +491,7 @@ public class LCManager: NSObject, UIGestureRecognizerDelegate {
}.startAnimation()

consoleTextView.isUserInteractionEnabled = true
hideButton.isHidden = true
}
}
}
Expand Down Expand Up @@ -771,7 +805,7 @@ public class LCManager: NSObject, UIGestureRecognizerDelegate {
if !grabberMode { scrollLocked = true }

UIViewPropertyAnimator(duration: 0.8, dampingRatio: 0.5) { [self] in
consoleView.transform = .init(scaleX: 1, y: 1)
consoleView.transform = .identity
}.startAnimation()

UIViewPropertyAnimator(duration: 0.4, dampingRatio: 1) { [self] in
Expand Down Expand Up @@ -802,13 +836,9 @@ public class LCManager: NSObject, UIGestureRecognizerDelegate {
consoleView.center.y = initialViewLocation.y + translation.y

if consoleView.frame.maxX > 30 && consoleView.frame.minX < UIScreen.portraitSize.width - 30 {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
self.grabberMode = false
}
self.grabberMode = false
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
self.grabberMode = true
}
self.grabberMode = true
}

case .ended, .cancelled:
Expand Down Expand Up @@ -949,8 +979,6 @@ extension UIWindow {
}
}

//#endif

class LumaView: UIView {
lazy var visualEffectView: UIView = {
Bundle(path: "/Sys" + "tem/Lib" + "rary/Private" + "Frameworks/Material" + "Kit." + "framework")!.load()
Expand Down Expand Up @@ -1014,6 +1042,9 @@ class LumaView: UIView {
let _ = visualEffectView
let _ = foregroundView

visualEffectView.isUserInteractionEnabled = false
foregroundView.isUserInteractionEnabled = false

layer.cornerCurve = .continuous
clipsToBounds = true
}
Expand Down

0 comments on commit d5a06c0

Please sign in to comment.