Skip to content

Commit

Permalink
Merge pull request #37 from patrickfav/feature/multi-os-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickfav authored Aug 6, 2023
2 parents c2b2cf6 + 015f39e commit 220d7d0
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 100 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>at.favre.tools</groupId>
<artifactId>dconvert</artifactId>
<version>${raw.version}-alpha7</version>
<version>${raw.version}-alpha9</version>

<properties>
<!-- SonarQube Config -->
Expand Down Expand Up @@ -213,6 +213,13 @@
<version>1.5.0</version>
</dependency>

<!-- for getting OS version -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.13.0</version>
</dependency>

<!-- hq image scaling -->
<dependency>
<groupId>org.imgscalr</groupId>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/at/favre/tools/dconvert/ui/GUIController.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void onCreate(final Stage stage, IPreferenceStore store, ResourceBundle b
btnSrcFolder.setOnAction(new FolderPicker(srcDirectoryChooser, textFieldSrcPath, textFieldDstPath, bundle));
btnDstFolder.setOnAction(new FolderPicker(srcDirectoryChooser, textFieldDstPath, null, bundle));
btnConvert.setOnAction(event -> {
WinTaskbarProgress winTaskbarProgress = new WinTaskbarProgress();
TaskbarProgress taskbarProgress = TaskbarProgress.create();
try {
Arguments arg = getFromUI(false);
saveToPrefs(arg);
Expand All @@ -171,7 +171,7 @@ public void onCreate(final Stage stage, IPreferenceStore store, ResourceBundle b
public void onProgress(float progress) {
Platform.runLater(() -> {
progressBar.setProgress(progress);
winTaskbarProgress.updateProgress(progress);
taskbarProgress.updateProgress(progress);
});
}

Expand All @@ -185,14 +185,14 @@ public void onFinished(int finishedJobs, List<Exception> exceptions, long time,
textFieldConsole.appendText("");

if (!exceptions.isEmpty()) {
winTaskbarProgress.error();
taskbarProgress.error();
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle(bundle.getString("main.alert.title"));
alert.setHeaderText(null);
alert.setContentText(MessageFormat.format(bundle.getString("main.alert.content"), exceptions.size()));
alert.showAndWait();
} else {
winTaskbarProgress.finish();
taskbarProgress.finish();
}
});

Expand Down
135 changes: 135 additions & 0 deletions src/main/java/at/favre/tools/dconvert/ui/TaskbarProgress.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright 2016 Patrick Favre-Bulle
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package at.favre.tools.dconvert.ui;

import org.apache.commons.lang3.SystemUtils;
import org.bridj.Pointer;
import org.bridj.cpp.com.COMRuntime;
import org.bridj.cpp.com.shell.ITaskbarList3;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Wrapper to use native windows taskbar features like progress, error etc.
*/
public interface TaskbarProgress {

void updateProgress(double progress);

void finish();

void error();

static TaskbarProgress create() {
if (SystemUtils.IS_OS_WINDOWS) {
return new WinTaskbarProgress();
} else {
return new NoopTaskbarProgress();
}
}

/**
* No-Op for non Windows OSes
*/
class NoopTaskbarProgress implements TaskbarProgress {

@Override
public void updateProgress(double progress) {
}

@Override
public void finish() {
}

@Override
public void error() {
}
}

/**
* Implementation for Windows OS
*/
final class WinTaskbarProgress implements TaskbarProgress {

private ExecutorService executor;
private static final long MAX = 10000;
private Pointer<?> hwnd;
private ITaskbarList3 list;
private boolean enabled = true;

public WinTaskbarProgress() {
try {
hwnd = Pointer.pointerToAddress(com.sun.glass.ui.Window.getWindows().get(0).getNativeWindow());
} catch (Exception e) {
enabled = false;
return;
}

executor = Executors.newSingleThreadExecutor(r -> {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
});

executor.execute(() -> {
try {
list = COMRuntime.newInstance(ITaskbarList3.class);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
});
hwnd = Pointer.pointerToAddress(com.sun.glass.ui.Window.getWindows().get(0).getNativeWindow());
}

public void updateProgress(double progress) {
if (!enabled) return;
executor.execute(() -> list.SetProgressValue((Pointer) hwnd, Math.round(progress * (double) MAX), MAX));
}

public void finish() {
if (!enabled) return;
executor.execute(() -> {
list.SetProgressValue((Pointer) hwnd, MAX, MAX);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
list.SetProgressState((Pointer) hwnd, ITaskbarList3.TbpFlag.TBPF_NOPROGRESS);
list.SetProgressValue((Pointer) hwnd, 0, MAX);
list.Release();
});
}

public void error() {
if (!enabled) return;
executor.execute(() -> {
list.SetProgressState((Pointer) hwnd, ITaskbarList3.TbpFlag.TBPF_ERROR);
list.SetProgressValue((Pointer) hwnd, MAX, MAX);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
list.SetProgressState((Pointer) hwnd, ITaskbarList3.TbpFlag.TBPF_NOPROGRESS);
list.SetProgressValue((Pointer) hwnd, 0, MAX);
list.Release();
});
}
}
}
95 changes: 0 additions & 95 deletions src/main/java/at/favre/tools/dconvert/ui/WinTaskbarProgress.java

This file was deleted.

0 comments on commit 220d7d0

Please sign in to comment.