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

Commit

Permalink
Add support for AdwAboutWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Dec 5, 2023
1 parent 475c38a commit e473755
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 12 deletions.
1 change: 1 addition & 0 deletions Documentation/Reference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

## Structs

- [AboutWindow](structs/AboutWindow.md)
- [AppearObserver](structs/AppearObserver.md)
- [Binding](structs/Binding.md)
- [Button](structs/Button.md)
Expand Down
88 changes: 88 additions & 0 deletions Documentation/Reference/structs/AboutWindow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
**STRUCT**

# `AboutWindow`

A structure representing an about window.

## Properties
### `id`

The window's identifier.

### `open`

Whether an instance of the window type should be opened when the app is starting up.

### `parentID`

The identifier of the window's parent.

### `appShortcuts`

The keyboard shortcuts on the app level.

### `appName`

The app's name.

### `developer`

The developer's name.

### `version`

The app version.

### `icon`

The app icon.

### `website`

The app's website.

## Methods
### `init(id:appName:developer:version:)`

Create a window type with a certain identifier and content.
- Parameters:
- id: The identifier.
- appName: The app's name.
- developer: The developer's name.
- version: The app version.

### `icon(_:)`

Set the app icon.
- Parameter icon: The app icon.
- Returns: The window.

### `website(_:)`

Set the app's website.
- Parameter url: The app's website.
- Returns: The window.

### `createWindow(app:)`

Get the storage for the window.
- Parameter app: The application.
- Returns: The storage.

### `createGTUIWindow(app:)`

Get the window.
- Parameter app: The application.
- Returns: The window.

### `update(_:app:)`

Update a window.
- Parameters:
- storage: The storage to update.
- app: The application.

### `updateData(window:)`

Update the data for a window.
- Parameter window: The window.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class WindowStorage {
/// The GTUI window.
public var window: GTUIWindow
/// The content's storage.
public var view: ViewStorage
public var view: ViewStorage?
/// The file dialog for the window.
public var fileDialog: FileDialog

Expand All @@ -28,7 +28,7 @@ public class WindowStorage {
/// - id: The window's identifier.
/// - window: The GTUI window.
/// - view: The content's storage.
public init(id: String, window: GTUIWindow, view: ViewStorage) {
public init(id: String, window: GTUIWindow, view: ViewStorage?) {
self.id = id
self.window = window
self.view = view
Expand Down
101 changes: 101 additions & 0 deletions Sources/Adwaita/Window/AboutWindow.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// AboutWindow.swift
// Adwaita
//
// Created by david-swift on 05.12.23.
//

import Foundation
import Libadwaita

/// A structure representing an about window.
public struct AboutWindow: WindowScene {

/// The window's identifier.
public var id: String
/// Whether an instance of the window type should be opened when the app is starting up.
public let `open` = 0
/// The identifier of the window's parent.
public var parentID: String?
/// The keyboard shortcuts on the app level.
public var appShortcuts: [String: (GTUIApp) -> Void] = [:]
/// The app's name.
var appName: String
/// The developer's name.
var developer: String
/// The app version.
var version: String
/// The app icon.
var icon: Icon?
/// The app's website.
var website: URL?

/// Create a window type with a certain identifier and content.
/// - Parameters:
/// - id: The identifier.
/// - appName: The app's name.
/// - developer: The developer's name.
/// - version: The app version.
public init(id: String, appName: String, developer: String, version: String) {
self.id = id
self.appName = appName
self.developer = developer
self.version = version
}

/// Set the app icon.
/// - Parameter icon: The app icon.
/// - Returns: The window.
public func icon(_ icon: Icon) -> Self {
var newSelf = self
newSelf.icon = icon
return newSelf
}

/// Set the app's website.
/// - Parameter url: The app's website.
/// - Returns: The window.
public func website(_ url: URL?) -> Self {
var newSelf = self
newSelf.website = url
return newSelf
}

/// Get the storage for the window.
/// - Parameter app: The application.
/// - Returns: The storage.
public func createWindow(app: GTUIApp) -> WindowStorage {
let window = createGTUIWindow(app: app)
let windowStorage = WindowStorage(id: id, window: window, view: nil)
windowStorage.parentID = parentID
return windowStorage
}

/// Get the window.
/// - Parameter app: The application.
/// - Returns: The window.
func createGTUIWindow(app: GTUIApp) -> Libadwaita.AboutWindow {
let window = Libadwaita.AboutWindow()
updateAppShortcuts(app: app)
updateData(window: window)
window.show()
return window
}

/// Update a window.
/// - Parameters:
/// - storage: The storage to update.
/// - app: The application.
public func update(_ storage: WindowStorage, app: GTUIApp) {
updateAppShortcuts(app: app)
storage.destroy = true
}

/// Update the data for a window.
/// - Parameter window: The window.
func updateData(window: Libadwaita.AboutWindow) {
_ = window.generalData(title: appName, icon: icon ?? .custom(name: ""), developer: developer, version: version)
if let website { _ = window.website(url: website.absoluteString) }
}

}
4 changes: 3 additions & 1 deletion Sources/Adwaita/Window/Window.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public struct Window: WindowScene {
public func update(_ storage: WindowStorage, app: GTUIApp) {
if let window = storage.window as? GTUIApplicationWindow {
let content = content(window)
content.widget(modifiers: []).updateStorage(storage.view, modifiers: [])
if let view = storage.view {
content.widget(modifiers: []).updateStorage(view, modifiers: [])
}
updateShortcuts(window: window)
updateAppShortcuts(app: app)
}
Expand Down
10 changes: 6 additions & 4 deletions Tests/Demo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ struct Demo: App {
DemoContent(window: window, app: app)
}
.overlay {
AboutWindow(id: "about", appName: "Demo", developer: "david-swift", version: "Test")
.icon(.default(icon: .emojiNature))
.website(.init(string: "david-swift.gitbook.io/adwaita"))
Window(id: "overlay", open: 0) { window in
OverlayWindowDemo.WindowContent(window: window)
}
Expand Down Expand Up @@ -73,10 +76,9 @@ struct Demo: App {
}
.keyboardShortcut("w".ctrl())
MenuSection {
MenuButton("Quit", window: false) {
app.quit()
}
.keyboardShortcut("q".ctrl())
MenuButton("About", window: false) { app.showWindow("about") }
MenuButton("Quit", window: false) { app.quit() }
.keyboardShortcut("q".ctrl())
}
}
}
Expand Down
19 changes: 14 additions & 5 deletions user-manual/Information/Widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ This is an overview of the available widgets and other components in _Adwaita_.
| Name | Description | Widget |
| -------------------- | ----------------------------------------------------------------- | ---------------------- |
| Window | A simple application window. | AdwApplicationWindow |
| AboutWindow | A GNOME about window. | AdwAboutWindow |

### Window Modifiers
| Syntax | Description |
Expand All @@ -69,11 +70,19 @@ This is an overview of the available widgets and other components in _Adwaita_.
| `quitShortcut()` | Create a keyboard shortcut for quitting the application with "Ctrl + q". |

### `Window` Modifiers
| Syntax | Description |
| ------------------------------- | --------------------------------------------------------------------------------------- |
| `keyboardShortcut(_:action:)` | Create a keyboard shortcut available in one window. |
| `closeShortcut()` | Create a keyboard shortcut for closing the window with "Ctrl + w". |
| `overlay(windows:)` | Add windows that attach to a window of this type when being presented. |
| Syntax | Description |
| ------------------------------------------------------------------ | --------------------------------------------------------------------------------------- |
| `keyboardShortcut(_:action:)` | Create a keyboard shortcut available in one window. |
| `closeShortcut()` | Create a keyboard shortcut for closing the window with "Ctrl + w". |
| `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. |

### `AboutWindow` Modifiers
| Syntax | Description |
| --------------- | --------------------------------------------------------------------------------------- |
| `icon(_:)` | Set the app icon. |
| `website(_:)` | Set the app's website |

### Menu Widgets
| Name | Description | Widget |
Expand Down

0 comments on commit e473755

Please sign in to comment.