Skip to content

Commit 1c5651e

Browse files
committed
Add data from winit
1 parent a9ff910 commit 1c5651e

File tree

11 files changed

+297
-42
lines changed

11 files changed

+297
-42
lines changed
+248
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,253 @@
1+
// Most of these are marked as MainThreadOnly automatically
12
data! {
3+
// TODO: This should be one of MainThreadOnly or Immutable (+Send/Sync)
4+
class NSAppearance {
5+
unsafe -appearanceNamed;
6+
unsafe -bestMatchFromAppearancesWithNames;
7+
}
8+
9+
class NSApplication {
10+
unsafe +sharedApplication;
11+
12+
unsafe -currentEvent;
13+
unsafe -postEvent_atStart;
14+
unsafe -presentationOptions;
15+
unsafe -windows;
16+
unsafe -keyWindow;
17+
unsafe -setDelegate;
18+
unsafe -setPresentationOptions;
19+
unsafe -hide;
20+
unsafe -orderFrontCharacterPalette;
21+
unsafe -hideOtherApplications;
22+
unsafe -stop;
23+
unsafe -activateIgnoringOtherApps;
24+
unsafe -requestUserAttention;
25+
unsafe -setActivationPolicy;
26+
unsafe -setMainMenu;
27+
unsafe -effectiveAppearance;
28+
unsafe -setAppearance;
29+
30+
// `run` cannot be safe, the user must ensure there is no re-entrancy.
31+
}
32+
33+
// Documentation says:
34+
// > Color objects are immutable and thread-safe
35+
//
36+
// TODO: Send + Sync
37+
class NSColor: Immutable {
38+
unsafe -clear;
39+
}
40+
41+
class NSControl {
42+
unsafe -isEnabled;
43+
unsafe -setEnabled;
44+
}
45+
46+
// NSCursor is immutable, stated here:
47+
// https://developer.apple.com/documentation/appkit/nscursor/1527062-image?language=objc
48+
//
49+
// TODO: Send + Sync
50+
class NSCursor: Immutable {
51+
unsafe -initWithImage_hotSpot;
52+
53+
unsafe -arrowCursor;
54+
unsafe -IBeamCursor;
55+
unsafe -pointingHandCursor;
56+
unsafe -closedHandCursor;
57+
unsafe -openHandCursor;
58+
unsafe -resizeLeftCursor;
59+
unsafe -resizeRightCursor;
60+
unsafe -resizeLeftRightCursor;
61+
unsafe -resizeUpCursor;
62+
unsafe -resizeDownCursor;
63+
unsafe -resizeUpDownCursor;
64+
unsafe -crosshairCursor;
65+
unsafe -disappearingItemCursor;
66+
unsafe -operationNotAllowedCursor;
67+
unsafe -dragLinkCursor;
68+
unsafe -dragCopyCursor;
69+
unsafe -contextualMenuCursor;
70+
unsafe -IBeamCursorForVerticalLayout;
71+
}
72+
73+
// Since this is immutable, it _may_ be possible to make Send+Sync, but
74+
// let's refrain from doing so, because of:
75+
// > Safely handled only on the same thread, whether that be the main
76+
// > thread or a secondary thread; otherwise you run the risk of having
77+
// > events get out of sequence.
78+
// <https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/AddingBehaviortoaCocoaProgram/AddingBehaviorCocoa.html#//apple_ref/doc/uid/TP40002974-CH5-SW47>
79+
// <https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html#//apple_ref/doc/uid/10000057i-CH12-123383>
80+
class NSEvent: Immutable {
81+
82+
}
83+
84+
// Documented Thread-Unsafe, but:
85+
// > One thread can create an NSImage object, draw to the image buffer,
86+
// > and pass it off to the main thread for drawing. The underlying image
87+
// > cache is shared among all threads.
88+
// <https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html#//apple_ref/doc/uid/10000057i-CH12-126728>
89+
//
90+
// So really only unsafe to mutate on several threads.
91+
//
92+
// Unsure yet if it would be beneficial to mark this as `Mutable`, or if
93+
// we should just keep it as interiormutable?
94+
class NSImage {
95+
unsafe -initWithData;
96+
unsafe -initByReferencingFile;
97+
}
98+
99+
class NSMenu: MainThreadOnly {
100+
unsafe -init;
101+
unsafe -addItem;
102+
}
103+
104+
// Any modification of the target or the action has to remain `unsafe`
105+
class NSMenuItem: MainThreadOnly {
106+
unsafe -init;
107+
unsafe +separatorItem;
108+
unsafe -setKeyEquivalentModifierMask;
109+
unsafe -setSubmenu;
110+
}
111+
112+
class NSPasteboard {
113+
unsafe -propertyListForType;
114+
}
115+
116+
// Documented as "Thread-Unsafe"
117+
class NSResponder {}
118+
119+
// Accesses the shared application, and hence is main thread only (even
120+
// though not marked so in Swift).
121+
class NSScreen: MainThreadOnly {
122+
unsafe +mainScreen;
123+
unsafe +screens;
124+
unsafe -frame;
125+
unsafe -visibleFrame;
126+
unsafe -deviceDescription;
127+
unsafe -backingScaleFactor;
128+
}
129+
130+
class NSWindowTabGroup: MainThreadOnly {
131+
unsafe -windows;
132+
unsafe -setSelectedWindow;
133+
}
134+
135+
class NSTextInputContext: MainThreadOnly {
136+
unsafe -invalidateCharacterCoordinates;
137+
unsafe -discardMarkedText;
138+
unsafe -selectedKeyboardInputSource;
139+
}
140+
2141
// Subclasses `NSMutableAttributedString`, though I think this should
3142
// actually be `InteriorMutable`?
4143
class NSTextStorage: Mutable {}
144+
145+
// Documented as "Main Thread Only".
146+
// > generally thread safe, although operations on views such as creating,
147+
// > resizing, and moving should happen on the main thread.
148+
// <https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/AddingBehaviortoaCocoaProgram/AddingBehaviorCocoa.html#//apple_ref/doc/uid/TP40002974-CH5-SW47>
149+
//
150+
// > If you want to use a thread to draw to a view, bracket all drawing code
151+
// > between the lockFocusIfCanDraw and unlockFocus methods of NSView.
152+
// <https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html#//apple_ref/doc/uid/10000057i-CH12-123351-BBCFIIEB>
153+
class NSView {
154+
unsafe -frame;
155+
unsafe -bounds;
156+
unsafe -inputContext;
157+
unsafe -visibleRect;
158+
unsafe -hasMarkedText;
159+
unsafe -convertPoint_fromView;
160+
unsafe -window;
161+
162+
unsafe -setWantsBestResolutionOpenGLSurface;
163+
unsafe -setWantsLayer;
164+
unsafe -setPostsFrameChangedNotifications;
165+
unsafe -removeTrackingRect;
166+
unsafe -addCursorRect_cursor;
167+
unsafe -setHidden;
168+
}
169+
170+
// Documented as "Main Thread Only", but:
171+
// > Thread safe in that you can create and manage them on a secondary
172+
// > thread.
173+
// <https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/AddingBehaviortoaCocoaProgram/AddingBehaviorCocoa.html#//apple_ref/doc/uid/TP40002974-CH5-SW47>
174+
// <https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html#//apple_ref/doc/uid/10000057i-CH12-123364>
175+
//
176+
// So could in theory be `Send`, and perhaps also `Sync` - but we would
177+
// like interior mutability on windows, since that's just much easier, and
178+
// in that case, they can't be!
179+
class NSWindow {
180+
// Initializers are not safe, since it is critical to memory safety
181+
// that `window.setReleasedWhenClosed(false)` is called.
182+
183+
unsafe -frame;
184+
unsafe -backingScaleFactor;
185+
unsafe -contentView;
186+
unsafe -setContentView;
187+
unsafe -setInitialFirstResponder;
188+
unsafe -makeFirstResponder;
189+
unsafe -contentRectForFrameRect;
190+
unsafe -screen;
191+
unsafe -setContentSize;
192+
unsafe -setFrameTopLeftPoint;
193+
unsafe -setMinSize;
194+
unsafe -setMaxSize;
195+
unsafe -setResizeIncrements;
196+
unsafe -contentResizeIncrements;
197+
unsafe -setContentResizeIncrements;
198+
unsafe -setFrame_display;
199+
unsafe -setMovable;
200+
unsafe -setSharingType;
201+
unsafe -setTabbingMode;
202+
unsafe -setOpaque;
203+
unsafe -hasShadow;
204+
unsafe -setHasShadow;
205+
unsafe -setIgnoresMouseEvents;
206+
unsafe -setBackgroundColor;
207+
unsafe -styleMask;
208+
unsafe -setStyleMask;
209+
unsafe -registerForDraggedTypes;
210+
unsafe -makeKeyAndOrderFront;
211+
unsafe -orderFront;
212+
unsafe -miniaturize;
213+
unsafe -sender;
214+
unsafe -toggleFullScreen;
215+
unsafe -orderOut;
216+
unsafe -zoom;
217+
unsafe -selectNextKeyView;
218+
unsafe -selectPreviousKeyView;
219+
unsafe -firstResponder;
220+
unsafe -standardWindowButton;
221+
unsafe -setTitle;
222+
unsafe -title;
223+
unsafe -setAcceptsMouseMovedEvents;
224+
unsafe -setTitlebarAppearsTransparent;
225+
unsafe -setTitleVisibility;
226+
unsafe -setMovableByWindowBackground;
227+
unsafe -setLevel;
228+
unsafe -setAllowsAutomaticWindowTabbing;
229+
unsafe -setTabbingIdentifier;
230+
unsafe -setDocumentEdited;
231+
unsafe -occlusionState;
232+
unsafe -center;
233+
unsafe -isResizable;
234+
unsafe -isMiniaturizable;
235+
unsafe -hasCloseBox;
236+
unsafe -isMiniaturized;
237+
unsafe -isVisible;
238+
unsafe -isKeyWindow;
239+
unsafe -isZoomed;
240+
unsafe -allowsAutomaticWindowTabbing;
241+
unsafe -selectNextTab;
242+
unsafe -tabbingIdentifier;
243+
unsafe -tabGroup;
244+
unsafe -isDocumentEdited;
245+
unsafe -close;
246+
unsafe -performWindowDragWithEvent;
247+
unsafe -invalidateCursorRectsForView;
248+
unsafe -setDelegate;
249+
unsafe -sendEvent;
250+
251+
// `addChildWindow:ordered:` is not safe, as cycles must be prevented
252+
}
5253
}

crates/header-translator/src/data/Foundation.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ data! {
139139
unsafe -setName;
140140
}
141141

142-
class NSValue {
142+
class NSValue: Immutable {
143143
unsafe -objCType;
144144
unsafe -isEqualToValue;
145145
}
146146

147-
class NSUUID {
147+
class NSUUID: Immutable {
148148
unsafe +UUID;
149149
unsafe -init;
150150
unsafe -initWithUUIDString;
@@ -186,7 +186,7 @@ data! {
186186
class NSIndexSet: ImmutableWithMutableSubclass<Foundation::NSMutableIndexSet> {}
187187
class NSMutableIndexSet: MutableWithImmutableSuperclass<Foundation::NSIndexSet> {}
188188

189-
class NSNumber {
189+
class NSNumber: Immutable {
190190
unsafe -initWithChar;
191191
unsafe -initWithUnsignedChar;
192192
unsafe -initWithShort;
@@ -237,6 +237,8 @@ data! {
237237
unsafe -stringValue;
238238
}
239239

240+
class NSDecimalNumber: Immutable {}
241+
240242
class NSURLRequest: ImmutableWithMutableSubclass<Foundation::NSMutableURLRequest> {}
241243
class NSMutableURLRequest: MutableWithImmutableSuperclass<Foundation::NSURLRequest> {}
242244
}

crates/header-translator/src/data/macros.rs

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ macro_rules! __set_mutability {
6969
($data:expr; Mutable) => {
7070
$data.mutability = $crate::stmt::Mutability::Mutable;
7171
};
72+
($data:expr; MainThreadOnly) => {
73+
$data.mutability = $crate::stmt::Mutability::MainThreadOnly;
74+
};
7275
}
7376

7477
macro_rules! __data_inner {

crates/header-translator/src/method.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,11 @@ impl Method {
266266
}
267267

268268
pub(crate) fn usable_in_default_id(&self) -> bool {
269-
self.selector == "new" && self.is_class && self.arguments.is_empty() && self.safe
269+
self.selector == "new"
270+
&& self.is_class
271+
&& self.arguments.is_empty()
272+
&& self.safe
273+
&& !self.mainthreadonly
270274
}
271275

272276
fn parent_type_data(entity: &Entity<'_>, context: &Context<'_>) -> (bool, bool) {

crates/header-translator/translation-config.toml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ tvos = "9.0"
2828
[library.AppKit]
2929
imports = ["CoreData", "Foundation"]
3030
gnustep-library = "gnustep-gui"
31+
additions = true
3132
fixes = true
3233
extra-features = [
3334
# Temporary, since some structs and statics use these

crates/icrate/CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1414
* Added `MainThreadMarker` `From` implementation for `MainThreadOnly` types.
1515
* Added `Send` and `Sync` implementations for a bunch more types (same as the
1616
ones Swift marks as `@Sendable`).
17+
* Made some common methods in `AppKit` safe.
1718

1819
### Changed
1920
* Moved the `ns_string!` macro to `icrate::Foundation::ns_string`. The old
@@ -39,12 +40,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
3940
let view = unsafe { NSView::initWithFrame(mtm.alloc(), frame) };
4041
// Do something with `app` and `view`
4142
```
43+
* **BREAKING**: Changed the `NSApp` static to be a function taking `MainThreadMarker`.
4244

4345
### Removed
4446
* **BREAKING**: Removed the `MainThreadMarker` argument from the closure
4547
passed to `MainThreadBound::get_on_main`.
46-
* **BREAKING**: Removed the `NSApp` static for now - it will likely be
47-
re-added later in another form.
4848

4949

5050
## icrate 0.0.4 - 2023-07-31

0 commit comments

Comments
 (0)