Skip to content

Commit 7375cb7

Browse files
committed
dev: add .undefinedOnCancel to open dialog too
1 parent 5522cfb commit 7375cb7

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/gui/src/IPC.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,12 @@ const ipc_listener = async (event, handled) => {
437437
path: '/' + window.user.username + '/Desktop',
438438
// this is the uuid of the window to which this dialog will return
439439
parent_uuid: event.data.appInstanceID,
440+
onDialogCancel: () => {
441+
target_iframe.contentWindow.postMessage({
442+
msg: "fileOpenCancelled",
443+
original_msg_id: msg_id,
444+
}, '*');
445+
},
440446
show_maximize_button: false,
441447
show_minimize_button: false,
442448
title: 'Open',

src/puter-js/src/modules/UI.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import EventListener from '../lib/EventListener.js';
44
import putility from '@heyputer/putility';
55

66
const FILE_SAVE_CANCELLED = Symbol('FILE_SAVE_CANCELLED');
7+
const FILE_OPEN_CANCELLED = Symbol('FILE_OPEN_CANCELLED');
78

89
// AppConnection provides an API for interacting with another app.
910
// It's returned by UI methods, and cannot be constructed directly by user code.
@@ -474,6 +475,10 @@ class UI extends EventListener {
474475
// execute callback
475476
this.#callbackFunctions[e.data.original_msg_id](FILE_SAVE_CANCELLED);
476477
}
478+
else if(e.data.msg === "fileOpenCancelled"){
479+
// execute callback
480+
this.#callbackFunctions[e.data.original_msg_id](FILE_OPEN_CANCELLED);
481+
}
477482
else{
478483
// execute callback
479484
this.#callbackFunctions[e.data.original_msg_id](e.data);
@@ -691,7 +696,8 @@ class UI extends EventListener {
691696
}
692697

693698
showOpenFilePicker = function(options, callback){
694-
return new Promise((resolve, reject) => {
699+
const undefinedOnCancel = new putility.libs.promise.TeePromise();
700+
const resolveOnlyPromise = new Promise((resolve, reject) => {
695701
if (!globalThis.open) {
696702
return reject("This API is not compatible in Web Workers.");
697703
}
@@ -716,8 +722,18 @@ class UI extends EventListener {
716722
'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
717723
}
718724
//register callback
719-
this.#callbackFunctions[msg_id] = resolve;
725+
this.#callbackFunctions[msg_id] = (maybe_result) => {
726+
// Only resolve cancel events if this was called with `.undefinedOnCancel`
727+
if ( maybe_result === FILE_OPEN_CANCELLED ) {
728+
undefinedOnCancel.resolve(undefined);
729+
return;
730+
}
731+
undefinedOnCancel.resolve(maybe_result);
732+
resolve(maybe_result);
733+
};
720734
})
735+
resolveOnlyPromise.undefinedOnCancel = undefinedOnCancel;
736+
return resolveOnlyPromise;
721737
}
722738

723739
showFontPicker = function(options){

0 commit comments

Comments
 (0)