Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Add additional window modifiers
Browse files Browse the repository at this point in the history
Set a window's default size, title, resizability and deletability
  • Loading branch information
david-swift committed Dec 7, 2023
1 parent d5b4334 commit 2249ef2
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 24 deletions.
47 changes: 47 additions & 0 deletions Documentation/Reference/structs/Window.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:)`

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
59 changes: 59 additions & 0 deletions Sources/Adwaita/Window/Window.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
}
Expand All @@ -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.
Expand Down Expand Up @@ -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
19 changes: 11 additions & 8 deletions Tests/Demo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -28,21 +29,26 @@ struct Demo: App {
OverlayWindowDemo.WindowContent(window: window)
}
.keyboardShortcut("Escape") { $0.close() }
.defaultSize(width: 300, height: 200)
}
HelperWindows()
}

struct HelperWindows: WindowSceneGroup {

var scene: Scene {
Window(id: "content", open: 0) { window in
WindowsDemo.WindowContent(window: window)
Window(id: "content", open: 0) { _ in
WindowsDemo.WindowContent()
}
.resizable(false)
.closeShortcut()
Window(id: "toolbar-demo", open: 0) { window in
ToolbarDemo.WindowContent(window: window)
.defaultSize(width: 400, height: 250)
Window(id: "toolbar-demo", open: 0) { _ in
ToolbarDemo.WindowContent()
}
.closeShortcut()
.defaultSize(width: 400, height: 250)
.title("Toolbar Demo")
}

}
Expand Down Expand Up @@ -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())
}
Expand All @@ -97,9 +103,6 @@ struct Demo: App {
}
.toast("This is a toast!", signal: toast)
}
.onAppear {
window.setDefaultSize(width: 650, height: 450)
}
}

}
Expand Down
7 changes: 2 additions & 5 deletions Tests/OverlayWindowDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
4 changes: 0 additions & 4 deletions Tests/ToolbarDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ struct ToolbarDemo: View {

@State private var visible = false
@State private var moreContent = false
var window: GTUIWindow

var view: Body {
VStack {
Expand All @@ -50,9 +49,6 @@ struct ToolbarDemo: View {
.topToolbar {
HeaderBar.empty()
}
.onAppear {
window.setDefaultSize(width: 400, height: 250)
}
}

}
Expand Down
9 changes: 2 additions & 7 deletions Tests/WindowsDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions user-manual/Information/Widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down

0 comments on commit 2249ef2

Please sign in to comment.