Skip to content

Commit e5b7789

Browse files
author
Vitor Silveira
committed
#188: Add support to swiping to dismiss keyboard for ScrollView
1 parent 6b4dfcf commit e5b7789

File tree

14 files changed

+72
-13
lines changed

14 files changed

+72
-13
lines changed

Examples/Sources/NotesExample/ContentView.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ struct ContentView: View {
3838

3939
var textEditorBackground: Color {
4040
switch colorScheme {
41-
case .light:
42-
Color(0.8, 0.8, 0.8)
43-
case .dark:
44-
Color(0.18, 0.18, 0.18)
41+
case .light:
42+
Color(0.8, 0.8, 0.8)
43+
case .dark:
44+
Color(0.18, 0.18, 0.18)
4545
}
4646
}
4747

Sources/AppKitBackend/AppKitBackend.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,10 @@ public final class AppKitBackend: AppBackend {
861861
return scrollView
862862
}
863863

864+
public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {
865+
let scrollView = scrollView as! NSScrollView
866+
}
867+
864868
public func setScrollBarPresence(
865869
ofScrollContainer scrollView: Widget,
866870
hasVerticalScrollBar: Bool,

Sources/CursesBackend/CursesBackend.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public final class CursesBackend: AppBackend {
8484

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

87+
public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {}
88+
8789
public func createTextView() -> Widget {
8890
let label = Label("")
8991
label.width = Dim.fill()

Sources/Gtk3Backend/Gtk3Backend.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,10 @@ public final class Gtk3Backend: AppBackend {
539539
return scrollView
540540
}
541541

542+
public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {
543+
let scrollView = scrollView as! ScrolledWindow
544+
}
545+
542546
public func setScrollBarPresence(
543547
ofScrollContainer scrollView: Widget,
544548
hasVerticalScrollBar: Bool,

Sources/GtkBackend/GtkBackend.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,10 @@ public final class GtkBackend: AppBackend {
503503
return scrollView
504504
}
505505

506+
public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {
507+
let scrollView = scrollView as! ScrolledWindow
508+
}
509+
506510
public func setScrollBarPresence(
507511
ofScrollContainer scrollView: Widget,
508512
hasVerticalScrollBar: Bool,

Sources/LVGLBackend/LVGLBackend.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ public final class LVGLBackend: AppBackend {
156156
}
157157
}
158158

159+
public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {}
160+
159161
public func createTextView() -> Widget {
160162
return Widget { parent in
161163
let label = LVLabel(with: parent)

Sources/QtBackend/QtBackend.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ public struct QtBackend: AppBackend {
108108
}
109109
}
110110

111+
public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {}
112+
111113
public func setSpacing(ofHStack widget: Widget, to spacing: Int) {
112114
(widget.layout as! QHBoxLayout).spacing = Int32(spacing)
113115
}

Sources/SwiftCrossUI/Backend/AppBackend.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,20 @@ public protocol AppBackend {
264264

265265
/// Creates a scrollable single-child container wrapping the given widget.
266266
func createScrollContainer(for child: Widget) -> Widget
267+
/// Updates a scroll container with environment-specific values.
268+
///
269+
/// This method is primarily used on iOS to apply environment changes
270+
/// that affect the scroll view’s behavior, such as keyboard dismissal mode.
271+
/// It allows the backend to update UIKit-specific properties (e.g. `keyboardDismissMode`)
272+
/// when the environment changes.
273+
///
274+
/// - Parameters:
275+
/// - scrollView: The scroll container widget previously created by `createScrollContainer(for:)`.
276+
/// - environment: The current `EnvironmentValues` to apply.
277+
func updateScrollContainer(
278+
_ scrollView: Widget,
279+
environment: EnvironmentValues
280+
)
267281
/// Sets the presence of scroll bars along each axis of a scroll container.
268282
func setScrollBarPresence(
269283
ofScrollContainer scrollView: Widget,
@@ -741,6 +755,10 @@ extension AppBackend {
741755
todo()
742756
}
743757

758+
public func updateScrollContainer(_ scrollView: Widget, environment: EnvironmentValues) {
759+
todo()
760+
}
761+
744762
public func setScrollBarPresence(
745763
ofScrollContainer scrollView: Widget,
746764
hasVerticalScrollBar: Bool,

Sources/SwiftCrossUI/Environment/EnvironmentValues.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public struct EnvironmentValues {
198198
listStyle = .default
199199
toggleStyle = .button
200200
isEnabled = true
201-
scrollDismissesKeyboardMode = .never
201+
scrollDismissesKeyboardMode = .interactively
202202
}
203203

204204
/// Returns a copy of the environment with the specified property set to the

Sources/SwiftCrossUI/Views/Modifiers/ScrollDismissesKeyboardModifier.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ extension View {
2323
/// behavior for other kinds of scrollable views, like a ``List`` or a
2424
/// ``TextEditor``.
2525
///
26-
/// By default, scrollable content never automatically dismisses the keyboard.
26+
/// By default, scrollable content dismisses the keyboard interactively as the user scrolls.
2727
/// Pass a different value of ``ScrollDismissesKeyboardMode`` to change this behavior.
28-
/// For example, ``ScrollDismissesKeyboardMode/interactively`` allows the keyboard to dismiss
29-
/// as the user scrolls. Note that ``TextEditor`` may still use a different
28+
/// For example, use ``ScrollDismissesKeyboardMode/never`` to prevent the keyboard from
29+
/// dismissing automatically. Note that ``TextEditor`` may still use a different
3030
/// default to preserve expected editing behavior.
3131
///
3232
/// - Parameter mode: The keyboard dismissal mode that scrollable content

0 commit comments

Comments
 (0)