From d32aaa9de7c346353a634b6e496bdc55f46b28ce Mon Sep 17 00:00:00 2001 From: david-swift Date: Thu, 21 Mar 2024 09:40:58 +0100 Subject: [PATCH] Add support for setting a dialog's width & height --- Sources/Adwaita/View/Dialogs/Dialog.swift | 21 ++++++++++++++++++--- Tests/DialogDemo.swift | 4 +++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Sources/Adwaita/View/Dialogs/Dialog.swift b/Sources/Adwaita/View/Dialogs/Dialog.swift index 9ac2616..6a7725c 100644 --- a/Sources/Adwaita/View/Dialogs/Dialog.swift +++ b/Sources/Adwaita/View/Dialogs/Dialog.swift @@ -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" @@ -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()) @@ -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, title: String? = nil, @ViewBuilder content: () -> Body) -> View { - Dialog(visible: visible, title: title, child: self, content: content()) + public func dialog( + visible: Binding, + 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) } } diff --git a/Tests/DialogDemo.swift b/Tests/DialogDemo.swift index 29c477e..65af03b 100644 --- a/Tests/DialogDemo.swift +++ b/Tests/DialogDemo.swift @@ -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 { @@ -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 {