Skip to content

Commit

Permalink
Fix status bar binding
Browse files Browse the repository at this point in the history
  • Loading branch information
crschnick committed Mar 4, 2024
1 parent d54fa90 commit a557fcb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
42 changes: 15 additions & 27 deletions app/src/main/java/io/xpipe/app/browser/BrowserStatusBarComp.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,21 @@ protected Region createSimple() {
}

private Comp<?> createProgressStatus() {
var transferredCount = PlatformThread.sync(Bindings.createStringBinding(
() -> {
return HumanReadableFormat.byteCount(
model.getProgress().getValue().getTransferred(), false);
},
model.getProgress()));
var allCount = PlatformThread.sync(Bindings.createStringBinding(
() -> {
return HumanReadableFormat.byteCount(
model.getProgress().getValue().getTotal(), true);
},
model.getProgress()));
var progressComp = new LabelComp(BindingsHelper.persist(Bindings.createStringBinding(
() -> {
if (model.getProgress().getValue() == null
|| model.getProgress().getValue().done()) {
return null;
} else {
var name = (model.getProgress().getValue().getName() != null
? " @ " + model.getProgress().getValue().getName() + " "
: "");
return transferredCount.getValue() + " / " + allCount.getValue() + name;
}
},
transferredCount,
allCount,
model.getProgress())));
var text = BindingsHelper.map(model.getProgress(), p -> {
if (p == null || p.done()) {
return null;
} else {
var transferred = HumanReadableFormat.byteCount(
p.getTransferred(), false);
var all = HumanReadableFormat.byteCount(
p.getTotal(), true);
var name = (p.getName() != null
? " @ " + p.getName() + " "
: "");
return transferred + " / " + all + name;
}
});
var progressComp = new LabelComp(text);
return progressComp;
}

Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/io/xpipe/app/fxcomps/impl/LabelComp.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.util.Subscription;

import java.util.concurrent.Flow;

public class LabelComp extends Comp<CompStructure<Label>> {

Expand All @@ -18,13 +21,15 @@ public LabelComp(String text) {
}

public LabelComp(ObservableValue<String> text) {
this.text = PlatformThread.sync(text);
this.text = text;
}

@Override
public CompStructure<Label> createBase() {
var label = new Label();
label.textProperty().bind(text);
text.subscribe(t -> {
PlatformThread.runLaterIfNeeded(() -> label.setText(t));
});
label.setAlignment(Pos.CENTER);
return new SimpleCompStructure<>(label);
}
Expand Down

0 comments on commit a557fcb

Please sign in to comment.