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 dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Mar 21, 2024
1 parent 2146a4f commit 6ee069e
Show file tree
Hide file tree
Showing 49 changed files with 263 additions and 146 deletions.
97 changes: 97 additions & 0 deletions Sources/Adwaita/View/Dialogs/Dialog.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//
// Dialog.swift
// Adwaita
//
// Created by david-swift on 20.03.24.
//

import CAdw

/// The dialog widget.
struct Dialog: Widget {

/// Whether the dialog is visible.
@Binding var visible: Bool
/// The dialog's title.
var title: String?
/// The wrapped view.
var child: View
/// The content of the dialog.
var content: Body

/// The ID for the dialog's storage.
let dialogID = "dialog"
/// The ID for the content's storage.
let contentID = "content"

/// Get the container of the child.
/// - Parameter modifiers: Modify views before being updated.
/// - Returns: The view storage.
func container(modifiers: [(View) -> View]) -> ViewStorage {
let storage = child.storage(modifiers: modifiers)
update(storage, modifiers: modifiers, updateProperties: true)
return storage
}

/// Update the view storage of the child, dialog, and dialog content.
/// - Parameters:
/// - storage: The view storage.
/// - modifiers: Modify views before being updated.
/// - updateProperties: Whether to update properties.
func update(_ storage: ViewStorage, modifiers: [(View) -> View], updateProperties: Bool) {
child.widget(modifiers: modifiers).update(storage, modifiers: modifiers, updateProperties: updateProperties)
if let storage = storage.content[contentID]?.first as? ViewStorage {
content
.widget(modifiers: modifiers)
.update(storage, modifiers: modifiers, updateProperties: updateProperties)
}
guard updateProperties else {
return
}
if visible {
if storage.content[dialogID]?.first == nil {
createDialog(storage: storage, modifiers: modifiers)
adw_dialog_present(storage.content[dialogID]?.first?.pointer?.cast(), storage.pointer?.cast())
}
adw_dialog_set_title(storage.content[dialogID]?.first?.pointer?.cast(), title ?? "")
} else {
if storage.content[dialogID]?.first != nil {
adw_dialog_close(storage.content[dialogID]?.first?.pointer?.cast())
}
}
}

/// Create a new instance of the dialog.
/// - Parameters:
/// - storage: The wrapped view's storage.
/// - modifiers: The view modifiers.
func createDialog(storage: ViewStorage, modifiers: [(View) -> View]) {
let pointer = adw_dialog_new()
let dialog = ViewStorage(pointer?.opaque())
storage.content[dialogID] = [dialog]
let contentStorage = content.widget(modifiers: modifiers).storage(modifiers: modifiers)
adw_dialog_set_child(pointer, contentStorage.pointer?.cast())
storage.content[contentID] = [contentStorage]
dialog.connectSignal(name: "closed") {
storage.content[dialogID] = []
storage.content[contentID] = []
if visible {
visible = false
}
}
}

}

extension View {

/// Add a dialog to the parent window.
/// - Parameters:
/// - visible: Whether the dialog is presented.
/// - title: The dialog's title.
/// - content: The dialog's content.
public func dialog(visible: Binding<Bool>, title: String? = nil, @ViewBuilder content: () -> Body) -> View {
Dialog(visible: visible, title: title, child: self, content: content())
}

}
2 changes: 1 addition & 1 deletion Sources/Adwaita/View/Generated/ActionRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// ActionRow.swift
// Adwaita
//
// Created by auto-generation on 17.02.24.
// Created by auto-generation on 20.03.24.
//

import CAdw
Expand Down
2 changes: 1 addition & 1 deletion Sources/Adwaita/View/Generated/Avatar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Avatar.swift
// Adwaita
//
// Created by auto-generation on 17.02.24.
// Created by auto-generation on 20.03.24.
//

import CAdw
Expand Down
2 changes: 1 addition & 1 deletion Sources/Adwaita/View/Generated/Banner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Banner.swift
// Adwaita
//
// Created by auto-generation on 17.02.24.
// Created by auto-generation on 20.03.24.
//

import CAdw
Expand Down
2 changes: 1 addition & 1 deletion Sources/Adwaita/View/Generated/Bin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Bin.swift
// Adwaita
//
// Created by auto-generation on 17.02.24.
// Created by auto-generation on 20.03.24.
//

import CAdw
Expand Down
2 changes: 1 addition & 1 deletion Sources/Adwaita/View/Generated/Box.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Box.swift
// Adwaita
//
// Created by auto-generation on 17.02.24.
// Created by auto-generation on 20.03.24.
//

import CAdw
Expand Down
2 changes: 1 addition & 1 deletion Sources/Adwaita/View/Generated/Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Button.swift
// Adwaita
//
// Created by auto-generation on 17.02.24.
// Created by auto-generation on 20.03.24.
//

import CAdw
Expand Down
7 changes: 4 additions & 3 deletions Sources/Adwaita/View/Generated/ButtonContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// ButtonContent.swift
// Adwaita
//
// Created by auto-generation on 17.02.24.
// Created by auto-generation on 20.03.24.
//

import CAdw
Expand All @@ -29,12 +29,13 @@ import LevenshteinTransformations
///
/// ```
/// buttoncontent
/// ╰── box
/// ├── image
/// ╰── label
/// ```
///
/// `AdwButtonContent`'s CSS node is called `buttoncontent`. It contains the
/// subnodes `image` and `label`.
/// `AdwButtonContent`'s CSS node is called `buttoncontent`. It contains a `box`
/// subnode that serves as a container for the `image` and `label` nodes.
///
/// When inside a `GtkButton` or `AdwSplitButton`, the button will receive the
/// `.image-text-button` style class. When inside a `GtkMenuButton`, the
Expand Down
14 changes: 9 additions & 5 deletions Sources/Adwaita/View/Generated/Carousel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Carousel.swift
// Adwaita
//
// Created by auto-generation on 17.02.24.
// Created by auto-generation on 20.03.24.
//

import CAdw
Expand Down Expand Up @@ -56,8 +56,10 @@ public struct Carousel<Element>: Widget where Element: Identifiable {
/// This signal is emitted after a page has been changed.
///
/// It can be used to implement "infinite scrolling" by amending the pages
/// after every scroll. Note that an empty carousel is indicated by
/// `(int)index == -1`.
/// after every scroll.
///
/// ::: note
/// An empty carousel is indicated by `(int)index == -1`.
var pageChanged: (() -> Void)?
/// The dynamic widget elements.
var elements: [Element]
Expand Down Expand Up @@ -219,8 +221,10 @@ public struct Carousel<Element>: Widget where Element: Identifiable {
/// This signal is emitted after a page has been changed.
///
/// It can be used to implement "infinite scrolling" by amending the pages
/// after every scroll. Note that an empty carousel is indicated by
/// `(int)index == -1`.
/// after every scroll.
///
/// ::: note
/// An empty carousel is indicated by `(int)index == -1`.
public func pageChanged(_ pageChanged: @escaping () -> Void) -> Self {
var newSelf = self
newSelf.pageChanged = pageChanged
Expand Down
2 changes: 1 addition & 1 deletion Sources/Adwaita/View/Generated/CenterBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// CenterBox.swift
// Adwaita
//
// Created by auto-generation on 17.02.24.
// Created by auto-generation on 20.03.24.
//

import CAdw
Expand Down
2 changes: 1 addition & 1 deletion Sources/Adwaita/View/Generated/CheckButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// CheckButton.swift
// Adwaita
//
// Created by auto-generation on 17.02.24.
// Created by auto-generation on 20.03.24.
//

import CAdw
Expand Down
2 changes: 1 addition & 1 deletion Sources/Adwaita/View/Generated/Clamp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Clamp.swift
// Adwaita
//
// Created by auto-generation on 17.02.24.
// Created by auto-generation on 20.03.24.
//

import CAdw
Expand Down
2 changes: 1 addition & 1 deletion Sources/Adwaita/View/Generated/ComboRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// ComboRow.swift
// Adwaita
//
// Created by auto-generation on 17.02.24.
// Created by auto-generation on 20.03.24.
//

import CAdw
Expand Down
12 changes: 11 additions & 1 deletion Sources/Adwaita/View/Generated/EntryRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// EntryRow.swift
// Adwaita
//
// Created by auto-generation on 17.02.24.
// Created by auto-generation on 20.03.24.
//

import CAdw
Expand Down Expand Up @@ -62,6 +62,8 @@ public struct EntryRow: Widget {
/// operation, e.g. network activity, to avoid triggering it after typing every
/// character.
var showApplyButton: Bool?
/// The length of the text in the entry row.
var textLength: UInt?
/// The title of the preference represented by this row.
///
/// The title is interpreted as Pango markup unless
Expand Down Expand Up @@ -226,6 +228,14 @@ public struct EntryRow: Widget {
return newSelf
}

/// The length of the text in the entry row.
public func textLength(_ textLength: UInt?) -> Self {
var newSelf = self
newSelf.textLength = textLength

return newSelf
}

/// The title of the preference represented by this row.
///
/// The title is interpreted as Pango markup unless
Expand Down
2 changes: 1 addition & 1 deletion Sources/Adwaita/View/Generated/ExpanderRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// ExpanderRow.swift
// Adwaita
//
// Created by auto-generation on 17.02.24.
// Created by auto-generation on 20.03.24.
//

import CAdw
Expand Down
2 changes: 1 addition & 1 deletion Sources/Adwaita/View/Generated/FlowBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// FlowBox.swift
// Adwaita
//
// Created by auto-generation on 17.02.24.
// Created by auto-generation on 20.03.24.
//

import CAdw
Expand Down
18 changes: 14 additions & 4 deletions Sources/Adwaita/View/Generated/HeaderBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// HeaderBar.swift
// Adwaita
//
// Created by auto-generation on 17.02.24.
// Created by auto-generation on 20.03.24.
//

import CAdw
Expand All @@ -16,6 +16,15 @@ import LevenshteinTransformations
/// features compared to it. Refer to `GtkHeaderBar` for details. It is typically
/// used as a top bar within [class@ToolbarView].
///
/// ## Dialog Integration
///
/// When placed inside an [class@Dialog], `AdwHeaderBar` will display the dialog
/// title intead of window title. It will also adjust the decoration layout to
/// ensure it always has a close button and nothing else. Set
/// [property@HeaderBar:show-start-title-buttons] and
/// [property@HeaderBar:show-end-title-buttons] to `FALSE` to remove it if it's
/// unwanted.
///
/// ## Navigation View Integration
///
/// When placed inside an [class@NavigationPage], `AdwHeaderBar` will display the
Expand All @@ -24,9 +33,10 @@ import LevenshteinTransformations
/// When used together with [class@NavigationView] or [class@NavigationSplitView],
/// it will also display a back button that can be used to go back to the previous
/// page. The button also has a context menu, allowing to pop multiple pages at
/// once, potentially across multiple navigation views. In rare scenarios, set
/// [property@HeaderBar:show-back-button] to `FALSE` to disable the back button
/// if it's unwanted (e.g. in an extra header bar on the same page).
/// once, potentially across multiple navigation views.
///
/// Set [property@HeaderBar:show-back-button] to `FALSE` to disable this behavior
/// in rare scenarios where it's unwanted.
///
/// ## Split View Integration
///
Expand Down
Loading

0 comments on commit 6ee069e

Please sign in to comment.