fix: assorted UX improvements

This commit is contained in:
zaaarf 2024-10-10 00:35:26 +02:00
parent 7507544fcb
commit 70aeca3d17
No known key found for this signature in database
GPG key ID: C91CFF9E2262BBA1
5 changed files with 65 additions and 107 deletions

View file

@ -1,85 +0,0 @@
package mp.code.intellij.actions.buffer;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import mp.code.BufferController;
import mp.code.data.TextChange;
import mp.code.exceptions.ControllerException;
import mp.code.intellij.CodeMP;
import mp.code.intellij.util.FileUtil;
import mp.code.intellij.util.InteractionUtil;
import mp.code.intellij.util.cb.BufferCallback;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.Optional;
import java.util.OptionalLong;
public class BufferShareAction extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
Project proj = e.getProject();
FileEditor currentEditor = FileEditorManager.getInstance(proj).getSelectedEditor();
if(currentEditor == null) {
Messages.showErrorDialog(
"No file is currently open!",
"CodeMP Buffer Share"
);
return;
}
String path = FileUtil.getRelativePath(proj, currentEditor.getFile());
if(path == null) {
Messages.showErrorDialog(
"File must belong to project!",
"CodeMP Buffer Share"
);
return;
}
InteractionUtil.createBuffer(proj, path);
Optional<BufferController> controller = InteractionUtil.bufferAttach(proj, CodeMP.getActiveWorkspace(), path);
if(controller.isEmpty()) {
Messages.showErrorDialog(
"An unknown error has occurred!",
"CodeMP Buffer Share"
);
return;
}
try {
controller.get().send(new TextChange(
0,
0,
new String(currentEditor.getFile().contentsToByteArray()),
OptionalLong.empty()
));
ApplicationManager.getApplication().runWriteAction(() -> {
try {
FileUtil.getAndRegisterBufferEquivalent(this, proj, controller.get());
} catch(ControllerException | IOException ex) {
throw new RuntimeException(ex);
} catch(UnsupportedOperationException ignored) {}
});
controller.get().callback(buf -> new BufferCallback(proj).accept(buf));
} catch(ControllerException | IOException ex) {
throw new RuntimeException(ex);
}
}
@Override
public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabled(CodeMP.isInWorkspace());
}
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.EDT;
}
}

View file

@ -21,15 +21,15 @@ public class BufferSyncAction extends AnAction {
public void actionPerformed(@NotNull AnActionEvent e) {
// TODO if current buffer is managed, sync that instead of making user choose
String[] active_buffers = CodeMP.getActiveWorkspace().activeBuffers();
String[] activeBuffers = CodeMP.getActiveWorkspace().activeBuffers();
int choice = Messages.showChooseDialog(
"Sync which buffer?",
"CodeMP Buffer Detach",
active_buffers,
activeBuffers,
"",
Messages.getQuestionIcon()
);
String path = active_buffers[choice];
String path = activeBuffers[choice];
Optional<BufferController> controller = CodeMP.getActiveWorkspace().getBuffer(path);
if (controller.isEmpty()) {

View file

@ -3,20 +3,17 @@ package mp.code.intellij.actions.workspace;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages;
import mp.code.intellij.CodeMP;
import mp.code.intellij.util.InteractionUtil;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public class WorkspaceLeaveAction extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
String workspaceId = Messages.showInputDialog(
"Workspace to leave:",
"CodeMP Workspace Leave",
Messages.getQuestionIcon());
InteractionUtil.leaveWorkspace(e.getProject(), workspaceId, null);
String workspaceId = CodeMP.getActiveWorkspace().getWorkspaceId();
InteractionUtil.leaveWorkspace(Objects.requireNonNull(e.getProject()), workspaceId, null);
}
@Override

View file

@ -1,11 +1,17 @@
package mp.code.intellij.ui;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.ui.components.JBList;
import com.intellij.ui.treeStructure.Tree;
import mp.code.BufferController;
import mp.code.Workspace;
import mp.code.data.TextChange;
import mp.code.exceptions.ControllerException;
import mp.code.intellij.CodeMP;
import mp.code.intellij.util.FileUtil;
@ -19,7 +25,10 @@ import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalLong;
public class CodeMPToolPanel extends JPanel {
public CodeMPToolPanel(Project project) {
@ -64,21 +73,60 @@ public class CodeMPToolPanel extends JPanel {
}
case JOINED -> {
this.setLayout(new BorderLayout(1, 0));
JButton createButton = new JButton(new AbstractAction("Create buffer") {
this.add(new JButton(new AbstractAction("Share buffer") {
@Override
public void actionPerformed(ActionEvent e) {
String bufferPath = Messages.showInputDialog(
"Name of buffer:",
"CodeMP Buffer Create",
Messages.getQuestionIcon()
);
Project proj = Objects.requireNonNull(project);
FileEditor currentEditor = FileEditorManager.getInstance(proj).getSelectedEditor();
if(currentEditor == null) {
Messages.showErrorDialog(
"No file is currently open!",
"CodeMP Buffer Share"
);
return;
}
InteractionUtil.createBuffer(project, bufferPath);
String path = FileUtil.getRelativePath(proj, currentEditor.getFile());
if(path == null) {
Messages.showErrorDialog(
"File must belong to project!",
"CodeMP Buffer Share"
);
return;
}
InteractionUtil.createBuffer(proj, path);
CodeMPToolPanel.this.redraw(project);
Optional<BufferController> controller = InteractionUtil.bufferAttach(proj, CodeMP.getActiveWorkspace(), path);
if(controller.isEmpty()) {
Messages.showErrorDialog(
"An unknown error has occurred!",
"CodeMP Buffer Share"
);
return;
}
try {
Editor ed = ((TextEditor) currentEditor).getEditor();
controller.get().send(new TextChange(
0,
0,
ed.getDocument().getText(),
OptionalLong.empty()
));
ApplicationManager.getApplication().runWriteAction(() -> {
try {
FileUtil.getAndRegisterBufferEquivalent(this, proj, controller.get());
} catch(ControllerException | IOException ex) {
throw new RuntimeException(ex);
} catch(UnsupportedOperationException ignored) {}
});
controller.get().callback(buf -> new BufferCallback(proj).accept(buf));
} catch(ControllerException ex) {
throw new RuntimeException(ex);
}
}
});
// createButton.setSize(createButton.getPreferredSize());
this.add(createButton, BorderLayout.NORTH);
}), BorderLayout.NORTH);
Workspace ws = CodeMP.getActiveWorkspace();
JTree tree = drawTree(ws.getWorkspaceId(), ws.getFileTree(Optional.empty(), false));

View file

@ -25,8 +25,6 @@
text="Leave Workspace"/>
</group>
<group id="codemp.buffer" text="Buffer" popup="true">
<action id="codemp.buffer.share" class="mp.code.intellij.actions.buffer.BufferShareAction"
text="Share Current Buffer"/>
<action id="codemp.buffer.attach" class="mp.code.intellij.actions.buffer.BufferAttachAction"
text="Attach to Remote Buffer"/>
<action id="codemp.buffer.detach" class="mp.code.intellij.actions.buffer.BufferDetachAction"