Skip to content

Commit 326658a

Browse files
authored
[GTK] Factor out common image initialization functionality (#1892)
The GTK implementation of the Image class currently contains the basically same piece of code for initializing and image based on an ImageFileNameProvider and ImageDataProvider twice, once in the Image constructors and once in the refresh logic for changing the zoom. This change factors out the common functionality in reused methods and streamlines the implementation for the ImageFileNameProvider.
1 parent 632681f commit 326658a

File tree

1 file changed

+28
-40
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics

1 file changed

+28
-40
lines changed

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -613,19 +613,7 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
613613
super(device);
614614
this.imageFileNameProvider = imageFileNameProvider;
615615
currentDeviceZoom = DPIUtil.getDeviceZoom();
616-
ElementAtZoom<String> filename = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, currentDeviceZoom);
617-
if (filename.zoom() == currentDeviceZoom) {
618-
initNative (filename.element());
619-
620-
if (this.surface == 0) {
621-
ImageData data = new ImageData(filename.element());
622-
init(data);
623-
}
624-
} else {
625-
ImageData imageData = new ImageData (filename.element());
626-
ImageData resizedData = DPIUtil.autoScaleImageData (device, imageData, filename.zoom());
627-
init(resizedData);
628-
}
616+
initFromFileNameProvider(currentDeviceZoom);
629617
init ();
630618
}
631619

@@ -662,9 +650,7 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
662650
super(device);
663651
this.imageDataProvider = imageDataProvider;
664652
currentDeviceZoom = DPIUtil.getDeviceZoom();
665-
ElementAtZoom<ImageData> data = DPIUtil.validateAndGetImageDataAtZoom(imageDataProvider, currentDeviceZoom);
666-
ImageData resizedData = DPIUtil.autoScaleImageData(device, data.element(), data.zoom());
667-
init (resizedData);
653+
initFromImageDataProvider(currentDeviceZoom);
668654
init ();
669655
}
670656

@@ -724,37 +710,19 @@ boolean refreshImageForZoom () {
724710
if (imageFileNameProvider != null) {
725711
int deviceZoomLevel = deviceZoom;
726712
if (deviceZoomLevel != currentDeviceZoom) {
727-
ElementAtZoom<String> filename = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, deviceZoomLevel);
728-
/* Avoid re-creating the fall-back image, when current zoom is already 100% */
729-
if (filename.zoom() == deviceZoomLevel) {
730-
/* Release current native resources */
731-
destroy ();
732-
initNative(filename.element());
733-
if (this.surface == 0) {
734-
ImageData data = new ImageData(filename.element());
735-
init(data);
736-
}
737-
init ();
738-
refreshed = true;
739-
} else {
740-
/* Release current native resources */
741-
destroy ();
742-
ImageData imageData = new ImageData (filename.element());
743-
ImageData resizedData = DPIUtil.autoScaleImageData (device, imageData, filename.zoom());
744-
init(resizedData);
745-
init ();
746-
refreshed = true;
747-
}
713+
/* Release current native resources */
714+
destroy ();
715+
initFromFileNameProvider(deviceZoomLevel);
716+
init ();
717+
refreshed = true;
748718
currentDeviceZoom = deviceZoomLevel;
749719
}
750720
} else if (imageDataProvider != null) {
751721
int deviceZoomLevel = deviceZoom;
752722
if (deviceZoomLevel != currentDeviceZoom) {
753-
ElementAtZoom<ImageData> data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, deviceZoomLevel);
754723
/* Release current native resources */
755724
destroy ();
756-
ImageData resizedData = DPIUtil.autoScaleImageData (device, data.element(), data.zoom());
757-
init(resizedData);
725+
initFromImageDataProvider(deviceZoomLevel);
758726
init();
759727
refreshed = true;
760728
currentDeviceZoom = deviceZoomLevel;
@@ -801,6 +769,26 @@ void initNative(String filename) {
801769
} catch (SWTException e) {}
802770
}
803771

772+
private void initFromFileNameProvider(int zoom) {
773+
ElementAtZoom<String> fileForZoom = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, zoom);
774+
if (fileForZoom.zoom() == zoom) {
775+
initNative(fileForZoom.element());
776+
}
777+
if (this.surface == 0) {
778+
ImageData imageData = new ImageData(fileForZoom.element());
779+
if (fileForZoom.zoom() != zoom) {
780+
imageData = DPIUtil.scaleImageData(device, imageData, zoom, fileForZoom.zoom());
781+
}
782+
init(imageData);
783+
}
784+
}
785+
786+
private void initFromImageDataProvider(int zoom) {
787+
ElementAtZoom<ImageData> data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, zoom);
788+
ImageData resizedData = DPIUtil.scaleImageData (device, data.element(), zoom, data.zoom());
789+
init(resizedData);
790+
}
791+
804792
void createFromPixbuf(int type, long pixbuf) {
805793
this.type = type;
806794

0 commit comments

Comments
 (0)