Skip to content

Commit 27cbcee

Browse files
committed
improve ergonomics of resizing the output window
- reveal window when pressing the "Run" button - double clicking the resize bar will open the output window - close the output window when it's almost closed
1 parent 7d1aec0 commit 27cbcee

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

src/editor.ts

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ export default class ZlsClient extends LspClient {
3131
if (!data.stderr) {
3232
switch (data.params.type) {
3333
case 5:
34-
console.debug("ZLS --- " , data.params.message);
34+
console.debug("ZLS --- ", data.params.message);
3535
break;
3636
case 4:
37-
console.log("ZLS --- " , data.params.message);
37+
console.log("ZLS --- ", data.params.message);
3838
break;
3939
case 3:
40-
console.info("ZLS --- " , data.params.message);
40+
console.info("ZLS --- ", data.params.message);
4141
break;
4242
case 2:
43-
console.warn("ZLS --- " , data.params.message);
43+
console.warn("ZLS --- ", data.params.message);
4444
break;
4545
case 1:
46-
console.error("ZLS --- " , data.params.message);
46+
console.error("ZLS --- ", data.params.message);
4747
break;
4848
default:
4949
console.error(data.params.message);
@@ -90,17 +90,21 @@ let editor = (async () => {
9090
return editor;
9191
})();
9292

93-
function scrollOutputToEnd() {
93+
function revealOutputWindow() {
9494
const outputs = document.getElementById("output")!;
9595
outputs.scrollTo(0, outputs.scrollHeight!);
96+
const editorHeightPercent = parseFloat(splitPane.style.getPropertyValue("--editor-height-percent"));
97+
if (editorHeightPercent == 100) {
98+
splitPane.style.setProperty("--editor-height-percent", `${resizeBarPreviousSize}%`);
99+
}
96100
}
97101

98102
let zigWorker = new ZigWorker();
99103

100104
zigWorker.onmessage = ev => {
101105
if (ev.data.stderr) {
102106
document.querySelector(".zig-output:last-child")!.textContent += ev.data.stderr;
103-
scrollOutputToEnd();
107+
revealOutputWindow();
104108
return;
105109
} else if (ev.data.failed) {
106110
const outputSplit = document.createElement("div");
@@ -113,13 +117,13 @@ zigWorker.onmessage = ev => {
113117
zigOutput.classList.add("runner-output");
114118
zigOutput.classList.add("latest");
115119
document.getElementById("output")!.appendChild(zigOutput);
116-
117-
runnerWorker.postMessage({run: ev.data.compiled});
120+
121+
runnerWorker.postMessage({ run: ev.data.compiled });
118122

119123
runnerWorker.onmessage = rev => {
120124
if (rev.data.stderr) {
121125
document.querySelector(".runner-output:last-child")!.textContent += rev.data.stderr;
122-
scrollOutputToEnd();
126+
revealOutputWindow();
123127
return;
124128
} else if (rev.data.done) {
125129
runnerWorker.terminate();
@@ -133,16 +137,7 @@ zigWorker.onmessage = ev => {
133137

134138
const splitPane = document.getElementById("split-pane")! as HTMLDivElement;
135139
const resizeBar = document.getElementById("resize-bar")! as HTMLDivElement;
136-
137-
function clamp(value, min, max) {
138-
if (value < min) {
139-
return min;
140-
} else if (value > max) {
141-
return max;
142-
} else {
143-
return value;
144-
}
145-
}
140+
let resizeBarPreviousSize = 70;
146141

147142
let resizing = false;
148143
resizeBar.addEventListener("mousedown", event => {
@@ -154,14 +149,29 @@ resizeBar.addEventListener("mousedown", event => {
154149
});
155150
window.addEventListener("mousemove", event => {
156151
if (resizing) {
157-
const percent = clamp(event.clientY / splitPane.getBoundingClientRect().height * 100, 40, 100);
152+
const percent = Math.min(Math.max(10, event.clientY / splitPane.getBoundingClientRect().height * 100), 100);
158153
splitPane.style.setProperty("--editor-height-percent", `${percent}%`);
159154
}
160155
});
161156
window.addEventListener("mouseup", event => {
162157
resizing = false;
163158
document.body.style.removeProperty("user-select");
164159
document.body.style.removeProperty("cursor");
160+
161+
// fully close the output window when it's almost closed
162+
const editorHeightPercent = parseFloat(splitPane.style.getPropertyValue("--editor-height-percent"));
163+
if (editorHeightPercent >= 90) {
164+
splitPane.style.setProperty("--editor-height-percent", "100%");
165+
}
166+
});
167+
resizeBar.addEventListener("dblclick", event => {
168+
const editorHeightPercent = parseFloat(splitPane.style.getPropertyValue("--editor-height-percent"));
169+
if (editorHeightPercent == 100) {
170+
splitPane.style.setProperty("--editor-height-percent", `${resizeBarPreviousSize}%`);
171+
} else {
172+
resizeBarPreviousSize = editorHeightPercent;
173+
splitPane.style.setProperty("--editor-height-percent", `100%`);
174+
}
165175
});
166176

167177
const outputsRun = document.getElementById("run")! as HTMLButtonElement;
@@ -177,6 +187,7 @@ outputsRun.addEventListener("click", async () => {
177187
zigOutput.classList.add("zig-output");
178188
zigOutput.classList.add("latest");
179189
document.getElementById("output")!.appendChild(zigOutput);
190+
revealOutputWindow();
180191

181192
zigWorker.postMessage({
182193
run: (await editor).state.doc.toString(),

0 commit comments

Comments
 (0)