Skip to content

#188: Add support to swiping to dismiss keyboard for UIKitBackend #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/AppKitBackend/AppKitBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,8 @@ public final class AppKitBackend: AppBackend {
return scrollView
}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {}

public func setScrollBarPresence(
ofScrollContainer scrollView: Widget,
hasVerticalScrollBar: Bool,
Expand Down
2 changes: 2 additions & 0 deletions Sources/CursesBackend/CursesBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public final class CursesBackend: AppBackend {

public func setSpacing(ofHStack container: Widget, to spacing: Int) {}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {}

public func createTextView() -> Widget {
let label = Label("")
label.width = Dim.fill()
Expand Down
2 changes: 2 additions & 0 deletions Sources/Gtk3Backend/Gtk3Backend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ public final class Gtk3Backend: AppBackend {
return scrollView
}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {}

public func setScrollBarPresence(
ofScrollContainer scrollView: Widget,
hasVerticalScrollBar: Bool,
Expand Down
2 changes: 2 additions & 0 deletions Sources/GtkBackend/GtkBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ public final class GtkBackend: AppBackend {
return scrollView
}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {}

public func setScrollBarPresence(
ofScrollContainer scrollView: Widget,
hasVerticalScrollBar: Bool,
Expand Down
2 changes: 2 additions & 0 deletions Sources/LVGLBackend/LVGLBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ public final class LVGLBackend: AppBackend {
}
}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {}

public func createTextView() -> Widget {
return Widget { parent in
let label = LVLabel(with: parent)
Expand Down
2 changes: 2 additions & 0 deletions Sources/QtBackend/QtBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public struct QtBackend: AppBackend {
}
}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {}

public func setSpacing(ofHStack widget: Widget, to spacing: Int) {
(widget.layout as! QHBoxLayout).spacing = Int32(spacing)
}
Expand Down
16 changes: 16 additions & 0 deletions Sources/SwiftCrossUI/Backend/AppBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
/// A default implementation is provided. It uses the backend's reported
/// device class and looks up the text style in a lookup table derived
/// from Apple's typography guidelines. See ``TextStyle/resolve(for:)``.
@Sendable func resolveTextStyle(_ textStyle: Font.TextStyle) -> Font.TextStyle.Resolved

Check warning on line 196 in Sources/SwiftCrossUI/Backend/AppBackend.swift

View workflow job for this annotation

GitHub Actions / uikit (iPhone)

'TextStyle' doesn't exist at '/SwiftCrossUI/AppBackend/resolveTextStyle(_:)'

/// Computes a window's environment based off the root environment. This may involve
/// updating ``EnvironmentValues/windowScaleFactor`` etc.
Expand Down Expand Up @@ -264,6 +264,18 @@

/// Creates a scrollable single-child container wrapping the given widget.
func createScrollContainer(for child: Widget) -> Widget
/// Updates a scroll container with environment-specific values.
///
/// This method is primarily used on iOS to apply environment changes
/// that affect the scroll view’s behavior, such as keyboard dismissal mode.
///
/// - Parameters:
/// - scrollView: The scroll container widget previously created by `createScrollContainer(for:)`.
/// - environment: The current `EnvironmentValues` to apply.
func updateScrollContainer(
_ scrollView: Widget,
environment: EnvironmentValues
)
/// Sets the presence of scroll bars along each axis of a scroll container.
func setScrollBarPresence(
ofScrollContainer scrollView: Widget,
Expand Down Expand Up @@ -741,6 +753,10 @@
todo()
}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {
todo()
}

public func setScrollBarPresence(
ofScrollContainer scrollView: Widget,
hasVerticalScrollBar: Bool,
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftCrossUI/Environment/EnvironmentValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public struct EnvironmentValues {
/// Whether user interaction is enabled. Set by ``View/disabled(_:)``.
public var isEnabled: Bool

/// The way that scrollable content interacts with the software keyboard.
public var scrollDismissesKeyboardMode: ScrollDismissesKeyboardMode

/// Called by view graph nodes when they resize due to an internal state
/// change and end up changing size. Each view graph node sets its own
/// handler when passing the environment on to its children, setting up
Expand Down Expand Up @@ -196,6 +199,7 @@ public struct EnvironmentValues {
listStyle = .default
toggleStyle = .button
isEnabled = true
scrollDismissesKeyboardMode = .automatic
}

/// Returns a copy of the environment with the specified property set to the
Expand Down
28 changes: 28 additions & 0 deletions Sources/SwiftCrossUI/Values/ScrollDismissesKeyboardMode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// The ways that scrollable content can interact with the software keyboard.
///
/// Use this type in a call to the ``View/scrollDismissesKeyboard(_:)``
/// modifier to specify the dismissal behavior of scrollable views.
public enum ScrollDismissesKeyboardMode: Sendable {
/// Determine the mode automatically based on the surrounding context.
///
/// Currently, this behaves the same as ``ScrollDismissesKeyboardMode/interactively``.
/// In the future, it may adapt dynamically based on the context, similar to how
/// SwiftUI's `.automatic` works (e.g., using `.interactively` in some views and
/// `.immediately` in others). Using this value avoids source-breaking changes later on.
case automatic

/// Dismiss the keyboard as soon as scrolling starts.
case immediately

/// Enable people to interactively dismiss the keyboard as part of the
/// scroll operation.
///
/// The software keyboard's position tracks the gesture that drives the
/// scroll operation if the gesture crosses into the keyboard's area of the
/// display. People can dismiss the keyboard by scrolling it off the
/// display, or reverse the direction of the scroll to cancel the dismissal.
case interactively

/// Never dismiss the keyboard automatically as a result of scrolling.
case never
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
extension View {
/// Configures the behavior in which scrollable content interacts with
/// the software keyboard.
///
/// You use this modifier to customize how scrollable content interacts
/// with the software keyboard. For example, you can specify a value of
/// ``ScrollDismissesKeyboardMode/immediately`` to indicate that you
/// would like scrollable content to immediately dismiss the keyboard if
/// present when a scroll drag gesture begins.
///
/// @State private var text = ""
///
/// ScrollView {
/// TextField("Prompt", text: $text)
/// ForEach(0 ..< 50) { index in
/// Text("\(index)")
/// .padding()
/// }
/// }
/// .scrollDismissesKeyboard(.immediately)
///
/// You can also use this modifier to customize the keyboard dismissal
/// behavior for other kinds of scrollable views, like a ``List`` or a
/// ``TextEditor``.
///
/// By default, scrollable content dismisses the keyboard interactively as the user scrolls.
/// Pass a different value of ``ScrollDismissesKeyboardMode`` to change this behavior.
/// For example, use ``ScrollDismissesKeyboardMode/never`` to prevent the keyboard from
/// dismissing automatically. Note that ``TextEditor`` may still use a different
/// default to preserve expected editing behavior.
///
/// - Parameter mode: The keyboard dismissal mode that scrollable content
/// uses.
///
/// - Returns: A view that uses the specified keyboard dismissal mode.
public func scrollDismissesKeyboard(_ mode: ScrollDismissesKeyboardMode) -> some View {
EnvironmentModifier(self) { environment in
environment.with(\.scrollDismissesKeyboardMode, mode)
}
}
}
1 change: 1 addition & 0 deletions Sources/SwiftCrossUI/Views/ScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public struct ScrollView<Content: View>: TypeSafeView, View {
hasVerticalScrollBar: hasVerticalScrollBar,
hasHorizontalScrollBar: hasHorizontalScrollBar
)
backend.updateScrollContainer(widget, environment: environment)
} else {
finalResult = childResult
}
Expand Down
20 changes: 20 additions & 0 deletions Sources/UIKitBackend/UIKitBackend+Container.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ final class ScrollWidget: ContainerWidget {
scrollView.showsVerticalScrollIndicator = hasVerticalScrollBar
scrollView.showsHorizontalScrollIndicator = hasHorizontalScrollBar
}

public func updateScrollContainer(environment: EnvironmentValues) {
#if os(iOS)
scrollView.keyboardDismissMode = switch environment.scrollDismissesKeyboardMode {
case .automatic:
.interactive
case .immediately:
.onDrag
case .interactively:
.interactive
case .never:
.none
}
#endif
}
}

extension UIKitBackend {
Expand Down Expand Up @@ -121,6 +136,11 @@ extension UIKitBackend {
ScrollWidget(child: child)
}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {
let scrollViewWidget = scrollView as! ScrollWidget
scrollViewWidget.updateScrollContainer(environment: environment)
}

public func setScrollBarPresence(
ofScrollContainer scrollView: Widget,
hasVerticalScrollBar: Bool,
Expand Down
12 changes: 12 additions & 0 deletions Sources/UIKitBackend/UIKitBackend+Control.swift
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,18 @@ extension UIKitBackend {
} else {
textEditorWidget.child.inputAccessoryView = nil
}

textEditorWidget.child.alwaysBounceVertical = environment.scrollDismissesKeyboardMode != .never
textEditorWidget.child.keyboardDismissMode = switch environment.scrollDismissesKeyboardMode {
case .automatic:
textEditorWidget.child.inputAccessoryView == nil ? .interactive : .interactiveWithAccessory
case .immediately:
textEditorWidget.child.inputAccessoryView == nil ? .onDrag : .onDragWithAccessory
case .interactively:
textEditorWidget.child.inputAccessoryView == nil ? .interactive : .interactiveWithAccessory
case .never:
.none
}
#endif
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/WinUIBackend/WinUIBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,8 @@ public final class WinUIBackend: AppBackend {
return scrollViewer
}

public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {}

public func setScrollBarPresence(
ofScrollContainer scrollView: Widget,
hasVerticalScrollBar: Bool,
Expand Down
Loading