From 6b5e1c1a254b98f3c363e307130845914da6af16 Mon Sep 17 00:00:00 2001 From: david-swift Date: Tue, 13 Feb 2024 15:48:24 +0100 Subject: [PATCH] Add freeze modifier Prevent a view from being updated --- Sources/Adwaita/View/Modifiers/Freeze.swift | 46 +++++++++++++++++++++ user-manual/Information/Widgets.md | 1 + 2 files changed, 47 insertions(+) create mode 100644 Sources/Adwaita/View/Modifiers/Freeze.swift diff --git a/Sources/Adwaita/View/Modifiers/Freeze.swift b/Sources/Adwaita/View/Modifiers/Freeze.swift new file mode 100644 index 0000000..737b54a --- /dev/null +++ b/Sources/Adwaita/View/Modifiers/Freeze.swift @@ -0,0 +1,46 @@ +// +// Freeze.swift +// Adwaita +// +// Created by david-swift on 13.02.24. +// + +/// State whether to update the child views or not. +struct Freeze: Widget { + + /// Whether not to update the child view. + var freeze: Bool + /// The wrapped view. + var content: View + + /// Get the content's container. + /// - Parameter modifiers: Modify views before being updated. + /// - Returns: The content's container. + func container(modifiers: [(View) -> View]) -> ViewStorage { + let storage = content.storage(modifiers: []) + return storage + } + + /// Update the content. + /// - Parameters: + /// - storage: The content's storage. + /// - modifiers: Modify views before being updated. + /// - updateProperties: Whether to update properties. + func update(_ storage: ViewStorage, modifiers: [(View) -> View], updateProperties: Bool) { + if !freeze { + content.updateStorage(storage, modifiers: [], updateProperties: updateProperties) + } + } + +} + +extension View { + + /// Prevent a view from being updated. + /// - Parameter freeze: Whether to freeze the view. + /// - Returns: A view. + public func freeze(_ freeze: Bool = true) -> View { + Freeze(freeze: freeze, content: self) + } + +} diff --git a/user-manual/Information/Widgets.md b/user-manual/Information/Widgets.md index e484527..0b59622 100644 --- a/user-manual/Information/Widgets.md +++ b/user-manual/Information/Widgets.md @@ -22,6 +22,7 @@ There are many more widgets available using auto-generation. Learn [how to use t | Syntax | Description | | --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ | +| `freeze(_:)` | Prevent a view from being updated. | | `inspect(_:)` | Edit the underlying Gtk or Libadwaita widget. | | `padding(_:_:)` | Add empty space around a view. | | `hexpand(_:)` | Enable or disable the horizontal expansion of a view. |