Skip to content

Commit

Permalink
Remove non-async supporting image loader functions
Browse files Browse the repository at this point in the history
  • Loading branch information
parg committed Dec 29, 2024
1 parent 7b64c54 commit ddaee98
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 154 deletions.
19 changes: 18 additions & 1 deletion core/src/com/biglybt/core/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4099,13 +4099,30 @@ public static long getUsableSpace(File f)
},false, strict_timeout ));
}


private static boolean
existsWithTimeoutAndException(
File file,
long strict_timeout )

throws IOException
{
return(runFileOpWithTimeoutEx(file,()->{
return( file.exists());
}, new IOException( "File system not responding/slow" ), strict_timeout ));
}

public static boolean
isResponding(
File file,
long strict_timeout )
{
try{
getCanonicalPathWithTimeout(file, strict_timeout);
long st = strict_timeout<=1?strict_timeout:(strict_timeout/2);

getCanonicalPathWithTimeout( file, st );

existsWithTimeoutAndException( file, st );

return( true );

Expand Down
17 changes: 13 additions & 4 deletions uis/src/com/biglybt/ui/swt/ImageRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,13 @@ static void addPath(String path, String id) {
"Ignore Icon Exts" );
}

public static Image getIconFromExtension(File file, String ext, boolean bBig,
boolean minifolder) {
public static Image
getIconFromExtension(
File file,
String ext,
boolean bBig,
boolean minifolder)
{
Image image = null;

try {
Expand Down Expand Up @@ -275,8 +280,12 @@ private static Image force16height(Image image) {
* @param path Absolute path to the file or directory
* @return The image
*/
public static Image getPathIcon(final String path, boolean bBig,
boolean minifolder) {
public static Image
getPathIcon(
final String path,
boolean bBig,
boolean minifolder)
{
if (path == null)
return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,29 @@ public void cellPaint(GC gc, TableCellSWT cell) {

Rectangle cellBounds = cell.getBounds();

Image img = entry.getIcon();

if (img != null && !img.isDisposed()) {
gc.fillRectangle( cellBounds );
Rectangle imgBounds = img.getBounds();
if (cellBounds.width < imgBounds.width || cellBounds.height < imgBounds.height) {
float dx = (float) cellBounds.width / imgBounds.width;
float dy = (float) cellBounds.height / imgBounds.height;
float d = Math.min(dx, dy);
int newWidth = (int) (imgBounds.width * d);
int newHeight = (int) (imgBounds.height * d);

gc.drawImage(img, 0, 0, imgBounds.width, imgBounds.height,
cellBounds.x + (cellBounds.width - newWidth) / 2,
cellBounds.y + (cellBounds.height - newHeight) / 2, newWidth,
newHeight);
} else {
gc.drawImage(img, cellBounds.x
+ ((cellBounds.width - imgBounds.width) / 2), cellBounds.y
+ ((cellBounds.height - imgBounds.height) / 2));
entry.getIcon(( img )->{

if (img != null && !img.isDisposed()) {
gc.fillRectangle( cellBounds );
Rectangle imgBounds = img.getBounds();
if (cellBounds.width < imgBounds.width || cellBounds.height < imgBounds.height) {
float dx = (float) cellBounds.width / imgBounds.width;
float dy = (float) cellBounds.height / imgBounds.height;
float d = Math.min(dx, dy);
int newWidth = (int) (imgBounds.width * d);
int newHeight = (int) (imgBounds.height * d);

gc.drawImage(img, 0, 0, imgBounds.width, imgBounds.height,
cellBounds.x + (cellBounds.width - newWidth) / 2,
cellBounds.y + (cellBounds.height - newHeight) / 2, newWidth,
newHeight);
} else {
gc.drawImage(img, cellBounds.x
+ ((cellBounds.width - imgBounds.width) / 2), cellBounds.y
+ ((cellBounds.height - imgBounds.height) / 2));
}
}
}
});
}

@Override
Expand All @@ -99,11 +100,12 @@ public void refresh(TableCell cell) {

String name = entry.getEngine().getName();

Image img = entry.getIcon();
entry.getIcon((img)->{

cell.setText(img == null || img.isDisposed() ? name : null);
cell.setText(img == null || img.isDisposed() ? name : null);

cell.setToolTip( name);
cell.setToolTip( name);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,37 +65,47 @@ public ColumnActivityType(String tableID) {
public void cellPaint(GC gc, final TableCellSWT cell) {
ActivitiesEntry entry = (ActivitiesEntry) cell.getDataSource();

Image imgIcon;
String iconID = entry.getIconID();
if (iconID != null) {
ImageLoader imageLoader = ImageLoader.getInstance();
if (iconID.startsWith("http")) {
imgIcon = imageLoader.getUrlImage(iconID,
if (iconID.startsWith("http")){
imageLoader.getUrlImage(iconID,
new ImageDownloaderListener() {
@Override
public void imageDownloaded(Image image, String key,
boolean returnedImmediately) {
public void
imageDownloaded(
Image image,
String key,
boolean returnedImmediately)
{
if (ImageLoader.isRealImage(image)) {
Rectangle cellBounds = cell.getBounds();
Rectangle imgBounds = image.getBounds();
gc.drawImage(image, cellBounds.x
+ ((cellBounds.width - imgBounds.width) / 2), cellBounds.y
+ ((cellBounds.height - imgBounds.height) / 2));
}
imageLoader.releaseImage(iconID);

if (returnedImmediately) {
return;
}
cell.invalidate();
}
});
if (imgIcon == null) {
return;
}
} else {
imgIcon = imageLoader.getImage(iconID);
}

if (ImageLoader.isRealImage(imgIcon)) {
Rectangle cellBounds = cell.getBounds();
Rectangle imgBounds = imgIcon.getBounds();
gc.drawImage(imgIcon, cellBounds.x
+ ((cellBounds.width - imgBounds.width) / 2), cellBounds.y
+ ((cellBounds.height - imgBounds.height) / 2));
}else{
Image imgIcon = imageLoader.getImage(iconID);

if (ImageLoader.isRealImage(imgIcon)) {
Rectangle cellBounds = cell.getBounds();
Rectangle imgBounds = imgIcon.getBounds();
gc.drawImage(imgIcon, cellBounds.x
+ ((cellBounds.width - imgBounds.width) / 2), cellBounds.y
+ ((cellBounds.height - imgBounds.height) / 2));
}
imageLoader.releaseImage(iconID);
}
imageLoader.releaseImage(iconID);
}
}

Expand Down
53 changes: 25 additions & 28 deletions uis/src/com/biglybt/ui/swt/devices/TranscodeChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -579,38 +579,35 @@ public void handleEvent(Event event) {
gridData = new GridData(GridData.FILL_VERTICAL);
gridData.heightHint = 50;
gridData.widthHint = 100;
lblImage.setLayoutData(gridData);

if (iconURL != null && iconURL.length() > 0) {
ImageLoader imageLoader = ImageLoader.getInstance();
Image image = imageLoader.getUrlImage(iconURL,
new ImageLoader.ImageDownloaderListener() {
@Override
public void imageDownloaded(Image image, String key, boolean returnedImmediately) {
if (!returnedImmediately) {
if (lblImage.isDisposed()) {
return;
}
lblImage.setData("Image", image);
Rectangle bounds = image.getBounds();
GridData gridData = (GridData) lblImage.getLayoutData();
gridData.heightHint = bounds.height + 10;
gridData.widthHint = bounds.width + 16;
lblImage.setLayoutData(gridData);
lblImage.getShell().layout(new Control[] {
lblImage
});
Point computeSize = shell.computeSize(600, SWT.DEFAULT, true);
shell.setSize(computeSize);
}
imageLoader.getUrlImage(
iconURL,
new ImageLoader.ImageDownloaderListener() {
@Override
public void imageDownloaded(Image image, String key, boolean returnedImmediately) {
if (lblImage.isDisposed()) {
return;
}
});
if (image != null) {
lblImage.setData("Image", image);
Rectangle bounds = image.getBounds();
gridData.heightHint = bounds.height + 10;
gridData.widthHint = bounds.width + 16;
}
lblImage.setData("Image", image);
Rectangle bounds = image.getBounds();
GridData gridData = (GridData) lblImage.getLayoutData();
gridData.heightHint = bounds.height + 10;
gridData.widthHint = bounds.width + 16;
lblImage.setLayoutData(gridData);

if ( !returnedImmediately ){
lblImage.getShell().layout(new Control[] {
lblImage
});
Point computeSize = shell.computeSize(600, SWT.DEFAULT, true);
shell.setSize(computeSize);
}
}
});
}
lblImage.setLayoutData(gridData);

Label label = new Label(c, SWT.WRAP | SWT.CENTER);
if (listenerMouseInout != null) {
Expand Down
53 changes: 32 additions & 21 deletions uis/src/com/biglybt/ui/swt/imageloader/ImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1188,31 +1188,42 @@ public int getAnimationDelay(String sKey) {
return 100;
}

public Image getUrlImage(String url, ImageDownloaderListener l) {
return getUrlImage(url, null, l);
public void
getUrlImage(
String url,
ImageDownloaderListener l)
{
getUrlImage(url, null, l);
}

/**
* Get an {@link Image} from an url. URL will be the key, which you
* can use later to {@link #releaseImage(String)}
*/
public Image getUrlImage(String url, final Point maxSize,
final ImageDownloaderListener l) {
public void
getUrlImage(
String url,
final Point maxSize,
final ImageDownloaderListener l)
{

if ( l == null || url == null ){

Debug.out( "eh" );
return null;

return;
}

if (!Utils.isThisThreadSWT()) {
if (!Utils.isThisThreadSWT()){

Utils.execSWTThread(()->{ getUrlImage( url, maxSize, l );});

return(null);
return;
}

String imageKey = url;

return( getUrlImageSupport( url, imageKey, maxSize, l ));
getUrlImageSupport( url, imageKey, maxSize, l );
}

/**
Expand All @@ -1224,21 +1235,24 @@ public Image getUrlImage(String url, final Point maxSize,
* @return
*/

public Image
public void
getFileImage(
File file,
Point maxSize,
final ImageDownloaderListener l)
final ImageDownloaderListener l )
{
if (!Utils.isThisThreadSWT()) {

Debug.out("Called on non-SWT thread");

return null;
return;
}

if (l == null || file == null) {
return null;
if ( l == null || file == null ){

Debug.out( "eh?" );

return;
}

try{
Expand All @@ -1253,17 +1267,15 @@ public Image getUrlImage(String url, final Point maxSize,
imageKey += ";" + lastModified;
}

return( getUrlImageSupport( url, imageKey, maxSize, l ));
getUrlImageSupport( url, imageKey, maxSize, l );

}catch( MalformedURLException e ){

Debug.out( e );

return( null );
}
}

private Image
private void
getUrlImageSupport(
String url,
String baseImageKey,
Expand All @@ -1285,12 +1297,12 @@ public Image getUrlImage(String url, final Point maxSize,
if ( refInfoFromImageMap != null ){
Image[] images = refInfoFromImageMap.getImages();
if ( images == null || images.length == 0 ){
return( null );
return;
}
Image image = images[0];
refInfoFromImageMap.addref();
l.imageDownloaded(image, sizedImageKey, true);
return image;
return;
}

final String cache_key = baseImageKey.hashCode() + ".ico";
Expand Down Expand Up @@ -1320,7 +1332,7 @@ public Image getUrlImage(String url, final Point maxSize,
}
putRefInfoToImageMap(sizedImageKey, new ImageLoaderRefInfo(image));
l.imageDownloaded(image, sizedImageKey, true);
return image;
return;
} finally {
fis.close();
}
Expand Down Expand Up @@ -1384,7 +1396,6 @@ public void runSupport() {
});
}
});
return null;
}

public Image resizeImageIfLarger(Image image, Point maxSize) {
Expand Down
Loading

0 comments on commit ddaee98

Please sign in to comment.