forked from aparoksha/adwaita-swift
Add support for acknowledgements and rework about links
This commit is contained in:
parent
4ec1ac3769
commit
83af8478c3
@ -8,8 +8,25 @@
|
|||||||
import CAdw
|
import CAdw
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
/// URL links for about dialog.
|
/// A link shown in the About dialog.
|
||||||
public typealias AboutLink = (title: String, url: URL?)
|
public struct AboutLink {
|
||||||
|
|
||||||
|
/// The link title.
|
||||||
|
public var title: String
|
||||||
|
|
||||||
|
/// The destination URL.
|
||||||
|
public var url: URL?
|
||||||
|
|
||||||
|
/// Create a new link.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - title: The link title.
|
||||||
|
/// - url: The destination URL.
|
||||||
|
public init(title: String, url: URL?) {
|
||||||
|
self.title = title
|
||||||
|
self.url = url
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/// The type of contribution made by a credited person.
|
/// The type of contribution made by a credited person.
|
||||||
public enum ContributionRole {
|
public enum ContributionRole {
|
||||||
@ -45,6 +62,26 @@ public struct CreditEntry {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// An acknowledgement entry.
|
||||||
|
public struct AcknowledgementEntry {
|
||||||
|
|
||||||
|
/// The acknowledgement section title.
|
||||||
|
public var title: String
|
||||||
|
|
||||||
|
/// Acknowledged person/organization name.
|
||||||
|
public var name: String
|
||||||
|
|
||||||
|
/// Create a new acknowledgement entry.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - title: The acknowledgement section title.
|
||||||
|
/// - name: The acknowledged person or organization.
|
||||||
|
public init(title: String, name: String) {
|
||||||
|
self.title = title
|
||||||
|
self.name = name
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/// Initialization options for the about dialog wrapper.
|
/// Initialization options for the about dialog wrapper.
|
||||||
public struct AdwaitaAboutDialogConfig {
|
public struct AdwaitaAboutDialogConfig {
|
||||||
|
|
||||||
@ -129,6 +166,8 @@ public struct AdwaitaAboutDialogConfig {
|
|||||||
public var comments: String?
|
public var comments: String?
|
||||||
/// Recognition by name and role of contributors.
|
/// Recognition by name and role of contributors.
|
||||||
public var credits: [CreditEntry]?
|
public var credits: [CreditEntry]?
|
||||||
|
/// Acknowledgements to display in the dialog.
|
||||||
|
public var acknowledgements: [AcknowledgementEntry]?
|
||||||
|
|
||||||
/// Initialize the about dialog wrapper.
|
/// Initialize the about dialog wrapper.
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
@ -145,6 +184,7 @@ public struct AdwaitaAboutDialogConfig {
|
|||||||
/// - releaseNotes: The app's release notes.
|
/// - releaseNotes: The app's release notes.
|
||||||
/// - comments: The comments about the application.
|
/// - comments: The comments about the application.
|
||||||
/// - credits: Recognition by name and role of contributors.
|
/// - credits: Recognition by name and role of contributors.
|
||||||
|
/// - acknowledgements: List of acknowledgements.
|
||||||
public init(
|
public init(
|
||||||
appName: String? = nil,
|
appName: String? = nil,
|
||||||
developer: String? = nil,
|
developer: String? = nil,
|
||||||
@ -158,7 +198,8 @@ public struct AdwaitaAboutDialogConfig {
|
|||||||
license: String? = nil,
|
license: String? = nil,
|
||||||
releaseNotes: String? = nil,
|
releaseNotes: String? = nil,
|
||||||
comments: String? = nil,
|
comments: String? = nil,
|
||||||
credits: [CreditEntry]? = nil
|
credits: [CreditEntry]? = nil,
|
||||||
|
acknowledgements: [AcknowledgementEntry]? = nil
|
||||||
) {
|
) {
|
||||||
self.appName = appName
|
self.appName = appName
|
||||||
self.developer = developer
|
self.developer = developer
|
||||||
@ -173,46 +214,104 @@ public struct AdwaitaAboutDialogConfig {
|
|||||||
self.releaseNotes = releaseNotes
|
self.releaseNotes = releaseNotes
|
||||||
self.comments = comments
|
self.comments = comments
|
||||||
self.credits = credits
|
self.credits = credits
|
||||||
|
self.acknowledgements = acknowledgements
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Apply the credit entries to the given dialog.
|
/// Applies a string value to the dialog using the given setter.
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - credits: The list of credit entries to apply.
|
/// - value: The optional string to apply.
|
||||||
/// - dialog: The underlying Adwaita dialog instance to update with the credit information.
|
/// - setter: The C function that sets the value on the dialog.
|
||||||
private func applyCredits(_ credits: [CreditEntry], to dialog: OpaquePointer) {
|
/// - dialog: The dialog instance.
|
||||||
var developers: [String] = []
|
@inline(__always)
|
||||||
var designers: [String] = []
|
private func set(
|
||||||
var artists: [String] = []
|
_ value: String?,
|
||||||
var translators: [String] = []
|
using setter: (OpaquePointer, UnsafePointer<CChar>?) -> Void,
|
||||||
|
on dialog: OpaquePointer
|
||||||
|
) {
|
||||||
|
if let value {
|
||||||
|
setter(dialog, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for entry in credits {
|
/// Applies a list of links to the dialog.
|
||||||
switch entry.role {
|
/// - Parameters:
|
||||||
case .developer:
|
/// - links: The optional list of links.
|
||||||
developers.append(entry.name)
|
/// - dialog: The dialog instance.
|
||||||
case .designer:
|
@inline(__always)
|
||||||
designers.append(entry.name)
|
private func set(
|
||||||
case .artist:
|
_ links: [AboutLink]?,
|
||||||
artists.append(entry.name)
|
on dialog: OpaquePointer
|
||||||
case .translator:
|
) {
|
||||||
translators.append(entry.name)
|
links?.forEach { link in
|
||||||
|
adw_about_dialog_add_link(dialog, link.title, link.url?.absoluteString)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Applies credit entries to the dialog.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - credits: The optional list of credit entries.
|
||||||
|
/// - dialog: The dialog instance.
|
||||||
|
@inline(__always)
|
||||||
|
private func set(
|
||||||
|
_ credits: [CreditEntry]?,
|
||||||
|
on dialog: OpaquePointer
|
||||||
|
) {
|
||||||
|
if let credits {
|
||||||
|
var developers: [String] = []
|
||||||
|
var designers: [String] = []
|
||||||
|
var artists: [String] = []
|
||||||
|
var translators: [String] = []
|
||||||
|
|
||||||
|
for entry in credits {
|
||||||
|
switch entry.role {
|
||||||
|
case .developer:
|
||||||
|
developers.append(entry.name)
|
||||||
|
case .designer:
|
||||||
|
designers.append(entry.name)
|
||||||
|
case .artist:
|
||||||
|
artists.append(entry.name)
|
||||||
|
case .translator:
|
||||||
|
translators.append(entry.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let ptr = developers.cMutableArray, !developers.isEmpty {
|
||||||
|
adw_about_dialog_set_developers(dialog, ptr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if let ptr = designers.cMutableArray, !designers.isEmpty {
|
||||||
|
adw_about_dialog_set_designers(dialog, ptr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if let ptr = artists.cMutableArray, !artists.isEmpty {
|
||||||
|
adw_about_dialog_set_artists(dialog, ptr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !translators.isEmpty {
|
||||||
|
let joined = translators.joined(separator: "\n")
|
||||||
|
adw_about_dialog_set_translator_credits(dialog, joined)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let ptr = developers.cMutableArray, !developers.isEmpty {
|
/// Applies acknowledgement entries to the dialog.
|
||||||
adw_about_dialog_set_developers(dialog, ptr)
|
/// - Parameters:
|
||||||
}
|
/// - acknowledgements: The optional list of acknowledgement entries.
|
||||||
|
/// - dialog: The dialog instance.
|
||||||
|
@inline(__always)
|
||||||
|
private func set(
|
||||||
|
_ acknowledgements: [AcknowledgementEntry]?,
|
||||||
|
on dialog: OpaquePointer
|
||||||
|
) {
|
||||||
|
if let acknowledgements {
|
||||||
|
let grouped = Dictionary(grouping: acknowledgements) { $0.title }
|
||||||
|
|
||||||
if let ptr = designers.cMutableArray, !designers.isEmpty {
|
for (title, entries) in grouped {
|
||||||
adw_about_dialog_set_designers(dialog, ptr)
|
let names = entries.map { $0.name }
|
||||||
}
|
guard let people = names.cMutableArray else { continue }
|
||||||
|
|
||||||
if let ptr = artists.cMutableArray, !artists.isEmpty {
|
adw_about_dialog_add_acknowledgement_section(dialog, title, people)
|
||||||
adw_about_dialog_set_artists(dialog, ptr)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if !translators.isEmpty {
|
|
||||||
let joined = translators.joined(separator: "\n")
|
|
||||||
adw_about_dialog_set_translator_credits(dialog, joined)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,29 +319,20 @@ public struct AdwaitaAboutDialogConfig {
|
|||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - dialog: The underlying Adwaita dialog instance to update with the configuration.
|
/// - dialog: The underlying Adwaita dialog instance to update with the configuration.
|
||||||
func apply(to dialog: OpaquePointer) {
|
func apply(to dialog: OpaquePointer) {
|
||||||
let handlers: [(String?, (OpaquePointer, UnsafePointer<CChar>?) -> Void)] = [
|
set(appName, using: adw_about_dialog_set_application_name, on: dialog)
|
||||||
(appName, adw_about_dialog_set_application_name),
|
set(developer, using: adw_about_dialog_set_developer_name, on: dialog)
|
||||||
(developer, adw_about_dialog_set_developer_name),
|
set(version, using: adw_about_dialog_set_version, on: dialog)
|
||||||
(version, adw_about_dialog_set_version),
|
set(icon?.string, using: adw_about_dialog_set_application_icon, on: dialog)
|
||||||
(icon?.string, adw_about_dialog_set_application_icon),
|
set(website?.absoluteString, using: adw_about_dialog_set_website, on: dialog)
|
||||||
(website?.absoluteString, adw_about_dialog_set_website),
|
set(issues?.absoluteString, using: adw_about_dialog_set_issue_url, on: dialog)
|
||||||
(issues?.absoluteString, adw_about_dialog_set_issue_url),
|
set(support?.absoluteString, using: adw_about_dialog_set_support_url, on: dialog)
|
||||||
(support?.absoluteString, adw_about_dialog_set_support_url),
|
set(copyright, using: adw_about_dialog_set_copyright, on: dialog)
|
||||||
(copyright, adw_about_dialog_set_copyright),
|
set(license, using: adw_about_dialog_set_license, on: dialog)
|
||||||
(license, adw_about_dialog_set_license),
|
set(releaseNotes, using: adw_about_dialog_set_release_notes, on: dialog)
|
||||||
(releaseNotes, adw_about_dialog_set_release_notes),
|
set(comments, using: adw_about_dialog_set_comments, on: dialog)
|
||||||
(comments, adw_about_dialog_set_comments)
|
set(links, on: dialog)
|
||||||
]
|
set(credits, on: dialog)
|
||||||
|
set(acknowledgements, on: dialog)
|
||||||
for (value, action) in handlers {
|
|
||||||
value.map { action(dialog, $0) }
|
|
||||||
}
|
|
||||||
|
|
||||||
links?.forEach { (title: String, url: URL?) in adw_about_dialog_add_link(dialog, title, url?.absoluteString) }
|
|
||||||
|
|
||||||
if let credits {
|
|
||||||
applyCredits(credits, to: dialog)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user