Skip to content

Commit

Permalink
[ikonli-browser] Remove RxJava from ikonli-browser
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed May 2, 2021
1 parent 86c8436 commit 17d7aa2
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 44 deletions.
1 change: 0 additions & 1 deletion apps/ikonli-browser/ikonli-browser.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ repositories {
dependencies {
implementation('org.kordamp.bootstrapfx:bootstrapfx-core:0.4.0') { transitive = false }
implementation('org.kordamp.desktoppanefx:desktoppanefx-core:0.15.0') { transitive = false }
implementation "io.reactivex.rxjava3:rxjava:$rxjavaVersion"
implementation "com.miglayout:miglayout-javafx:$miglayoutVersion"

implementation project(':ikonli-core')
Expand Down
2 changes: 0 additions & 2 deletions apps/ikonli-browser/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@
requires org.kordamp.ikonli.win10;
requires org.kordamp.ikonli.zondicons;
requires com.miglayout.javafx;
requires io.reactivex.rxjava3;
requires org.reactivestreams;

uses org.kordamp.ikonli.IkonHandler;
uses org.kordamp.ikonli.IkonProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.kordamp.bootstrapfx.BootstrapFX;
import org.kordamp.desktoppanefx.scene.layout.DesktopPane;
import org.kordamp.desktoppanefx.scene.layout.IncubatingFeatures;
import org.kordamp.desktoppanefx.scene.layout.InternalWindow;
import org.kordamp.desktoppanefx.scene.layout.TaskBar;
import org.kordamp.desktoppanefx.scene.layout.TaskBarIcon;
import org.kordamp.ikonli.boxicons.BoxiconsRegular;
Expand Down Expand Up @@ -70,6 +71,12 @@ public void start(Stage stage) throws Exception {
stage.show();
}

@Override
public void stop() throws Exception {
desktopPane.getInternalWindows()
.forEach(InternalWindow::closeWindow);
}

private MenuBar createMenuBar() {
MenuBar menuBar = new MenuBar();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.kordamp.desktoppanefx.scene.layout.DesktopPane;
import org.kordamp.ikonli.IkonProvider;

import java.util.Collection;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.TreeSet;
Expand All @@ -45,11 +44,11 @@
* @author Andres Almiray
*/
public class IkonPickerDialog extends Dialog<IkonProvider> {
private final DesktopPane desktopPane;
private final ObservableList<IkonData> data;
private final Set<IkonData> ikons;

private IkonPickerDialog(DesktopPane desktopPane, Collection<IkonData> ikonData) {
this.desktopPane = desktopPane;
private IkonPickerDialog() {
this.ikons = resolveIkonData();
DialogPane dialogPane = getDialogPane();

GridPane grid = new GridPane();
Expand All @@ -61,7 +60,7 @@ private IkonPickerDialog(DesktopPane desktopPane, Collection<IkonData> ikonData)
label.setPrefWidth(Region.USE_COMPUTED_SIZE);

setTitle("Ikon Picker");
dialogPane.setHeaderText("Select an Ikon provider (" + ikonData.size() + " in total)");
dialogPane.setHeaderText("Select an Ikon provider (" + ikons.size() + " in total)");
dialogPane.getStyleClass().add("ikon-picker");
dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
dialogPane.getScene().getStylesheets().addAll(
Expand All @@ -75,7 +74,7 @@ private IkonPickerDialog(DesktopPane desktopPane, Collection<IkonData> ikonData)

Button okButton = (Button) dialogPane.lookupButton(ButtonType.OK);

data = FXCollections.observableArrayList(ikonData);
data = FXCollections.observableArrayList(ikons);

ListView<IkonData> listView = new ListView<>();
listView.setItems(data);
Expand Down Expand Up @@ -109,6 +108,21 @@ private IkonPickerDialog(DesktopPane desktopPane, Collection<IkonData> ikonData)
Platform.runLater(listView::requestFocus);
}

private Set<IkonData> resolveIkonData() {
Set<IkonData> ikons = new TreeSet<>();
if (null != IkonProvider.class.getModule().getLayer()) {
for (IkonProvider provider : ServiceLoader.load(IkonProvider.class.getModule().getLayer(), IkonProvider.class)) {
ikons.add(IkonData.of(provider));
}
} else {
for (IkonProvider provider : ServiceLoader.load(IkonProvider.class)) {
ikons.add(IkonData.of(provider));
}
}

return ikons;
}

private IkonProvider getSelectedItem(ListView<IkonData> listView) {
IkonData selectedItem = listView.getSelectionModel().getSelectedItem();
return selectedItem != null ? selectedItem.ikonProvider : null;
Expand All @@ -125,18 +139,7 @@ private Label createContentLabel(String text) {
}

public static void show(DesktopPane desktopPane) {
Set<IkonData> ikons = new TreeSet<>();
if (null != IkonProvider.class.getModule().getLayer()) {
for (IkonProvider provider : ServiceLoader.load(IkonProvider.class.getModule().getLayer(), IkonProvider.class)) {
ikons.add(IkonData.of(provider));
}
} else {
for (IkonProvider provider : ServiceLoader.load(IkonProvider.class)) {
ikons.add(IkonData.of(provider));
}
}

new IkonPickerDialog(desktopPane, ikons)
new IkonPickerDialog()
.showAndWait()
.ifPresent(ikonProvider -> IkonInternalWindow.show(ikonProvider, desktopPane));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
package org.kordamp.ikonli.browser;

import eu.hansolo.tilesfx.tools.FlowGridPane;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
Expand All @@ -31,6 +28,7 @@
import javafx.beans.property.StringProperty;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
Expand Down Expand Up @@ -59,6 +57,8 @@
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static java.util.EnumSet.allOf;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -200,6 +200,12 @@ private FontIcon createIcon(Ikon ikon) {
setOnHidden(e -> controller.cancel());
}

@Override
public void closeWindow() {
controller.shutdown();
super.closeWindow();
}

private void searchIcons() {
controller.cancel();
controller.search();
Expand Down Expand Up @@ -240,7 +246,7 @@ private static class Model {
private StringProperty term;
private StringProperty selection;
private ObjectProperty<State> state;
private Disposable disposable;
private Task<Void> task;

public Model() {
stateProperty().addListener((observable, oldValue, newValue) -> {
Expand All @@ -254,12 +260,12 @@ public ObservableList<Ikon> getIkons() {
return ikons;
}

public Disposable getDisposable() {
return disposable;
public Task<Void> getTask() {
return task;
}

public void setDisposable(Disposable disposable) {
this.disposable = disposable;
public void setTask(Task<Void> task) {
this.task = task;
}

public StringProperty termProperty() {
Expand Down Expand Up @@ -311,39 +317,54 @@ public ObjectProperty<State> stateProperty() {
private static class Controller {
private final Model model;
private final List<Ikon> ikons = new ArrayList<>();
private ExecutorService executor;

public Controller(Model model) {
this.model = model;
}

public void search() {
if (executor == null) {
executor = Executors.newSingleThreadExecutor();
}

if (ikons.isEmpty()) {
loadIcons();
}

String term = model.getTerm().toLowerCase();
Flowable<Ikon> flowable = Flowable.fromIterable(ikons)
.filter(ikon -> ikon.getDescription().contains(term));

model.setDisposable(flowable
.doOnSubscribe(disposable -> {
model.getIkons().clear();
model.setState(State.RUNNING);
})
.doOnTerminate(() -> model.setState(State.READY))
.doOnError(Throwable::printStackTrace)
.subscribeOn(Schedulers.io())
.subscribe(model.getIkons()::add));
Task<Void> task = createTask();
model.setTask(task);
executor.execute(task);
}

private Task<Void> createTask() {
return new Task<>() {
@Override
protected Void call() throws Exception {
String term = model.getTerm().toLowerCase();
ikons.stream()
.filter(ikon -> ikon.getDescription().contains(term))
.peek(model.getIkons()::add)
.forEach(Controller::noop);

return null;
}
};
}

public void cancel() {
model.setSelection("");
if (model.getDisposable() != null) {
model.getDisposable().dispose();
if (model.getTask() != null) {
model.getTask().cancel(true);
model.setState(State.READY);
}
}

public void shutdown() {
executor.shutdownNow();
executor = null;
}

private void loadIcons() {
if (null != IkonProvider.class.getModule().getLayer()) {
for (IkonProvider provider : ServiceLoader.load(IkonProvider.class.getModule().getLayer(), IkonProvider.class)) {
Expand All @@ -359,5 +380,8 @@ private void loadIcons() {
private void add(EnumSet<? extends Ikon> set, List<Ikon> ikons) {
ikons.addAll(set);
}

private static void noop(Ikon ikon) {
}
}
}

0 comments on commit 17d7aa2

Please sign in to comment.