@@ -77,6 +77,80 @@ interface SaveDialogOptions {
7777 canCreateDirectories ?: boolean
7878}
7979
80+ /**
81+ * Default buttons for a message dialog.
82+ *
83+ * @since 2.4.0
84+ */
85+ export type MessageDialogDefaultButtons =
86+ | 'Ok'
87+ | 'OkCancel'
88+ | 'YesNo'
89+ | 'YesNoCancel'
90+
91+ /** All possible button keys. */
92+ type ButtonKey = 'ok' | 'cancel' | 'yes' | 'no'
93+
94+ /** Ban everything except a set of keys. */
95+ type BanExcept < Allowed extends ButtonKey > = Partial <
96+ Record < Exclude < ButtonKey , Allowed > , never >
97+ >
98+
99+ /**
100+ * The Yes, No and Cancel buttons of a message dialog.
101+ *
102+ * @since 2.4.0
103+ */
104+ export type MessageDialogButtonsYesNoCancel = {
105+ /** The Yes button. */
106+ yes : string
107+ /** The No button. */
108+ no : string
109+ /** The Cancel button. */
110+ cancel : string
111+ } & BanExcept < 'yes' | 'no' | 'cancel' >
112+
113+ /**
114+ * The Ok and Cancel buttons of a message dialog.
115+ *
116+ * @since 2.4.0
117+ */
118+ export type MessageDialogButtonsOkCancel = {
119+ /** The Ok button. */
120+ ok : string
121+ /** The Cancel button. */
122+ cancel : string
123+ } & BanExcept < 'ok' | 'cancel' >
124+
125+ /**
126+ * The Ok button of a message dialog.
127+ *
128+ * @since 2.4.0
129+ */
130+ export type MessageDialogButtonsOk = {
131+ /** The Ok button. */
132+ ok : string
133+ } & BanExcept < 'ok' >
134+
135+ /**
136+ * Custom buttons for a message dialog.
137+ *
138+ * @since 2.4.0
139+ */
140+ export type MessageDialogCustomButtons =
141+ | MessageDialogButtonsYesNoCancel
142+ | MessageDialogButtonsOkCancel
143+ | MessageDialogButtonsOk
144+
145+ /**
146+ * The buttons of a message dialog.
147+ *
148+ * @since 2.4.0
149+ */
150+ export type MessageDialogButtons =
151+ | MessageDialogDefaultButtons
152+ | MessageDialogCustomButtons
153+
80154/**
81155 * @since 2.0.0
82156 */
@@ -85,8 +159,58 @@ interface MessageDialogOptions {
85159 title ?: string
86160 /** The kind of the dialog. Defaults to `info`. */
87161 kind ?: 'info' | 'warning' | 'error'
88- /** The label of the confirm button. */
162+ /**
163+ * The label of the Ok button.
164+ *
165+ * @deprecated Use {@linkcode MessageDialogOptions.buttons} instead.
166+ */
89167 okLabel ?: string
168+ /**
169+ * The buttons of the dialog.
170+ *
171+ * @example
172+ *
173+ * ```ts
174+ * // Use system default buttons texts
175+ * await message('Hello World!', { buttons: 'Ok' })
176+ * await message('Hello World!', { buttons: 'OkCancel' })
177+ *
178+ * // Or with custom button texts
179+ * await message('Hello World!', { buttons: { ok: 'Yes!' } })
180+ * await message('Take on the task?', {
181+ * buttons: { ok: 'Accept', cancel: 'Cancel' }
182+ * })
183+ * await message('Show the file content?', {
184+ * buttons: { yes: 'Show content', no: 'Show in folder', cancel: 'Cancel' }
185+ * })
186+ * ```
187+ *
188+ * @since 2.4.0
189+ */
190+ buttons ?: MessageDialogButtons
191+ }
192+
193+ /**
194+ * Internal function to convert the buttons to the Rust type.
195+ */
196+ function buttonsToRust ( buttons : MessageDialogButtons | undefined ) {
197+ if ( buttons === undefined ) {
198+ return undefined
199+ }
200+
201+ if ( typeof buttons === 'string' ) {
202+ return buttons
203+ } else if ( 'ok' in buttons && 'cancel' in buttons ) {
204+ return { OkCancelCustom : [ buttons . ok , buttons . cancel ] }
205+ } else if ( 'yes' in buttons && 'no' in buttons && 'cancel' in buttons ) {
206+ return {
207+ YesNoCancelCustom : [ buttons . yes , buttons . no , buttons . cancel ]
208+ }
209+ } else if ( 'ok' in buttons ) {
210+ return { OkCustom : buttons . ok }
211+ }
212+
213+ return undefined
90214}
91215
92216interface ConfirmDialogOptions {
@@ -202,6 +326,16 @@ async function save(options: SaveDialogOptions = {}): Promise<string | null> {
202326 return await invoke ( 'plugin:dialog|save' , { options } )
203327}
204328
329+ /**
330+ * The result of a message dialog.
331+ *
332+ * The result is a string if the dialog has custom buttons,
333+ * otherwise it is one of the default buttons.
334+ *
335+ * @since 2.4.0
336+ */
337+ export type MessageDialogResult = 'Yes' | 'No' | 'Ok' | 'Cancel' | ( string & { } )
338+
205339/**
206340 * Shows a message dialog with an `Ok` button.
207341 * @example
@@ -222,13 +356,15 @@ async function save(options: SaveDialogOptions = {}): Promise<string | null> {
222356async function message (
223357 message : string ,
224358 options ?: string | MessageDialogOptions
225- ) : Promise < void > {
359+ ) : Promise < MessageDialogResult > {
226360 const opts = typeof options === 'string' ? { title : options } : options
227- await invoke ( 'plugin:dialog|message' , {
361+
362+ return invoke < MessageDialogResult > ( 'plugin:dialog|message' , {
228363 message : message . toString ( ) ,
229364 title : opts ?. title ?. toString ( ) ,
230365 kind : opts ?. kind ,
231- okButtonLabel : opts ?. okLabel ?. toString ( )
366+ okButtonLabel : opts ?. okLabel ?. toString ( ) ,
367+ buttons : buttonsToRust ( opts ?. buttons )
232368 } )
233369}
234370
0 commit comments