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 setting a dialog's width & height
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Mar 21, 2024
1 parent 8b4ce2b commit d32aaa9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
21 changes: 18 additions & 3 deletions Sources/Adwaita/View/Dialogs/Dialog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ struct Dialog: Widget {
var child: View
/// The content of the dialog.
var content: Body
/// The dialog's width.
var width: Int?
/// The dialog's height.
var height: Int?

/// The ID for the dialog's storage.
let dialogID = "dialog"
Expand Down Expand Up @@ -53,7 +57,10 @@ struct Dialog: Widget {
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 ?? "")
let pointer = storage.content[dialogID]?.first?.pointer
adw_dialog_set_title(pointer?.cast(), title ?? "")
adw_dialog_set_content_width(pointer?.cast(), width?.cInt ?? -1)
adw_dialog_set_content_height(pointer?.cast(), height?.cInt ?? -1)
} else {
if storage.content[dialogID]?.first != nil {
adw_dialog_close(storage.content[dialogID]?.first?.pointer?.cast())
Expand Down Expand Up @@ -89,9 +96,17 @@ extension View {
/// - Parameters:
/// - visible: Whether the dialog is presented.
/// - title: The dialog's title.
/// - width: The dialog's width.
/// - height: The dialog's height.
/// - 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())
public func dialog(
visible: Binding<Bool>,
title: String? = nil,
width: Int? = nil,
height: Int? = nil,
@ViewBuilder content: () -> Body
) -> View {
Dialog(visible: visible, title: title, child: self, content: content(), width: width, height: height)
}

}
4 changes: 3 additions & 1 deletion Tests/DialogDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ struct DialogDemo: View {

@State private var dialog = false
let padding = 20
let width = 300
let height = 200

var view: Body {
VStack {
Expand All @@ -23,7 +25,7 @@ struct DialogDemo: View {
.frame(maxSize: 100)
.padding()
}
.dialog(visible: $dialog, title: "Counter") {
.dialog(visible: $dialog, title: "Counter", width: width, height: height) {
CounterDemo()
.padding(padding)
.topToolbar {
Expand Down

0 comments on commit d32aaa9

Please sign in to comment.