Skip to content

Commit

Permalink
fix: docks are restorable
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Feb 11, 2024
1 parent 12b7f89 commit 5a0ef1d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 45 deletions.
16 changes: 13 additions & 3 deletions src/main/java/viewtify/ui/dock/DockModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,28 @@ private boolean showOnInitial() {
return true;
}

public String id() {
/**
* Get the id of dock.
*
* @return
*/
public final String id() {
return I.make(view()).id();
}

public Variable<String> title() {
/**
* Get the titke of dock.
*
* @return
*/
public final Variable<String> title() {
return I.make(view()).title();
}

/**
* Show view.
*/
public void show() {
public final void show() {
registration().accept((Dock) this);
}
}
73 changes: 33 additions & 40 deletions src/main/java/viewtify/ui/dock/DockSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,23 @@ public final class DockSystem {
/** The user configuration. */
static TabClosingPolicy tabPolicy = TabClosingPolicy.SELECTED_TAB;

/** Avoid multiplex requesting. */
private static boolean requesting;

/**
* Save the current layout info.
*/
static void requestSavingLayout() {
if (!requesting) {
requesting = true;
try {
layout().save.accept(true);
} finally {
requesting = false;
}
}
}

/**
* Hide.
*/
Expand Down Expand Up @@ -142,27 +159,13 @@ public static void configure(TabClosingPolicy policy) {
});
}

/**
* Save the current layout info.
*/
static void requestSavingLayout() {
layout().save.accept(true);
}

/**
* The root user interface for the docking system.
*/
public static UIPane ui() {
return layout().findRoot().node;
}

/**
* Validate dock system. Clean up the invalidate area.
*/
public static void validate() {
layout().roots.forEach(RootArea::validate);
}

/**
* Test whether the specified view has already opend or not.
*
Expand Down Expand Up @@ -235,41 +238,28 @@ public static UITab register(String id, UnaryOperator<DockRecommendedLocation> o

// First, if there is an area where the specified view's ID is registered,
// add the view there.
ViewArea area = I.signal(layout.roots)
.as(ViewArea.class)
.recurseMap(s -> s.flatIterable(v -> (List<ViewArea>) v.children))
.take(v -> v.hasView(id))
.first()
.to().v;

if (area != null) {
area.add(tab, PositionRestore);
Variable<TabArea> area = layout.find(TabArea.class).take(x -> x.hasView(id)).first().to();
if (area.isPresent()) {
area.v.add(tab, PositionRestore);
opened.add(id);
return tab;
}

// Next, if there is an area where adding the specified view is recommended,
// add the view there.
area = I.signal(layout.roots)
.as(ViewArea.class)
.recurseMap(s -> s.flatIterable(v -> (List<ViewArea>) v.children))
.take(v -> v.location == o.recommendedArea)
.first()
.to().v;

if (area != null) {
area.add(tab, PositionRestore);
area = layout.find(TabArea.class).take(v -> v.location == o.recommendedArea).first().to();
if (area.isPresent()) {
area.v.add(tab, PositionRestore);
opened.add(id);
return tab;
}

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

area = base.add(tab, o.recommendedArea);
area.location = o.recommendedArea;
area.setViewportRatio(o.recommendedArea == PositionRight || o.recommendedArea == PositionBottom ? 1 - o.recommendedRatio
ViewArea created = base.add(tab, o.recommendedArea);
created.location = o.recommendedArea;
created.setViewportRatio(o.recommendedArea == PositionRight || o.recommendedArea == PositionBottom ? 1 - o.recommendedRatio
: o.recommendedRatio);
opened.add(id);
return tab;
Expand Down Expand Up @@ -330,7 +320,7 @@ public static void initialize() {
*/
public static void initialize(WiseConsumer<UILabel> menuBuilder) {
DockLayout layout = layout();
if (Locator.file(layout.locate()).isAbsent() || true) {
if (Locator.file(layout.locate()).isAbsent()) {
for (DockProvider provider : I.find(DockProvider.class)) {
for (Dock dock : provider.findDocks()) {
if (dock.initialView) {
Expand All @@ -346,12 +336,15 @@ public static void initialize(WiseConsumer<UILabel> menuBuilder) {
}
} else {
layout.find(TabArea.class).flatIterable(TabArea::getIds).to(id -> {
for (DockProvider register : I.find(DockProvider.class)) {
if (register.register(id)) {
for (DockProvider provider : I.find(DockProvider.class)) {
if (provider.register(id)) {
break;
}
}
});

// validate all area
// layout.roots.forEach(RootArea::validate);
}

if (menuBuilder != null) {
Expand Down Expand Up @@ -384,8 +377,8 @@ private static class DockLayout implements Storable<DockLayout> {
*
*/
private DockLayout() {
// restore();
save.expose.debounce(1000, TimeUnit.MILLISECONDS).to(this::store);
restore();
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/viewtify/ui/dock/TabArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import javafx.scene.control.skin.TabPaneSkin;
import javafx.scene.input.DragEvent;
import javafx.scene.layout.StackPane;

import kiss.I;
import kiss.WiseConsumer;
import viewtify.Viewtify;
Expand Down Expand Up @@ -242,7 +241,10 @@ void handleEmpty() {
*/
@Override
public TabArea add(UITab tab, int position) {
boolean restore = false;

if (position == DockSystem.PositionRestore) {
restore = true;
position = restorePosition(tab);
}

Expand All @@ -262,7 +264,7 @@ public TabArea add(UITab tab, int position) {
tab.setOnCloseRequest(e -> remove(tab, true));

selectInitialTabOnlyOnce(tab);
updatePosition();
if (!restore) updatePosition();
return this;
}
}
Expand Down

0 comments on commit 5a0ef1d

Please sign in to comment.