Skip to content

Commit 0b56815

Browse files
committed
Fix window sizing issue, fix view hiding, bring window to front on URL
1 parent e237115 commit 0b56815

File tree

12 files changed

+51
-10
lines changed

12 files changed

+51
-10
lines changed

Sources/AppKitBackend/AppKitBackend.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ public final class AppKitBackend: AppBackend {
107107
window.makeKeyAndOrderFront(nil)
108108
}
109109

110+
public func activate(window: Window) {
111+
window.makeKeyAndOrderFront(nil)
112+
}
113+
110114
private static func renderMenuItems(_ items: [ResolvedMenu.Item]) -> [NSMenuItem] {
111115
items.map { item in
112116
switch item {

Sources/Gtk/Widgets/Window.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,8 @@ open class Window: Widget {
7979
public func getChild() -> Widget? {
8080
return child
8181
}
82+
83+
public func present() {
84+
gtk_window_present(castedPointer())
85+
}
8286
}

Sources/Gtk3/Widgets/Window.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ open class Window: Bin {
5757
}
5858
}
5959

60+
public func present() {
61+
gtk_window_present(castedPointer())
62+
}
63+
6064
public func setMinimumSize(to minimumSize: Size) {
6165
gtk_widget_set_size_request(
6266
castedPointer(),

Sources/Gtk3Backend/Gtk3Backend.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ public final class Gtk3Backend: AppBackend {
211211
window.showAll()
212212
}
213213

214+
public func activate(window: Window) {
215+
window.present()
216+
}
217+
214218
class ThreadActionContext {
215219
var action: () -> Void
216220

Sources/GtkBackend/GtkBackend.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ public final class GtkBackend: AppBackend {
147147
window.show()
148148
}
149149

150+
public func activate(window: Window) {
151+
window.present()
152+
}
153+
150154
private func renderMenu(
151155
_ menu: ResolvedMenu,
152156
actionMap: any GActionMap,

Sources/SwiftCrossUI/Backend/AppBackend.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ public protocol AppBackend {
129129
/// for some backends). Predominantly used by window-based ``Scene``
130130
/// implementations after propagating updates.
131131
func show(window: Window)
132+
/// Brings a window to the front if possible. Called when the window
133+
/// receives an external URL or file to handle from the desktop environment.
134+
/// May be used in other circumstances eventually.
135+
func activate(window: Window)
132136

133137
/// Sets the application's global menu. Some backends may make use of the host
134138
/// platform's global menu bar (such as macOS's menu bar), and others may render their

Sources/SwiftCrossUI/Environment/EnvironmentValues.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ public struct EnvironmentValues {
4141
/// a bottom-up update chain up which resize events can propagate.
4242
var onResize: (_ newSize: ViewSize) -> Void
4343

44+
/// Brings the current window forward, not guaranteed to always bring
45+
/// the window to the top (due to focus stealing prevention).
46+
func bringWindowForward() {
47+
func activate<Backend: AppBackend>(with backend: Backend) {
48+
backend.activate(window: window as! Backend.Window)
49+
}
50+
activate(with: backend)
51+
print("Activated")
52+
}
53+
4454
/// The backend's representation of the window that the current view is
4555
/// in, if any. This is a very internal detail that should never get
4656
/// exposed to users.

Sources/SwiftCrossUI/LayoutSystem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public enum LayoutSystem {
5858
environment: environment,
5959
dryRun: true
6060
)
61-
isHidden[i] = result.participatesInStackLayouts
61+
isHidden[i] = !result.participatesInStackLayouts
6262
}
6363

6464
// My thanks go to this great article for investigating and explaining

Sources/SwiftCrossUI/Modifiers/OnOpenURLModifier.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import Foundation
22

33
extension View {
44
public func onOpenURL(perform action: @escaping (URL) -> Void) -> some View {
5-
PreferenceModifier(self) { preferences in
5+
PreferenceModifier(self) { preferences, environment in
66
var newPreferences = preferences
77
newPreferences.onOpenURL = { url in
88
action(url)
9-
preferences.onOpenURL?(url)
9+
if let innerHandler = preferences.onOpenURL {
10+
innerHandler(url)
11+
} else {
12+
environment.bringWindowForward()
13+
}
1014
}
1115
return newPreferences
1216
}

Sources/SwiftCrossUI/Modifiers/PreferenceModifier.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
extension View {
2-
public func preference<V>(key: WritableKeyPath<PreferenceValues, V>, value: V) -> some View {
3-
PreferenceModifier(self) { preferences in
2+
public func preference<V>(
3+
key: WritableKeyPath<PreferenceValues, V>,
4+
value: V
5+
) -> some View {
6+
PreferenceModifier(self) { preferences, _ in
47
var preferences = preferences
58
preferences[keyPath: key] = value
69
return preferences
@@ -10,11 +13,11 @@ extension View {
1013

1114
struct PreferenceModifier<Child: View>: View {
1215
var body: TupleView1<Child>
13-
var modification: (PreferenceValues) -> PreferenceValues
16+
var modification: (PreferenceValues, EnvironmentValues) -> PreferenceValues
1417

1518
init(
1619
_ child: Child,
17-
modification: @escaping (PreferenceValues) -> PreferenceValues
20+
modification: @escaping (PreferenceValues, EnvironmentValues) -> PreferenceValues
1821
) {
1922
self.body = TupleView1(child)
2023
self.modification = modification
@@ -36,7 +39,7 @@ struct PreferenceModifier<Child: View>: View {
3639
backend: backend,
3740
dryRun: dryRun
3841
)
39-
result.preferences = modification(result.preferences)
42+
result.preferences = modification(result.preferences, environment)
4043
return result
4144
}
4245
}

0 commit comments

Comments
 (0)