{"id":18031756,"url":"https://github.com/dagronf/viviewinvalidating","last_synced_at":"2025-03-27T05:30:58.437Z","repository":{"id":46614994,"uuid":"375895432","full_name":"dagronf/VIViewInvalidating","owner":"dagronf","description":"A swift PropertyWrapper providing automatic NSView and UIView invalidation when the properties value changes. (Backwards compatible version of @Invalidating)","archived":false,"fork":false,"pushed_at":"2021-10-03T23:34:04.000Z","size":41,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-23T04:31:40.105Z","etag":null,"topics":["appkit","invalidating","invalidation-support","nsview","propertywrapper","uikit","uiview"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dagronf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-06-11T03:38:49.000Z","updated_at":"2024-01-09T13:42:47.000Z","dependencies_parsed_at":"2022-07-19T14:38:53.934Z","dependency_job_id":null,"html_url":"https://github.com/dagronf/VIViewInvalidating","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FVIViewInvalidating","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FVIViewInvalidating/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FVIViewInvalidating/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FVIViewInvalidating/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dagronf","download_url":"https://codeload.github.com/dagronf/VIViewInvalidating/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245791340,"owners_count":20672665,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["appkit","invalidating","invalidation-support","nsview","propertywrapper","uikit","uiview"],"created_at":"2024-10-30T10:10:41.096Z","updated_at":"2025-03-27T05:30:57.394Z","avatar_url":"https://github.com/dagronf.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# @VIViewInvalidating\n\nA swift `PropertyWrapper` to provide automatic `NSView`/`UIView` invalidation when the properties value changes. It duplicates the `@Invalidating` propertyWrapper for build targets prior to macOS 12 and iOS 15.\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/v/tag/dagronf/VIViewInvalidating\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/macOS-10.11+-red\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/iOS-11.0+-blue\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/tvOS-11.0+-orange\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/macCatalyst-1.0+-purple\" /\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/Swift-5.1-orange.svg\" /\u003e\n    \u003cimg src=\"https://img.shields.io/badge/License-MIT-lightgrey\" /\u003e\n    \u003ca href=\"https://swift.org/package-manager\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/spm-compatible-brightgreen.svg?style=flat\" alt=\"Swift Package Manager\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n## Why?\n\nI saw in the WWDC2021 video ['What's new in AppKit'](https://developer.apple.com/wwdc21/10054) they make a brief mention of a new propertyWrapper type `@Invalidating()` that automatically updates views when the wrappedValue is changed. It appears this propertyWrapper is only available in later versions of AppKit (and presumably UIKit).\n\nGiven that a lot of AppKit/UIKit devs aren't going to be able to move their minimum target version to macOS 12 or iOS 15 soon I decided to try to replicate what I saw in the video.\n\n`@VIViewInvalidating()` was born!\n\nAnd once your target is set to macOS 13 or above, your `@VIViewInvalidating()` definitions will generate deprecation warnings telling you to move to `@Invalidating()`.\n\nI've tried to make sure that the APIs are as close to `@Invalidating` as possible so moving your app target should in theory be as simple as changing some names\n\n* `@VIViewInvalidating` -\u003e `@Invalidating` \n* `VIViewCustomInvalidating` -\u003e `UIViewInvalidating` (for iOS/tvOS)\n* `VIViewCustomInvalidating` -\u003e `NSViewInvalidating` (for macOS)\n\n\n## Invalidating types\n\n### Built-in\n\nProvides built-in invalidators for\n\n- needsDisplay (`.display`)\n- needsLayout (`.layout`)\n- needsUpdateConstraints (`.constraints`)\n- invalidateIntrinsicContentSize() (`.intrinsicContentSize`)\n- invalidateRestorableState() (`.restorableState`)    [***macOS only***]\n\n#### Example\n\n```swift\nclass BadgeView: NSView {\n   // Automatically sets needsDisplay = true on the view when the value changes\n   @VIViewInvalidating(.display) var color: NSColor = NSColor.blue\n   \n   // Set needsDisplay, needsLayout and invalidateIntrinsicContentSize() on \n   // the view when the value changes\n   @VIViewInvalidating(.display, .layout, .intrinsicContentSize)\n   var position: NSControl.ImagePosition = .imageLeft\n}\n```\n\n## Custom invalidation\n\n### Conforming your view to the `VIViewCustomInvalidating` protocol\n\nThe protocol method provides a very high-level callback when any of the `@VIViewInvalidating` properties are updated within your view. This is equivalent to the `NSViewInvalidating` protocol in later SDKs (macOS 12 and iOS 15)\n\n#### Example\n\n```swift\nclass BadgeView: NSView  {\n   @VIViewInvalidating(.display) var color: NSColor = NSColor.blue\n   @VIViewInvalidating(.display) var backgroundColor: NSColor = NSColor.white\n}\n\nextension BadgeView: VIViewCustomInvalidating {\n   // Will be called when any `@VIViewInvalidating` property is updated in the view\n   func invalidate(view: NSView) {\n      Swift.print(\"custom invalidation!\")\n   }\n}\n```\n\n### Providing your own invalidator (not recommended!)\n\n**NOTE** that this behaviour is NOT compatible with Apple's `@Invalidating` property wrapper.  `@Invalidating` doesn't provide a similar functionality, so be aware when you move your build target up to macOS13/iOS15 there is no direct replacement so your code will break.\n\nYou can provide custom invalidators by defining a new class of type `VIViewType.VIViewInvalidatorAction`.\n\n#### Example\n\n```swift\nclass CustomInvalidator: VIViewInvalidatorAction {\n   public override func invalidate(_ view: VIViewType) {\n      Swift.print(\"Custom invalidator called\")\n   }\n}\n\nclass ExcitingView: NSView {\n   @VIViewInvalidating(.display) var color: NSColor = .white\n   @VIViewInvalidating(.display, CustomInvalidator()) var backgroundColor: NSColor = .systemBlue\n   override func draw(_ dirtyRect: NSRect) {\n      self.backgroundColor.setFill()\n      dirtyRect.fill()\n   }\n}\n```\n\n# Updates\n\n### 3.0.1\n\n* Fixed macOS demo linkage\n\n### 3.0.0\n\n* Added separate dynamic and static targets (thanks [BeehiveInnovations](https://github.com/BeehiveInnovations))!\n\n### 2.0.1\n\n* Resolved Swift runtime crash when building in Xcode 11 and Swift 5.1. Previous version would crash the Swift runtime as it tries to resolve generic arguments for a class nested in an extension. Removing the nested extension containing the property wrapper (it wasn't required) solved the issue. This has no impact when compiling with Xcode 12 and above.\n\n### 2.0.0\n\n* [**BREAKING**] Now that Apple has made `@Invalidating` available through Xcode, I've changed custom callback to match Apple's 'Invalidating' protocol to aid adoption when upgrading the SDK to one that supports `@Invalidating`.\n* [**BREAKING**] Changed the mechanism for handling custom invalidations.\n* Added `restorableState` as an invalidation type on macOS to be compatible with `@Invalidating` on macOS 12+\n\n### 1.0.0\n\n* Initial release\n\n# Thanks\n\n### John Sundell\n\n[Twitter](https://twitter.com/johnsundell), [Swift By Sundell](https://www.swiftbysundell.com)\n\n* [Accessing a swift property wrappers enclosing instance](https://www.swiftbysundell.com/articles/accessing-a-swift-property-wrappers-enclosing-instance/)\n* [The power of subscripts in swift](https://www.swiftbysundell.com/articles/the-power-of-subscripts-in-swift/#static-subscripts)\n\n\n# License\n\nMIT. Use it for anything you want! Let me know if you do use it somewhere, I'd love to hear about it.\n\n```\nMIT License\n\nCopyright (c) 2021 Darren Ford\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdagronf%2Fviviewinvalidating","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdagronf%2Fviviewinvalidating","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdagronf%2Fviviewinvalidating/lists"}