-
Notifications
You must be signed in to change notification settings - Fork 231
Proper selection coloring in JFace Structured Viewers #3659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Christopher-Hermann
wants to merge
2
commits into
eclipse-platform:master
Choose a base branch
from
Christopher-Hermann:selection_coloring
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+351
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
...s/org.eclipse.jface/src/org/eclipse/jface/viewers/ColumnViewerSelectionColorListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2024 SAP SE. | ||
| * | ||
| * 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 | ||
| * | ||
| * Contributors: | ||
| * SAP SE - initial API and implementation | ||
| *******************************************************************************/ | ||
| package org.eclipse.jface.viewers; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| import org.eclipse.jface.resource.ColorRegistry; | ||
| import org.eclipse.jface.resource.JFaceResources; | ||
| import org.eclipse.swt.SWT; | ||
| import org.eclipse.swt.graphics.Color; | ||
| import org.eclipse.swt.graphics.GC; | ||
| import org.eclipse.swt.graphics.RGB; | ||
| import org.eclipse.swt.widgets.Control; | ||
| import org.eclipse.swt.widgets.Event; | ||
| import org.eclipse.swt.widgets.Listener; | ||
| import org.eclipse.swt.widgets.Scrollable; | ||
|
|
||
| /** | ||
| * EraseItem event listener that provides custom selection coloring for JFace | ||
| * viewers. This listener only activates when no custom owner draw label | ||
| * provider is registered, ensuring it doesn't conflict with existing custom | ||
| * drawing implementations. | ||
| * <p> | ||
| * The listener provides different colors for: | ||
| * <ul> | ||
| * <li>Selected items when the control has focus</li> | ||
| * <li>Selected items when the control doesn't have focus</li> | ||
| * </ul> | ||
| * </p> | ||
| * | ||
| * @see org.eclipse.jface.viewers.OwnerDrawLabelProvider | ||
| * @see org.eclipse.jface.viewers.StyledCellLabelProvider | ||
| * @see org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter | ||
| * @since 3.39 | ||
| */ | ||
| public class ColumnViewerSelectionColorListener implements Listener { | ||
|
|
||
| private static final String LISTENER_KEY = "org.eclipse.jface.viewers.selection_color_listener"; //$NON-NLS-1$ | ||
| private static final String OWNER_DRAW_LISTENER_KEY = "owner_draw_label_provider_listener"; //$NON-NLS-1$ | ||
|
|
||
| private static final String COLOR_SELECTION_BG_FOCUS = "org.eclipse.jface.SELECTION_BACKGROUND_FOCUSED"; //$NON-NLS-1$ | ||
| private static final String COLOR_SELECTION_FG_FOCUS = "org.eclipse.jface.SELECTION_FOREGROUND_FOCUSED"; //$NON-NLS-1$ | ||
| private static final String COLOR_SELECTION_BG_NO_FOCUS = "org.eclipse.jface.SELECTION_BACKGROUND_NO_FOCUS"; //$NON-NLS-1$ | ||
| private static final String COLOR_SELECTION_FG_NO_FOCUS = "org.eclipse.jface.SELECTION_FOREGROUND_NO_FOCUS"; //$NON-NLS-1$ | ||
|
|
||
| /** | ||
| * Registers the selection color listener on the given viewer. | ||
| * <p> | ||
| * This method is idempotent - calling it multiple times on the same viewer has | ||
| * no additional effect. | ||
| * </p> | ||
| * | ||
| * @param viewer the viewer to which the listener should be added | ||
| */ | ||
| public static void addListenerToViewer(StructuredViewer viewer) { | ||
| if ("gtk".equals(SWT.getPlatform())) { //$NON-NLS-1$ | ||
| return; // Skip on Linux | ||
| } | ||
|
|
||
| Control control = viewer.getControl(); | ||
| if (control.isDisposed() || isListenerRegistered(control)) { | ||
| return; // Already registered or disposed | ||
| } | ||
|
|
||
| ColumnViewerSelectionColorListener listener = new ColumnViewerSelectionColorListener(); | ||
| control.setData(LISTENER_KEY, listener); | ||
| control.addListener(SWT.EraseItem, listener); | ||
| } | ||
|
|
||
| private static boolean isListenerRegistered(Control control) { | ||
| return control.getData(LISTENER_KEY) != null; | ||
| } | ||
|
|
||
| @Override | ||
| public void handleEvent(Event event) { | ||
| if ((event.detail & SWT.SELECTED) == 0) { | ||
| return; // Not selected | ||
| } | ||
|
|
||
| if (event.widget instanceof Control control && !control.isEnabled()) { | ||
| return; // Disabled control | ||
| } | ||
|
|
||
| if (hasAdditionalEraseItemListeners(event)) { | ||
| return; // Let other listeners handle selection | ||
| } | ||
|
|
||
| drawSelection(event); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if additional EraseItem listeners were registered after this listener | ||
| * that are NOT the OwnerDrawListener. This allows user code to override the | ||
| * selection coloring by adding their own EraseItem listener, while still | ||
| * allowing StyledCellLabelProvider to work (which uses OwnerDrawListener but | ||
| * doesn't draw selection). | ||
| * | ||
| * @param event the erase event | ||
| * @return <code>true</code> if other custom listeners are present that should | ||
| * handle selection, <code>false</code> otherwise | ||
| */ | ||
| private boolean hasAdditionalEraseItemListeners(Event event) { | ||
| if (!(event.widget instanceof Control control)) { | ||
| return false; | ||
| } | ||
|
|
||
| Listener[] listeners = control.getListeners(SWT.EraseItem); | ||
| Object ownerDrawListener = control.getData(OWNER_DRAW_LISTENER_KEY); | ||
| return Arrays.stream(listeners).anyMatch(l -> l != this && l != ownerDrawListener); | ||
| } | ||
|
|
||
| /** | ||
| * Draws custom selection coloring for the given event. | ||
| * <p> | ||
| * This method provides consistent selection rendering across different viewers | ||
| * and owner draw implementations. It handles both focused and unfocused | ||
| * selection states using themed colors from the ColorRegistry with appropriate | ||
| * fallbacks. | ||
| * </p> | ||
| * | ||
| * @param event the erase event containing the widget, GC, and coordinates | ||
| * @since 3.32 | ||
| */ | ||
| public static void drawSelection(Event event) { | ||
| if ("gtk".equals(SWT.getPlatform())) { //$NON-NLS-1$ | ||
| return; // Skip on Linux | ||
| } | ||
|
|
||
| Control control = (Control) event.widget; | ||
| GC gc = event.gc; | ||
|
|
||
| Color backgroundColor; | ||
| Color foregroundColor; | ||
|
|
||
| if (control.isFocusControl()) { | ||
| backgroundColor = getSelectionColor(COLOR_SELECTION_BG_FOCUS, event.display); | ||
| foregroundColor = getSelectionColor(COLOR_SELECTION_FG_FOCUS, event.display); | ||
| } else { | ||
| backgroundColor = getSelectionColor(COLOR_SELECTION_BG_NO_FOCUS, event.display); | ||
| foregroundColor = getSelectionColor(COLOR_SELECTION_FG_NO_FOCUS, event.display); | ||
| } | ||
|
|
||
| gc.setBackground(backgroundColor); | ||
| gc.setForeground(foregroundColor); | ||
|
|
||
| int width = event.width; | ||
| if (event.widget instanceof Scrollable scrollable) { | ||
| width = scrollable.getClientArea().width; | ||
| } | ||
|
|
||
| gc.fillRectangle(0, event.y, width, event.height); | ||
|
|
||
| // Remove SELECTED and BACKGROUND flags to prevent native drawing from | ||
| // overwriting our custom colors | ||
| event.detail &= ~(SWT.SELECTED | SWT.BACKGROUND); | ||
| } | ||
|
|
||
| private static Color getSelectionColor(String key, org.eclipse.swt.graphics.Device device) { | ||
| ColorRegistry registry = JFaceResources.getColorRegistry(); | ||
|
|
||
| if (registry.hasValueFor(key)) { | ||
| return registry.get(key); | ||
| } | ||
|
|
||
| RGB systemColor; | ||
| switch (key) { | ||
| case COLOR_SELECTION_BG_FOCUS: | ||
| systemColor = device.getSystemColor(SWT.COLOR_TITLE_BACKGROUND).getRGB(); | ||
| break; | ||
| case COLOR_SELECTION_FG_FOCUS: | ||
| systemColor = device.getSystemColor(SWT.COLOR_WHITE).getRGB(); | ||
| break; | ||
| case COLOR_SELECTION_BG_NO_FOCUS: | ||
| systemColor = device.getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND).getRGB(); | ||
| break; | ||
| case COLOR_SELECTION_FG_NO_FOCUS: | ||
| systemColor = device.getSystemColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND).getRGB(); | ||
| break; | ||
| default: | ||
| systemColor = device.getSystemColor(SWT.COLOR_LIST_SELECTION).getRGB(); | ||
| break; | ||
| } | ||
|
|
||
| registry.put(key, systemColor); | ||
| return registry.get(key); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...workbench/eclipseui/org/eclipse/ui/internal/themes/ColumnViewerSelectionColorFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2024 SAP SE. | ||
| * | ||
| * 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 | ||
| * | ||
| * Contributors: | ||
| * SAP SE - initial API and implementation | ||
| *******************************************************************************/ | ||
| package org.eclipse.ui.internal.themes; | ||
|
|
||
| import java.util.Hashtable; | ||
| import org.eclipse.core.runtime.IConfigurationElement; | ||
| import org.eclipse.core.runtime.IExecutableExtension; | ||
| import org.eclipse.swt.SWT; | ||
| import org.eclipse.swt.graphics.RGB; | ||
| import org.eclipse.swt.widgets.Display; | ||
| import org.eclipse.ui.themes.IColorFactory; | ||
|
|
||
| /** | ||
| * Color factory for viewer selection colors that adapts to the OS/desktop | ||
| * theme. Provides default colors based on system colors for focused and | ||
| * unfocused selections. | ||
| * <p> | ||
| * The default colors are based on system title bar colors which automatically | ||
| * adapt to light/dark themes and high contrast modes. Themes can override these | ||
| * defaults to provide custom styling. | ||
| * </p> | ||
| * | ||
| * @since 3.39 | ||
| */ | ||
| public class ColumnViewerSelectionColorFactory implements IColorFactory, IExecutableExtension { | ||
|
|
||
| private String color = null; | ||
|
|
||
| @Override | ||
| public RGB createColor() { | ||
| Display display = Display.getDefault(); | ||
|
|
||
| if ("SELECTED_CELL_BACKGROUND".equals(color)) { //$NON-NLS-1$ | ||
| return display.getSystemColor(SWT.COLOR_TITLE_BACKGROUND).getRGB(); | ||
|
|
||
| } else if ("SELECTED_CELL_FOREGROUND".equals(color)) { //$NON-NLS-1$ | ||
| return display.getSystemColor(SWT.COLOR_WHITE).getRGB(); | ||
|
|
||
| } else if ("SELECTED_CELL_BACKGROUND_NO_FOCUS".equals(color)) { //$NON-NLS-1$ | ||
| return display.getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND).getRGB(); | ||
|
|
||
| } else if ("SELECTED_CELL_FOREGROUND_NO_FOCUS".equals(color)) { //$NON-NLS-1$ | ||
| return display.getSystemColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND).getRGB(); | ||
|
|
||
| } else { | ||
| return new RGB(0, 0, 0); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void setInitializationData(IConfigurationElement config, String propertyName, Object data) { | ||
| if (data instanceof Hashtable table) { | ||
| this.color = (String) table.get("color"); //$NON-NLS-1$ | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.