Add support for setting a dialog's width & height

This commit is contained in:
david-swift 2024-03-21 09:40:58 +01:00
parent 8b4ce2bede
commit d32aaa9de7
2 changed files with 21 additions and 4 deletions

View File

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

View File

@ -13,6 +13,8 @@ struct DialogDemo: View {
@State private var dialog = false @State private var dialog = false
let padding = 20 let padding = 20
let width = 300
let height = 200
var view: Body { var view: Body {
VStack { VStack {
@ -23,7 +25,7 @@ struct DialogDemo: View {
.frame(maxSize: 100) .frame(maxSize: 100)
.padding() .padding()
} }
.dialog(visible: $dialog, title: "Counter") { .dialog(visible: $dialog, title: "Counter", width: width, height: height) {
CounterDemo() CounterDemo()
.padding(padding) .padding(padding)
.topToolbar { .topToolbar {