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
30 changes: 30 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI

on:
push:
branches: [main]
pull_request:

permissions:
contents: read

concurrency:
group: ci-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
verify:
name: Test and build without Xcode project
runs-on: macos-14
timeout-minutes: 20
steps:
- name: Check out repository
uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5

- name: Run core tests
shell: /bin/zsh {0}
run: ./scripts/test.sh

- name: Build Universal 2 app
shell: /bin/zsh {0}
run: ./scripts/build.sh
6 changes: 6 additions & 0 deletions .github/workflows/publish-appcast.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ jobs:
dmg_file=${dmg_files[1]}

curl --fail --location --show-error \
--retry 3 \
--retry-all-errors \
--retry-delay 2 \
--output "$work_root/$sparkle_archive" \
"https://github.com/sparkle-project/Sparkle/releases/download/$sparkle_version/$sparkle_archive"
actual_sha256=$(shasum -a 256 "$work_root/$sparkle_archive" | awk '{print $1}')
Expand Down Expand Up @@ -85,6 +88,9 @@ jobs:

xmllint --noout "$appcast_file"
grep -q 'sparkle:edSignature=' "$appcast_file"
grep -Fq \
"https://github.com/$REPOSITORY/releases/download/$TAG_NAME/${dmg_file:t}" \
"$appcast_file"
gh release upload "$TAG_NAME" "$appcast_file" \
--repo "$REPOSITORY" \
--clobber
13 changes: 8 additions & 5 deletions InputStatus.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
A10000000000000000000007 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A20000000000000000000007 /* Assets.xcassets */; };
A10000000000000000000008 /* InputStatusWidgetExtension.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = A20000000000000000000008 /* InputStatusWidgetExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
A10000000000000000000009 /* Sparkle in Frameworks */ = {isa = PBXBuildFile; productRef = AB0000000000000000000001 /* Sparkle */; };
A10000000000000000000010 /* AppIcon.icns in Resources */ = {isa = PBXBuildFile; fileRef = A20000000000000000000015 /* AppIcon.icns */; };
A10000000000000000000011 /* AppIcon.icns in Resources */ = {isa = PBXBuildFile; fileRef = A20000000000000000000015 /* AppIcon.icns */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -55,6 +57,7 @@
A20000000000000000000012 /* InputStatus.entitlements */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.entitlements; path = InputStatus.entitlements; sourceTree = "<group>"; };
A20000000000000000000013 /* InputStatusWidget.entitlements */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.entitlements; path = InputStatusWidget.entitlements; sourceTree = "<group>"; };
A20000000000000000000014 /* project.yml */ = {isa = PBXFileReference; lastKnownFileType = text; path = project.yml; sourceTree = "<group>"; };
A20000000000000000000015 /* AppIcon.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = AppIcon.icns; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -76,7 +79,7 @@
A80000000000000000000002 /* App */ = {isa = PBXGroup; children = (A20000000000000000000001 /* AppModel.swift */, A20000000000000000000002 /* InputStatusApp.swift */); path = App; sourceTree = "<group>"; };
A80000000000000000000003 /* Shared */ = {isa = PBXGroup; children = (A20000000000000000000003 /* StatusAPI.swift */, A20000000000000000000004 /* StatusModels.swift */); path = Shared; sourceTree = "<group>"; };
A80000000000000000000004 /* Widget */ = {isa = PBXGroup; children = (A20000000000000000000006 /* InputStatusWidget.swift */); path = Widget; sourceTree = "<group>"; };
A80000000000000000000005 /* Resources */ = {isa = PBXGroup; children = (A20000000000000000000007 /* Assets.xcassets */, A20000000000000000000010 /* InputStatus-Info.plist */, A20000000000000000000011 /* InputStatusWidget-Info.plist */, A20000000000000000000012 /* InputStatus.entitlements */, A20000000000000000000013 /* InputStatusWidget.entitlements */); path = Resources; sourceTree = "<group>"; };
A80000000000000000000005 /* Resources */ = {isa = PBXGroup; children = (A20000000000000000000007 /* Assets.xcassets */, A20000000000000000000015 /* AppIcon.icns */, A20000000000000000000010 /* InputStatus-Info.plist */, A20000000000000000000011 /* InputStatusWidget-Info.plist */, A20000000000000000000012 /* InputStatus.entitlements */, A20000000000000000000013 /* InputStatusWidget.entitlements */); path = Resources; sourceTree = "<group>"; };
A80000000000000000000006 /* Products */ = {isa = PBXGroup; children = (A20000000000000000000009 /* InputStatus.app */, A20000000000000000000008 /* InputStatusWidgetExtension.appex */); name = Products; sourceTree = "<group>"; };
/* End PBXGroup section */

Expand Down Expand Up @@ -125,8 +128,8 @@
/* End PBXProject section */

/* Begin PBXResourcesBuildPhase section */
A60000000000000000000003 /* Resources */ = {isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = (A10000000000000000000007 /* Assets.xcassets in Resources */); runOnlyForDeploymentPostprocessing = 0; };
A60000000000000000000005 /* Resources */ = {isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = (); runOnlyForDeploymentPostprocessing = 0; };
A60000000000000000000003 /* Resources */ = {isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = (A10000000000000000000007 /* Assets.xcassets in Resources */, A10000000000000000000010 /* AppIcon.icns in Resources */); runOnlyForDeploymentPostprocessing = 0; };
A60000000000000000000005 /* Resources */ = {isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = (A10000000000000000000011 /* AppIcon.icns in Resources */); runOnlyForDeploymentPostprocessing = 0; };
/* End PBXResourcesBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
Expand All @@ -141,8 +144,8 @@
/* Begin XCBuildConfiguration section */
AA0000000000000000000001 /* Debug */ = {isa = XCBuildConfiguration; buildSettings = {ALWAYS_SEARCH_USER_PATHS = NO; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = ""; MACOSX_DEPLOYMENT_TARGET = 14.0; SDKROOT = macosx; SWIFT_VERSION = 5.0; }; name = Debug; };
AA0000000000000000000002 /* Release */ = {isa = XCBuildConfiguration; buildSettings = {ALWAYS_SEARCH_USER_PATHS = NO; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = ""; MACOSX_DEPLOYMENT_TARGET = 14.0; SDKROOT = macosx; SWIFT_VERSION = 5.0; }; name = Release; };
AA0000000000000000000003 /* Debug */ = {isa = XCBuildConfiguration; buildSettings = {ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = InputStatus/Resources/InputStatus.entitlements; INFOPLIST_FILE = InputStatus/Resources/InputStatus-Info.plist; LD_RUNPATH_SEARCH_PATHS = ("$(inherited)", "@executable_path/../Frameworks"); PRODUCT_BUNDLE_IDENTIFIER = com.inputstatus.desktop; PRODUCT_NAME = "$(TARGET_NAME)"; }; name = Debug; };
AA0000000000000000000004 /* Release */ = {isa = XCBuildConfiguration; buildSettings = {ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = InputStatus/Resources/InputStatus.entitlements; INFOPLIST_FILE = InputStatus/Resources/InputStatus-Info.plist; LD_RUNPATH_SEARCH_PATHS = ("$(inherited)", "@executable_path/../Frameworks"); PRODUCT_BUNDLE_IDENTIFIER = com.inputstatus.desktop; PRODUCT_NAME = "$(TARGET_NAME)"; }; name = Release; };
AA0000000000000000000003 /* Debug */ = {isa = XCBuildConfiguration; buildSettings = {CODE_SIGN_ENTITLEMENTS = InputStatus/Resources/InputStatus.entitlements; INFOPLIST_FILE = InputStatus/Resources/InputStatus-Info.plist; LD_RUNPATH_SEARCH_PATHS = ("$(inherited)", "@executable_path/../Frameworks"); PRODUCT_BUNDLE_IDENTIFIER = com.inputstatus.desktop; PRODUCT_NAME = "$(TARGET_NAME)"; }; name = Debug; };
AA0000000000000000000004 /* Release */ = {isa = XCBuildConfiguration; buildSettings = {CODE_SIGN_ENTITLEMENTS = InputStatus/Resources/InputStatus.entitlements; INFOPLIST_FILE = InputStatus/Resources/InputStatus-Info.plist; LD_RUNPATH_SEARCH_PATHS = ("$(inherited)", "@executable_path/../Frameworks"); PRODUCT_BUNDLE_IDENTIFIER = com.inputstatus.desktop; PRODUCT_NAME = "$(TARGET_NAME)"; }; name = Release; };
AA0000000000000000000005 /* Debug */ = {isa = XCBuildConfiguration; buildSettings = {APPLICATION_EXTENSION_API_ONLY = YES; CODE_SIGN_ENTITLEMENTS = InputStatus/Resources/InputStatusWidget.entitlements; INFOPLIST_FILE = InputStatus/Resources/InputStatusWidget-Info.plist; LD_RUNPATH_SEARCH_PATHS = ("$(inherited)", "@executable_path/../Frameworks"); PRODUCT_BUNDLE_IDENTIFIER = com.inputstatus.desktop.widget; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; }; name = Debug; };
AA0000000000000000000006 /* Release */ = {isa = XCBuildConfiguration; buildSettings = {APPLICATION_EXTENSION_API_ONLY = YES; CODE_SIGN_ENTITLEMENTS = InputStatus/Resources/InputStatusWidget.entitlements; INFOPLIST_FILE = InputStatus/Resources/InputStatusWidget-Info.plist; LD_RUNPATH_SEARCH_PATHS = ("$(inherited)", "@executable_path/../Frameworks"); PRODUCT_BUNDLE_IDENTIFIER = com.inputstatus.desktop.widget; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; }; name = Release; };
/* End XCBuildConfiguration section */
Expand Down
2 changes: 1 addition & 1 deletion InputStatus/App/AppModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class AppModel: ObservableObject {
object: nil,
queue: .main
) { [weak self] _ in
Task { @MainActor in self?.refreshNow() }
Task { @MainActor [weak self] in self?.refreshNow() }
}
startRefreshing()
}
Expand Down
76 changes: 69 additions & 7 deletions InputStatus/App/InputStatusApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import ServiceManagement
import SwiftUI

@MainActor
final class InputStatusAppDelegate: NSObject, NSApplicationDelegate, @MainActor SPUStandardUserDriverDelegate {
final class InputStatusAppDelegate: NSObject, NSApplicationDelegate, SPUStandardUserDriverDelegate {
let model = AppModel()
lazy var updaterController = SPUStandardUpdaterController(
startingUpdater: true,
updaterDelegate: nil,
userDriverDelegate: self
)
private var desktopWidgetController: DesktopWidgetWindowController?
private var updaterIsPresenting = false

var isDesktopWidgetVisible: Bool {
desktopWidgetController?.isVisible == true
Expand All @@ -36,20 +37,32 @@ final class InputStatusAppDelegate: NSObject, NSApplicationDelegate, @MainActor
desktopWidgetController?.toggle()
}

var supportsGentleScheduledUpdateReminders: Bool {
true
}

func standardUserDriverWillHandleShowingUpdate(
nonisolated func standardUserDriverWillHandleShowingUpdate(
_ handleShowingUpdate: Bool,
forUpdate update: SUAppcastItem,
state: SPUUserUpdateState
) {
guard handleShowingUpdate else { return }
Task { @MainActor [weak self] in
self?.beginPresentingUpdate()
}
}

nonisolated func standardUserDriverWillFinishUpdateSession() {
Task { @MainActor [weak self] in
self?.finishPresentingUpdate()
}
}

private func beginPresentingUpdate() {
updaterIsPresenting = true
NSApp.setActivationPolicy(.regular)
NSApp.activate()
}

func standardUserDriverWillFinishUpdateSession() {
private func finishPresentingUpdate() {
guard updaterIsPresenting else { return }
updaterIsPresenting = false
NSApp.setActivationPolicy(.accessory)
}

Expand Down Expand Up @@ -241,13 +254,29 @@ private final class DesktopWidgetWindowController: NSObject {
private let settings = DesktopWidgetSettings()
private var window: NSPanel?
private var hostingController: NSHostingController<DesktopWidgetView>?
private var screenParametersObserver: NSObjectProtocol?

init(model: AppModel) {
self.model = model
super.init()
settings.onLockChanged = { [weak self] isLocked in
self?.applyLockState(isLocked)
}
screenParametersObserver = NotificationCenter.default.addObserver(
forName: NSApplication.didChangeScreenParametersNotification,
object: nil,
queue: .main
) { [weak self] _ in
Task { @MainActor [weak self] in
self?.ensureWindowIsVisible()
}
}
}

deinit {
if let screenParametersObserver {
NotificationCenter.default.removeObserver(screenParametersObserver)
}
}

var isVisible: Bool {
Expand All @@ -258,6 +287,7 @@ private final class DesktopWidgetWindowController: NSObject {
if window == nil {
makeWindow()
}
ensureWindowIsVisible()
window?.orderFrontRegardless()
}

Expand Down Expand Up @@ -287,6 +317,7 @@ private final class DesktopWidgetWindowController: NSObject {
panel.backgroundColor = .clear
panel.hasShadow = true
panel.hidesOnDeactivate = false
panel.isMovable = !settings.isLocked
panel.isMovableByWindowBackground = !settings.isLocked
let desktopWidgetLevel = Int(CGWindowLevelForKey(.desktopIconWindow)) + 1
panel.level = NSWindow.Level(rawValue: desktopWidgetLevel)
Expand All @@ -306,11 +337,34 @@ private final class DesktopWidgetWindowController: NSObject {
}

window = panel
ensureWindowIsVisible()
}

private func applyLockState(_ isLocked: Bool) {
window?.isMovable = !isLocked
window?.isMovableByWindowBackground = !isLocked
}

private func ensureWindowIsVisible() {
guard let window else { return }
let screens = NSScreen.screens
guard let screen = screens.first(where: { $0.visibleFrame.intersects(window.frame) })
?? NSScreen.main
?? screens.first else {
return
}

let visibleFrame = screen.visibleFrame
let maximumX = max(visibleFrame.minX, visibleFrame.maxX - window.frame.width)
let maximumY = max(visibleFrame.minY, visibleFrame.maxY - window.frame.height)
let origin = NSPoint(
x: min(max(window.frame.minX, visibleFrame.minX), maximumX),
y: min(max(window.frame.minY, visibleFrame.minY), maximumY)
)
if origin != window.frame.origin {
window.setFrameOrigin(origin)
}
}
}

@MainActor
Expand Down Expand Up @@ -497,6 +551,7 @@ private struct DesktopWidgetView: View {
private struct DesktopGlassModifier: ViewModifier {
@ViewBuilder
func body(content: Content) -> some View {
#if compiler(>=6.2)
if #available(macOS 26.0, *) {
content
.glassEffect(.regular, in: .rect(cornerRadius: 18))
Expand All @@ -507,5 +562,12 @@ private struct DesktopGlassModifier: ViewModifier {
in: RoundedRectangle(cornerRadius: 18, style: .continuous)
)
}
#else
content
.background(
.ultraThinMaterial,
in: RoundedRectangle(cornerRadius: 18, style: .continuous)
)
#endif
}
}
6 changes: 2 additions & 4 deletions InputStatus/Resources/InputStatus-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
<array>
<string>AppIcon</string>
</array>
<key>CFBundleIconName</key>
<string>AppIcon</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>InputStatus</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.5.0</string>
<string>1.5.1</string>
<key>CFBundleSupportedPlatforms</key>
<array>
<string>MacOSX</string>
Expand All @@ -42,7 +40,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>6</string>
<string>7</string>
<key>LSUIElement</key>
<true/>
<key>LSMinimumSystemVersion</key>
Expand Down
6 changes: 2 additions & 4 deletions InputStatus/Resources/InputStatusWidget-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@
<array>
<string>AppIcon</string>
</array>
<key>CFBundleIconName</key>
<string>AppIcon</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>InputStatusWidgetExtension</string>
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>1.5.0</string>
<string>1.5.1</string>
<key>CFBundleSupportedPlatforms</key>
<array>
<string>MacOSX</string>
</array>
<key>CFBundleVersion</key>
<string>6</string>
<string>7</string>
<key>CHSDisableImplicitWidgetDiscovery</key>
<false/>
<key>NSExtension</key>
Expand Down
27 changes: 23 additions & 4 deletions InputStatus/Shared/StatusAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import Foundation
enum StatusAPIError: LocalizedError, Sendable {
case badResponse(Int)
case malformedResponse
case responseTooLarge

var errorDescription: String? {
switch self {
case .badResponse(let code):
return "服务器返回 HTTP \(code)"
case .malformedResponse:
return "状态数据格式无法识别"
case .responseTooLarge:
return "状态数据超过预期大小"
}
}
}
Expand All @@ -30,9 +33,13 @@ struct StatusAPI: Sendable {
guard (200..<300).contains(httpResponse.statusCode) else {
throw StatusAPIError.badResponse(httpResponse.statusCode)
}
guard data.count <= InputStatusConstants.maximumResponseBytes else {
throw StatusAPIError.responseTooLarge
}

do {
return try JSONDecoder().decode(StatusSnapshot.self, from: data)
let snapshot = try JSONDecoder().decode(StatusSnapshot.self, from: data)
return try snapshot.validated()
} catch {
throw StatusAPIError.malformedResponse
}
Expand All @@ -44,7 +51,18 @@ enum StatusStore {

static func load() -> CachedStatus? {
guard let data = UserDefaults.standard.data(forKey: key) else { return nil }
return try? JSONDecoder().decode(CachedStatus.self, from: data)
guard var cachedStatus = try? JSONDecoder().decode(CachedStatus.self, from: data) else {
UserDefaults.standard.removeObject(forKey: key)
return nil
}
if let snapshot = cachedStatus.snapshot {
guard let validatedSnapshot = try? snapshot.validated() else {
UserDefaults.standard.removeObject(forKey: key)
return nil
}
cachedStatus.snapshot = validatedSnapshot
}
return cachedStatus
}

static func save(_ cachedStatus: CachedStatus) throws {
Expand All @@ -57,10 +75,11 @@ enum StatusRefresher {
static func refresh() async throws -> CachedStatus {
do {
let snapshot = try await StatusAPI().fetch()
let now = Date()
let result = CachedStatus(
snapshot: snapshot,
fetchedAt: Date(),
lastAttemptAt: Date(),
fetchedAt: min(now, snapshot.generatedAt),
lastAttemptAt: now,
lastError: nil
)
try StatusStore.save(result)
Expand Down
Loading