Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions macgit/App/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ final class AppState: ObservableObject {
private static let preferredSearchFileApplicationKey = "preferredSearchFileApplication"

private let userDefaults: UserDefaults
private let diffAppearanceSettingsStore: DiffAppearanceSettingsStore
private let advancedSettingsStore: AdvancedSettingsStore
private var isApplyingSnapshot = false
@Published private var currentSettingsSnapshot: AppSettingsSnapshot =
AppSettingsSnapshot(showToolbarButtonText: true, showSubmodules: false, showSubtrees: false)
Expand Down Expand Up @@ -286,8 +288,20 @@ final class AppState: ObservableObject {
.eraseToAnyPublisher()
}

init(userDefaults: UserDefaults = .standard) {
init(
userDefaults: UserDefaults = .standard,
diffAppearanceSettingsStore: DiffAppearanceSettingsStore? = nil,
advancedSettingsStore: AdvancedSettingsStore? = nil
) {
self.userDefaults = userDefaults
self.diffAppearanceSettingsStore = diffAppearanceSettingsStore
?? (userDefaults === UserDefaults.standard
? .shared
: DiffAppearanceSettingsStore(userDefaults: userDefaults))
self.advancedSettingsStore = advancedSettingsStore
?? (userDefaults === UserDefaults.standard
? .shared
: AdvancedSettingsStore(userDefaults: userDefaults))
let appearance = userDefaults.string(forKey: Self.appearanceKey)
.flatMap(AppAppearance.init(rawValue:)) ?? .system
let textSize = userDefaults.string(forKey: Self.textSizeKey)
Expand Down Expand Up @@ -325,6 +339,7 @@ final class AppState: ObservableObject {
let preferredSearchFileApplicationBundleIdentifier = userDefaults.string(
forKey: Self.preferredSearchFileApplicationKey
)
let diffAppearance = self.diffAppearanceSettingsStore.snapshotValues
let loadedSnapshot = AppSettingsSnapshot(
appearance: appearance,
showToolbarButtonText: showToolbarButtonText,
Expand All @@ -349,7 +364,14 @@ final class AppState: ObservableObject {
historyBranchFilter: historyBranchFilter,
historyIncludeRemotes: historyIncludeRemotes,
autoFetchEnabled: autoFetchEnabled,
refreshOnAppActive: refreshOnAppActive
refreshOnAppActive: refreshOnAppActive,
diffFontName: diffAppearance.fontName,
diffFontSize: diffAppearance.fontSize,
diffAddedBackgroundColor: diffAppearance.addedBackgroundColor,
diffAddedTextColor: diffAppearance.addedTextColor,
diffRemovedBackgroundColor: diffAppearance.removedBackgroundColor,
diffRemovedTextColor: diffAppearance.removedTextColor,
diffSyntaxHighlighting: self.advancedSettingsStore.diffSyntaxHighlighting
).normalizedRepositoryToolbarShortcuts()

self.appearance = appearance
Expand Down Expand Up @@ -385,7 +407,8 @@ final class AppState: ObservableObject {
}

var snapshot: AppSettingsSnapshot {
AppSettingsSnapshot(
let diffAppearance = diffAppearanceSettingsStore.snapshotValues
return AppSettingsSnapshot(
appearance: appearance,
showToolbarButtonText: showToolbarButtonText,
showGitFlow: showGitFlow,
Expand All @@ -409,10 +432,27 @@ final class AppState: ObservableObject {
historyBranchFilter: historyBranchFilter,
historyIncludeRemotes: historyIncludeRemotes,
autoFetchEnabled: autoFetchEnabled,
refreshOnAppActive: refreshOnAppActive
refreshOnAppActive: refreshOnAppActive,
diffFontName: diffAppearance.fontName,
diffFontSize: diffAppearance.fontSize,
diffAddedBackgroundColor: diffAppearance.addedBackgroundColor,
diffAddedTextColor: diffAppearance.addedTextColor,
diffRemovedBackgroundColor: diffAppearance.removedBackgroundColor,
diffRemovedTextColor: diffAppearance.removedTextColor,
diffSyntaxHighlighting: advancedSettingsStore.diffSyntaxHighlighting
)
}

func applyLocalDiffSettings(
appearance: DiffAppearanceSettingsDraft,
syntaxHighlighting: Bool
) {
diffAppearanceSettingsStore.apply(appearance)
advancedSettingsStore.diffSyntaxHighlighting = syntaxHighlighting
guard !isApplyingSnapshot else { return }
currentSettingsSnapshot = snapshot
}

func apply(_ snapshot: AppSettingsSnapshot) {
let snapshot = snapshot.normalizedRepositoryToolbarShortcuts()
isApplyingSnapshot = true
Expand Down Expand Up @@ -441,6 +481,8 @@ final class AppState: ObservableObject {
historyIncludeRemotes = snapshot.historyIncludeRemotes
autoFetchEnabled = snapshot.autoFetchEnabled
refreshOnAppActive = snapshot.refreshOnAppActive
diffAppearanceSettingsStore.apply(snapshot)
advancedSettingsStore.diffSyntaxHighlighting = snapshot.diffSyntaxHighlighting
currentSettingsSnapshot = snapshot
}

Expand Down
40 changes: 39 additions & 1 deletion macgit/Models/AppSettingsSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ struct AppSettingsSnapshot: Codable, Equatable, Sendable {
var historyIncludeRemotes: Bool
var autoFetchEnabled: Bool
var refreshOnAppActive: Bool
var diffFontName: String
var diffFontSize: Double
var diffAddedBackgroundColor: String
var diffAddedTextColor: String
var diffRemovedBackgroundColor: String
var diffRemovedTextColor: String
var diffSyntaxHighlighting: Bool

init(
appearance: AppAppearance = .system,
Expand All @@ -69,7 +76,14 @@ struct AppSettingsSnapshot: Codable, Equatable, Sendable {
historyBranchFilter: HistoryBranchFilter = .all,
historyIncludeRemotes: Bool = false,
autoFetchEnabled: Bool = false,
refreshOnAppActive: Bool = true
refreshOnAppActive: Bool = true,
diffFontName: String = "Menlo-Regular",
diffFontSize: Double = 12,
diffAddedBackgroundColor: String = "#E6FAF0FF",
diffAddedTextColor: String = "#006E4FFF",
diffRemovedBackgroundColor: String = "#FFEDE8FF",
diffRemovedTextColor: String = "#C72E0DFF",
diffSyntaxHighlighting: Bool = false
) {
schemaVersion = 1
self.appearance = appearance
Expand All @@ -96,6 +110,13 @@ struct AppSettingsSnapshot: Codable, Equatable, Sendable {
self.historyIncludeRemotes = historyIncludeRemotes
self.autoFetchEnabled = autoFetchEnabled
self.refreshOnAppActive = refreshOnAppActive
self.diffFontName = diffFontName
self.diffFontSize = diffFontSize
self.diffAddedBackgroundColor = diffAddedBackgroundColor
self.diffAddedTextColor = diffAddedTextColor
self.diffRemovedBackgroundColor = diffRemovedBackgroundColor
self.diffRemovedTextColor = diffRemovedTextColor
self.diffSyntaxHighlighting = diffSyntaxHighlighting
}

init(from decoder: Decoder) throws {
Expand Down Expand Up @@ -125,5 +146,22 @@ struct AppSettingsSnapshot: Codable, Equatable, Sendable {
historyIncludeRemotes = try container.decode(Bool.self, forKey: .historyIncludeRemotes)
autoFetchEnabled = try container.decode(Bool.self, forKey: .autoFetchEnabled)
refreshOnAppActive = try container.decode(Bool.self, forKey: .refreshOnAppActive)
diffFontName = try container.decodeIfPresent(String.self, forKey: .diffFontName) ?? "Menlo-Regular"
diffFontSize = try container.decodeIfPresent(Double.self, forKey: .diffFontSize) ?? 12
diffAddedBackgroundColor = try container.decodeIfPresent(
String.self, forKey: .diffAddedBackgroundColor
) ?? "#E6FAF0FF"
diffAddedTextColor = try container.decodeIfPresent(
String.self, forKey: .diffAddedTextColor
) ?? "#006E4FFF"
diffRemovedBackgroundColor = try container.decodeIfPresent(
String.self, forKey: .diffRemovedBackgroundColor
) ?? "#FFEDE8FF"
diffRemovedTextColor = try container.decodeIfPresent(
String.self, forKey: .diffRemovedTextColor
) ?? "#C72E0DFF"
diffSyntaxHighlighting = try container.decodeIfPresent(
Bool.self, forKey: .diffSyntaxHighlighting
) ?? false
}
}
17 changes: 17 additions & 0 deletions macgit/Models/DiffAppearanceSettingsDraft.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

import AppKit

struct DiffAppearanceSettingsDraft {
var fontName: String
var fontSize: Double
var addedBackgroundColor: NSColor
var addedTextColor: NSColor
var removedBackgroundColor: NSColor
var removedTextColor: NSColor

var font: NSFont {
NSFont(name: fontName, size: fontSize)
?? NSFont.monospacedSystemFont(ofSize: fontSize, weight: .regular)
}
}
27 changes: 27 additions & 0 deletions macgit/Models/RemoteBranchDeleteResolver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

import Foundation

enum RemoteBranchDeleteResolver {
static func target(
localBranch: String,
upstream: String?,
branchesByRemote: [String: [String]]
) -> RemoteBranchDeleteTarget? {
if let upstream,
let tracked = RemoteBranchDeleteTarget(fullPath: upstream),
branchesByRemote[tracked.remote]?.contains(tracked.branch) == true {
return tracked
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

let matches = branchesByRemote
.filter { $0.value.contains(localBranch) }
.map { RemoteBranchDeleteTarget(remote: $0.key, branch: localBranch) }
.sorted { $0.fullPath < $1.fullPath }

if let origin = matches.first(where: { $0.remote == "origin" }) {
return origin
}
return matches.count == 1 ? matches[0] : nil
}
}
Loading
Loading