Control window width and height
Some checks failed
Deploy Docs / publish (push) Waiting to run
SwiftLint / SwiftLint (push) Failing after 4s

This commit is contained in:
david-swift 2024-10-16 23:14:09 +02:00
parent 40858df21d
commit f4dff46d62
2 changed files with 37 additions and 0 deletions

View File

@ -24,6 +24,10 @@ public struct Window: WinUISceneElement {
var `open`: Int
/// Whether to extend the content into the title bar.
var extendContent: Bool?
/// The window's width.
var width: Meta.Binding<Int>?
/// The window's height.
var height: Meta.Binding<Int>?
/// Create a window type with a certain identifier and user interface.
/// - Parameters:
@ -86,6 +90,16 @@ public struct Window: WinUISceneElement {
}
scene.content[.mainContent] = [storage]
window.systemBackdrop = MicaBackdrop()
if width != nil || height != nil {
var size = window.appWindow.size
if let width {
size.width = .init(width.wrappedValue)
}
if let height {
size.height = .init(height.wrappedValue)
}
try? window.appWindow.resize(size)
}
update(scene, app: app, updateProperties: true)
return scene
}
@ -110,6 +124,10 @@ public struct Window: WinUISceneElement {
updateProperties: updateProperties,
type: WinUIMainView.self
)
StateManager.blockUpdates = true
width?.wrappedValue = .init(window.appWindow.size.width)
height?.wrappedValue = .init(window.appWindow.size.height)
StateManager.blockUpdates = false
guard updateProperties else {
return
}
@ -132,6 +150,22 @@ public struct Window: WinUISceneElement {
return newSelf
}
/// The window's width and height.
/// - Parameters:
/// - width: The width.
/// - height: The height.
/// - Returns: The window.
///
/// Currently, the properties get updated only on view updates.
/// The binding can be used for storing the current state of the window for the next launch,
/// but not for live updating the user interface (this will be handled automatically by WinUI).
public func frame(width: Meta.Binding<Int>? = nil, height: Meta.Binding<Int>? = nil) -> Self {
var newSelf = self
newSelf.width = width
newSelf.height = height
return newSelf
}
}
/// A WinUI window.

View File

@ -14,6 +14,8 @@ Demo.main()
struct Demo: App {
@State private var width = 1000
@State private var height = 800
let app = WinUIApp()
var scene: Scene {
@ -21,6 +23,7 @@ struct Demo: App {
ContentView(app: app)
}
.extendContentIntoTitleBar()
.frame(width: $width, height: $height)
}
}