Add additional window modifiers

Set a window's default size, title, resizability and deletability
This commit is contained in:
david-swift 2023-12-07 22:27:28 +01:00
parent d5b43344a1
commit 2249ef2d6a
7 changed files with 127 additions and 26 deletions

View File

@ -75,6 +75,22 @@ The closure to run when the import is not successful.
The closure to run when the export is not successful.
### `defaultSize`
The default window size.
### `title`
The window's title.
### `resizable`
Whether the window is resizable.
### `deletable`
Whether the window is deletable.
## Methods
### `init(id:open:content:)`
@ -107,6 +123,11 @@ Get the storage of the content view.
Update a window storage's content.
- Parameter storage: The storage to update.
### `setProperties(window:)`
Set some general propreties of the window.
- Parameter window: The window.
### `overlay(windows:)`
Add windows that overlay the last instance of this window if presented.
@ -156,3 +177,29 @@ Open a file importer or exporter if a signal has been activated and update chang
Add the shortcut "<Ctrl>w" which closes the window.
- Returns: The window.
### `defaultSize(width:height:)`
Set the window's default size.
- Parameters:
- width: The window's width.
- height: The window's height.
- Returns: The window.
### `title(_:)`
Set the window's title.
- Parameter title: The title.
- Returns: The window.
### `resizable(_:)`
Set whether the window is resizable.
- Parameter resizable: The resizability.
- Returns: The window.
### `deletable(_:)`
Set whether the window is deletable.
- Parameter resizable: The deletability.
- Returns: The window.

View File

@ -49,6 +49,14 @@ public struct Window: WindowScene {
var importerCancel: (() -> Void)?
/// The closure to run when the export is not successful.
var exporterCancel: (() -> Void)?
/// The default window size.
var defaultSize: (Int, Int)?
/// The window's title.
var title: String?
/// Whether the window is resizable.
var resizable = true
/// Whether the window is deletable.
var deletable = true
/// Create a window type with a certain identifier and user interface.
/// - Parameters:
@ -94,6 +102,7 @@ public struct Window: WindowScene {
let content = content(window)
let storage = content.widget(modifiers: []).container(modifiers: [])
window.setChild(storage.view)
setProperties(window: window)
updateShortcuts(window: window)
return storage
}
@ -106,12 +115,24 @@ public struct Window: WindowScene {
if let view = storage.view {
content.widget(modifiers: []).updateStorage(view, modifiers: [])
}
setProperties(window: window)
updateShortcuts(window: window)
updateAppShortcuts(app: app)
}
updateFileDialog(storage: storage)
}
/// Set some general propreties of the window.
/// - Parameter window: The window.
func setProperties(window: GTUIApplicationWindow) {
window.setDefaultSize(width: defaultSize?.0, height: defaultSize?.1)
if let title {
window.setTitle(title)
}
window.setResizability(resizable)
window.setDeletability(deletable)
}
/// Add windows that overlay the last instance of this window if presented.
/// - Parameter windows: The windows.
/// - Returns: The new windows and this window.
@ -211,6 +232,44 @@ public struct Window: WindowScene {
keyboardShortcut("w".ctrl()) { $0.close() }
}
/// Set the window's default size.
/// - Parameters:
/// - width: The window's width.
/// - height: The window's height.
/// - Returns: The window.
public func defaultSize(width: Int, height: Int) -> Self {
var newSelf = self
newSelf.defaultSize = (width, height)
return newSelf
}
/// Set the window's title.
/// - Parameter title: The title.
/// - Returns: The window.
public func title(_ title: String) -> Self {
var newSelf = self
newSelf.title = title
return newSelf
}
/// Set whether the window is resizable.
/// - Parameter resizable: The resizability.
/// - Returns: The window.
public func resizable(_ resizable: Bool) -> Self {
var newSelf = self
newSelf.resizable = resizable
return newSelf
}
/// Set whether the window is deletable.
/// - Parameter resizable: The deletability.
/// - Returns: The window.
public func deletable(_ deletable: Bool) -> Self {
var newSelf = self
newSelf.deletable = deletable
return newSelf
}
}
// swiftlint:enable discouraged_optional_collection

View File

@ -20,6 +20,7 @@ struct Demo: App {
Window(id: "main") { window in
DemoContent(window: window, app: app)
}
.defaultSize(width: 650, height: 450)
.overlay {
AboutWindow(id: "about", appName: "Demo", developer: "david-swift", version: "Test")
.icon(.default(icon: .emojiNature))
@ -28,6 +29,7 @@ struct Demo: App {
OverlayWindowDemo.WindowContent(window: window)
}
.keyboardShortcut("Escape") { $0.close() }
.defaultSize(width: 300, height: 200)
}
HelperWindows()
}
@ -35,14 +37,18 @@ struct Demo: App {
struct HelperWindows: WindowSceneGroup {
var scene: Scene {
Window(id: "content", open: 0) { window in
WindowsDemo.WindowContent(window: window)
}
.closeShortcut()
Window(id: "toolbar-demo", open: 0) { window in
ToolbarDemo.WindowContent(window: window)
Window(id: "content", open: 0) { _ in
WindowsDemo.WindowContent()
}
.resizable(false)
.closeShortcut()
.defaultSize(width: 400, height: 250)
Window(id: "toolbar-demo", open: 0) { _ in
ToolbarDemo.WindowContent()
}
.closeShortcut()
.defaultSize(width: 400, height: 250)
.title("Toolbar Demo")
}
}
@ -76,7 +82,7 @@ struct Demo: App {
}
.keyboardShortcut("w".ctrl())
MenuSection {
MenuButton("About") { app.addWindow("about", parent: window); print(window.nativePtr) }
MenuButton("About") { app.addWindow("about", parent: window) }
MenuButton("Quit", window: false) { app.quit() }
.keyboardShortcut("q".ctrl())
}
@ -97,9 +103,6 @@ struct Demo: App {
}
.toast("This is a toast!", signal: toast)
}
.onAppear {
window.setDefaultSize(width: 650, height: 450)
}
}
}

View File

@ -5,7 +5,7 @@
// Created by david-swift on 09.11.23.
//
// swiftlint:disable missing_docs implicitly_unwrapped_optional no_magic_numbers
// swiftlint:disable missing_docs implicitly_unwrapped_optional
import Adwaita
@ -42,13 +42,10 @@ struct OverlayWindowDemo: View {
.topToolbar {
HeaderBar.empty()
}
.onAppear {
window.setDefaultSize(width: 300, height: 200)
}
}
}
}
// swiftlint:enable missing_docs implicitly_unwrapped_optional no_magic_numbers
// swiftlint:enable missing_docs implicitly_unwrapped_optional

View File

@ -27,7 +27,6 @@ struct ToolbarDemo: View {
@State private var visible = false
@State private var moreContent = false
var window: GTUIWindow
var view: Body {
VStack {
@ -50,9 +49,6 @@ struct ToolbarDemo: View {
.topToolbar {
HeaderBar.empty()
}
.onAppear {
window.setDefaultSize(width: 400, height: 250)
}
}
}

View File

@ -5,7 +5,7 @@
// Created by david-swift on 25.09.23.
//
// swiftlint:disable missing_docs implicitly_unwrapped_optional no_magic_numbers
// swiftlint:disable missing_docs implicitly_unwrapped_optional
import Adwaita
@ -34,21 +34,16 @@ struct WindowsDemo: View {
struct WindowContent: View {
var window: GTUIWindow
var view: Body {
Text("This window exists at most once.")
.padding()
.topToolbar {
HeaderBar.empty()
}
.onAppear {
window.setDefaultSize(width: 400, height: 250)
}
}
}
}
// swiftlint:enable missing_docs implicitly_unwrapped_optional no_magic_numbers
// swiftlint:enable missing_docs implicitly_unwrapped_optional

View File

@ -77,6 +77,10 @@ This is an overview of the available widgets and other components in _Adwaita_.
| `overlay(windows:)` | Add windows that attach to a window of this type when being presented. |
| `fileImporter(_:initialFolder:extensions:folders:onOpen:onClose:)` | Add an import file dialog. |
| `fileExporter(_:initialFolder:initialName:onSave:onClose:)` | Add an export file dialog. |
| `defaultSize(width:height:)` | Set the window's initial size. |
| `title(_:)` | Set the window's title. |
| `resizable(_:)` | Set the window's resizability. |
| `deletable(_:)` | Set the window's deletability. |
### `AboutWindow` Modifiers
| Syntax | Description |