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

Commit

Permalink
Make FileDialog a window
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Dec 9, 2023
1 parent 2249ef2 commit 083b352
Show file tree
Hide file tree
Showing 16 changed files with 455 additions and 115 deletions.
4 changes: 4 additions & 0 deletions Documentation/Reference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [Widget](protocols/Widget.md)
- [WindowScene](protocols/WindowScene.md)
- [WindowSceneGroup](protocols/WindowSceneGroup.md)
- [WindowType](protocols/WindowType.md)

## Structs

Expand All @@ -20,6 +21,7 @@
- [Clamp](structs/Clamp.md)
- [ContentModifier](structs/ContentModifier.md)
- [EitherView](structs/EitherView.md)
- [FileDialog](structs/FileDialog.md)
- [HStack](structs/HStack.md)
- [HeaderBar](structs/HeaderBar.md)
- [InspectorWrapper](structs/InspectorWrapper.md)
Expand Down Expand Up @@ -59,6 +61,8 @@

- [App](extensions/App.md)
- [Array](extensions/Array.md)
- [GTUIWindow](extensions/GTUIWindow.md)
- [Libadwaita.FileDialog](extensions/Libadwaita.FileDialog.md)
- [MenuItem](extensions/MenuItem.md)
- [MenuItemGroup](extensions/MenuItemGroup.md)
- [NativeWidgetPeer](extensions/NativeWidgetPeer.md)
Expand Down
8 changes: 2 additions & 6 deletions Documentation/Reference/classes/WindowStorage.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,17 @@ Whether the reference to the window should disappear in the next update.

### `window`

The GTUI window.
The window.

### `view`

The content's storage.

### `fileDialog`

The file dialog for the window.

## Methods
### `init(id:window:view:)`

Initialize a window storage.
- Parameters:
- id: The window's identifier.
- window: The GTUI window.
- window: The window.
- view: The content's storage.
9 changes: 9 additions & 0 deletions Documentation/Reference/extensions/GTUIWindow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
**EXTENSION**

# `GTUIWindow`

## Methods
### `setParentWindow(_:)`

Set the window's parent window.
- Parameter parent: The parent window.
48 changes: 48 additions & 0 deletions Documentation/Reference/extensions/Libadwaita.FileDialog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
**EXTENSION**

# `Libadwaita.FileDialog`

## Properties
### `importer`

An ID for the importer field.

### `folder`

An ID for the folder field.

### `result`

An ID for the result field.

### `cancel`

An ID for the cancel field.

### `isImporter`

Whether the file dialog is an importer.

### `folder`

The selected folder in the file dialog.

### `onResult`

A closure triggered on selecting a file in the dialog.

### `onCancel`

A closure triggered when the dialog is canceled.

## Methods
### `setParentWindow(_:)`

Set the window's parent window.
- Parameter parent: The parent window.

Currently not implemented.

### `show()`

Display the file dialog.
20 changes: 20 additions & 0 deletions Documentation/Reference/protocols/WindowType.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
**PROTOCOL**

# `WindowType`

A window type.

## Properties
### `fields`

A dictionary for custom data.

## Methods
### `setParentWindow(_:)`

Set a parent window.
- Parameter parent: The parent window.

### `show()`

Show the window.
90 changes: 90 additions & 0 deletions Documentation/Reference/structs/FileDialog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
**STRUCT**

# `FileDialog`

A structure representing a file dialog window.

## Properties
### `id`

The window's identifier.

### `importer`

Whether the window is an importer.

### `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.

### `initialFolder`

The initial folder.

### `initialName`

The initial file name for the file exporter.

### `extensions`

The accepted extensions for the file importer.

### `folders`

Whether folders are accepted in the file importer.

### `result`

The closure to run when the import or export is successful.

### `cancel`

The closure to run when the import or export is not successful.

## Methods
### `init(importer:initialFolder:extensions:folders:onOpen:onClose:)`

Create an importer file dialog window.
- Parameters:
- importer: The window's identifier.
- initialFolder: The URL to the folder open when being opened.
- extensions: The accepted file extensions.
- folders: Whether folders are accepted.
- onOpen: Run this when a file for importing has been chosen.
- onClose: Run this when the user cancelled the action.

### `init(exporter:initialFolder:initialName:onSave:onClose:)`

Create an exporter file dialog window.
- Parameters:
- exporter: The window's identifier.
- initialFolder: The URL to the folder open when being opened.
- initialName: The default file name.
- onSave: Run this when a path for exporting has been chosen.
- onClose: Run this when the user cancelled the action.

### `createWindow(app:)`

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

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

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

### `update(window:)`

Update the window.
- Parameter window: The window.
4 changes: 4 additions & 0 deletions Documentation/Reference/structs/Signal.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ A type that signalizes an action.

An action is signalized by toggling a boolean to `true` and back to `false`.

### `id`

A signal has a unique identifier.

### `update`

Whether the action has caused an update.
Expand Down
53 changes: 4 additions & 49 deletions Documentation/Reference/structs/Window.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,50 +31,6 @@ The keyboard shortcuts.

The keyboard shortcuts on the app level.

### `fileImporter`

The signal for the file importer.

### `fileExporter`

The signal for the file exporter.

### `initialImporterFolder`

The initial folder for the file importer.

### `initialExporterFolder`

The initial folder for the file exporter.

### `initialName`

The initial file name for the file exporter.

### `extensions`

The accepted extensions for the file importer.

### `folders`

Whether folders are accepted in the file importer.

### `importerResult`

The closure to run when the import is successful.

### `exporterResult`

The closure to run when the export is successful.

### `importerCancel`

The closure to run when the import is not successful.

### `exporterCancel`

The closure to run when the export is not successful.

### `defaultSize`

The default window size.
Expand All @@ -91,6 +47,10 @@ Whether the window is resizable.

Whether the window is deletable.

### `signals`

The signals for the importers and exporters.

## Methods
### `init(id:open:content:)`

Expand Down Expand Up @@ -168,11 +128,6 @@ Add a keyboard shortcut.
Update the keyboard shortcuts.
- Parameter window: The application window.

### `updateFileDialog(storage:)`

Open a file importer or exporter if a signal has been activated and update changes.
- Parameter storage: The window storage.

### `closeShortcut()`

Add the shortcut "<Ctrl>w" which closes the window.
Expand Down
4 changes: 4 additions & 0 deletions Sources/Adwaita/Model/Data Flow/Signal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
// Created by david-swift on 30.11.23.
//

import Foundation

/// A type that signalizes an action.
public struct Signal {

/// An action is signalized by toggling a boolean to `true` and back to `false`.
@State var boolean = false
/// A signal has a unique identifier.
public let id: UUID = .init()

/// Whether the action has caused an update.
public var update: Bool { boolean }
Expand Down
74 changes: 74 additions & 0 deletions Sources/Adwaita/Model/Extensions/Libadwaita.FileDialog.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// Libadwaita.FileDialog.swift
// Adwaita
//
// Created by david-swift on 09.12.23.
//

import Foundation
import Libadwaita

extension Libadwaita.FileDialog: WindowType {

/// An ID for the importer field.
static var importer: String { "importer" }
/// An ID for the folder field.
static var folder: String { "folder" }
/// An ID for the result field.
static var result: String { "result" }
/// An ID for the cancel field.
static var cancel: String { "cancel" }

/// Whether the file dialog is an importer.
var isImporter: Bool {
get {
fields[Self.importer] as? Bool ?? true
}
set {
fields[Self.importer] = newValue
}
}
/// The selected folder in the file dialog.
var folder: URL? {
get {
fields[Self.folder] as? URL
}
set {
fields[Self.folder] = newValue
}
}
/// A closure triggered on selecting a file in the dialog.
var onResult: ((URL) -> Void) {
get {
fields[Self.result] as? (URL) -> Void ?? { _ in }
}
set {
fields[Self.result] = newValue
}
}
/// A closure triggered when the dialog is canceled.
var onCancel: (() -> Void) {
get {
fields[Self.cancel] as? () -> Void ?? { }
}
set {
fields[Self.cancel] = newValue
}
}

/// Set the window's parent window.
/// - Parameter parent: The parent window.
///
/// Currently not implemented.
public func setParentWindow(_ parent: WindowType) { }

/// Display the file dialog.
public func show() {
if isImporter {
self.open(folder: folder, onResult, onClose: onCancel)
} else {
self.save(folder: folder, onResult, onClose: onCancel)
}
}

}
Loading

0 comments on commit 083b352

Please sign in to comment.