Skip to content

Commit

Permalink
feat: enahance dock location
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Feb 9, 2024
1 parent 853a057 commit ff8d893
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 23 deletions.
11 changes: 6 additions & 5 deletions src/main/java/viewtify/ui/dock/DockModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ public abstract class DockModel {
@Property
public abstract Class<? extends View> view();

@Property
public String id() {
return I.make(view()).id();
}

/**
* Sets the behaviour when actually displayed on the tab.
*
* @return
*/
@Property
public WiseConsumer<Dock> registration() {
return dock -> DockSystem.register(dock.id()).text(dock.title()).contentsLazy(tab -> I.make(dock.view()));
return dock -> DockSystem.register(dock.id(), location()).text(dock.title()).contentsLazy(tab -> I.make(dock.view()));
}

/**
Expand Down Expand Up @@ -70,10 +75,6 @@ private boolean showOnInitial() {
return true;
}

public String id() {
return I.make(view()).id();
}

public Variable<String> title() {
return I.make(view()).title();
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/viewtify/ui/dock/DockProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
package viewtify.ui.dock;

import java.util.Comparator;
import java.util.List;

import kiss.Extensible;
Expand Down Expand Up @@ -40,7 +39,6 @@ private List<Dock> docks() {
docks = I.signal(getClass().getFields())
.take(field -> field.getType() == Dock.class)
.map(field -> (Dock) field.get(this))
.sort(Comparator.comparing(Dock::id))
.toList();
}
return docks;
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/viewtify/ui/dock/DockRecommendedLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public final class DockRecommendedLocation {
/** Default value is 0.5. */
double recommendedRatio = 0.5;

Dock base;

/**
* Hide
*/
Expand All @@ -43,6 +45,17 @@ public DockRecommendedLocation top() {
return this;
}

/**
* Specify the recommended area.
*
* @return
*/
public DockRecommendedLocation top(Dock base) {
recommendedArea = DockSystem.PositionTop;
this.base = base;
return this;
}

/**
* Specify the recommended area.
*
Expand All @@ -53,6 +66,17 @@ public DockRecommendedLocation bottom() {
return this;
}

/**
* Specify the recommended area.
*
* @return
*/
public DockRecommendedLocation bottom(Dock base) {
recommendedArea = DockSystem.PositionBottom;
this.base = base;
return this;
}

/**
* Specify the recommended area.
*
Expand All @@ -63,6 +87,17 @@ public DockRecommendedLocation right() {
return this;
}

/**
* Specify the recommended area.
*
* @return
*/
public DockRecommendedLocation right(Dock base) {
recommendedArea = DockSystem.PositionRight;
this.base = base;
return this;
}

/**
* Specify the recommended area.
*
Expand All @@ -73,6 +108,17 @@ public DockRecommendedLocation left() {
return this;
}

/**
* Specify the recommended area.
*
* @return
*/
public DockRecommendedLocation left(Dock base) {
recommendedArea = DockSystem.PositionLeft;
this.base = base;
return this;
}

/**
* Specify the recommended area division ratio.
*
Expand All @@ -82,4 +128,13 @@ public DockRecommendedLocation ratio(double division) {
recommendedRatio = division;
return this;
}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "DockRecommendedLocation [recommendedArea=" + recommendedArea + ", recommendedRatio=" + recommendedRatio + ", base=" + base + "]";
}

}
21 changes: 17 additions & 4 deletions src/main/java/viewtify/ui/dock/DockSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,12 @@ public static UITab register(String id, UnaryOperator<DockRecommendedLocation> o

// Since the recommended area does not exist,
// add a view after creating that area.
area = layout.findRoot().add(tab, o.recommendedArea);
ViewArea base = layout.find(o.base).or(layout.findRoot());

area = base.add(tab, o.recommendedArea);
area.location = o.recommendedArea;
area.setViewportRatio(o.recommendedRatio);
area.setViewportRatio(o.recommendedArea == PositionRight || o.recommendedArea == PositionBottom ? 1 - o.recommendedRatio
: o.recommendedRatio);
opened.add(id);
return tab;
}
Expand Down Expand Up @@ -327,7 +330,7 @@ public static void initialize() {
*/
public static void initialize(WiseConsumer<UILabel> menuBuilder) {
DockLayout layout = layout();
if (Locator.file(layout.locate()).isAbsent()) {
if (Locator.file(layout.locate()).isAbsent() || true) {
for (DockProvider provider : I.find(DockProvider.class)) {
for (Dock dock : provider.findDocks()) {
if (dock.initialView) {
Expand Down Expand Up @@ -381,7 +384,7 @@ private static class DockLayout implements Storable<DockLayout> {
*
*/
private DockLayout() {
restore();
// restore();
save.expose.debounce(1000, TimeUnit.MILLISECONDS).to(this::store);
}

Expand Down Expand Up @@ -419,6 +422,16 @@ private synchronized RootArea findRoot() {
private <X extends ViewArea> Signal<X> find(Class<X> type) {
return I.signal(roots).as(ViewArea.class).recurseMap(s -> s.flatIterable(v -> (List<ViewArea>) v.children)).as(type);
}

/**
* Find view area by dock.
*
* @param dock
* @return
*/
private Variable<ViewArea> find(Dock dock) {
return dock == null ? Variable.empty() : find(TabArea.class).take(v -> v.hasView(dock.id())).first().as(ViewArea.class).to();
}
}

// ==================================================================================
Expand Down
17 changes: 5 additions & 12 deletions src/main/java/viewtify/ui/dock/SplitArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;

import javafx.application.Platform;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.SplitPane.Divider;

import viewtify.ui.UISplitPane;
import viewtify.ui.helper.User;

Expand Down Expand Up @@ -101,16 +99,11 @@ private final List<Double> getDividers() {
* @param dividers The dividers value to set.
*/
private final void setDividers(List<Double> dividers) {
// During Stage initialization, window size changes several times until layout is completed.
// Every change modifies divider positions. If we want to control divider positions, they
// have to be set after Stage is fully initialized:
Platform.runLater(() -> {
double[] values = new double[dividers.size()];
for (int i = 0; i < values.length; i++) {
values[i] = dividers.get(i);
}
node.ui.setDividerPositions(values);
});
double[] values = new double[dividers.size()];
for (int i = 0; i < values.length; i++) {
values[i] = dividers.get(i);
}
node.ui.setDividerPositions(values);
}

/**
Expand Down

0 comments on commit ff8d893

Please sign in to comment.