Skip to content

Commit

Permalink
fix: floating tabs now have a menubar copy and pass events to model e…
Browse files Browse the repository at this point in the history
…ditor controller
  • Loading branch information
sillydan1 committed Mar 8, 2024
1 parent 162a033 commit f1f69f0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
import org.slf4j.LoggerFactory;

import atlantafx.base.theme.Styles;
import dk.yalibs.yadi.DI;
import javafx.collections.ListChangeListener;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.control.MenuBar;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
Expand Down Expand Up @@ -59,7 +64,7 @@ private record InsertData(int index, TabPane insertPane) {}
* normal tabs and DrragableTabs mixed will cause issues!
* @param text the text to appear on the tag label.
*/
public DraggableTabController(String text) {
public DraggableTabController(String text, ModelEditorController editor) {
nameLabel = new Label(text);
setGraphic(nameLabel);
detachable = true;
Expand Down Expand Up @@ -123,6 +128,7 @@ public DraggableTabController(String text) {
if(!detachable)
return;
var newStage = new Stage();
var topPane = new BorderPane();
var pane = new TabPane();
pane.getStyleClass().add(TabPane.STYLE_CLASS_FLOATING);
tabPanes.add(pane);
Expand All @@ -133,8 +139,13 @@ public DraggableTabController(String text) {
if(pane.getTabs().isEmpty())
newStage.hide();
});
var spawnScene = new Scene(pane);
topPane.setCenter(pane);
topPane.setTop(DI.get(MenuBar.class));
var spawnScene = new Scene(topPane);
spawnScene.getStylesheets().add(getClass().getResource("/css/styles.css").toExternalForm());
spawnScene.addEventHandler(KeyEvent.ANY, e -> editor.onKeyEvent(e));
spawnScene.addEventHandler(MouseEvent.ANY, e -> editor.onMouseEvent(e));
// TODO: add closed event, and actually close the buffer
newStage.setScene(spawnScene);
newStage.initStyle(StageStyle.UTILITY);
newStage.setX(t.getScreenX());
Expand Down
23 changes: 23 additions & 0 deletions core/src/main/java/dk/gtz/graphedit/view/EditorController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Region;
Expand All @@ -43,6 +45,8 @@ public class EditorController {
@FXML
private BorderPane bottomBorderPane;
@FXML
private MenuBar menubar;
@FXML
private Menu runTargetsMenu;
@FXML
private Menu importFileMenu;
Expand Down Expand Up @@ -80,6 +84,25 @@ private void initProjectMenu() {
var project = DI.get(ViewModelProject.class);
updateRunTargets();
project.runTargets().addListener((e,o,n) -> updateRunTargets());
DI.add(MenuBar.class, () -> {
var newMenubar = new MenuBar();
newMenubar.setUseSystemMenuBar(menubar.isUseSystemMenuBar());
for(var menu : menubar.getMenus()) {
var newMenu = new Menu(menu.getText());
for(var item : menu.getItems()) {
if(item instanceof MenuItem) {
var newItem = new MenuItem(item.getText());
newItem.setOnAction(item.getOnAction());
newItem.setAccelerator(item.getAccelerator());
newMenu.getItems().add(newItem);
}
if(item instanceof SeparatorMenuItem)
newMenu.getItems().add(new SeparatorMenuItem());
}
newMenubar.getMenus().add(newMenu);
}
return newMenubar;
});
}

private void updateRunTargets() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ private void initTabpaneBufferContainer() {
var tabTitle = changedKey;
if(changedVal.metadata().containsKey("name"))
tabTitle = changedVal.metadata().get("name");
var tab = new DraggableTabController(tabTitle);
var editorController = new ModelEditorController(changedKey, changedVal, MetadataUtils.getSyntaxFactory(changedVal.metadata()));
var tab = new DraggableTabController(tabTitle, editorController);
var lastSavedModel = new SimpleObjectProperty<>(changedVal.toModel());
var lastSavedModelSyntax = new SimpleObjectProperty<>(MetadataUtils.getSyntaxFactory(changedVal.getSyntaxName().get()));
changedVal.addView(tab);
Expand All @@ -89,7 +90,6 @@ private void initTabpaneBufferContainer() {
if(changedVal.getViews().isEmpty())
DI.get(IBufferContainer.class).close(changedKey);
});
var editorController = new ModelEditorController(changedKey, changedVal, MetadataUtils.getSyntaxFactory(changedVal.metadata()));
tab.addEditor(editorController);
tabpane.getTabs().add(tab);
tabControllerMapping.put(tab, editorController);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public void onMouseEvent(MouseEvent e) {
* Propogate the provided key event to the appropriate tool.
* @param e The key event to handle.
*/
public void onKeyEvent(KeyEvent e) {
public void onKeyEvent(KeyEvent e) { // TODO: onKeyEvent and the rest of the family should be an interface
// TODO: the "isTargetDrawpane" field solution is hacky and doesnt work in detached tabs. It should be fixed, see https://github.com/sillydan1/graphedit/issues/17
var isTargetDrawpane = e.getTarget() == getParent().getParent();
var keyEvent = new ViewportKeyEvent(e, drawGroupTransform, isTargetDrawpane, syntaxFactory, resource.syntax(), bufferKey, settings);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/resources/dk/gtz/graphedit/view/Editor.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<BorderPane HBox.hgrow="ALWAYS" fx:id="primaryBorderPane">
<top>
<VBox fx:id="menubarTopBox">
<MenuBar useSystemMenuBar="true">
<MenuBar useSystemMenuBar="true" fx:id="menubar">
<Menu text="File">
<MenuItem onAction="#newModel" text="New Model File" accelerator="Shortcut+N"/>
<MenuItem onAction="#openModel" text="Open Model File" accelerator="Shortcut+O"/>
Expand Down

0 comments on commit f1f69f0

Please sign in to comment.