Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Add support for overlays
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Jan 3, 2024
1 parent e94885f commit 4f876fa
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions Documentation/Reference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
- [MenuSection](structs/MenuSection.md)
- [ModifierStopper](structs/ModifierStopper.md)
- [NavigationSplitView](structs/NavigationSplitView.md)
- [Overlay](structs/Overlay.md)
- [OverlaySplitView](structs/OverlaySplitView.md)
- [ScrollView](structs/ScrollView.md)
- [Signal](structs/Signal.md)
Expand Down
7 changes: 7 additions & 0 deletions Documentation/Reference/extensions/View.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ Run a function when the view gets an update.
Remove all of the content modifiers for the wrapped views.
- Returns: A view.

### `overlay(_:)`

Add an overlay view.
- Parameters:
- overlay: The overlay view.
- Returns: A view.

### `toast(_:signal:)`

Present a toast when the signal gets activated.
Expand Down
32 changes: 32 additions & 0 deletions Documentation/Reference/structs/Overlay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
**STRUCT**

# `Overlay`

A wrapper around a view for adding other views on top.

## Properties
### `child`

The child view.

### `overlay`

The overlay view.

### `overlayID`

The identifier for the overlay content.

## Methods
### `container(modifiers:)`

Get the overlay's view storage.
- Parameter modifiers: The view modifiers.
- Returns: The view storage.

### `update(_:modifiers:)`

Update the overlay's view storage.
- Parameters:
- storage: The view storage.
- modifiers: The view modifiers.
56 changes: 56 additions & 0 deletions Sources/Adwaita/View/Modifiers/Overlay.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Overlay.swift
// Adwaita
//
// Created by david-swift on 03.01.24.
//

import Libadwaita

/// A wrapper around a view for adding other views on top.
struct Overlay: Widget {

/// The child view.
var child: View
/// The overlay view.
var overlay: Body

/// The identifier for the overlay content.
let overlayID = "overlay"

/// Get the overlay's view storage.
/// - Parameter modifiers: The view modifiers.
/// - Returns: The view storage.
func container(modifiers: [(View) -> View]) -> ViewStorage {
let contentStorage = child.storage(modifiers: modifiers)
let overlayStorage = overlay.widget(modifiers: modifiers).storage(modifiers: modifiers)
let overlay = Libadwaita.Overlay().child(contentStorage.view).addOverlay(overlayStorage.view)
return .init(overlay, content: [.mainContent: [contentStorage], overlayID: [overlayStorage]])
}

/// Update the overlay's view storage.
/// - Parameters:
/// - storage: The view storage.
/// - modifiers: The view modifiers.
func update(_ storage: ViewStorage, modifiers: [(View) -> View]) {
if let storage = storage.content[.mainContent]?.first {
child.updateStorage(storage, modifiers: modifiers)
}
if let storage = storage.content[overlayID]?.first {
overlay.widget(modifiers: modifiers).update(storage, modifiers: modifiers)
}
}

}

extension View {

/// Add an overlay view.
/// - Parameters:
/// - overlay: The overlay view.
/// - Returns: A view.
public func overlay(@ViewBuilder _ overlay: @escaping () -> Body) -> View {
Overlay(child: self, overlay: overlay())
}

}
1 change: 1 addition & 0 deletions user-manual/Information/Widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ This is an overview of the available widgets and other components in _Adwaita_.
| `stopModifiers()` | Ignore all the `modifyContent(_:modify:)` modifiers from higher above in the view tree. |
| `toast(_:signal:)` | Show a toast on top of the view whenever the signal gets activated. |
| `toast(_:signal:button:handler:)` | Show a toast with a button on top of the view whenever the signal gets activated. |
| `overlay(_:)` | Overlay a view with another view. |

### `Button` Modifiers
| Syntax | Description |
Expand Down

0 comments on commit 4f876fa

Please sign in to comment.