Skip to content

Commit

Permalink
Add NativeView.rawView (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
xtyxtyx authored Jan 14, 2025
1 parent fa999ed commit 44ddc19
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Sources/Shaft/Backend/SDLMetalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
drawable.present()
}
}

public override var rawView: UnsafeMutableRawPointer? {
sdlMetalView
}
}

#endif
4 changes: 4 additions & 0 deletions Sources/Shaft/Backend/SDLView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ public class SDLView: NativeView {
SDL_SetWindowTitle(sdlWindow, newValue)
}
}

public var rawView: UnsafeMutableRawPointer? {
nil
}
}

// typedef bool (SDLCALL * SDL_EventFilter) (void *userdata, SDL_Event * event);
Expand Down
13 changes: 13 additions & 0 deletions Sources/Shaft/Core/Backend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ public protocol NativeView: AnyObject {

/// Getting/Setting the title of the view if possible.
var title: String { get set }

/// A raw pointer to the underlying view object that this NativeView wraps.
///
/// - On MacOS, this is an pointer to a `NSView`.
/// - On iOS/tvOS, this is a pointer to a `UIView`.
///
/// This is an optional property, and it is not guaranteed to be implemented
/// on all platforms.
var rawView: UnsafeMutableRawPointer? { get }
}

extension NativeView {
public var rawView: UnsafeMutableRawPointer? { nil }
}

#if canImport(AppKit)
Expand Down
10 changes: 7 additions & 3 deletions Sources/Shaft/Widgets/Binding/WidgetsBinding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,18 @@ public class WidgetsBinding {
/// against the previous widget tree and any differences are applied to the
/// underlying render tree, similar to what happens when a [StatefulWidget]
/// rebuilds after calling [State.setState].
public func runApp(_ app: Widget) {
///
/// An additional optional parameter `view` can be passed to specify the
/// `NativeView` to use for rendering the app. If not provided, a new
/// `NativeView` will be created.
public func runApp(_ app: Widget, view: NativeView? = nil) {
runPlainApp(
DefaultApp { app }
)
}

public func runPlainApp(_ app: Widget) {
guard let view = backend.createView() else {
public func runPlainApp(_ app: Widget, view: NativeView? = nil) {
guard let view = view ?? backend.createView() else {
fatalError("Failed to create view")
}

Expand Down

0 comments on commit 44ddc19

Please sign in to comment.