Skip to content

Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit

feat(app): ask for a GitHub star after a few game launches (#2337)

Rutterherne3e63749bc89
diff --git a/app/Sources/BridgeplayApp/App.swift b/app/Sources/BridgeplayApp/App.swift
index 5663fe2..ebadbde 100644
--- a/app/Sources/BridgeplayApp/App.swift
+++ b/app/Sources/BridgeplayApp/App.swift
@@ -12,7 +12,7 @@ struct BridgeplayApp: App {
var body: some Scene {
Window("Bridgeplay", id: "main") {
- RootView(pane: $pane)
+ RootView(pane: $pane, showsSetup: showsSetup)
.environment(status)
.environment(prefixes)
.frame(minWidth: 800, minHeight: 600)
@@ -34,8 +34,12 @@ struct BridgeplayApp: App {
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
checkSteamPatch("foreground")
PlayReports.shared.scan()
+ Task { await StarPrompt.shared.check() }
+ }
+ .task {
+ PlayReports.shared.scan()
+ await StarPrompt.shared.check()
}
- .task { PlayReports.shared.scan() }
.task {
while !Task.isCancelled {
try? await Task.sleep(for: SteamPatchKeeper.pollInterval)
diff --git a/app/Sources/BridgeplayApp/Model/PlayReports.swift b/app/Sources/BridgeplayApp/Model/PlayReports.swift
index 5197f23..5fd5ba5 100644
--- a/app/Sources/BridgeplayApp/Model/PlayReports.swift
+++ b/app/Sources/BridgeplayApp/Model/PlayReports.swift
@@ -210,6 +210,11 @@ final class PlayReports {
private let sender: @Sendable () -> ReportSender
private let version: String
private var scanning = false
+ // Looking for sessions to ask about, before the slower retries of queued reports.
+ private(set) var finding = false
+
+ // Asking the player something, which the star prompt waits for.
+ var isAsking: Bool { nudge != nil || thanking || consentRequest != nil || bulk != nil }
private static let consentKey = "PlayReportsConsent"
private static let autoShareKey = "PlayReportsAutoShare"
@@ -427,6 +432,7 @@ final class PlayReports {
func scan() {
guard !scanning else { return }
scanning = true
+ finding = true
let sender = sender()
Task {
defer { scanning = false }
@@ -434,6 +440,7 @@ final class PlayReports {
verifiedHere = await Task.detached { await VerifiedGames.load(chip: chip) }.value
let found = await Task.detached { PlayReports.findSessions(prefixes: PrefixStore.all()) }.value
handle(found)
+ finding = false
await sender.retryPending()
}
}
diff --git a/app/Sources/BridgeplayApp/Model/StarPrompt.swift b/app/Sources/BridgeplayApp/Model/StarPrompt.swift
new file mode 100644
index 0000000..d0f7c8a
--- /dev/null
+++ b/app/Sources/BridgeplayApp/Model/StarPrompt.swift
@@ -0,0 +1,100 @@
+import AppKit
+import Foundation
+import Observation
+
+// Asks for a GitHub star after a few game launches through Bridgeplay, and once more five
+// launches after a Later. Everything stays in the app's defaults; nothing is sent.
+@MainActor
+@Observable
+final class StarPrompt {
+
+ static let shared = StarPrompt()
+ static let repository = URL(string: "https://github.com/raymondchins/Bridgeplay")!
+ static let firstAfter = 3
+ static let againAfter = 5
+
+ // Shared with the launch progress helper, a separate process of this app.
+ nonisolated static let launchesKey = "StarPromptLaunches"
+ private static let shownKey = "StarPromptShown"
+ private static let shownAtKey = "StarPromptShownAt"
+ private static let doneKey = "StarPromptDone"
+
+ // Waiting for the card, which shows it once nothing else is asking.
+ private(set) var pending = false
+ // On screen this run.
+ private(set) var shown = false
+ private var checking = false
+ // Later, or the window closed: nothing brings the card back this run.
+ private var laterThisRun = false
+ private let defaults: UserDefaults
+ private let opener: (URL) -> Void
+ private let gameRunning: () async -> Bool
+ private let reportScanRunning: () -> Bool
+
+ init(defaults: UserDefaults = .standard, opener: @escaping (URL) -> Void = { NSWorkspace.shared.open($0) },
+ gameRunning: @escaping () async -> Bool = { await RestartGuard.gameRunning() },
+ reportScanRunning: @escaping () -> Bool = { PlayReports.shared.finding }) {
+ self.defaults = defaults
+ self.opener = opener
+ self.gameRunning = gameRunning
+ self.reportScanRunning = reportScanRunning
+ }
+
+ // Every game launch starts the launch progress helper, which counts it here.
+ nonisolated static func countLaunch(in defaults: UserDefaults = .standard) {
+ defaults.set(defaults.integer(forKey: launchesKey) + 1, forKey: launchesKey)
+ }
+
+ var due: Bool {
+ guard !shown, !defaults.bool(forKey: Self.doneKey) else { return false }
+ let launches = defaults.integer(forKey: Self.launchesKey)
+ switch defaults.integer(forKey: Self.shownKey) {
+ case 0: return launches >= Self.firstAfter
+ case 1: return launches >= defaults.integer(forKey: Self.shownAtKey) + Self.againAfter
+ default: return false
+ }
+ }
+
+ // When the app comes forward. A game started since takes the card back, even one already
+ // on screen, which then stays away for this run without counting as another showing.
+ func check() async {
+ guard due || pending, !laterThisRun, !checking else { return }
+ checking = true
+ defer { checking = false }
+ // A card not seen yet waits for a game check made after the play report scan, which
+ // can take a while on the daily VerifiedGames download, so a game started meanwhile
+ // keeps it hidden and uncounted.
+ if !shown { pending = false }
+ while reportScanRunning() { try? await Task.sleep(for: .milliseconds(250)) }
+ let running = await gameRunning()
+ if running {
+ pending = false
+ } else if due, !laterThisRun {
+ pending = true
+ }
+ }
+
+ // A showing counts once the card is on screen, so closing the window or quitting is a
+ // Later as much as the button.
+ func appeared() {
+ guard pending, !shown else { return }
+ shown = true
+ defaults.set(defaults.integer(forKey: Self.shownKey) + 1, forKey: Self.shownKey)
+ defaults.set(defaults.integer(forKey: Self.launchesKey), forKey: Self.shownAtKey)
+ }
+
+ func star() {
+ opener(Self.repository)
+ never()
+ }
+
+ func later() {
+ pending = false
+ laterThisRun = true
+ }
+
+ func never() {
+ defaults.set(true, forKey: Self.doneKey)
+ pending = false
+ }
+}
diff --git a/app/Sources/BridgeplayApp/Resources/Localization/Localizable.xcstrings b/app/Sources/BridgeplayApp/Resources/Localization/Localizable.xcstrings
index 324931a..a1a67ea 100644
--- a/app/Sources/BridgeplayApp/Resources/Localization/Localizable.xcstrings
+++ b/app/Sources/BridgeplayApp/Resources/Localization/Localizable.xcstrings
@@ -1244,6 +1244,22 @@
}
}
},
+ "A star on GitHub helps other people find it." : {
+ "localizations" : {
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Una stella su GitHub aiuta altre persone a trovarlo."
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "在 GitHub 上点个星标,能帮更多人发现它。"
+ }
+ }
+ }
+ },
"About 500 MB. Also installs Steam if this Mac doesn't have it yet." : {
"localizations" : {
"it" : {
@@ -4418,6 +4434,22 @@
}
}
},
+ "Don't show again" : {
+ "localizations" : {
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Non mostrare più"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "不再显示"
+ }
+ }
+ }
+ },
"Done" : {
"localizations" : {
"it" : {
@@ -4722,6 +4754,22 @@
}
}
},
+ "Enjoying Bridgeplay?" : {
+ "localizations" : {
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Ti piace Bridgeplay?"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "喜欢 Bridgeplay 吗?"
+ }
+ }
+ }
+ },
"Enter at least one verb, like vcrun2022." : {
"localizations" : {
"it" : {
@@ -6146,6 +6194,22 @@
}
}
},
+ "Later" : {
+ "localizations" : {
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Più tardi"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "稍后"
+ }
+ }
+ }
+ },
"Launch Log" : {
"localizations" : {
"it" : {
@@ -9666,6 +9730,22 @@
}
}
},
+ "Star on GitHub" : {
+ "localizations" : {
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Stella su GitHub"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "在 GitHub 上加星"
+ }
+ }
+ }
+ },
"Started %@ in %@." : {
"localizations" : {
"it" : {
diff --git a/app/Sources/BridgeplayApp/Support/LaunchProgress.swift b/app/Sources/BridgeplayApp/Support/LaunchProgress.swift
index bd590e1..75682fc 100644
--- a/app/Sources/BridgeplayApp/Support/LaunchProgress.swift
+++ b/app/Sources/BridgeplayApp/Support/LaunchProgress.swift
@@ -17,6 +17,7 @@ enum LaunchProgressCommand {
guard arguments.count == 4, arguments[0].hasPrefix("/"), arguments[2].hasPrefix("/"),
let launcher = pid_t(arguments[3])
else { exit(1) }
+ StarPrompt.countLaunch()
let compatdata = arguments[0]
// The run script started this, so any other parent means the launch has ended, even
// one that ended before this got here.
diff --git a/app/Sources/BridgeplayApp/Views/RootView.swift b/app/Sources/BridgeplayApp/Views/RootView.swift
index 1b89f40..eac9f5a 100644
--- a/app/Sources/BridgeplayApp/Views/RootView.swift
+++ b/app/Sources/BridgeplayApp/Views/RootView.swift
@@ -34,6 +34,7 @@ enum Pane: String, CaseIterable, Identifiable, Hashable {
struct RootView: View {
@Binding var pane: Pane
+ var showsSetup = false
@Environment(SystemStatus.self) private var status
@Environment(PrefixesModel.self) private var prefixes
@Environment(\.accessibilityReduceMotion) private var reduceMotion
@@ -102,6 +103,19 @@ struct RootView: View {
case .guide: GuideView()
}
}
+ .overlay(alignment: .bottomTrailing) {
+ // Not over a play report question. StarPrompt.check waits for the scan that may raise one.
+ let reports = PlayReports.shared
+ let shows = StarPrompt.shared.pending && !reports.isAsking && !showsSetup && !MigrationLaunch.shared.pending
+ ZStack {
+ if shows {
+ StarPromptCard()
+ .padding(16)
+ .transition(reduceMotion ? .opacity : .move(edge: .bottom).combined(with: .opacity))
+ }
+ }
+ .animation(.spring(duration: 0.45, bounce: 0.18), value: shows)
+ }
.safeAreaInset(edge: .bottom, spacing: 0) {
if let nudge = PlayReports.shared.nudge {
PlayReportCard(nudge: nudge)
@@ -126,6 +140,8 @@ struct RootView: View {
.font(BP.sans(13))
.tint(BP.blue)
.preferredColorScheme(.dark)
+ // The window closed: a Later for the star card, which stays away this run.
+ .onDisappear { StarPrompt.shared.later() }
}
private var credits: some View {
diff --git a/app/Sources/BridgeplayApp/Views/StarPromptCard.swift b/app/Sources/BridgeplayApp/Views/StarPromptCard.swift
new file mode 100644
index 0000000..ee0cd3c
--- /dev/null
+++ b/app/Sources/BridgeplayApp/Views/StarPromptCard.swift
@@ -0,0 +1,80 @@
+import BridgeplayUI
+import SwiftUI
+
+// A small card floating over the bottom corner of the window, never a sheet or alert.
+struct StarPromptCard: View {
+ private var prompt: StarPrompt { .shared }
+
+ var body: some View {
+ VStack(alignment: .leading, spacing: 12) {
+ HStack(alignment: .top, spacing: 10) {
+ Image(systemName: "star.fill")
+ .font(.system(size: 15, weight: .semibold))
+ .foregroundStyle(BP.iridescentDiagonal)
+ .frame(width: 30, height: 30)
+ .background(Circle().fill(BP.raise2))
+ .overlay(Circle().strokeBorder(BP.line, lineWidth: 1))
+ .accessibilityHidden(true)
+ VStack(alignment: .leading, spacing: 3) {
+ Text("Enjoying Bridgeplay?")
+ .font(BP.sans(13, .semibold))
+ .foregroundStyle(BP.fg)
+ Text("A star on GitHub helps other people find it.")
+ .font(BP.sans(12))
+ .foregroundStyle(BP.fg2)
+ .fixedSize(horizontal: false, vertical: true)
+ }
+ Spacer(minLength: 0)
+ Button(String(localized: "Close"), systemImage: "xmark", action: prompt.later)
+ .labelStyle(.iconOnly)
+ .buttonStyle(.plain)
+ .font(.system(size: 11, weight: .semibold))
+ .foregroundStyle(BP.fg3)
+ .help("Later")
+ }
+ // Longer translations put Don't Show Again on a line of its own.
+ ViewThatFits(in: .horizontal) {
+ HStack(spacing: 8) {
+ answers
+ Spacer(minLength: 8)
+ never
+ }
+ VStack(alignment: .leading, spacing: 8) {
+ HStack(spacing: 8) { answers }
+ never
+ }
+ }
+ }
+ .padding(14)
+ .frame(width: 360)
+ .background(BP.raise, in: RoundedRectangle(cornerRadius: BP.radius, style: .continuous))
+ .overlay(RoundedRectangle(cornerRadius: BP.radius, style: .continuous).strokeBorder(BP.lineStrong, lineWidth: 1))
+ .shadow(color: .black.opacity(0.55), radius: 18, y: 8)
+ .accessibilityElement(children: .contain)
+ .accessibilityLabel("Enjoying Bridgeplay?")
+ // Behind a sheet or alert it is not seen yet, so it counts once that closes.
+ .onAppear { if !NSApp.windows.contains(where: { $0.attachedSheet != nil }) { prompt.appeared() } }
+ .onReceive(NotificationCenter.default.publisher(for: NSWindow.didEndSheetNotification)) { _ in prompt.appeared() }
+ }
+
+ @ViewBuilder private var answers: some View {
+ Button("Star on GitHub", action: prompt.star)
+ .buttonStyle(.pill)
+ .fixedSize()
+ Button("Later", action: prompt.later)
+ .buttonStyle(.pillSecondary)
+ .fixedSize()
+ }
+
+ private var never: some View {
+ Button(action: prompt.never) {
+ Text("Don't show again")
+ .font(BP.sans(12))
+ .foregroundStyle(BP.fg3)
+ .padding(.vertical, 6)
+ .contentShape(Rectangle())
+ }
+ .buttonStyle(.plain)
+ .fixedSize()
+ }
+}
diff --git a/app/Tests/BridgeplayAppTests/StarPromptTests.swift b/app/Tests/BridgeplayAppTests/StarPromptTests.swift
new file mode 100644
index 0000000..bf9e8af
--- /dev/null
+++ b/app/Tests/BridgeplayAppTests/StarPromptTests.swift
@@ -0,0 +1,161 @@
+import Foundation
+import Testing
+
+@testable import BridgeplayApp
+
+@MainActor
+@Suite("GitHub star prompt")
+struct StarPromptTests {
+
+ private func defaults(launches: Int = 0) -> UserDefaults {
+ let defaults = scratchDefaults()
+ for _ in 0..<launches { StarPrompt.countLaunch(in: defaults) }
+ return defaults
+ }
+
+ private static func prompt(_ defaults: UserDefaults, opener: @escaping (URL) -> Void = { _ in },
+ gameRunning: Bool = false) -> StarPrompt {
+ StarPrompt(defaults: defaults, opener: opener, gameRunning: { gameRunning }, reportScanRunning: { false })
+ }
+
+ // The card goes on screen.
+ private static func show(_ prompt: StarPrompt) async {
+ await prompt.check()
+ prompt.appeared()
+ }
+
+ @Test("Due from the third launch, once per run")
+ func firstShowing() async {
+ let defaults = defaults(launches: 2)
+ #expect(!Self.prompt(defaults).due)
+ StarPrompt.countLaunch(in: defaults)
+ let prompt = Self.prompt(defaults)
+ #expect(prompt.due)
+ await Self.show(prompt)
+ #expect(prompt.pending)
+ prompt.later() // also what closing the window does
+ StarPrompt.countLaunch(in: defaults)
+ #expect(!prompt.due)
+ await prompt.check()
+ #expect(!prompt.pending)
+ #expect(defaults.integer(forKey: "StarPromptShown") == 1 && defaults.integer(forKey: "StarPromptShownAt") == 3)
+ }
+
+ @Test("Later, or closing it any other way, shows it once more five launches on, then never")
+ func later() async {
+ let defaults = defaults(launches: 3)
+ await Self.show(Self.prompt(defaults)) // closed without an answer
+ for _ in 0..<4 { StarPrompt.countLaunch(in: defaults) }
+ #expect(!Self.prompt(defaults).due)
+ StarPrompt.countLaunch(in: defaults)
+ let second = Self.prompt(defaults)
+ #expect(second.due)
+ await Self.show(second)
+ second.later()
+ for _ in 0..<20 { StarPrompt.countLaunch(in: defaults) }
+ #expect(!Self.prompt(defaults).due)
+ }
+
+ @Test("Star on GitHub opens the repository and ends it")
+ func star() async {
+ let defaults = defaults(launches: 3)
+ var opened: [URL] = []
+ let prompt = Self.prompt(defaults, opener: { opened.append($0) })
+ await Self.show(prompt)
+ prompt.star()
+ #expect(opened == [URL(string: "https://github.com/raymondchins/Bridgeplay")!])
+ #expect(!prompt.pending)
+ for _ in 0..<10 { StarPrompt.countLaunch(in: defaults) }
+ #expect(!Self.prompt(defaults).due)
+ }
+
+ @Test("Don't show again ends it, even before the second showing")
+ func never() async {
+ let defaults = defaults(launches: 3)
+ let prompt = Self.prompt(defaults)
+ await Self.show(prompt)
+ prompt.never()
+ #expect(!prompt.pending)
+ for _ in 0..<10 { StarPrompt.countLaunch(in: defaults) }
+ #expect(!Self.prompt(defaults).due)
+ }
+
+ @Test("Not while a game is running, and a game started while it waits takes it back")
+ func gameRunning() async {
+ let defaults = defaults(launches: 3)
+ var running = true
+ let prompt = StarPrompt(defaults: defaults, opener: { _ in }, gameRunning: { running }, reportScanRunning: { false })
+ await prompt.check()
+ #expect(!prompt.pending)
+ running = false
+ await prompt.check()
+ #expect(prompt.pending)
+ running = true
+ await prompt.check()
+ #expect(!prompt.pending)
+ prompt.appeared()
+ #expect(defaults.integer(forKey: "StarPromptShown") == 0)
+ }
+
+ @Test("A card on screen is taken back when a game starts, without another showing")
+ func shownThenGame() async {
+ let defaults = defaults(launches: 3)
+ var running = false
+ let prompt = StarPrompt(defaults: defaults, opener: { _ in }, gameRunning: { running }, reportScanRunning: { false })
+ await Self.show(prompt)
+ running = true
+ await prompt.check()
+ #expect(!prompt.pending)
+ running = false
+ await prompt.check()
+ #expect(!prompt.pending)
+ #expect(defaults.integer(forKey: "StarPromptShown") == 1)
+ }
+
+ @Test("Closing the window during a check, or before the card was seen, keeps it away this run")
+ func closedDuringCheck() async {
+ let defaults = defaults(launches: 3)
+ let prompt = StarPrompt(defaults: defaults, opener: { _ in }, gameRunning: {
+ try? await Task.sleep(for: .milliseconds(100))
+ return false
+ }, reportScanRunning: { false })
+ let checking = Task { await prompt.check() }
+ try? await Task.sleep(for: .milliseconds(20))
+ prompt.later()
+ await checking.value
+ #expect(!prompt.pending)
+ await prompt.check()
+ #expect(!prompt.pending)
+ #expect(defaults.integer(forKey: "StarPromptShown") == 0)
+ }
+
+ @Test("A game started during the play report scan keeps the card hidden and uncounted")
+ func gameDuringScan() async {
+ let defaults = defaults(launches: 3)
+ var running = false, scanning = false
+ let prompt = StarPrompt(defaults: defaults, opener: { _ in }, gameRunning: { running },
+ reportScanRunning: { scanning })
+ await prompt.check()
+ #expect(prompt.pending)
+ // The app comes forward again and a new scan starts: the unseen card waits for it.
+ scanning = true
+ let checking = Task { await prompt.check() }
+ try? await Task.sleep(for: .milliseconds(50))
+ #expect(!prompt.pending)
+ running = true
+ scanning = false
+ await checking.value
+ #expect(!prompt.pending)
+ prompt.appeared()
+ #expect(defaults.integer(forKey: "StarPromptShown") == 0)
+ }
+
+ @Test("Due but never on screen, nothing is used up")
+ func notShown() async {
+ let defaults = defaults(launches: 3)
+ let prompt = Self.prompt(defaults)
+ await prompt.check()
+ prompt.later()
+ #expect(Self.prompt(defaults).due)
+ }
+}