diff --git a/Sources/Adwaita/Window/Window.swift b/Sources/Adwaita/Window/Window.swift index 7780dfb..9090c27 100644 --- a/Sources/Adwaita/Window/Window.swift +++ b/Sources/Adwaita/Window/Window.swift @@ -41,6 +41,8 @@ public struct Window: WindowScene { var height: Binding? /// Whether to update the default size. var setDefaultSize = false + /// Whether the window is maximized. + var maximized: Binding? /// Create a window type with a certain identifier and user interface. /// - Parameters: @@ -135,6 +137,11 @@ public struct Window: WindowScene { } window.setResizability(template.resizable) window.setDeletability(template.deletable) + if !(template.maximized?.wrappedValue ?? false) { + gtk_window_unmaximize(window.pointer) + } else { + gtk_window_maximize(window.pointer) + } var storage = window.fields["storage"] as? ViewStorage if storage == nil { storage = .init(window.pointer?.opaque()) @@ -153,6 +160,11 @@ public struct Window: WindowScene { updateSize(window: window, template: template) } } + if template.maximized != nil { + storage?.notify(name: "maximized") { + updateSize(window: window, template: template) + } + } } /// Update the window's size. @@ -163,12 +175,16 @@ public struct Window: WindowScene { var width: Int32 = 0 var height: Int32 = 0 gtk_window_get_default_size(window.pointer, &width, &height) - if width != template.width?.wrappedValue ?? 0 { + let maximized = gtk_window_is_maximized(window.pointer) != 0 + if width != template.width?.wrappedValue ?? -1 { template.width?.wrappedValue = .init(width) } - if height != template.height?.wrappedValue ?? 0 { + if height != template.height?.wrappedValue ?? -1 { template.height?.wrappedValue = .init(height) } + if maximized != template.maximized?.wrappedValue { + template.maximized?.wrappedValue = maximized + } } /// Add windows that overlay the last instance of this window if presented. @@ -304,7 +320,7 @@ public struct Window: WindowScene { return newSelf } - /// Add a tooltip to the widget. + /// Get the window's width and height. /// - Parameters: /// - width: The window's actual width. /// - height: The window's actual height. @@ -316,6 +332,15 @@ public struct Window: WindowScene { return newSelf } + /// Get and set whether the window is maximized. + /// - Parameter maximized: Whether the window is maximized. + /// - Returns: The window. + public func maximized(_ maximized: Binding) -> Self { + var newSelf = self + newSelf.maximized = maximized + return newSelf + } + } // swiftlint:enable discouraged_optional_collection diff --git a/Tests/Demo.swift b/Tests/Demo.swift index 7c45b70..f3f232a 100644 --- a/Tests/Demo.swift +++ b/Tests/Demo.swift @@ -81,6 +81,8 @@ struct Demo: App { private var width = 650 @State("height") private var height = 450 + @State("maximized") + private var maximized = false var window: GTUIApplicationWindow var app: GTUIApp! @@ -158,6 +160,7 @@ struct Demo: App { func window(_ window: Window) -> Window { window .size(width: $width, height: $height) + .maximized($maximized) } } diff --git a/user-manual/Information/Widgets.md b/user-manual/Information/Widgets.md index d629018..eea673a 100644 --- a/user-manual/Information/Widgets.md +++ b/user-manual/Information/Widgets.md @@ -165,6 +165,8 @@ There are many more widgets available using auto-generation. Learn [how to use t | `title(_:)` | Set the window's title. | | `resizable(_:)` | Set the window's resizability. | | `deletable(_:)` | Set the window's deletability. | +| `maximized(_:)` | Get and set whether the window is maximized. | +| `size(width:height:)` | Get and set the window's width and height. | ### `AboutWindow` Modifiers | Syntax | Description |