Skip to content

Commit 17875d2

Browse files
committed
onAppear now get called correctly, onDisappear only on interactive dismiss
No idea why it doesn't get called on programmatic dismissal. Neither like it is now nor through a completion handler of the programmatic dismiss
1 parent 022274b commit 17875d2

File tree

5 files changed

+63
-43
lines changed

5 files changed

+63
-43
lines changed

Examples/Sources/WindowingExample/WindowingApp.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,22 @@ struct SheetDemo: View {
135135
.sheet(isPresented: $showNextChild) {
136136
DoubleNestedSheetBody(dismissParent: { dismiss() })
137137
.interactiveDismissDisabled()
138+
.onAppear {
139+
print("deepest nested sheet appeared")
140+
}
141+
.onDisappear {
142+
print("deepest nested sheet disappeared")
143+
}
138144
}
139145
Button("dismiss parent sheet") {
140146
dismissParent()
141147
}
142148
Button("dismiss") {
143149
dismiss()
144150
}
151+
.onDisappear {
152+
print("nested sheet disappeared")
153+
}
145154
}
146155
}
147156
struct DoubleNestedSheetBody: View {

Sources/Gtk/Widgets/Window.swift

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ open class Window: Widget {
8282

8383
public func present() {
8484
gtk_window_present(castedPointer())
85-
85+
8686
addSignal(name: "close-request") { [weak self] () in
8787
guard let self = self else { return }
8888
self.onCloseRequest?(self)
@@ -94,35 +94,37 @@ open class Window: Widget {
9494
Unmanaged<ValueBox<() -> Void>>.fromOpaque(data).release()
9595
escapeKeyHandlerData = nil
9696
}
97-
97+
9898
if let oldController = escapeKeyEventController {
9999
gtk_widget_remove_controller(widgetPointer, oldController)
100100
escapeKeyEventController = nil
101101
}
102-
102+
103103
escapeKeyPressed = handler
104-
104+
105105
guard handler != nil else { return }
106-
106+
107107
let keyEventController = gtk_event_controller_key_new()
108108
gtk_event_controller_set_propagation_phase(keyEventController, GTK_PHASE_BUBBLE)
109-
110-
let thunk: @convention(c) (
111-
UnsafeMutableRawPointer?, guint, guint, GdkModifierType, gpointer?
112-
) -> gboolean = { _, keyval, _, _, userData in
113-
if keyval == GDK_KEY_Escape {
114-
guard let userData else { return 1 }
115-
let box = Unmanaged<ValueBox<() -> Void>>.fromOpaque(userData).takeUnretainedValue()
116-
box.value()
117-
return 1
109+
110+
let thunk:
111+
@convention(c) (
112+
UnsafeMutableRawPointer?, guint, guint, GdkModifierType, gpointer?
113+
) -> gboolean = { _, keyval, _, _, userData in
114+
if keyval == GDK_KEY_Escape {
115+
guard let userData else { return 1 }
116+
let box = Unmanaged<ValueBox<() -> Void>>.fromOpaque(userData)
117+
.takeUnretainedValue()
118+
box.value()
119+
return 1
120+
}
121+
return 0
118122
}
119-
return 0
120-
}
121-
123+
122124
let boxedHandler = Unmanaged.passRetained(
123125
ValueBox(value: handler!)
124126
).toOpaque()
125-
127+
126128
g_signal_connect_data(
127129
UnsafeMutableRawPointer(keyEventController),
128130
"key-pressed",
@@ -135,15 +137,15 @@ open class Window: Widget {
135137
},
136138
.init(0)
137139
)
138-
140+
139141
gtk_widget_add_controller(widgetPointer, keyEventController)
140142
escapeKeyEventController = keyEventController
141143
escapeKeyHandlerData = boxedHandler
142144
}
143-
145+
144146
private var escapeKeyEventController: OpaquePointer?
145147
private var escapeKeyHandlerData: UnsafeMutableRawPointer?
146-
148+
147149
public var onCloseRequest: ((Window) -> Int32)?
148150
public var escapeKeyPressed: (() -> Void)?
149151
}

Sources/GtkBackend/GtkBackend.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,7 +1604,7 @@ public final class GtkBackend: AppBackend {
16041604
sheet.css.set(property: .cornerRadius(defaultSheetCornerRadius))
16051605

16061606
if connectedCloseHandlers.insert(key).inserted {
1607-
sheet.onCloseRequest = {[weak self] _ in
1607+
sheet.onCloseRequest = { [weak self] _ in
16081608
if ctx.interactiveDismissDisabled { return 1 }
16091609

16101610
if ctx.isProgrammaticDismiss {
@@ -1625,7 +1625,7 @@ public final class GtkBackend: AppBackend {
16251625
ctx.onDismiss()
16261626
}
16271627
}
1628-
1628+
16291629
}
16301630
}
16311631

Sources/SwiftCrossUI/ViewGraph/PreferenceValues.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public struct PreferenceValues: Sendable {
2323

2424
/// The backgroundcolor of a sheet. Applies to enclosing sheets.
2525
public var presentationBackground: Color?
26-
26+
2727
/// Controls whether the user can interactively dismiss enclosing sheets. Applies to enclosing sheets.
2828
public var interactiveDismissDisabled: Bool?
2929

@@ -57,9 +57,10 @@ public struct PreferenceValues: Sendable {
5757
// For presentation modifiers, take the outer-most value (using child ordering to break ties).
5858
presentationDetents = children.compactMap { $0.presentationDetents }.first
5959
presentationCornerRadius = children.compactMap { $0.presentationCornerRadius }.first
60-
presentationDragIndicatorVisibility = children.compactMap {
61-
$0.presentationDragIndicatorVisibility
62-
}.first
60+
presentationDragIndicatorVisibility =
61+
children.compactMap {
62+
$0.presentationDragIndicatorVisibility
63+
}.first
6364
presentationBackground = children.compactMap { $0.presentationBackground }.first
6465
interactiveDismissDisabled = children.compactMap { $0.interactiveDismissDisabled }.first
6566
}

Sources/SwiftCrossUI/Views/Modifiers/SheetModifier.swift

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,9 @@ struct SheetModifier<Content: View, SheetContent: View>: TypeSafeView {
3535
)
3636
let bodyNode = AnyViewGraphNode(bodyViewGraphNode)
3737

38-
let sheetViewGraphNode = ViewGraphNode(
39-
for: sheetContent(),
40-
backend: backend,
41-
environment: environment
42-
)
43-
let sheetContentNode = AnyViewGraphNode(sheetViewGraphNode)
44-
4538
return SheetModifierViewChildren(
4639
childNode: bodyNode,
47-
sheetContentNode: sheetContentNode,
40+
sheetContentNode: nil,
4841
sheet: nil
4942
)
5043
}
@@ -72,16 +65,24 @@ struct SheetModifier<Content: View, SheetContent: View>: TypeSafeView {
7265
)
7366

7467
if isPresented.wrappedValue && children.sheet == nil {
68+
let sheetViewGraphNode = ViewGraphNode(
69+
for: sheetContent(),
70+
backend: backend,
71+
environment: environment
72+
)
73+
let sheetContentNode = AnyViewGraphNode(sheetViewGraphNode)
74+
children.sheetContentNode = sheetContentNode
75+
7576
let sheet = backend.createSheet(
76-
content: children.sheetContentNode.widget.into()
77+
content: children.sheetContentNode!.widget.into()
7778
)
7879

7980
let dismissAction = DismissAction(action: { [isPresented] in
8081
isPresented.wrappedValue = false
8182
})
8283
let sheetEnvironment = environment.with(\.dismiss, dismissAction)
8384

84-
let dryRunResult = children.sheetContentNode.update(
85+
let dryRunResult = children.sheetContentNode!.update(
8586
with: sheetContent(),
8687
proposedSize: backend.sizeOf(sheet),
8788
environment: sheetEnvironment,
@@ -90,7 +91,7 @@ struct SheetModifier<Content: View, SheetContent: View>: TypeSafeView {
9091

9192
let preferences = dryRunResult.preferences
9293

93-
let _ = children.sheetContentNode.update(
94+
let _ = children.sheetContentNode!.update(
9495
with: sheetContent(),
9596
proposedSize: backend.sizeOf(sheet),
9697
environment: sheetEnvironment,
@@ -99,7 +100,7 @@ struct SheetModifier<Content: View, SheetContent: View>: TypeSafeView {
99100

100101
backend.updateSheet(
101102
sheet,
102-
onDismiss: handleDismiss
103+
onDismiss: { handleDismiss(children: children) }
103104
)
104105

105106
// MARK: Sheet Presentation Preferences
@@ -137,13 +138,16 @@ struct SheetModifier<Content: View, SheetContent: View>: TypeSafeView {
137138
window: environment.window! as! Backend.Window
138139
)
139140
children.sheet = nil
141+
children.sheetContentNode = nil
140142
}
141143
return childResult
142144
}
143145

144-
func handleDismiss() {
146+
func handleDismiss(children: Children) {
145147
onDismiss?()
146148
isPresented.wrappedValue = false
149+
children.sheet = nil
150+
children.sheetContentNode = nil
147151
}
148152
}
149153

@@ -153,16 +157,20 @@ class SheetModifierViewChildren<Child: View, SheetContent: View>: ViewGraphNodeC
153157
}
154158

155159
var erasedNodes: [ErasedViewGraphNode] {
156-
[ErasedViewGraphNode(wrapping: childNode), ErasedViewGraphNode(wrapping: sheetContentNode)]
160+
var nodes: [ErasedViewGraphNode] = [ErasedViewGraphNode(wrapping: childNode)]
161+
if let sheetContentNode = sheetContentNode {
162+
nodes.append(ErasedViewGraphNode(wrapping: sheetContentNode))
163+
}
164+
return nodes
157165
}
158166

159167
var childNode: AnyViewGraphNode<Child>
160-
var sheetContentNode: AnyViewGraphNode<SheetContent>
168+
var sheetContentNode: AnyViewGraphNode<SheetContent>?
161169
var sheet: Any?
162170

163171
init(
164172
childNode: AnyViewGraphNode<Child>,
165-
sheetContentNode: AnyViewGraphNode<SheetContent>,
173+
sheetContentNode: AnyViewGraphNode<SheetContent>?,
166174
sheet: Any?
167175
) {
168176
self.childNode = childNode

0 commit comments

Comments
 (0)