Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Feb 2, 2024
1 parent 86e47cf commit ebafde1
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 72 deletions.
30 changes: 1 addition & 29 deletions src/main/java/viewtify/ui/dock/DockItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,6 @@

import kiss.Variable;
import kiss.WiseRunnable;
import viewtify.ui.View;

public class DockItem {

public final String id;

public final Variable<String> title;

public final WiseRunnable registration;

/**
* Create dock item for the specified {@link View}.
*
* @param view
* @param registration
*/
public DockItem(View view, WiseRunnable registration) {
this(view.id(), view.title(), registration);
}

/**
* @param id
* @param title
* @param registration
*/
public DockItem(String id, Variable<String> title, WiseRunnable registration) {
this.id = id;
this.title = title;
this.registration = registration;
}
public record DockItem(String id, Variable<String> title, WiseRunnable registration) {
}
30 changes: 30 additions & 0 deletions src/main/java/viewtify/ui/dock/DockModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2023 Nameless Production Committee
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://opensource.org/licenses/mit-license.php
*/
package viewtify.ui.dock;

import icy.manipulator.Icy;
import icy.manipulator.Icy.Property;
import kiss.Variable;
import kiss.WiseConsumer;

@Icy
public abstract class DockModel {

@Property
public abstract String id();

@Property
public Variable<String> title() {
return Variable.of(id());
}

@Property
public abstract WiseConsumer<Dock> register();
}
39 changes: 29 additions & 10 deletions src/main/java/viewtify/ui/dock/DockRegister.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.function.UnaryOperator;

import kiss.Extensible;
import kiss.I;
Expand Down Expand Up @@ -51,7 +52,16 @@ protected DockRegister() {
* @param view
*/
protected void register(Class<? extends View> view) {
register(I.make(view), 1);
register(estimateId(1), I.make(view), UnaryOperator.identity());
}

/**
* Register the specified view.
*
* @param view
*/
protected void register(Class<? extends View> view, UnaryOperator<DockRecommendedLocation> option) {
register(estimateId(1), I.make(view), option);
}

/**
Expand All @@ -60,22 +70,30 @@ protected void register(Class<? extends View> view) {
* @param view
*/
protected void register(View view) {
register(view, 1);
register(estimateId(1), view, UnaryOperator.identity());
}

/**
* Register the specified view.
*
* @param view
*/
private void register(View view, int depth) {
protected void register(View view, UnaryOperator<DockRecommendedLocation> option) {
register(estimateId(1), view, option);
}

/**
* Register the specified view.
*
* @param view
*/
private void register(String id, View view, UnaryOperator<DockRecommendedLocation> option) {
Objects.requireNonNull(view);
String id = estimateId(depth + 1);

if (inspect) {
independents.add(new DockItem(id, view.title(), () -> register(view)));
independents.add(new DockItem(id, view.title(), () -> register(id, view, option)));
} else {
DockSystem.register(id).text(view.title()).contentsLazy(tab -> view);
DockSystem.register(id, option).text(view.title()).contentsLazy(tab -> view);
}
}

Expand Down Expand Up @@ -107,14 +125,15 @@ public List<DockItem> queryIndependentDocks() {
}

/**
* Request tab registration by id.
*
* @param id
* @return
*/
boolean request(String id) {
protected boolean queryBy(String id) {
for (DockItem item : independents) {
if (item.id.equals(id)) {
item.registration.run();
System.out.println("Register " + item.id);
if (item.id().equals(id)) {
item.registration().run();
return true;
}
}
Expand Down
20 changes: 5 additions & 15 deletions src/main/java/viewtify/ui/dock/DockSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;

import kiss.I;
import kiss.JSON;
import kiss.Managed;
Expand Down Expand Up @@ -267,24 +268,14 @@ 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);
area.location = o.recommendedArea;
area.setViewportRatio(o.recommendedRatio);
opened.add(id);
return tab;
}

/**
* Register a new view within this dock system.
* <p/>
* The Position will give an advice where this view should be placed.
*
* @param id The view to register.
*/
public static void register(String id, UnaryOperator<DockRecommendedLocation> option, WiseConsumer<UITab> tab) {

}

/**
* Open new window with the specified {@link RootArea}.
*
Expand Down Expand Up @@ -317,18 +308,17 @@ private static void openNewWindow(RootArea area, Bounds bounds, EventHandler<Win
*
* @param defaultLayout
*/
public static void registerLayout(WiseRunnable defaultLayout) {
public static void initializeLayout(WiseRunnable defaultLayout) {
Objects.requireNonNull(defaultLayout);

DockLayout layout = layout();
if (Locator.file(layout.locate()).isAbsent()) {
// use default setup
defaultLayout.run();
} else {
layout.find(TabArea.class).effect(x -> System.out.println(x.getIds())).flatIterable(TabArea::getIds).to(id -> {
System.out.println(id);
layout.find(TabArea.class).flatIterable(TabArea::getIds).to(id -> {
for (DockRegister register : I.find(DockRegister.class)) {
if (register.request(id)) {
if (register.queryBy(id)) {
break;
}
}
Expand Down
27 changes: 18 additions & 9 deletions src/main/java/viewtify/ui/dock/TabArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
import java.util.List;
import java.util.Objects;

import javafx.application.Platform;
import javafx.beans.InvalidationListener;
import javafx.collections.ObservableList;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
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 @@ -90,7 +90,10 @@ class TabArea extends ViewArea<UITabPane> {
header.addEventHandler(DragEvent.DRAG_EXITED, e -> DockSystem.onHeaderDragExited(e, this));
header.addEventHandler(DragEvent.DRAG_DROPPED, e -> DockSystem.onHeaderDragDropped(e, this));
header.addEventHandler(DragEvent.DRAG_OVER, e -> DockSystem.onHeaderDragOver(e, this));
node.when(User.input(Key.Alt)).take(v -> node.items().size() == 1).to(e -> setHeader(!header.isVisible()));
node.when(User.input(Key.Alt)).take(v -> node.items().size() == 1).to(e -> {
setHeader(!header.isVisible());
DockSystem.requestSavingLayout();
});

// If the user has not set a title, the title of the selected tab will be used as the title
// of the window.
Expand Down Expand Up @@ -129,10 +132,7 @@ private final boolean isHeader() {
* @param show The header value to set.
*/
private final void setHeader(boolean show) {
Platform.runLater(() -> {
node.showHeader(show);
DockSystem.requestSavingLayout();
});
node.showHeader(show);
}

/**
Expand All @@ -141,7 +141,7 @@ private final void setHeader(boolean show) {
* @return The ids property.
*/
final List<String> getIds() {
return I.signal(node.ui.getTabs()).map(Tab::getId).toList();
return views;
}

/**
Expand Down Expand Up @@ -214,7 +214,7 @@ void remove(Tab tab, boolean checkEmpty) {
if (checkEmpty) {
handleEmpty();
}

updatePosition();
DockSystem.opened.remove(tab.getId());
}

Expand Down Expand Up @@ -260,9 +260,9 @@ public TabArea add(UITab tab, int position) {
default:
node.ui.getTabs().add(position, tab);
tab.setOnCloseRequest(e -> remove(tab, true));
views.add(tab.getId());

selectInitialTabOnlyOnce(tab);
updatePosition();
return this;
}
}
Expand Down Expand Up @@ -336,4 +336,13 @@ void select(String id) {
}
}
}

/**
* Update tab position.
*/
private void updatePosition() {
views = I.signal(node.ui.getTabs()).map(Tab::getId).toList();

DockSystem.requestSavingLayout();
}
}
18 changes: 9 additions & 9 deletions src/test/java/viewtify/ui/dock/DockRegisterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public void dock() {
assert items.size() == 1;

DockItem item = items.get(0);
assert item.id.equals("Dock");
assert item.title.is("Main");
assert item.registration != null;
assert item.id().equals("Dock");
assert item.title().is("Main");
assert item.registration() != null;
}

@Test
Expand All @@ -55,14 +55,14 @@ public void dockSub() {
assert items.size() == 2;

DockItem item = items.get(0);
assert item.id.equals("DockMain");
assert item.title.is("Main");
assert item.registration != null;
assert item.id().equals("DockMain");
assert item.title().is("Main");
assert item.registration() != null;

item = items.get(1);
assert item.id.equals("DockSub");
assert item.title.is("Sub");
assert item.registration != null;
assert item.id().equals("DockSub");
assert item.title().is("Sub");
assert item.registration() != null;
}

private static class MainView extends View {
Expand Down

0 comments on commit ebafde1

Please sign in to comment.