From 6a710da083c4f431e064602a274aae1e2afa5c10 Mon Sep 17 00:00:00 2001 From: dnutiu Date: Sat, 13 Apr 2024 17:10:08 +0300 Subject: [PATCH] make ApplicationMenuBar work on Windows --- .idea/misc.xml | 1 - .../imagetagger/ui/BasicServiceLocator.kt | 1 + .../dev/nuculabs/imagetagger/ui/MainPage.kt | 6 ++-- .../{programatic => }/ApplicationMenuBar.kt | 28 ++++++++++++++-- .../ui/controls/application-menu-bar.fxml | 5 +++ .../imagetagger/ui/main-window-view.fxml | 32 +++++++++++-------- 6 files changed, 52 insertions(+), 21 deletions(-) rename img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/controls/{programatic => }/ApplicationMenuBar.kt (52%) create mode 100644 img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/controls/application-menu-bar.fxml diff --git a/.idea/misc.xml b/.idea/misc.xml index fe0b0da..f937d01 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/BasicServiceLocator.kt b/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/BasicServiceLocator.kt index e31f6ec..85aa287 100644 --- a/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/BasicServiceLocator.kt +++ b/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/BasicServiceLocator.kt @@ -10,6 +10,7 @@ import dev.nuculabs.imagetagger.ai.IImageTagsPrediction */ class BasicServiceLocator private constructor() { internal lateinit var imageTagsPrediction: IImageTagsPrediction + internal lateinit var mainPageController: MainPageController // Singleton Pattern companion object { diff --git a/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/MainPage.kt b/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/MainPage.kt index 206a90a..05d6e9a 100644 --- a/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/MainPage.kt +++ b/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/MainPage.kt @@ -2,13 +2,11 @@ package dev.nuculabs.imagetagger.ui import dev.nuculabs.imagetagger.ai.IImageTagsPrediction import dev.nuculabs.imagetagger.ai.ImageTagsPrediction -import dev.nuculabs.imagetagger.ui.controls.programatic.ApplicationMenuBar import javafx.application.Application import javafx.application.Platform import javafx.fxml.FXMLLoader import javafx.scene.Scene import javafx.scene.image.Image -import javafx.scene.layout.BorderPane import javafx.stage.Stage import java.awt.Taskbar import java.awt.Toolkit @@ -39,6 +37,8 @@ class MainPage : Application() { // Initialize the controller. val mainPageController = fxmlLoader.getController() mainPageController.initialize() + // Set MainPage controller. + serviceLocator.mainPageController = fxmlLoader.getController() // Set up the stage. stage.title = "Image Tagger" @@ -51,7 +51,7 @@ class MainPage : Application() { } // Add menu bar - (scene.root as BorderPane).children.add(ApplicationMenuBar(mainPageController)) + // (scene.root as BorderPane).children.add(ApplicationMenuBar(mainPageController)) stage.show() } diff --git a/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/controls/programatic/ApplicationMenuBar.kt b/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/controls/ApplicationMenuBar.kt similarity index 52% rename from img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/controls/programatic/ApplicationMenuBar.kt rename to img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/controls/ApplicationMenuBar.kt index 1f34571..460f9ed 100644 --- a/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/controls/programatic/ApplicationMenuBar.kt +++ b/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/controls/ApplicationMenuBar.kt @@ -1,20 +1,42 @@ -package dev.nuculabs.imagetagger.ui.controls.programatic +package dev.nuculabs.imagetagger.ui.controls +import dev.nuculabs.imagetagger.ui.BasicServiceLocator import dev.nuculabs.imagetagger.ui.MainPageController import dev.nuculabs.imagetagger.ui.pages.AboutPage +import javafx.fxml.FXMLLoader import javafx.scene.control.Menu import javafx.scene.control.MenuBar import javafx.scene.control.MenuItem +import java.io.IOException /** * Used as the application menu bar. */ -class ApplicationMenuBar(private val mainPageController: MainPageController) : MenuBar() { +class ApplicationMenuBar : MenuBar() { private val fileMenu = Menu("File") private val aboutMenu = Menu("About") + private val mainPageController: MainPageController by lazy { + BasicServiceLocator.getInstance().mainPageController + } init { - useSystemMenuBarProperty().set(true) + // Load component + val fxmlLoader = FXMLLoader( + ImageTagsSessionHeader::class.java.getResource("application-menu-bar.fxml") + ) + fxmlLoader.setRoot(this) + fxmlLoader.setController(this) + try { + fxmlLoader.load() + } catch (exception: IOException) { + throw RuntimeException(exception) + } + + initialize() + } + + private fun initialize() { + useSystemMenuBarProperty().set(false) menus.addAll(fileMenu, aboutMenu) setupFileMenu() setupAboutMenu() diff --git a/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/controls/application-menu-bar.fxml b/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/controls/application-menu-bar.fxml new file mode 100644 index 0000000..e8fc2e9 --- /dev/null +++ b/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/controls/application-menu-bar.fxml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/main-window-view.fxml b/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/main-window-view.fxml index 5ec0c64..74b1975 100644 --- a/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/main-window-view.fxml +++ b/img-ui/src/main/resources/dev/nuculabs/imagetagger/ui/main-window-view.fxml @@ -5,31 +5,35 @@ + - - - - - - - - - - + + + + + + + + + +
+ + +