generated from bitwarden/template
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PM-11712] Fix keyboard scroll on cipher view so it doesn't overlap c…
…ursor (#1292)
- Loading branch information
Showing
6 changed files
with
127 additions
and
23 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
BitwardenShared/UI/Platform/Application/Extensions/View+Backport.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import SwiftUI | ||
|
||
// MARK: - View | ||
|
||
/// Extension of `View` to have the `backport` object availalbe to ease | ||
/// with available APIs. | ||
/// | ||
/// Adapted from https://davedelong.com/blog/2021/10/09/simplifying-backwards-compatibility-in-swift/ | ||
/// | ||
extension View { | ||
/// Helper to apply backport operations for available APIs. | ||
var backport: Backport<Self> { Backport(self) } | ||
} | ||
|
||
// MARK: - Backport<View> | ||
|
||
/// Backport for `View` content. | ||
/// | ||
/// Adapted from https://davedelong.com/blog/2021/10/09/simplifying-backwards-compatibility-in-swift/ | ||
/// | ||
extension Backport where Content: View { | ||
/// On iOS 16+, configures the scroll view to dismiss the keyboard immediately. | ||
/// | ||
func dismissKeyboardImmediately() -> some View { | ||
if #available(iOS 16, *) { | ||
return content.scrollDismissesKeyboard(.immediately) | ||
} else { | ||
return content | ||
} | ||
} | ||
|
||
/// On iOS 16+, configures the scroll view to dismiss the keyboard interactively. | ||
/// | ||
func dismissKeyboardInteractively() -> some View { | ||
if #available(iOS 16, *) { | ||
return content.scrollDismissesKeyboard(.interactively) | ||
} else { | ||
return content | ||
} | ||
} | ||
|
||
//// Configures the content margin for scroll content of a specific view. | ||
/// | ||
/// Use this modifier to customize the content margins of different | ||
/// kinds of views. For example, you can use this modifier to customize | ||
/// the scroll content margins of scrollable views like ``ScrollView``. In the | ||
/// following example, the scroll view will automatically inset | ||
/// its content by the safe area plus an additional 20 points | ||
/// on the leading and trailing edge. | ||
/// | ||
/// ScrollView(.horizontal) { | ||
/// // ... | ||
/// } | ||
/// .contentMargins(.horizontal, 20.0) | ||
/// | ||
/// - Parameters: | ||
/// - edges: The edges to add the margins to. | ||
/// - length: The amount of margins to add. | ||
@ViewBuilder | ||
func scrollContentMargins(_ edges: Edge.Set = .all, _ length: CGFloat?) -> some View { | ||
if #available(iOS 17.0, *) { | ||
content.contentMargins(edges, length, for: .scrollContent) | ||
} else { | ||
content | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Backport<Content> | ||
|
||
/// Helper to deal with available APIs and provide backport operations | ||
/// | ||
/// Adapted from https://davedelong.com/blog/2021/10/09/simplifying-backwards-compatibility-in-swift/ | ||
/// | ||
public struct Backport<Content> { | ||
/// The content to apply backport operations. | ||
public let content: Content | ||
|
||
/// Initializes a backport with some content to apply backrpot operations. | ||
/// - Parameter content: The content to apply backport operations. | ||
public init(_ content: Content) { | ||
self.content = content | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
BitwardenShared/UI/Platform/Application/Utilities/KeyboardResponder.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Combine | ||
import Foundation | ||
import UIKit | ||
|
||
/// An observable responder to handle keyboard show/hide notifications. | ||
final class KeyboardResponder: ObservableObject { | ||
// MARK: Properties | ||
|
||
/// Whether the keyboard is shown. | ||
@Published var isShown: Bool = false | ||
|
||
/// A publisher when the keyboard will hide. | ||
var keyboardWillHideNotification = NotificationCenter.default.publisher( | ||
for: UIResponder.keyboardWillHideNotification | ||
) | ||
|
||
/// A publisher when the keyboard will show. | ||
var keyboardWillShowNotification = NotificationCenter.default.publisher( | ||
for: UIResponder.keyboardWillShowNotification | ||
) | ||
|
||
// MARK: Initializer | ||
|
||
/// Initializes a `KeyboardResponder`. | ||
init() { | ||
keyboardWillHideNotification.map { _ in | ||
false | ||
} | ||
.assign(to: &$isShown) | ||
|
||
keyboardWillShowNotification.map { _ in | ||
true | ||
} | ||
.assign(to: &$isShown) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters