From 69af8e1b1d042ab99471c4a4449042ebfb1c327d Mon Sep 17 00:00:00 2001 From: Amartya Parijat Date: Tue, 2 Jul 2024 14:55:25 +0200 Subject: [PATCH] Scaling of Text Icon for multi zoom level This commit provides the ability to scaling the special icons of Text in win 32, i.e., Search and Cancel icon. The scaled icon is obtained at the draw time in the method Text.WM_DRAWITEM. We get rid of the fields hIconSearch and hIconCancel and we maintain a hashmap of size to icon for both the icons. contributes to #62 and #127 --- .../org/eclipse/swt/widgets/Display.java | 30 ++++++++++++++++--- .../win32/org/eclipse/swt/widgets/Text.java | 23 +++----------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java index 7bdf21ece94..c8070feabdb 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java @@ -271,8 +271,8 @@ public class Display extends Device implements Executor { boolean textUseDarkthemeIcons = false; /* Custom icons */ - long hIconSearch; - long hIconCancel; + private HashMap sizeToSearchIconHandle = new HashMap<>(); + private HashMap sizeToCancelIconHandle = new HashMap<>(); /* Focus */ int focusEvent; @@ -1188,6 +1188,26 @@ static Image createIcon (Image image) { return Image.win32_new (device, SWT.ICON, hIcon); } +long getTextSearchIcon(int size) { + if (!sizeToSearchIconHandle.containsKey(size)) { + int searchIconResource = textUseDarkthemeIcons ? Text.IDI_SEARCH_DARKTHEME : Text.IDI_SEARCH; + long iconHandle = OS.LoadImage (OS.GetLibraryHandle (), searchIconResource, OS.IMAGE_ICON, size, size, 0); + if (iconHandle == 0) error(SWT.ERROR_NO_HANDLES); + sizeToSearchIconHandle.put(size, iconHandle); + } + return sizeToSearchIconHandle.get(size); +} + +long getTextCancelIcon(int size) { + if (!sizeToCancelIconHandle.containsKey(size)) { + int searchIconResource = textUseDarkthemeIcons ? Text.IDI_CANCEL_DARKTHEME : Text.IDI_CANCEL; + long iconHandle = OS.LoadImage (OS.GetLibraryHandle (), searchIconResource, OS.IMAGE_ICON, size, size, 0); + if (iconHandle == 0) error(SWT.ERROR_NO_HANDLES); + sizeToCancelIconHandle.put(size, iconHandle); + } + return sizeToCancelIconHandle.get(size); +} + static void deregister (Display display) { synchronized (Device.class) { for (int i=0; i OS.DestroyIcon(handle)); + sizeToCancelIconHandle.values().forEach(handle -> OS.DestroyIcon(handle)); + sizeToSearchIconHandle.clear(); + sizeToCancelIconHandle.clear(); /* Release XP Themes */ resetThemes(); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Text.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Text.java index af693c6ed77..8d9a4609724 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Text.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Text.java @@ -337,24 +337,7 @@ void createHandle () { state |= THEME_BACKGROUND; } } - addIcons(); -} - -private void addIcons() { if ((style & SWT.SEARCH) != 0) { - if (display.hIconSearch == 0) { - long [] phicon = new long [1]; - - int searchIconResource = display.textUseDarkthemeIcons ? IDI_SEARCH_DARKTHEME : IDI_SEARCH; - int hresult = OS.LoadIconMetric (OS.GetLibraryHandle (), searchIconResource, OS.LIM_SMALL, phicon); - if (hresult != OS.S_OK) error (SWT.ERROR_NO_HANDLES); - display.hIconSearch = phicon [0]; - - int cancelIconResource = display.textUseDarkthemeIcons ? IDI_CANCEL_DARKTHEME : IDI_CANCEL; - hresult = OS.LoadIconMetric (OS.GetLibraryHandle (), cancelIconResource, OS.LIM_SMALL, phicon); - if (hresult != OS.S_OK) error (SWT.ERROR_NO_HANDLES); - display.hIconCancel = phicon [0]; - } if ((style & SWT.ICON_SEARCH) != 0) { long hwndSearch = OS.CreateWindowEx ( 0, @@ -2770,7 +2753,10 @@ LRESULT WM_DRAWITEM (long wParam, long lParam) { int state = OS.GetKeyState (OS.VK_LBUTTON) < 0 ? OS.PBS_PRESSED : OS.PBS_HOT; OS.DrawThemeBackground (display.hButtonThemeAuto (), struct.hDC, OS.BP_PUSHBUTTON, state, rect, null); } - long hIcon = (struct.CtlID == SWT.ICON_SEARCH) ? display.hIconSearch : display.hIconCancel; + int width = rect.right - rect.left; + int height = rect.bottom - rect.top; + int size = Math.min(width, height); + long hIcon = (struct.CtlID == SWT.ICON_SEARCH) ? display.getTextSearchIcon(size) : display.getTextCancelIcon(size); int y = (rect.bottom - rect.right) / 2; OS.DrawIconEx (struct.hDC, 0, y, hIcon, 0, 0, 0, 0, OS.DI_NORMAL); return LRESULT.ONE; @@ -3167,7 +3153,6 @@ private static void handleDPIChange(Widget widget, int newZoom, float scalingFac if (!(widget instanceof Text text)) { return; } - text.addIcons(); text.setMargins(); } }