Skip to content

Commit 1aef855

Browse files
committed
Fix RecorderCocoa zero-size issue when added without constraints
Fixes #209
1 parent 872e0b6 commit 1aef855

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

Sources/KeyboardShortcuts/RecorderCocoa.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ extension KeyboardShortcuts {
8888
self.shortcutName = name
8989
self.onChange = onChange
9090

91-
super.init(frame: .zero)
91+
// Use a default frame that matches our intrinsic size to prevent zero-size issues
92+
// when added without constraints (issue #209)
93+
super.init(frame: NSRect(x: 0, y: 0, width: minimumWidth, height: 24))
9294
self.delegate = self
9395
self.placeholderString = "record_shortcut".localized
9496
self.alignment = .center

Tests/KeyboardShortcutsTests/KeyboardShortcutsTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Foundation
33
import AppKit
44
import KeyboardShortcuts
55

6-
@Suite("KeyboardShortcuts Tests")
6+
@Suite("KeyboardShortcuts Tests", .serialized)
77
struct KeyboardShortcutsTests {
88
init() {
99
UserDefaults.standard.removeAllKeyboardShortcuts()
@@ -247,7 +247,7 @@ struct KeyboardShortcutsTests {
247247

248248
// MARK: - Modifier Symbol Tests
249249

250-
@Suite("Modifier Symbol Tests")
250+
@Suite("Modifier Symbol Tests", .serialized)
251251
struct ModifierSymbolTests {
252252
@Test("Individual modifier symbols")
253253
func testIndividualModifierSymbols() {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Testing
2+
import Foundation
3+
import AppKit
4+
import KeyboardShortcuts
5+
6+
@Suite("RecorderCocoa Layout Tests")
7+
struct RecorderCocoaLayoutTests {
8+
@Test("RecorderCocoa has default size")
9+
func testRecorderDefaultSize() throws {
10+
let recorder = KeyboardShortcuts.RecorderCocoa(for: .init("test"))
11+
12+
#expect(recorder.frame.width >= 130)
13+
#expect(recorder.frame.height > 0)
14+
}
15+
16+
@Test("RecorderCocoa works with addSubview")
17+
@MainActor
18+
func testRecorderAddSubview() throws {
19+
let recorder = KeyboardShortcuts.RecorderCocoa(for: .init("test"))
20+
let containerView = NSView(frame: NSRect(x: 0, y: 0, width: 400, height: 100))
21+
22+
containerView.addSubview(recorder)
23+
24+
#expect(recorder.frame.size != .zero)
25+
}
26+
}

0 commit comments

Comments
 (0)