@wailsio/runtime 3.0.0-alpha.20 → 3.0.0-alpha.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/docs/{functions/Window.Size.html → assets/icons.js} +15 -2
  2. package/docs/assets/icons.svg +1 -0
  3. package/docs/assets/main.js +4 -4
  4. package/docs/assets/navigation.js +1 -1
  5. package/docs/assets/search.js +1 -1
  6. package/docs/assets/style.css +2 -1
  7. package/docs/classes/Events.WailsEvent.html +4 -4
  8. package/docs/functions/Application.Hide.html +2 -2
  9. package/docs/functions/Application.Quit.html +2 -2
  10. package/docs/functions/Application.Show.html +2 -2
  11. package/docs/functions/Browser.OpenURL.html +3 -3
  12. package/docs/functions/Call.ByID.html +4 -4
  13. package/docs/functions/Call.ByName.html +4 -4
  14. package/docs/functions/Call.Call.html +3 -3
  15. package/docs/functions/Call.Plugin.html +5 -5
  16. package/docs/functions/Clipboard.SetText.html +3 -3
  17. package/docs/functions/Clipboard.Text.html +2 -2
  18. package/docs/functions/Dialogs.Error.html +2 -2
  19. package/docs/functions/Dialogs.Info.html +2 -2
  20. package/docs/functions/Dialogs.OpenFile.html +2 -2
  21. package/docs/functions/Dialogs.Question.html +2 -2
  22. package/docs/functions/Dialogs.SaveFile.html +2 -2
  23. package/docs/functions/Dialogs.Warning.html +2 -2
  24. package/docs/functions/Events.Emit.html +3 -3
  25. package/docs/functions/Events.Off.html +4 -4
  26. package/docs/functions/Events.OffAll.html +2 -2
  27. package/docs/functions/Events.On.html +4 -4
  28. package/docs/functions/Events.OnMultiple.html +5 -5
  29. package/docs/functions/Events.Once.html +4 -4
  30. package/docs/functions/Events.setup.html +1 -1
  31. package/docs/functions/Flags.GetFlag.html +3 -3
  32. package/docs/functions/Screens.GetAll.html +3 -3
  33. package/docs/functions/Screens.GetCurrent.html +3 -3
  34. package/docs/functions/Screens.GetPrimary.html +3 -3
  35. package/docs/functions/System.Capabilities.html +2 -2
  36. package/docs/functions/System.Environment.html +2 -2
  37. package/docs/functions/System.IsAMD64.html +2 -2
  38. package/docs/functions/System.IsARM.html +2 -2
  39. package/docs/functions/System.IsARM64.html +2 -2
  40. package/docs/functions/System.IsDarkMode.html +2 -2
  41. package/docs/functions/System.IsDebug.html +1 -1
  42. package/docs/functions/System.IsLinux.html +2 -2
  43. package/docs/functions/System.IsMac.html +2 -2
  44. package/docs/functions/System.IsWindows.html +2 -2
  45. package/docs/functions/System.invoke.html +1 -1
  46. package/docs/functions/WML.Enable.html +2 -0
  47. package/docs/functions/WML.Reload.html +2 -2
  48. package/docs/index.html +2 -2
  49. package/docs/interfaces/Dialogs.Button.html +7 -7
  50. package/docs/interfaces/Dialogs.FileFilter.html +5 -5
  51. package/docs/interfaces/Dialogs.MessageDialogOptions.html +9 -9
  52. package/docs/interfaces/Dialogs.OpenFileDialogOptions.html +33 -33
  53. package/docs/interfaces/Dialogs.SaveFileDialogOptions.html +35 -35
  54. package/docs/interfaces/Screens.Position.html +5 -5
  55. package/docs/interfaces/Screens.Rect.html +9 -9
  56. package/docs/interfaces/Screens.Screen.html +19 -19
  57. package/docs/interfaces/Screens.Size.html +5 -5
  58. package/docs/interfaces/System.EnvironmentInfo.html +11 -5
  59. package/docs/interfaces/System.OSInfo.html +9 -0
  60. package/docs/modules/Application.html +4 -4
  61. package/docs/modules/Browser.html +2 -2
  62. package/docs/modules/Call.html +5 -5
  63. package/docs/modules/Clipboard.html +3 -3
  64. package/docs/modules/Dialogs.html +12 -12
  65. package/docs/modules/Events.html +10 -10
  66. package/docs/modules/Flags.html +2 -2
  67. package/docs/modules/Screens.html +8 -9
  68. package/docs/modules/System.html +14 -13
  69. package/docs/modules/WML.html +3 -2
  70. package/docs/modules.html +12 -12
  71. package/docs/variables/Events.Types.html +1 -1
  72. package/docs/variables/Window.html +2 -0
  73. package/package.json +31 -31
  74. package/src/application.js +46 -46
  75. package/src/browser.js +24 -24
  76. package/src/calls.js +180 -161
  77. package/src/clipboard.js +35 -35
  78. package/src/contextmenu.js +97 -97
  79. package/src/dialogs.js +200 -200
  80. package/src/drag.js +119 -125
  81. package/src/event_types.js +5 -0
  82. package/src/events.js +140 -140
  83. package/src/flags.js +25 -25
  84. package/src/index.js +46 -95
  85. package/src/runtime.js +123 -122
  86. package/src/screens.js +77 -83
  87. package/src/system.js +128 -117
  88. package/src/utils.js +86 -0
  89. package/src/window.js +638 -316
  90. package/src/wml.js +250 -171
  91. package/tsconfig.json +19 -19
  92. package/types/event_types.d.ts +5 -0
  93. package/types/events.d.ts +5 -0
  94. package/types/index.d.ts +2 -2
  95. package/types/runtime.d.ts +1 -0
  96. package/types/screens.d.ts +1 -5
  97. package/types/system.d.ts +44 -4
  98. package/types/utils.d.ts +14 -0
  99. package/types/window.d.ts +400 -133
  100. package/types/wml.d.ts +6 -0
  101. package/docs/functions/Window.Center.html +0 -2
  102. package/docs/functions/Window.Close.html +0 -2
  103. package/docs/functions/Window.Fullscreen.html +0 -2
  104. package/docs/functions/Window.Get.html +0 -6
  105. package/docs/functions/Window.GetZoomLevel.html +0 -2
  106. package/docs/functions/Window.Height.html +0 -2
  107. package/docs/functions/Window.Hide.html +0 -2
  108. package/docs/functions/Window.Maximise.html +0 -2
  109. package/docs/functions/Window.Minimise.html +0 -2
  110. package/docs/functions/Window.RelativePosition.html +0 -2
  111. package/docs/functions/Window.Restore.html +0 -2
  112. package/docs/functions/Window.Screen-1.html +0 -2
  113. package/docs/functions/Window.SetAlwaysOnTop.html +0 -3
  114. package/docs/functions/Window.SetBackgroundColour.html +0 -6
  115. package/docs/functions/Window.SetMaxSize.html +0 -4
  116. package/docs/functions/Window.SetMinSize.html +0 -4
  117. package/docs/functions/Window.SetRelativePosition.html +0 -4
  118. package/docs/functions/Window.SetResizable.html +0 -3
  119. package/docs/functions/Window.SetSize.html +0 -4
  120. package/docs/functions/Window.SetTitle.html +0 -3
  121. package/docs/functions/Window.SetZoomLevel.html +0 -3
  122. package/docs/functions/Window.Show.html +0 -2
  123. package/docs/functions/Window.ToggleMaximise.html +0 -2
  124. package/docs/functions/Window.UnMaximise.html +0 -2
  125. package/docs/functions/Window.UnMinimise.html +0 -2
  126. package/docs/functions/Window.Width.html +0 -2
  127. package/docs/functions/Window.ZoomIn.html +0 -2
  128. package/docs/functions/Window.ZoomOut.html +0 -2
  129. package/docs/functions/Window.ZoomReset.html +0 -2
  130. package/docs/modules/Window.html +0 -32
  131. package/docs/types/Screens.Rotation.html +0 -2
  132. package/src/log.js +0 -13
@@ -1,97 +1,97 @@
1
- /*
2
- _ __ _ __
3
- | | / /___ _(_) /____
4
- | | /| / / __ `/ / / ___/
5
- | |/ |/ / /_/ / / (__ )
6
- |__/|__/\__,_/_/_/____/
7
- The electron alternative for Go
8
- (c) Lea Anthony 2019-present
9
- */
10
-
11
- /* jshint esversion: 9 */
12
-
13
- import {newRuntimeCallerWithID, objectNames} from "./runtime";
14
- import {IsDebug} from "./system";
15
-
16
- // setup
17
- window.addEventListener('contextmenu', contextMenuHandler);
18
-
19
- const call = newRuntimeCallerWithID(objectNames.ContextMenu, '');
20
- const ContextMenuOpen = 0;
21
-
22
- function openContextMenu(id, x, y, data) {
23
- void call(ContextMenuOpen, {id, x, y, data});
24
- }
25
-
26
- function contextMenuHandler(event) {
27
- // Check for custom context menu
28
- let element = event.target;
29
- let customContextMenu = window.getComputedStyle(element).getPropertyValue("--custom-contextmenu");
30
- customContextMenu = customContextMenu ? customContextMenu.trim() : "";
31
- if (customContextMenu) {
32
- event.preventDefault();
33
- let customContextMenuData = window.getComputedStyle(element).getPropertyValue("--custom-contextmenu-data");
34
- openContextMenu(customContextMenu, event.clientX, event.clientY, customContextMenuData);
35
- return
36
- }
37
-
38
- processDefaultContextMenu(event);
39
- }
40
-
41
-
42
- /*
43
- --default-contextmenu: auto; (default) will show the default context menu if contentEditable is true OR text has been selected OR element is input or textarea
44
- --default-contextmenu: show; will always show the default context menu
45
- --default-contextmenu: hide; will always hide the default context menu
46
-
47
- This rule is inherited like normal CSS rules, so nesting works as expected
48
- */
49
- function processDefaultContextMenu(event) {
50
-
51
- // Debug builds always show the menu
52
- if (IsDebug()) {
53
- return;
54
- }
55
-
56
- // Process default context menu
57
- const element = event.target;
58
- const computedStyle = window.getComputedStyle(element);
59
- const defaultContextMenuAction = computedStyle.getPropertyValue("--default-contextmenu").trim();
60
- switch (defaultContextMenuAction) {
61
- case "show":
62
- return;
63
- case "hide":
64
- event.preventDefault();
65
- return;
66
- default:
67
- // Check if contentEditable is true
68
- if (element.isContentEditable) {
69
- return;
70
- }
71
-
72
- // Check if text has been selected
73
- const selection = window.getSelection();
74
- const hasSelection = (selection.toString().length > 0)
75
- if (hasSelection) {
76
- for (let i = 0; i < selection.rangeCount; i++) {
77
- const range = selection.getRangeAt(i);
78
- const rects = range.getClientRects();
79
- for (let j = 0; j < rects.length; j++) {
80
- const rect = rects[j];
81
- if (document.elementFromPoint(rect.left, rect.top) === element) {
82
- return;
83
- }
84
- }
85
- }
86
- }
87
- // Check if tagname is input or textarea
88
- if (element.tagName === "INPUT" || element.tagName === "TEXTAREA") {
89
- if (hasSelection || (!element.readOnly && !element.disabled)) {
90
- return;
91
- }
92
- }
93
-
94
- // hide default context menu
95
- event.preventDefault();
96
- }
97
- }
1
+ /*
2
+ _ __ _ __
3
+ | | / /___ _(_) /____
4
+ | | /| / / __ `/ / / ___/
5
+ | |/ |/ / /_/ / / (__ )
6
+ |__/|__/\__,_/_/_/____/
7
+ The electron alternative for Go
8
+ (c) Lea Anthony 2019-present
9
+ */
10
+
11
+ /* jshint esversion: 9 */
12
+
13
+ import {newRuntimeCallerWithID, objectNames} from "./runtime";
14
+ import {IsDebug} from "./system";
15
+
16
+ // setup
17
+ window.addEventListener('contextmenu', contextMenuHandler);
18
+
19
+ const call = newRuntimeCallerWithID(objectNames.ContextMenu, '');
20
+ const ContextMenuOpen = 0;
21
+
22
+ function openContextMenu(id, x, y, data) {
23
+ void call(ContextMenuOpen, {id, x, y, data});
24
+ }
25
+
26
+ function contextMenuHandler(event) {
27
+ // Check for custom context menu
28
+ let element = event.target;
29
+ let customContextMenu = window.getComputedStyle(element).getPropertyValue("--custom-contextmenu");
30
+ customContextMenu = customContextMenu ? customContextMenu.trim() : "";
31
+ if (customContextMenu) {
32
+ event.preventDefault();
33
+ let customContextMenuData = window.getComputedStyle(element).getPropertyValue("--custom-contextmenu-data");
34
+ openContextMenu(customContextMenu, event.clientX, event.clientY, customContextMenuData);
35
+ return
36
+ }
37
+
38
+ processDefaultContextMenu(event);
39
+ }
40
+
41
+
42
+ /*
43
+ --default-contextmenu: auto; (default) will show the default context menu if contentEditable is true OR text has been selected OR element is input or textarea
44
+ --default-contextmenu: show; will always show the default context menu
45
+ --default-contextmenu: hide; will always hide the default context menu
46
+
47
+ This rule is inherited like normal CSS rules, so nesting works as expected
48
+ */
49
+ function processDefaultContextMenu(event) {
50
+
51
+ // Debug builds always show the menu
52
+ if (IsDebug()) {
53
+ return;
54
+ }
55
+
56
+ // Process default context menu
57
+ const element = event.target;
58
+ const computedStyle = window.getComputedStyle(element);
59
+ const defaultContextMenuAction = computedStyle.getPropertyValue("--default-contextmenu").trim();
60
+ switch (defaultContextMenuAction) {
61
+ case "show":
62
+ return;
63
+ case "hide":
64
+ event.preventDefault();
65
+ return;
66
+ default:
67
+ // Check if contentEditable is true
68
+ if (element.isContentEditable) {
69
+ return;
70
+ }
71
+
72
+ // Check if text has been selected
73
+ const selection = window.getSelection();
74
+ const hasSelection = (selection.toString().length > 0)
75
+ if (hasSelection) {
76
+ for (let i = 0; i < selection.rangeCount; i++) {
77
+ const range = selection.getRangeAt(i);
78
+ const rects = range.getClientRects();
79
+ for (let j = 0; j < rects.length; j++) {
80
+ const rect = rects[j];
81
+ if (document.elementFromPoint(rect.left, rect.top) === element) {
82
+ return;
83
+ }
84
+ }
85
+ }
86
+ }
87
+ // Check if tagname is input or textarea
88
+ if (element.tagName === "INPUT" || element.tagName === "TEXTAREA") {
89
+ if (hasSelection || (!element.readOnly && !element.disabled)) {
90
+ return;
91
+ }
92
+ }
93
+
94
+ // hide default context menu
95
+ event.preventDefault();
96
+ }
97
+ }
package/src/dialogs.js CHANGED
@@ -1,200 +1,200 @@
1
- /*
2
- _ __ _ __
3
- | | / /___ _(_) /____
4
- | | /| / / __ `/ / / ___/
5
- | |/ |/ / /_/ / / (__ )
6
- |__/|__/\__,_/_/_/____/
7
- The electron alternative for Go
8
- (c) Lea Anthony 2019-present
9
- */
10
-
11
- /* jshint esversion: 9 */
12
-
13
- /**
14
- * @typedef {Object} OpenFileDialogOptions
15
- * @property {boolean} [CanChooseDirectories] - Indicates if directories can be chosen.
16
- * @property {boolean} [CanChooseFiles] - Indicates if files can be chosen.
17
- * @property {boolean} [CanCreateDirectories] - Indicates if directories can be created.
18
- * @property {boolean} [ShowHiddenFiles] - Indicates if hidden files should be shown.
19
- * @property {boolean} [ResolvesAliases] - Indicates if aliases should be resolved.
20
- * @property {boolean} [AllowsMultipleSelection] - Indicates if multiple selection is allowed.
21
- * @property {boolean} [HideExtension] - Indicates if the extension should be hidden.
22
- * @property {boolean} [CanSelectHiddenExtension] - Indicates if hidden extensions can be selected.
23
- * @property {boolean} [TreatsFilePackagesAsDirectories] - Indicates if file packages should be treated as directories.
24
- * @property {boolean} [AllowsOtherFiletypes] - Indicates if other file types are allowed.
25
- * @property {FileFilter[]} [Filters] - Array of file filters.
26
- * @property {string} [Title] - Title of the dialog.
27
- * @property {string} [Message] - Message to show in the dialog.
28
- * @property {string} [ButtonText] - Text to display on the button.
29
- * @property {string} [Directory] - Directory to open in the dialog.
30
- * @property {boolean} [Detached] - Indicates if the dialog should appear detached from the main window.
31
- */
32
-
33
-
34
- /**
35
- * @typedef {Object} SaveFileDialogOptions
36
- * @property {string} [Filename] - Default filename to use in the dialog.
37
- * @property {boolean} [CanChooseDirectories] - Indicates if directories can be chosen.
38
- * @property {boolean} [CanChooseFiles] - Indicates if files can be chosen.
39
- * @property {boolean} [CanCreateDirectories] - Indicates if directories can be created.
40
- * @property {boolean} [ShowHiddenFiles] - Indicates if hidden files should be shown.
41
- * @property {boolean} [ResolvesAliases] - Indicates if aliases should be resolved.
42
- * @property {boolean} [AllowsMultipleSelection] - Indicates if multiple selection is allowed.
43
- * @property {boolean} [HideExtension] - Indicates if the extension should be hidden.
44
- * @property {boolean} [CanSelectHiddenExtension] - Indicates if hidden extensions can be selected.
45
- * @property {boolean} [TreatsFilePackagesAsDirectories] - Indicates if file packages should be treated as directories.
46
- * @property {boolean} [AllowsOtherFiletypes] - Indicates if other file types are allowed.
47
- * @property {FileFilter[]} [Filters] - Array of file filters.
48
- * @property {string} [Title] - Title of the dialog.
49
- * @property {string} [Message] - Message to show in the dialog.
50
- * @property {string} [ButtonText] - Text to display on the button.
51
- * @property {string} [Directory] - Directory to open in the dialog.
52
- * @property {boolean} [Detached] - Indicates if the dialog should appear detached from the main window.
53
- */
54
-
55
- /**
56
- * @typedef {Object} MessageDialogOptions
57
- * @property {string} [Title] - The title of the dialog window.
58
- * @property {string} [Message] - The main message to show in the dialog.
59
- * @property {Button[]} [Buttons] - Array of button options to show in the dialog.
60
- * @property {boolean} [Detached] - True if the dialog should appear detached from the main window (if applicable).
61
- */
62
-
63
- /**
64
- * @typedef {Object} Button
65
- * @property {string} [Label] - Text that appears within the button.
66
- * @property {boolean} [IsCancel] - True if the button should cancel an operation when clicked.
67
- * @property {boolean} [IsDefault] - True if the button should be the default action when the user presses enter.
68
- */
69
-
70
- /**
71
- * @typedef {Object} FileFilter
72
- * @property {string} [DisplayName] - Display name for the filter, it could be "Text Files", "Images" etc.
73
- * @property {string} [Pattern] - Pattern to match for the filter, e.g. "*.txt;*.md" for text markdown files.
74
- */
75
-
76
- // setup
77
- window._wails = window._wails || {};
78
- window._wails.dialogErrorCallback = dialogErrorCallback;
79
- window._wails.dialogResultCallback = dialogResultCallback;
80
-
81
- import {newRuntimeCallerWithID, objectNames} from "./runtime";
82
-
83
- import { nanoid } from 'nanoid/non-secure';
84
-
85
- // Define constants from the `methods` object in Title Case
86
- const DialogInfo = 0;
87
- const DialogWarning = 1;
88
- const DialogError = 2;
89
- const DialogQuestion = 3;
90
- const DialogOpenFile = 4;
91
- const DialogSaveFile = 5;
92
-
93
- const call = newRuntimeCallerWithID(objectNames.Dialog, '');
94
- const dialogResponses = new Map();
95
-
96
- /**
97
- * Generates a unique id that is not present in dialogResponses.
98
- * @returns {string} unique id
99
- */
100
- function generateID() {
101
- let result;
102
- do {
103
- result = nanoid();
104
- } while (dialogResponses.has(result));
105
- return result;
106
- }
107
-
108
- /**
109
- * Shows a dialog of specified type with the given options.
110
- * @param {number} type - type of dialog
111
- * @param {MessageDialogOptions|OpenFileDialogOptions|SaveFileDialogOptions} options - options for the dialog
112
- * @returns {Promise} promise that resolves with result of dialog
113
- */
114
- function dialog(type, options = {}) {
115
- const id = generateID();
116
- options["dialog-id"] = id;
117
- return new Promise((resolve, reject) => {
118
- dialogResponses.set(id, {resolve, reject});
119
- call(type, options).catch((error) => {
120
- reject(error);
121
- dialogResponses.delete(id);
122
- });
123
- });
124
- }
125
-
126
- /**
127
- * Handles the callback from a dialog.
128
- *
129
- * @param {string} id - The ID of the dialog response.
130
- * @param {string} data - The data received from the dialog.
131
- * @param {boolean} isJSON - Flag indicating whether the data is in JSON format.
132
- *
133
- * @return {undefined}
134
- */
135
- function dialogResultCallback(id, data, isJSON) {
136
- let p = dialogResponses.get(id);
137
- if (p) {
138
- if (isJSON) {
139
- p.resolve(JSON.parse(data));
140
- } else {
141
- p.resolve(data);
142
- }
143
- dialogResponses.delete(id);
144
- }
145
- }
146
-
147
- /**
148
- * Callback function for handling errors in dialog.
149
- *
150
- * @param {string} id - The id of the dialog response.
151
- * @param {string} message - The error message.
152
- *
153
- * @return {void}
154
- */
155
- function dialogErrorCallback(id, message) {
156
- let p = dialogResponses.get(id);
157
- if (p) {
158
- p.reject(message);
159
- dialogResponses.delete(id);
160
- }
161
- }
162
-
163
-
164
- // Replace `methods` with constants in Title Case
165
-
166
- /**
167
- * @param {MessageDialogOptions} options - Dialog options
168
- * @returns {Promise<string>} - The label of the button pressed
169
- */
170
- export const Info = (options) => dialog(DialogInfo, options);
171
-
172
- /**
173
- * @param {MessageDialogOptions} options - Dialog options
174
- * @returns {Promise<string>} - The label of the button pressed
175
- */
176
- export const Warning = (options) => dialog(DialogWarning, options);
177
-
178
- /**
179
- * @param {MessageDialogOptions} options - Dialog options
180
- * @returns {Promise<string>} - The label of the button pressed
181
- */
182
- export const Error = (options) => dialog(DialogError, options);
183
-
184
- /**
185
- * @param {MessageDialogOptions} options - Dialog options
186
- * @returns {Promise<string>} - The label of the button pressed
187
- */
188
- export const Question = (options) => dialog(DialogQuestion, options);
189
-
190
- /**
191
- * @param {OpenFileDialogOptions} options - Dialog options
192
- * @returns {Promise<string[]|string>} Returns selected file or list of files. Returns blank string if no file is selected.
193
- */
194
- export const OpenFile = (options) => dialog(DialogOpenFile, options);
195
-
196
- /**
197
- * @param {SaveFileDialogOptions} options - Dialog options
198
- * @returns {Promise<string>} Returns the selected file. Returns blank string if no file is selected.
199
- */
200
- export const SaveFile = (options) => dialog(DialogSaveFile, options);
1
+ /*
2
+ _ __ _ __
3
+ | | / /___ _(_) /____
4
+ | | /| / / __ `/ / / ___/
5
+ | |/ |/ / /_/ / / (__ )
6
+ |__/|__/\__,_/_/_/____/
7
+ The electron alternative for Go
8
+ (c) Lea Anthony 2019-present
9
+ */
10
+
11
+ /* jshint esversion: 9 */
12
+
13
+ /**
14
+ * @typedef {Object} OpenFileDialogOptions
15
+ * @property {boolean} [CanChooseDirectories] - Indicates if directories can be chosen.
16
+ * @property {boolean} [CanChooseFiles] - Indicates if files can be chosen.
17
+ * @property {boolean} [CanCreateDirectories] - Indicates if directories can be created.
18
+ * @property {boolean} [ShowHiddenFiles] - Indicates if hidden files should be shown.
19
+ * @property {boolean} [ResolvesAliases] - Indicates if aliases should be resolved.
20
+ * @property {boolean} [AllowsMultipleSelection] - Indicates if multiple selection is allowed.
21
+ * @property {boolean} [HideExtension] - Indicates if the extension should be hidden.
22
+ * @property {boolean} [CanSelectHiddenExtension] - Indicates if hidden extensions can be selected.
23
+ * @property {boolean} [TreatsFilePackagesAsDirectories] - Indicates if file packages should be treated as directories.
24
+ * @property {boolean} [AllowsOtherFiletypes] - Indicates if other file types are allowed.
25
+ * @property {FileFilter[]} [Filters] - Array of file filters.
26
+ * @property {string} [Title] - Title of the dialog.
27
+ * @property {string} [Message] - Message to show in the dialog.
28
+ * @property {string} [ButtonText] - Text to display on the button.
29
+ * @property {string} [Directory] - Directory to open in the dialog.
30
+ * @property {boolean} [Detached] - Indicates if the dialog should appear detached from the main window.
31
+ */
32
+
33
+
34
+ /**
35
+ * @typedef {Object} SaveFileDialogOptions
36
+ * @property {string} [Filename] - Default filename to use in the dialog.
37
+ * @property {boolean} [CanChooseDirectories] - Indicates if directories can be chosen.
38
+ * @property {boolean} [CanChooseFiles] - Indicates if files can be chosen.
39
+ * @property {boolean} [CanCreateDirectories] - Indicates if directories can be created.
40
+ * @property {boolean} [ShowHiddenFiles] - Indicates if hidden files should be shown.
41
+ * @property {boolean} [ResolvesAliases] - Indicates if aliases should be resolved.
42
+ * @property {boolean} [AllowsMultipleSelection] - Indicates if multiple selection is allowed.
43
+ * @property {boolean} [HideExtension] - Indicates if the extension should be hidden.
44
+ * @property {boolean} [CanSelectHiddenExtension] - Indicates if hidden extensions can be selected.
45
+ * @property {boolean} [TreatsFilePackagesAsDirectories] - Indicates if file packages should be treated as directories.
46
+ * @property {boolean} [AllowsOtherFiletypes] - Indicates if other file types are allowed.
47
+ * @property {FileFilter[]} [Filters] - Array of file filters.
48
+ * @property {string} [Title] - Title of the dialog.
49
+ * @property {string} [Message] - Message to show in the dialog.
50
+ * @property {string} [ButtonText] - Text to display on the button.
51
+ * @property {string} [Directory] - Directory to open in the dialog.
52
+ * @property {boolean} [Detached] - Indicates if the dialog should appear detached from the main window.
53
+ */
54
+
55
+ /**
56
+ * @typedef {Object} MessageDialogOptions
57
+ * @property {string} [Title] - The title of the dialog window.
58
+ * @property {string} [Message] - The main message to show in the dialog.
59
+ * @property {Button[]} [Buttons] - Array of button options to show in the dialog.
60
+ * @property {boolean} [Detached] - True if the dialog should appear detached from the main window (if applicable).
61
+ */
62
+
63
+ /**
64
+ * @typedef {Object} Button
65
+ * @property {string} [Label] - Text that appears within the button.
66
+ * @property {boolean} [IsCancel] - True if the button should cancel an operation when clicked.
67
+ * @property {boolean} [IsDefault] - True if the button should be the default action when the user presses enter.
68
+ */
69
+
70
+ /**
71
+ * @typedef {Object} FileFilter
72
+ * @property {string} [DisplayName] - Display name for the filter, it could be "Text Files", "Images" etc.
73
+ * @property {string} [Pattern] - Pattern to match for the filter, e.g. "*.txt;*.md" for text markdown files.
74
+ */
75
+
76
+ // setup
77
+ window._wails = window._wails || {};
78
+ window._wails.dialogErrorCallback = dialogErrorCallback;
79
+ window._wails.dialogResultCallback = dialogResultCallback;
80
+
81
+ import {newRuntimeCallerWithID, objectNames} from "./runtime";
82
+
83
+ import { nanoid } from 'nanoid/non-secure';
84
+
85
+ // Define constants from the `methods` object in Title Case
86
+ const DialogInfo = 0;
87
+ const DialogWarning = 1;
88
+ const DialogError = 2;
89
+ const DialogQuestion = 3;
90
+ const DialogOpenFile = 4;
91
+ const DialogSaveFile = 5;
92
+
93
+ const call = newRuntimeCallerWithID(objectNames.Dialog, '');
94
+ const dialogResponses = new Map();
95
+
96
+ /**
97
+ * Generates a unique id that is not present in dialogResponses.
98
+ * @returns {string} unique id
99
+ */
100
+ function generateID() {
101
+ let result;
102
+ do {
103
+ result = nanoid();
104
+ } while (dialogResponses.has(result));
105
+ return result;
106
+ }
107
+
108
+ /**
109
+ * Shows a dialog of specified type with the given options.
110
+ * @param {number} type - type of dialog
111
+ * @param {MessageDialogOptions|OpenFileDialogOptions|SaveFileDialogOptions} options - options for the dialog
112
+ * @returns {Promise} promise that resolves with result of dialog
113
+ */
114
+ function dialog(type, options = {}) {
115
+ const id = generateID();
116
+ options["dialog-id"] = id;
117
+ return new Promise((resolve, reject) => {
118
+ dialogResponses.set(id, {resolve, reject});
119
+ call(type, options).catch((error) => {
120
+ reject(error);
121
+ dialogResponses.delete(id);
122
+ });
123
+ });
124
+ }
125
+
126
+ /**
127
+ * Handles the callback from a dialog.
128
+ *
129
+ * @param {string} id - The ID of the dialog response.
130
+ * @param {string} data - The data received from the dialog.
131
+ * @param {boolean} isJSON - Flag indicating whether the data is in JSON format.
132
+ *
133
+ * @return {undefined}
134
+ */
135
+ function dialogResultCallback(id, data, isJSON) {
136
+ let p = dialogResponses.get(id);
137
+ if (p) {
138
+ if (isJSON) {
139
+ p.resolve(JSON.parse(data));
140
+ } else {
141
+ p.resolve(data);
142
+ }
143
+ dialogResponses.delete(id);
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Callback function for handling errors in dialog.
149
+ *
150
+ * @param {string} id - The id of the dialog response.
151
+ * @param {string} message - The error message.
152
+ *
153
+ * @return {void}
154
+ */
155
+ function dialogErrorCallback(id, message) {
156
+ let p = dialogResponses.get(id);
157
+ if (p) {
158
+ p.reject(message);
159
+ dialogResponses.delete(id);
160
+ }
161
+ }
162
+
163
+
164
+ // Replace `methods` with constants in Title Case
165
+
166
+ /**
167
+ * @param {MessageDialogOptions} options - Dialog options
168
+ * @returns {Promise<string>} - The label of the button pressed
169
+ */
170
+ export const Info = (options) => dialog(DialogInfo, options);
171
+
172
+ /**
173
+ * @param {MessageDialogOptions} options - Dialog options
174
+ * @returns {Promise<string>} - The label of the button pressed
175
+ */
176
+ export const Warning = (options) => dialog(DialogWarning, options);
177
+
178
+ /**
179
+ * @param {MessageDialogOptions} options - Dialog options
180
+ * @returns {Promise<string>} - The label of the button pressed
181
+ */
182
+ export const Error = (options) => dialog(DialogError, options);
183
+
184
+ /**
185
+ * @param {MessageDialogOptions} options - Dialog options
186
+ * @returns {Promise<string>} - The label of the button pressed
187
+ */
188
+ export const Question = (options) => dialog(DialogQuestion, options);
189
+
190
+ /**
191
+ * @param {OpenFileDialogOptions} options - Dialog options
192
+ * @returns {Promise<string[]|string>} Returns selected file or list of files. Returns blank string if no file is selected.
193
+ */
194
+ export const OpenFile = (options) => dialog(DialogOpenFile, options);
195
+
196
+ /**
197
+ * @param {SaveFileDialogOptions} options - Dialog options
198
+ * @returns {Promise<string>} Returns the selected file. Returns blank string if no file is selected.
199
+ */
200
+ export const SaveFile = (options) => dialog(DialogSaveFile, options);