objc2_app_kit/generated/NSAlert.rs
1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::ffi::*;
4use core::ptr::NonNull;
5use objc2::__framework_prelude::*;
6use objc2_foundation::*;
7
8use crate::*;
9
10/// The alert styles used by the `alertStyle` property on instances of `NSAlert`.
11///
12/// See also [Apple's documentation](https://developer.apple.com/documentation/appkit/nsalertstyle?language=objc)
13// NS_ENUM
14#[repr(transparent)]
15#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
16pub struct NSAlertStyle(pub NSUInteger);
17impl NSAlertStyle {
18 /// An alert style to be used to warn the user about a current or impending event.
19 /// This style should be used when the alert’s content is more severe than `NSAlertStyleInformational` but less than `NSAlertStyleCritical`.
20 /// This is the default alert style.
21 #[doc(alias = "NSAlertStyleWarning")]
22 pub const Warning: Self = Self(0);
23 /// An alert style to be used to inform the user about a current or impending event.
24 /// - Note: Currently, there is no visual difference between informational and warning alerts.
25 #[doc(alias = "NSAlertStyleInformational")]
26 pub const Informational: Self = Self(1);
27 /// An alert style to be used to inform the user about a critical event.
28 /// Causes the alert’s icon to be badged with a caution icon.
29 /// The critical (or “caution”) style should be used sparingly. Using it too frequently diminishes its significance. Use the symbol only when extra attention is really needed, as when confirming an action that might result in unexpected loss of data. Don’t use the symbol for tasks whose only purpose is to overwrite or remove data, such as a save or empty trash.
30 #[doc(alias = "NSAlertStyleCritical")]
31 pub const Critical: Self = Self(2);
32}
33
34unsafe impl Encode for NSAlertStyle {
35 const ENCODING: Encoding = NSUInteger::ENCODING;
36}
37
38unsafe impl RefEncode for NSAlertStyle {
39 const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
40}
41
42/// [Apple's documentation](https://developer.apple.com/documentation/appkit/nsalertfirstbuttonreturn?language=objc)
43#[cfg(feature = "NSApplication")]
44pub static NSAlertFirstButtonReturn: NSModalResponse = 1000;
45
46/// [Apple's documentation](https://developer.apple.com/documentation/appkit/nsalertsecondbuttonreturn?language=objc)
47#[cfg(feature = "NSApplication")]
48pub static NSAlertSecondButtonReturn: NSModalResponse = 1001;
49
50/// [Apple's documentation](https://developer.apple.com/documentation/appkit/nsalertthirdbuttonreturn?language=objc)
51#[cfg(feature = "NSApplication")]
52pub static NSAlertThirdButtonReturn: NSModalResponse = 1002;
53
54extern_class!(
55 /// A modal dialog or sheet attached to a document window.
56 /// The `NSAlert` class is not designed for subclassing.
57 ///
58 /// See also [Apple's documentation](https://developer.apple.com/documentation/appkit/nsalert?language=objc)
59 #[unsafe(super(NSObject))]
60 #[thread_kind = MainThreadOnly]
61 #[derive(Debug, PartialEq, Eq, Hash)]
62 pub struct NSAlert;
63);
64
65extern_conformance!(
66 unsafe impl NSObjectProtocol for NSAlert {}
67);
68
69impl NSAlert {
70 extern_methods!(
71 /// Returns an alert initialized from information in an error object.
72 /// - Parameter error: Error information to display.
73 /// - Returns: An initialized alert.
74 /// - Note: The `NSAlert` class extracts the localized error description, recovery suggestion, and recovery options from the error parameter and uses them as the alert’s message text, informative text, and button titles, respectively.
75 #[unsafe(method(alertWithError:))]
76 #[unsafe(method_family = none)]
77 pub fn alertWithError(error: &NSError, mtm: MainThreadMarker) -> Retained<NSAlert>;
78
79 /// The text that is displayed prominently in the alert.
80 /// - Note: Use this string to get the user’s attention and communicate the reason for displaying the alert.
81 #[unsafe(method(messageText))]
82 #[unsafe(method_family = none)]
83 pub fn messageText(&self) -> Retained<NSString>;
84
85 /// Setter for [`messageText`][Self::messageText].
86 ///
87 /// This is [copied][objc2_foundation::NSCopying::copy] when set.
88 #[unsafe(method(setMessageText:))]
89 #[unsafe(method_family = none)]
90 pub fn setMessageText(&self, message_text: &NSString);
91
92 /// The descriptive text that provides more details about the reason for the alert.
93 /// - Note: The informative text string is displayed below the message text and is less prominent. Use this string to provide additional context about the reason for the alert or about the actions that the user might take.
94 #[unsafe(method(informativeText))]
95 #[unsafe(method_family = none)]
96 pub fn informativeText(&self) -> Retained<NSString>;
97
98 /// Setter for [`informativeText`][Self::informativeText].
99 ///
100 /// This is [copied][objc2_foundation::NSCopying::copy] when set.
101 #[unsafe(method(setInformativeText:))]
102 #[unsafe(method_family = none)]
103 pub fn setInformativeText(&self, informative_text: &NSString);
104
105 #[cfg(feature = "NSImage")]
106 /// The custom icon displayed in the alert.
107 /// By default, the image used in an alert is the app icon (`NSApplicationIcon`). If this property’s value is set, the specified custom image is used in place of the app icon.
108 /// If a custom alert icon had been set, it can be cleared by setting this property’s value to `nil`, which restores use of the app icon for the alert.
109 #[unsafe(method(icon))]
110 #[unsafe(method_family = none)]
111 pub fn icon(&self) -> Option<Retained<NSImage>>;
112
113 #[cfg(feature = "NSImage")]
114 /// Setter for [`icon`][Self::icon].
115 ///
116 /// # Safety
117 ///
118 /// `icon` might not allow `None`.
119 #[unsafe(method(setIcon:))]
120 #[unsafe(method_family = none)]
121 pub unsafe fn setIcon(&self, icon: Option<&NSImage>);
122
123 #[cfg(all(
124 feature = "NSButton",
125 feature = "NSControl",
126 feature = "NSResponder",
127 feature = "NSView"
128 ))]
129 /// Adds a button with a given title to the alert.
130 ///
131 /// Buttons should be added from most-to-least prominent. The exact visual arrangement of the buttons is determined by `NSAlert` internally. In general, they are arranged from trailing-to-leading edge when laid out horizontally, and top-to-bottom when laid out vertically, with some exceptions for buttons like “Cancel”.
132 ///
133 /// The first three buttons are identified by the order in which they are added (not the order in which they may appear visually) as `NSAlertFirstButtonReturn`, `NSAlertSecondButtonReturn`, `NSAlertThirdButtonReturn` in the return-code parameter. Subsequent buttons are identified as `NSAlertThirdButtonReturn` + *n*, where *n* is an integer.
134 ///
135 /// By default, the first button has a key equivalent of Return, any button with a title of “Cancel” has a key equivalent of Escape, and any button with the title “Don’t Save” has a key equivalent of Command-D (but only if it’s not the first button). You can also assign different key equivalents for the buttons using the `keyEquivalent` method of the `NSButton` class. In addition, you can use the `tag` property of the `NSButton` class to set the alert presentation’s return-code. The framework reserves the use of the button’s `target` and `action`.
136 ///
137 /// - Parameter title: Title of the button to add to the alert.
138 /// - Returns: The button that was added to the alert.
139 #[unsafe(method(addButtonWithTitle:))]
140 #[unsafe(method_family = none)]
141 pub fn addButtonWithTitle(&self, title: &NSString) -> Retained<NSButton>;
142
143 #[cfg(all(
144 feature = "NSButton",
145 feature = "NSControl",
146 feature = "NSResponder",
147 feature = "NSView"
148 ))]
149 /// The array of response buttons for the alert.
150 /// The buttons are in the order in which they were added, and do not necessarily reflect the order they are arranged visually. The array does not include the default “OK” button that is shown in an alert presented without any buttons added with `-addButtonWithTitle:`.
151 #[unsafe(method(buttons))]
152 #[unsafe(method_family = none)]
153 pub fn buttons(&self) -> Retained<NSArray<NSButton>>;
154
155 /// Indicates the alert’s severity level. See the `NSAlertStyle` enumeration for the list of alert style constants.
156 #[unsafe(method(alertStyle))]
157 #[unsafe(method_family = none)]
158 pub fn alertStyle(&self) -> NSAlertStyle;
159
160 /// Setter for [`alertStyle`][Self::alertStyle].
161 #[unsafe(method(setAlertStyle:))]
162 #[unsafe(method_family = none)]
163 pub fn setAlertStyle(&self, alert_style: NSAlertStyle);
164
165 /// Specifies whether the alert has a help button.
166 ///
167 /// Set this property’s value to `YES` to specify that the alert has a help button, or `NO` to specify it does not.
168 ///
169 /// When a user clicks an alert’s help button, the alert delegate (`delegate`) receives an `alertShowHelp:` message. The delegate is responsible for displaying the help information related to this particular alert.
170 ///
171 /// Clicking an alert’s help button can alternately cause the `-openHelpAnchor:inBook:` message to be sent to the app’s help manager with a `nil` book and the anchor specified by the `helpAnchor` property, if any of the following conditions are true:
172 /// - There is no alert delegate.
173 /// - The alert delegate does not implement `-alertShowHelp:`.
174 /// - The alert delegate implements `-alertShowHelp:` but returns `NO`. When this is the case, an exception is raised if no help anchor is set.
175 #[unsafe(method(showsHelp))]
176 #[unsafe(method_family = none)]
177 pub fn showsHelp(&self) -> bool;
178
179 /// Setter for [`showsHelp`][Self::showsHelp].
180 #[unsafe(method(setShowsHelp:))]
181 #[unsafe(method_family = none)]
182 pub fn setShowsHelp(&self, shows_help: bool);
183
184 #[cfg(feature = "NSHelpManager")]
185 /// The alert’s HTML help anchor used when the user clicks the alert’s help button
186 #[unsafe(method(helpAnchor))]
187 #[unsafe(method_family = none)]
188 pub fn helpAnchor(&self) -> Option<Retained<NSHelpAnchorName>>;
189
190 #[cfg(feature = "NSHelpManager")]
191 /// Setter for [`helpAnchor`][Self::helpAnchor].
192 ///
193 /// This is [copied][objc2_foundation::NSCopying::copy] when set.
194 #[unsafe(method(setHelpAnchor:))]
195 #[unsafe(method_family = none)]
196 pub fn setHelpAnchor(&self, help_anchor: Option<&NSHelpAnchorName>);
197
198 /// The delegate of the receiver, currently only allows for custom help behavior of the alert.
199 /// For apps linked against 10.12, this property has zeroing weak memory semantics. When linked against an older SDK this back to having `retain` semantics, matching legacy behavior.
200 #[unsafe(method(delegate))]
201 #[unsafe(method_family = none)]
202 pub fn delegate(&self) -> Option<Retained<ProtocolObject<dyn NSAlertDelegate>>>;
203
204 /// Setter for [`delegate`][Self::delegate].
205 ///
206 /// This is a [weak property][objc2::topics::weak_property].
207 #[unsafe(method(setDelegate:))]
208 #[unsafe(method_family = none)]
209 pub fn setDelegate(&self, delegate: Option<&ProtocolObject<dyn NSAlertDelegate>>);
210
211 #[cfg(all(feature = "NSResponder", feature = "NSView"))]
212 /// The accessory view displayed in the alert, placed between the informative text or suppression checkbox (if present) and the response buttons. Before changing the location of the accessory view, first call the `-layout` method.
213 #[unsafe(method(accessoryView))]
214 #[unsafe(method_family = none)]
215 pub fn accessoryView(&self) -> Option<Retained<NSView>>;
216
217 #[cfg(all(feature = "NSResponder", feature = "NSView"))]
218 /// Setter for [`accessoryView`][Self::accessoryView].
219 #[unsafe(method(setAccessoryView:))]
220 #[unsafe(method_family = none)]
221 pub fn setAccessoryView(&self, accessory_view: Option<&NSView>);
222
223 /// Specifies that the alert must do immediate layout instead of lazily just before display.
224 /// Used to indicate that the alert panel should do immediate layout, overriding the default behavior of laying out lazily just before showing panel. Only call this method if wanting to do custom layout after it returns. Call this method only after the alert’s other customization, including setting message and informative text, and adding buttons and an accessory view if needed. Layout changes can be made after this method returns, in particular to adjust the frame of an accessory view. Note that the standard layout of the alert may change in the future, so layout customization should be done with caution.
225 #[unsafe(method(layout))]
226 #[unsafe(method_family = none)]
227 pub fn layout(&self);
228
229 #[cfg(feature = "NSApplication")]
230 /// Runs the alert as an app-modal dialog and returns the constant that identifies the button clicked.
231 #[unsafe(method(runModal))]
232 #[unsafe(method_family = none)]
233 pub fn runModal(&self) -> NSModalResponse;
234
235 /// Specifies whether the alert includes a suppression checkbox, which can be employed to allow a user to opt out of seeing the alert again.
236 /// The default value of this property is `NO`, which specifies the absence of a suppression checkbox in the alert. Set the value to `YES` to show a suppression checkbox in the alert.
237 /// By default, a suppression checkbox has the title, “Do not show this message again.” In macOS 11.0 and later, if the alert displays multiple buttons that prompt the user to make a choice, the title is “Do not ask again.” To customize it, use the checkbox’s title property, as follows:
238 ///
239 /// myAlert.suppressionButton.title =
240 /// "
241 /// Do not show this warning again";
242 ///
243 /// To create an alert that responds to the selection state of the suppression checkbox, check `myAlert.suppressionButton.state`.
244 #[unsafe(method(showsSuppressionButton))]
245 #[unsafe(method_family = none)]
246 pub fn showsSuppressionButton(&self) -> bool;
247
248 /// Setter for [`showsSuppressionButton`][Self::showsSuppressionButton].
249 #[unsafe(method(setShowsSuppressionButton:))]
250 #[unsafe(method_family = none)]
251 pub fn setShowsSuppressionButton(&self, shows_suppression_button: bool);
252
253 #[cfg(all(
254 feature = "NSButton",
255 feature = "NSControl",
256 feature = "NSResponder",
257 feature = "NSView"
258 ))]
259 /// The alert’s suppression checkbox. The checkbox may be customized, including the title and the initial state. Additionally, use this method to get the state of the button after the alert is dismissed, which may be stored in user defaults and checked before showing the alert again. In order to show the suppression button in the alert panel, you must set `showsSuppressionButton` to `YES`.
260 #[unsafe(method(suppressionButton))]
261 #[unsafe(method_family = none)]
262 pub fn suppressionButton(&self) -> Option<Retained<NSButton>>;
263
264 #[cfg(all(
265 feature = "NSApplication",
266 feature = "NSResponder",
267 feature = "NSWindow",
268 feature = "block2"
269 ))]
270 /// Runs the alert modally as a sheet attached to the specified window.
271 /// - Parameters:
272 /// - sheetWindow: The window on which to display the sheet.
273 /// - handler: The completion handler that gets called when the sheet’s modal session ends.
274 ///
275 /// This method uses the `NSWindow` sheet methods to display the alert. If the alert has an alert style of `NSCriticalAlertStyle`, it is presented as a critical sheet, which means that it can display on top of other sheets that might already be attached to the window. Otherwise, it is presented--or queued for presentation--as a standard sheet.
276 /// Note that `-orderOut:` no longer needs to be called in the completion handler. If the alert isn’t don’t dismiss the alert, it will be done for you after the completion handler finishes.
277 #[unsafe(method(beginSheetModalForWindow:completionHandler:))]
278 #[unsafe(method_family = none)]
279 pub fn beginSheetModalForWindow_completionHandler(
280 &self,
281 sheet_window: &NSWindow,
282 handler: Option<&block2::DynBlock<dyn Fn(NSModalResponse)>>,
283 );
284
285 #[cfg(all(feature = "NSResponder", feature = "NSWindow"))]
286 /// The app-modal panel or document-modal sheet that corresponds to the alert
287 #[unsafe(method(window))]
288 #[unsafe(method_family = none)]
289 pub fn window(&self) -> Retained<NSWindow>;
290 );
291}
292
293/// Methods declared on superclass `NSObject`.
294impl NSAlert {
295 extern_methods!(
296 #[unsafe(method(init))]
297 #[unsafe(method_family = init)]
298 pub fn init(this: Allocated<Self>) -> Retained<Self>;
299
300 #[unsafe(method(new))]
301 #[unsafe(method_family = new)]
302 pub fn new(mtm: MainThreadMarker) -> Retained<Self>;
303 );
304}
305
306extern_protocol!(
307 /// A set of optional methods implemented by the delegate of an `NSAlert` object to respond to a user’s request for help.
308 ///
309 /// See also [Apple's documentation](https://developer.apple.com/documentation/appkit/nsalertdelegate?language=objc)
310 pub unsafe trait NSAlertDelegate: NSObjectProtocol + MainThreadOnly {
311 /// Sent to the delegate when the user clicks the alert’s help button. The delegate causes help to be displayed for an alert, directly or indirectly.
312 /// - Returns: `YES` when the delegate displayed help directly, `NO` otherwise. When `NO` and the alert has a help anchor (`helpAnchor`), the application’s help manager displays help using the help anchor.
313 /// - Discussion: The delegate implements this method only to override the help-anchor lookup behavior.
314 #[optional]
315 #[unsafe(method(alertShowHelp:))]
316 #[unsafe(method_family = none)]
317 fn alertShowHelp(&self, alert: &NSAlert) -> bool;
318 }
319);
320
321/// NSAlertDeprecated.
322impl NSAlert {
323 extern_methods!(
324 #[cfg(all(feature = "NSResponder", feature = "NSWindow"))]
325 /// # Safety
326 ///
327 /// - `delegate` should be of the correct type.
328 /// - `did_end_selector` must be a valid selector.
329 /// - `context_info` must be a valid pointer or null.
330 #[deprecated = "Use -beginSheetModalForWindow:completionHandler: instead"]
331 #[unsafe(method(beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:))]
332 #[unsafe(method_family = none)]
333 pub unsafe fn beginSheetModalForWindow_modalDelegate_didEndSelector_contextInfo(
334 &self,
335 window: &NSWindow,
336 delegate: Option<&AnyObject>,
337 did_end_selector: Option<Sel>,
338 context_info: *mut c_void,
339 );
340 );
341}
342
343/// [Apple's documentation](https://developer.apple.com/documentation/appkit/nswarningalertstyle?language=objc)
344#[deprecated]
345pub static NSWarningAlertStyle: NSAlertStyle = NSAlertStyle(NSAlertStyle::Warning.0);
346
347/// [Apple's documentation](https://developer.apple.com/documentation/appkit/nsinformationalalertstyle?language=objc)
348#[deprecated]
349pub static NSInformationalAlertStyle: NSAlertStyle = NSAlertStyle(NSAlertStyle::Informational.0);
350
351/// [Apple's documentation](https://developer.apple.com/documentation/appkit/nscriticalalertstyle?language=objc)
352#[deprecated]
353pub static NSCriticalAlertStyle: NSAlertStyle = NSAlertStyle(NSAlertStyle::Critical.0);