From 783a36032c7a1a35162771168d6071a4cc142f55 Mon Sep 17 00:00:00 2001 From: insjang Date: Thu, 5 Nov 2026 15:36:27 +0900 Subject: [PATCH] Terminal: fix moving a terminal tab to another terminals view Dragging a terminal tab to another terminals view recreates the terminal controls in the new view. This failed in two ways: - The text line renderer of the new canvas measured the font on fCtlText, which at that point still is the canvas that was just disposed. The move stopped with "Widget is disposed", leaving the tab behind and both terminals blank. - The preferences were only applied to freshly created terminals, so a moved terminal fell back to the default colors and font. setupTerminal() now drops fCtlText with the old parent, so the renderer measures on the display until the new canvas exists. A moved terminal gets the colors and the font from the preferences (the defaults when it has no preference store), and keeps what was set on it alone: the inverted colors of its context menu, and the buffer line limit a client may have set through ITerminalViewControl. TerminalControlRecreateUITest recreates the controls in a new parent and checks the background color, the font, the inverted colors and the buffer line limit, and the inverted colors of a terminal made without a preference store; on master the tests fail with "Widget is disposed". It disposes the terminal afterwards, as the terminals view does when a tab is closed, and reads the background color through the renderer, whose getter is public. --- .../emulator/VT100TerminalControl.java | 20 ++- .../TerminalControlRecreateUITest.java | 121 ++++++++++++++++++ .../terminal/test/AutomatedTestSuite.java | 1 + 3 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/textcanvas/TerminalControlRecreateUITest.java diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100TerminalControl.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100TerminalControl.java index 5d4692115b0..78255c3f53c 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100TerminalControl.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100TerminalControl.java @@ -650,16 +650,30 @@ public void setupTerminal(Composite parent) { fClipboard.dispose(); fPollingTextCanvasModel.stopPolling(); } + // set on this terminal alone (Inverted colors in its menu): kept across a move + boolean inverted = fCtlText != null && fCtlText.isInvertedColors(); if (fWndParent != null && !fWndParent.isDisposed()) { // terminal widget gets a new parent (DnD) fWndParent.dispose(); } + // the old canvas, if any, went with its parent: the new one measures on the display + // until it exists + fCtlText = null; setupControls(parent); setCommandInputField(fCommandInputField); setupListeners(); - if (fPreferenceStore != null && wasDisposed) { - updatePreferences(null); - fPreferenceStore.addPropertyChangeListener(fPreferenceListener); + if (wasDisposed) { + if (fPreferenceStore != null) { + updatePreferences(null); + fPreferenceStore.addPropertyChangeListener(fPreferenceListener); + } + } else { + // moved: the new canvas needs the colors and the font (the defaults without a + // preference store), but what was set on this terminal stays, the buffer line + // limit included + onTerminalColorsChanged(); + onTerminalFontChanged(); + setInvertedColors(inverted); } JFaceResources.getFontRegistry().addListener(fFontListener); setupHelp(fWndParent, TerminalPlugin.HELP_VIEW); diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/textcanvas/TerminalControlRecreateUITest.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/textcanvas/TerminalControlRecreateUITest.java new file mode 100644 index 00000000000..4820462fdab --- /dev/null +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/textcanvas/TerminalControlRecreateUITest.java @@ -0,0 +1,121 @@ +/******************************************************************************* + * Copyright (c) 2026 Eclipse contributors and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + *******************************************************************************/ +package org.eclipse.terminal.internal.textcanvas; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.eclipse.jface.preference.PreferenceStore; +import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.terminal.connector.ITerminalConnector; +import org.eclipse.terminal.connector.TerminalState; +import org.eclipse.terminal.control.ITerminalListener; +import org.eclipse.terminal.control.TerminalTitleRequestor; +import org.eclipse.terminal.internal.emulator.VT100TerminalControl; +import org.eclipse.terminal.internal.preferences.ITerminalConstants; +import org.eclipse.terminal.model.TerminalColor; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests recreating the terminal controls in a new parent, as moving a terminal tab to another + * terminals view does. + */ +public class TerminalControlRecreateUITest { + + private static final RGB BACKGROUND = new RGB(1, 2, 3); + + private Display display; + private Shell shell; + private VT100TerminalControl terminal; + private ITerminalListener listener; + + @BeforeEach + public void createTerminal() { + display = Display.getCurrent() != null ? null : new Display(); + shell = new Shell(); + shell.setLayout(new FillLayout()); + PreferenceStore preferences = new PreferenceStore(); + preferences.setValue(ITerminalConstants.getPrefForTerminalColor(TerminalColor.BACKGROUND), + "1,2,3"); //$NON-NLS-1$ + preferences.setValue(ITerminalConstants.PREF_BUFFERLINES, 1000); + listener = new ITerminalListener() { + @Override + public void setState(TerminalState state) { + } + + @Override + public void setTerminalSelectionChanged() { + } + + @Override + public void setTerminalTitle(String title, TerminalTitleRequestor requestor) { + } + }; + terminal = new VT100TerminalControl(listener, new Composite(shell, 0), new ITerminalConnector[0], preferences); + } + + @AfterEach + public void dispose() { + // as the terminals view does when a terminal tab is closed + terminal.disposeTerminal(); + shell.dispose(); + if (display != null) { + display.dispose(); + } + } + + private void recreateInNewParent() { + terminal.setupTerminal(new Composite(shell, 0)); + } + + @Test + public void recreatingDoesNotUseTheDisposedCanvas() { + assertDoesNotThrow(this::recreateInNewParent); + } + + @Test + public void recreatedCanvasKeepsThePreferences() { + assertEquals(BACKGROUND, ((TextCanvas) terminal.getControl()).getCellRenderer().getDefaultBackgroundColor().getRGB()); + recreateInNewParent(); + assertEquals(BACKGROUND, ((TextCanvas) terminal.getControl()).getCellRenderer().getDefaultBackgroundColor().getRGB()); + // the font too: setting it measured on the disposed canvas and threw "Widget is disposed" + assertSame(JFaceResources.getTextFont(), terminal.getControl().getFont()); + } + + @Test + public void recreatedCanvasKeepsWhatWasSetOnThisTerminal() { + // set on this terminal, not in the preferences: a move must not reset them to the preferences + terminal.setInvertedColors(true); + terminal.setBufferLineLimit(123); + recreateInNewParent(); + assertTrue(terminal.isInvertedColors()); + assertEquals(123, terminal.getBufferLineLimit()); + } + + @Test + public void recreatedWithoutPreferencesKeepsTheInvertedColors() { + // a control made without a preference store, as TerminalViewControlFactory's default makes it + terminal.disposeTerminal(); + terminal = new VT100TerminalControl(listener, new Composite(shell, 0), new ITerminalConnector[0]); + terminal.setInvertedColors(true); + recreateInNewParent(); + assertTrue(terminal.isInvertedColors()); + } +} diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/test/AutomatedTestSuite.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/test/AutomatedTestSuite.java index 099e8196874..a6c8b0970ab 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/test/AutomatedTestSuite.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/test/AutomatedTestSuite.java @@ -26,6 +26,7 @@ org.eclipse.terminal.internal.textcanvas.TextCanvasModelTest.class, // org.eclipse.terminal.internal.connector.TerminalConnectorTest.class, // org.eclipse.terminal.internal.connector.TerminalToRemoteInjectionOutputStreamTest.class, // + org.eclipse.terminal.internal.textcanvas.TerminalControlRecreateUITest.class, // org.eclipse.terminal.view.ui.tests.TerminalsViewReorderTest.class, // }) public class AutomatedTestSuite {