BaseStubs.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. import Foundation
  2. extension BackendFeatures {
  3. /// A protocol that provides default implementations of all methods in
  4. /// ``BaseAppBackend`` so as to enable rapid iteration on custom backends.
  5. ///
  6. /// - Warning: **This protocol is _not_ intended to be used by
  7. /// production-ready backends!** Every default implementation that it
  8. /// provides will crash the user's app when called. This protocol is
  9. /// solely intended to make the compiler shut up when you're in the middle
  10. /// of implementing a brand-new backend.
  11. ///
  12. /// If you are unable to write a required backend method because your
  13. /// underlying UI framework doesn't properly support the associated
  14. /// functionality, you can call `fatalError` in your own implementation
  15. /// of the method. That way you'll still get notified by the compiler if
  16. /// we add new methods in future SwiftCrossUI versions.
  17. ///
  18. /// ## Workflow
  19. ///
  20. /// A typical workflow for implementing a new backend might go something
  21. /// like this:
  22. ///
  23. /// 1. Declare a type (usually a `class`, but there's no technical reason
  24. /// it can't be a `struct`) that conforms to `BaseStubs`.
  25. /// 2. Write a small example app using that type as its backend.
  26. /// 3. Implement enough of ``Core`` to get the example app to launch and
  27. /// show an empty window.
  28. /// 4. Switch the conformance over to ``BaseAppBackend``, and examine the
  29. /// compiler errors for suitable methods to implement.
  30. /// 5. Copy their declarations into the type, then switch back over to
  31. /// `BaseStubs` so the backend compiles again.
  32. /// 6. Iterate on the method implementations until they work properly.
  33. /// 7. Repeat steps 4 through 6 until you have a more-or-less complete
  34. /// backend.
  35. ///
  36. /// Of course, you can use whatever workflow works best for you; this
  37. /// protocol just serves as a tool to keep you from having to implement the
  38. /// entire backend in one go.
  39. #if !DEBUG
  40. @available(
  41. *,
  42. deprecated,
  43. message: """
  44. 'BaseStubs' should not be used in release builds, conform to 'BaseAppBackend' instead
  45. """
  46. )
  47. #endif
  48. public protocol BaseStubs: BaseAppBackend {}
  49. }
  50. // This type isn't actually used anywhere, so keep it out of release builds.
  51. #if DEBUG
  52. /// A backend "implementation" solely for testing whether
  53. /// ``BackendFeatures/BaseStubs`` has default implementations for all required
  54. /// backend features.
  55. ///
  56. /// Aside from empty nested structs to satisfy associated type requirements,
  57. /// **this struct must remain empty** -- all backend methods and properties
  58. /// should have default implementations provided by `BaseStubs`. If any errors
  59. /// show up here, and you've added empty structs for all associated types,
  60. /// you're missing some default implementations.
  61. ///
  62. /// 1. Accept all fix-mes for the errors in question.
  63. /// 2. Move the compiler-generated declarations out of this type and into the
  64. /// `BaseStubs` extension just below this type in the
  65. /// `BackendFeatures+BaseStubs` file. (Precisely _where_ you move them is
  66. /// unimportant, just try to keep some semblance of a logical order.)
  67. /// 3. **Make all declarations `public`.** This is important, and the
  68. /// compiler likely won't help you here because this struct is `private`.
  69. /// 4. Write `todo()` in the bodies of every method and property.
  70. private struct BaseStubsTest: BackendFeatures.BaseStubs {
  71. struct Window {}
  72. struct Widget {}
  73. }
  74. #endif
  75. #if !DEBUG
  76. @available(*, deprecated)
  77. #endif
  78. extension BackendFeatures.BaseStubs {
  79. fileprivate func todo(function: String = #function) -> Never {
  80. print("FATAL_TODO: \(Self.self): \(function) not implemented")
  81. fflush(nil)
  82. fatalError("\(Self.self): \(function) not implemented")
  83. }
  84. }
  85. #if !DEBUG
  86. @available(*, deprecated)
  87. #endif
  88. extension BackendFeatures.BaseStubs {
  89. public func createScrollContainer(for child: Widget) -> Widget {
  90. todo()
  91. }
  92. public func updateScrollContainer(
  93. _ scrollView: Widget,
  94. environment: EnvironmentValues,
  95. bounceHorizontally: Bool,
  96. bounceVertically: Bool,
  97. hasHorizontalScrollBar: Bool,
  98. hasVerticalScrollBar: Bool
  99. ) {
  100. todo()
  101. }
  102. public func createSelectableListView() -> Widget {
  103. todo()
  104. }
  105. public func updateSelectableListView(
  106. _ selectableListView: Widget,
  107. environment: EnvironmentValues
  108. ) {
  109. todo()
  110. }
  111. public func baseItemPadding(ofSelectableListView listView: Widget) -> EdgeInsets {
  112. todo()
  113. }
  114. public func minimumRowSize(ofSelectableListView listView: Widget) -> SIMD2<Int> {
  115. todo()
  116. }
  117. public func setItems(
  118. ofSelectableListView listView: Widget,
  119. to items: [Widget],
  120. withRowHeights rowHeights: [Int]
  121. ) {
  122. todo()
  123. }
  124. public func setSelectionHandler(
  125. forSelectableListView listView: Widget,
  126. to action: @escaping (Int) -> Void
  127. ) {
  128. todo()
  129. }
  130. public func setSelectedItem(
  131. ofSelectableListView listView: Widget,
  132. toItemAt index: Int?
  133. ) {
  134. todo()
  135. }
  136. public func createSplitView(leadingChild: Widget, trailingChild: Widget) -> Widget {
  137. todo()
  138. }
  139. public func setResizeHandler(
  140. ofSplitView splitView: Widget,
  141. to action: @escaping () -> Void
  142. ) {
  143. todo()
  144. }
  145. public func sidebarWidth(ofSplitView splitView: Widget) -> Int {
  146. todo()
  147. }
  148. public func setSidebarWidthBounds(
  149. ofSplitView splitView: Widget,
  150. minimum minimumWidth: Int,
  151. maximum maximumWidth: Int
  152. ) {
  153. todo()
  154. }
  155. public func size(
  156. of text: String,
  157. whenDisplayedIn widget: Widget,
  158. proposedWidth: Int?,
  159. proposedHeight: Int?,
  160. environment: EnvironmentValues
  161. ) -> SIMD2<Int> {
  162. todo()
  163. }
  164. public func createTextView() -> Widget {
  165. todo()
  166. }
  167. public func updateTextView(
  168. _ textView: Widget,
  169. content: String,
  170. environment: EnvironmentValues
  171. ) {
  172. todo()
  173. }
  174. public func createImageView() -> Widget {
  175. todo()
  176. }
  177. public func updateImageView(
  178. _ imageView: Widget,
  179. rgbaData: [UInt8],
  180. width: Int,
  181. height: Int,
  182. targetWidth: Int,
  183. targetHeight: Int,
  184. dataHasChanged: Bool,
  185. environment: EnvironmentValues
  186. ) {
  187. todo()
  188. }
  189. public func createSimpleButton() -> Widget {
  190. todo()
  191. }
  192. public func updateSimpleButton(
  193. _ button: Widget,
  194. label: String,
  195. environment: EnvironmentValues,
  196. action: @escaping () -> Void
  197. ) {
  198. todo()
  199. }
  200. public func createButton(
  201. wrapping: Widget
  202. ) -> Widget {
  203. todo()
  204. }
  205. public func updateButton(
  206. _ button: Widget,
  207. environment: EnvironmentValues,
  208. action: @escaping () -> Void
  209. ) {
  210. todo()
  211. }
  212. public func buttonPadding(in environment: EnvironmentValues) -> SIMD2<Int> {
  213. todo()
  214. }
  215. public func defaultButtonStyle() -> ButtonStyle {
  216. todo()
  217. }
  218. public func createToggle() -> Widget {
  219. todo()
  220. }
  221. public func updateToggle(
  222. _ toggle: Widget,
  223. label: String,
  224. environment: EnvironmentValues,
  225. onChange: @escaping (Bool) -> Void
  226. ) {
  227. todo()
  228. }
  229. public func setState(ofToggle toggle: Widget, to state: Bool) {
  230. todo()
  231. }
  232. public func createSwitch() -> Widget {
  233. todo()
  234. }
  235. public func updateSwitch(
  236. _ switchWidget: Widget,
  237. environment: EnvironmentValues,
  238. onChange: @escaping (Bool) -> Void
  239. ) {
  240. todo()
  241. }
  242. public func setState(ofSwitch switchWidget: Widget, to state: Bool) {
  243. todo()
  244. }
  245. public func createCheckbox() -> Widget {
  246. todo()
  247. }
  248. public func updateCheckbox(
  249. _ checkboxWidget: Widget,
  250. environment: EnvironmentValues,
  251. onChange: @escaping (Bool) -> Void
  252. ) {
  253. todo()
  254. }
  255. public func setState(ofCheckbox checkboxWidget: Widget, to state: Bool) {
  256. todo()
  257. }
  258. public func createSlider() -> Widget {
  259. todo()
  260. }
  261. public func updateSlider(
  262. _ slider: Widget,
  263. minimum: Double,
  264. maximum: Double,
  265. decimalPlaces: Int,
  266. environment: EnvironmentValues,
  267. onChange: @escaping (Double) -> Void
  268. ) {
  269. todo()
  270. }
  271. public func setValue(ofSlider slider: Widget, to value: Double) {
  272. todo()
  273. }
  274. public func createTextField() -> Widget {
  275. todo()
  276. }
  277. public func updateTextField(
  278. _ textField: Widget,
  279. placeholder: String,
  280. environment: EnvironmentValues,
  281. onChange: @escaping (String) -> Void,
  282. onSubmit: @escaping () -> Void
  283. ) {
  284. todo()
  285. }
  286. public func setContent(ofTextField textField: Widget, to content: String) {
  287. todo()
  288. }
  289. public func getContent(ofTextField textField: Widget) -> String {
  290. todo()
  291. }
  292. public func setContent(ofSecureField secureField: Widget, to content: String) {
  293. todo()
  294. }
  295. public func getContent(ofSecureField secureField: Widget) -> String {
  296. todo()
  297. }
  298. public func createTextEditor() -> Widget {
  299. todo()
  300. }
  301. public func updateTextEditor(
  302. _ textEditor: Widget,
  303. environment: EnvironmentValues,
  304. onChange: @escaping (String) -> Void
  305. ) {
  306. todo()
  307. }
  308. public func setContent(ofTextEditor textEditor: Widget, to content: String) {
  309. todo()
  310. }
  311. public func getContent(ofTextEditor textEditor: Widget) -> String {
  312. todo()
  313. }
  314. public func createPicker(style: BackendPickerStyle) -> Widget {
  315. todo()
  316. }
  317. public func updatePicker(
  318. _ picker: Widget,
  319. options: [String],
  320. environment: EnvironmentValues,
  321. onChange: @escaping (Int?) -> Void
  322. ) {
  323. todo()
  324. }
  325. public func setSelectedOption(ofPicker picker: Widget, to selectedOption: Int?) {
  326. todo()
  327. }
  328. public func createProgressSpinner() -> Widget {
  329. todo()
  330. }
  331. public func createProgressBar() -> Widget {
  332. todo()
  333. }
  334. public func updateProgressBar(
  335. _ widget: Widget,
  336. progressFraction: Double?,
  337. environment: EnvironmentValues
  338. ) {
  339. todo()
  340. }
  341. public var deviceClass: DeviceClass {
  342. todo()
  343. }
  344. public func runMainLoop(_ callback: @escaping @MainActor () -> Void) {
  345. todo()
  346. }
  347. public func runInMainThread(action: @escaping @MainActor () -> Void) {
  348. todo()
  349. }
  350. public func computeRootEnvironment(defaultEnvironment: EnvironmentValues) -> EnvironmentValues {
  351. todo()
  352. }
  353. public func setRootEnvironmentChangeHandler(
  354. to action: @escaping @Sendable @MainActor () -> Void
  355. ) {
  356. todo()
  357. }
  358. public var defaultPaddingAmount: Int {
  359. todo()
  360. }
  361. public func show(widget: Widget) {
  362. todo()
  363. }
  364. public func naturalSize(of widget: Widget) -> SIMD2<Int> {
  365. todo()
  366. }
  367. public func setSize(of widget: Widget, to size: SIMD2<Int>) {
  368. todo()
  369. }
  370. public var supportsMultipleWindows: Bool {
  371. todo()
  372. }
  373. public var canOverrideWindowColorScheme: Bool {
  374. todo()
  375. }
  376. public var restoresWindowFrames: Bool {
  377. todo()
  378. }
  379. public func createWindow(withDefaultSize defaultSize: SIMD2<Int>?, id: String) -> Window {
  380. todo()
  381. }
  382. public func updateWindow(_ window: Window, environment: EnvironmentValues) {
  383. todo()
  384. }
  385. public func setTitle(ofWindow window: Window, to title: String) {
  386. todo()
  387. }
  388. public func setChild(ofWindow window: Window, to child: Widget) {
  389. todo()
  390. }
  391. public func size(ofWindow window: Window) -> SIMD2<Int> {
  392. todo()
  393. }
  394. public func isWindowProgrammaticallyResizable(_ window: Window) -> Bool {
  395. todo()
  396. }
  397. public func setSize(ofWindow window: Window, to newSize: SIMD2<Int>) {
  398. todo()
  399. }
  400. public func setSizeLimits(
  401. ofWindow window: Window,
  402. minimum minimumSize: SIMD2<Int>,
  403. maximum maximumSize: SIMD2<Int>?
  404. ) {
  405. todo()
  406. }
  407. public func setResizeHandler(
  408. ofWindow window: Window,
  409. to action: @escaping (SIMD2<Int>) -> Void
  410. ) {
  411. todo()
  412. }
  413. public func show(window: Window) {
  414. todo()
  415. }
  416. public func activate(window: Window) {
  417. todo()
  418. }
  419. public func computeWindowEnvironment(
  420. window: Window,
  421. rootEnvironment: EnvironmentValues
  422. ) -> EnvironmentValues {
  423. todo()
  424. }
  425. public func setWindowEnvironmentChangeHandler(
  426. of window: Window,
  427. to action: @escaping @Sendable @MainActor () -> Void
  428. ) {
  429. todo()
  430. }
  431. public func createContainer() -> Widget {
  432. todo()
  433. }
  434. public func removeAllChildren(of container: Widget) {
  435. todo()
  436. }
  437. public func insert(_ child: Widget, into container: Widget, at index: Int) {
  438. todo()
  439. }
  440. public func swap(
  441. childAt firstIndex: Int,
  442. withChildAt secondIndex: Int,
  443. in container: Widget
  444. ) {
  445. todo()
  446. }
  447. public func setPosition(
  448. ofChildAt index: Int,
  449. in container: Widget,
  450. to position: SIMD2<Int>
  451. ) {
  452. todo()
  453. }
  454. public func remove(childAt index: Int, from container: Widget) {
  455. todo()
  456. }
  457. public var scrollBarWidth: Int {
  458. todo()
  459. }
  460. public var requiresImageUpdateOnScaleFactorChange: Bool {
  461. todo()
  462. }
  463. public var requiresToggleSwitchSpacer: Bool {
  464. todo()
  465. }
  466. public func createSecureField() -> Widget {
  467. todo()
  468. }
  469. public func updateSecureField(
  470. _ secureField: Widget,
  471. placeholder: String,
  472. environment: EnvironmentValues,
  473. onChange: @escaping (String) -> Void,
  474. onSubmit: @escaping () -> Void
  475. ) {
  476. todo()
  477. }
  478. public var supportedPickerStyles: [BackendPickerStyle] {
  479. todo()
  480. }
  481. }