Skip to content

Commit

Permalink
external FS dialog provider and confirmations
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillp committed Oct 10, 2024
1 parent f4872b8 commit 91dd354
Show file tree
Hide file tree
Showing 20 changed files with 225 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public JsITextModel getRightModel() {
}

@Override
public void setExternalDialogProvider(JsExternalDialogProvider opener) {}
public void setExternalDialogProvider(JsDialogProvider opener) {}

public static Promise<JsFileDiffView> newDiff(EditArgs arguments) {
return JsLauncher.start(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void setTheme(JSObject jsTheme) {
}

@Override
public void setExternalDialogProvider(JsExternalDialogProvider opener) {}
public void setExternalDialogProvider(JsDialogProvider opener) {}

public static Promise<JsIFolderDiffView> newDiff(EditArgs arguments) {
return JsLauncher.start(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public JsDisposable onControllerUpdate(
}

@Override
public void setExternalDialogProvider(JsExternalDialogProvider opener) {}
public void setExternalDialogProvider(JsDialogProvider opener) {}

static Function<SceneApi, Scene> sf(Channel channel) {
return api -> new RemoteFileDiffScene(api, channel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ public JsDisposable onControllerUpdate(
}

@Override
public void setExternalDialogProvider(JsExternalDialogProvider opener) {}
public void setExternalDialogProvider(JsDialogProvider provider) {
folderDiff.dialogProvider = provider;
}

private FolderDiffRootView rootView() {
return folderDiff.rootView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import org.sudu.experiments.diff.folder.RemoteFolderDiffModel;
import org.sudu.experiments.editor.EditorWindow;
import org.sudu.experiments.editor.ui.colors.EditorColorScheme;
import org.sudu.experiments.esm.JsDialogProvider;
import org.sudu.experiments.esm.JsExternalFileOpener;
import org.sudu.experiments.esm.dlg.FsDialogs;
import org.sudu.experiments.js.JsArray;
import org.sudu.experiments.js.JsMemoryAccess;
import org.sudu.experiments.math.ArrayOp;
Expand Down Expand Up @@ -66,6 +68,7 @@ public class RemoteFolderDiffWindow extends ToolWindow0 {
new Subscribers<>(new ViewEventListener[0]);

JsExternalFileOpener opener;
JsDialogProvider dialogProvider;

public RemoteFolderDiffWindow(
EditorColorScheme theme,
Expand Down Expand Up @@ -274,7 +277,7 @@ protected void updateDiffInfo() {
rootView.left.model(),
rootView.right.model()
));
rootView.setMergeButtons(this::sendApplyDiff);
rootView.setMergeButtons(this::askApplyDiff);
window.context.window.repaint();
}

Expand Down Expand Up @@ -504,6 +507,18 @@ private void sendOpenFile(RemoteFileNode node, boolean left) {
keyCnt = (keyCnt + 1) % MAP_SIZE;
}

private void askApplyDiff(FolderDiffModel model, boolean left) {
if (dialogProvider != null) {
RemoteFolderDiffModel remoteModel = (RemoteFolderDiffModel) model;
String fromPath = remoteModel.getFullPath(left ? leftRoot.name() : rightRoot.name());
String toPath = remoteModel.getFullPath(!left ? leftRoot.name() : rightRoot.name());
FsDialogs.showDlg(dialogProvider, fromPath, toPath,
() -> sendApplyDiff(model, left));
} else {
sendApplyDiff(model, left);
}
}

private void sendApplyDiff(FolderDiffModel model, boolean left) {
int[] path = model.getPathFromRoot();
var result = JsArray.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,5 +346,5 @@ public static Promise<JsIEditorView> newEdit(EditArgs arguments) {
}

@Override
public void setExternalDialogProvider(JsExternalDialogProvider opener) {}
public void setExternalDialogProvider(JsDialogProvider opener) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.sudu.experiments.esm;

import org.sudu.experiments.esm.dlg.JsDialogInput;
import org.sudu.experiments.esm.dlg.JsDialogResult;
import org.sudu.experiments.js.Promise;
import org.teavm.jso.JSObject;

// export interface ExternalDialogProvider
public interface JsDialogProvider extends JSObject {
// showModalDialog(input: DialogInput): Promise<DialogResult | null>
Promise<JsDialogResult> showModalDialog(JsDialogInput input);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,5 @@ public static Promise<JsRemoteEditorView> create(EditArgs arguments, Channel cha
}

@Override
public void setExternalDialogProvider(JsExternalDialogProvider opener) {}
public void setExternalDialogProvider(JsDialogProvider opener) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ JsDisposable onControllerUpdate(
JsFunctions.Consumer<JsViewController> callback
);

void setExternalDialogProvider(JsExternalDialogProvider opener);
void setExternalDialogProvider(JsDialogProvider opener);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.sudu.experiments.esm.dlg;

import org.sudu.experiments.esm.JsDialogProvider;
import org.sudu.experiments.js.JsArray;
import org.sudu.experiments.js.JsHelper;

public class FsDialogs {
public static void showDlg(
JsDialogProvider p,
String from, String to,
Runnable action
) {
var bOk = JsNative.createButton("OK", true);
var bCancel = JsNative.createButton("cancel", false);
var i = JsNative.createInput(
"confirm file copy operation",
"from file " + from + " to " + to,
JsArray.create(),
JsHelper.toJsArray(bOk, bCancel)
);
p.showModalDialog(i).then(
result -> {
if (result.getButton() == bOk)
action.run();
}, e -> {}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.sudu.experiments.esm.dlg;

import org.sudu.experiments.js.JsHelper;
import org.teavm.jso.JSFunctor;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.core.JSString;

// export type DialogButton
public interface JsDialogButton extends JSObject {
// title: string
@JSProperty
JSString getTitle();

@JSProperty
boolean isDefault();

// isDefault?: boolean
static boolean isDefault(JsDialogButton button) {
return JsHelper.hasProperty(button, "default")
&& button.isDefault();
}

// isEnabled: (state: DialogState) => boolean
@JSProperty
IsEnabled getIsEnabled();

@JSFunctor
interface IsEnabled extends JSObject {
boolean get(JsDialogState state);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.sudu.experiments.esm.dlg;

import org.sudu.experiments.js.JsArray;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.core.JSString;

// export type DialogInput
public interface JsDialogInput extends JSObject {
// title: string
@JSProperty
JSString getTitle();

// text: string
@JSProperty
JSString getText();

// options: DialogOption[]
@JSProperty
JsArray<JsDialogOption> getOptions();

// buttons: DialogButton[]
@JSProperty
JsArray<JsDialogButton> getButtons();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.sudu.experiments.esm.dlg;

import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;

/*
export type DialogOption = {
title: string
isEnabled: boolean
}
*/

public interface JsDialogOption extends JSObject {
@JSProperty
JSObject getTitle();

@JSProperty
boolean getIsEnabled();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.sudu.experiments.esm.dlg;

import org.sudu.experiments.js.JsArray;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;

// export type DialogResult
public interface JsDialogResult extends JSObject {
// button: DialogButton
@JSProperty
JsDialogButton getButton();

// options: DialogOption[]
@JSProperty
JsArray<JsDialogOption> getOptions();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.sudu.experiments.esm.dlg;

import org.sudu.experiments.js.JsArray;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;

// export type DialogState
public interface JsDialogState extends JSObject {
// options: DialogOption[]
@JSProperty
JsArray<JsDialogOption> getOptions();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.sudu.experiments.esm.dlg;

import org.sudu.experiments.js.JsArray;
import org.teavm.jso.JSBody;

public class JsNative {
@JSBody(
params = {"title", "isDefault", "isEnabled"},
script = "return {title:title, isDefault:isDefault, isEnabled:isEnabled};"
)
public static native JsDialogButton createButton(
String title,
boolean isDefault,
JsDialogButton.IsEnabled isEnabled
);

@JSBody(
params = {"title", "isDefault"},
script =
"return {title:title, isDefault:isDefault, " +
"isEnabled: state => true };"
)
public static native JsDialogButton createButton(
String title,
boolean isDefault
);

@JSBody(
params = {"title", "isDefault"},
script = "return {title:title, isDefault:isDefault};"
)
public static native JsDialogOption createOption(
String title,
boolean isDefault
);

@JSBody(
params = "options",
script = "return {options:options};"
)
public static native JsDialogState createState(
JsArray<JsDialogOption> options
);

@JSBody(
params = {"title", "text", "options", "buttons"},
script = "return {title:title, text:text, options:options, buttons:buttons};"
)
public static native JsDialogInput createInput(
String title,
String text,
JsArray<JsDialogOption> options,
JsArray<JsDialogButton> buttons
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public Window addWindow(Window window) {
fireMouseLeave();
} else {
++lastMouseWindow;
System.out.println("lastMouseWindow = " + lastMouseWindow);
}
}
windows.add(0, window);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,9 @@ interface HTMLElement extends org.teavm.jso.dom.html.HTMLElement {
@NoSideEffects
public static native JSString typeof(JSObject a);

@JSBody(params = {"array"}, script = "return array;")
@NoSideEffects
public static native JsArray<JSObject> toJsArray(@JSByRef JSObject ... array);

@JSBody(params = {"a", "b"}, script = "return [a, b];")
@NoSideEffects
public static native JsArray<JSObject> toJsArray(JSObject a, JSObject b);
public static native <T extends JSObject> JsArray<T> toJsArray(T a, T b);

@JSBody(params = {"a", "b", "c"}, script = "return [a, b, c];")
@NoSideEffects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ public static native <T extends JSObject> Promise<T> create(
)
public static native Promise<JsArrayReader<JSObject>> all(JsArrayReader<?> iterable);

public static Promise<JsArrayReader<JSObject>> all(JSObject[] array) {
return all(JsHelper.toJsArray(array));
}

public static Promise<JsArrayReader<JSObject>> all(JSObject a, JSObject b) {
return all(JsHelper.toJsArray(a, b));
}
Expand Down

0 comments on commit 91dd354

Please sign in to comment.