Skip to content

Commit

Permalink
feat: remake dock api
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Feb 7, 2024
1 parent d739b4c commit 079fdfd
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 55 deletions.
61 changes: 48 additions & 13 deletions src/main/java/viewtify/ui/dock/DockModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.function.UnaryOperator;

import icy.manipulator.Icy;
import icy.manipulator.Icy.Overload;
import icy.manipulator.Icy.Property;
import kiss.I;
import kiss.Variable;
Expand All @@ -21,11 +22,53 @@
@Icy
public abstract class DockModel {

/**
* Set the View class to be displayed.
*
* @return
*/
@Property
public abstract Class<? extends View> view();

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

/**
* Determines the area to be displayed when showing.
*
* @return
*/
@Property
public UnaryOperator<DockRecommendedLocation> location() {
return UnaryOperator.identity();
}

/**
* Sets whether or not to display this information during the initial layout.
*
* @return
*/
@Property
public boolean initialView() {
return false;
}

/**
* Set as the View to be displayed during the initial layout.
*
* @return
*/
@Overload("initialView")
private boolean showOnInitial() {
return true;
}

public String id() {
return I.make(view()).id();
Expand All @@ -35,18 +78,10 @@ public Variable<String> title() {
return I.make(view()).title();
}

@Property
public UnaryOperator<DockRecommendedLocation> location() {
return UnaryOperator.identity();
}

public void register() {
/**
* Show view.
*/
public void show() {
registration().accept((Dock) this);
}

public static Dock of(Class<? extends View> type) {
return Dock.with.view(type).registration(dock -> {
DockSystem.register(dock.id()).text(dock.title()).contentsLazy(tab -> I.make(dock.view()));
});
}
}
19 changes: 13 additions & 6 deletions src/main/java/viewtify/ui/dock/DockProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public abstract class DockProvider implements Extensible {

/** The managed independent dock items. */
private List<Dock> independents;
private List<Dock> docks;

private List<<TypedDock, Class>> typed;

Expand All @@ -35,14 +35,14 @@ protected DockProvider() {
}

private List<Dock> docks() {
if (independents == null) {
independents = I.signal(getClass().getFields())
if (docks == null) {
docks = I.signal(getClass().getFields())
.take(field -> Modifier.isFinal(field.getModifiers()) && field.getType() == Dock.class)
.map(field -> (Dock) field.get(this))
.sort(Comparator.comparing(Dock::id))
.toList();
}
return independents;
return docks;
}

private List<<TypedDock, Class>> typed() {
Expand All @@ -59,10 +59,17 @@ private List<Dock> docks() {
/**
* Query all independent views.
*/
public List<Dock> queryIndependentDocks() {
public List<Dock> findDocks() {
return docks();
}

/**
* Query all independent views.
*/
public List<TypedDock> findTypedDocks() {
return typed().stream().map(x -> x.).toList();
}

/**
* Request registering dock by id.
*
Expand All @@ -84,7 +91,7 @@ protected boolean register(String id) {

for (<TypedDock, Class> dock : typed()) {
if (dock..id.equalsIgnoreCase(prefix)) {
dock..register(I.transform(param, dock.));
dock..show(I.transform(param, dock.));
}
}
}
Expand Down
50 changes: 24 additions & 26 deletions src/main/java/viewtify/ui/dock/DockSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;

import kiss.I;
import kiss.Managed;
import kiss.Signal;
Expand Down Expand Up @@ -265,7 +264,6 @@ 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);
Expand Down Expand Up @@ -302,16 +300,23 @@ private static void openNewWindow(RootArea area, Bounds bounds, EventHandler<Win

/**
* Register the layout.
*
* @param defaultLayout
*/
public static void initializeLayout(WiseRunnable defaultLayout) {
Objects.requireNonNull(defaultLayout);

public static void initialize(WiseConsumer<UILabel> menuBuilder) {
DockLayout layout = layout();
if (Locator.file(layout.locate()).isAbsent()) {
// use default setup
defaultLayout.run();
for (DockProvider provider : I.find(DockProvider.class)) {
for (Dock dock : provider.findDocks()) {
if (dock.initialView) {
dock.show();
}
}

for (TypedDock dock : provider.findTypedDocks()) {
for (Object param : dock.showOnInitial) {
dock.show(param);
}
}
}
} else {
layout.find(TabArea.class).flatIterable(TabArea::getIds).to(id -> {
for (DockProvider register : I.find(DockProvider.class)) {
Expand All @@ -321,6 +326,16 @@ public static void initializeLayout(WiseRunnable defaultLayout) {
}
});
}

if (menuBuilder != null) {
menuBuilders.add(menuBuilder);

for (RootArea area : layout().roots) {
area.findAll(TabArea.class).to(x -> {
x.node.registerIcon(menuBuilder);
});
}
}
}

public static void registerBuilder(String pattern, WiseRunnable builder) {
Expand All @@ -333,23 +348,6 @@ public static <T> void registerBuilder(String pattern, Class<T> type, WiseConsum
}
}

/**
* Register the menu on tab header.
*
* @param builder A menu builder.
*/
public static void registerMenu(WiseConsumer<UILabel> builder) {
if (builder != null) {
menuBuilders.add(builder);

for (RootArea area : layout().roots) {
area.findAll(TabArea.class).to(x -> {
x.node.registerIcon(builder);
});
}
}
}

/**
* Manage all docking windows and tabs.
*/
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/viewtify/ui/dock/TypedDockModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package viewtify.ui.dock;

import java.util.List;
import java.util.function.UnaryOperator;

import icy.manipulator.Icy;
Expand All @@ -31,7 +32,22 @@ public UnaryOperator<DockRecommendedLocation> location() {
return UnaryOperator.identity();
}

public void register(T param) {
/**
* Set as the View to be displayed during the initial layout.
*
* @return
*/
@Property
public List<T> showOnInitial() {
return List.of();
}

/**
* Show view with the specified parameter.
*
* @param param
*/
public void show(T param) {
UITab tab = DockSystem.register(id() + " " + I.transform(param, String.class));
registration().accept(tab, param);
}
Expand Down
4 changes: 0 additions & 4 deletions src/project/java/viewtify/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
*/
package viewtify;

import javax.lang.model.SourceVersion;

public class Project extends bee.api.Project {

{
Expand Down Expand Up @@ -45,8 +43,6 @@ public class Project extends bee.api.Project {
* Browser manipulation
""");

require(SourceVersion.RELEASE_21, SourceVersion.RELEASE_21);

require("com.github.teletha", "altfx");
require("com.github.teletha", "sinobu");
require("com.github.teletha", "psychopath");
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/viewtify/ui/dock/DockRegisterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class DockRegisterTest {
void register() {
@SuppressWarnings("unused")
class Register extends DockProvider {
public final Dock main = Dock.of(MainView.class);
public final Dock main = Dock.with.view(MainView.class);
}

Register register = I.make(Register.class);
List<Dock> items = register.queryIndependentDocks();
List<Dock> items = register.findDocks();
assert items.size() == 1;

Dock item = items.get(0);
Expand All @@ -39,13 +39,13 @@ class Register extends DockProvider {
void registerMultiple() {
@SuppressWarnings("unused")
class Register extends DockProvider {
public final Dock main = Dock.of(MainView.class);
public final Dock main = Dock.with.view(MainView.class);

public final Dock sub = Dock.of(SubView.class);
public final Dock sub = Dock.with.view(SubView.class);
}

Register register = I.make(Register.class);
List<Dock> items = register.queryIndependentDocks();
List<Dock> items = register.findDocks();
assert items.size() == 2;

Dock item = items.get(0);
Expand Down

0 comments on commit 079fdfd

Please sign in to comment.