From cbd104ae9d63d44c3294d47de5425e025fa59018 Mon Sep 17 00:00:00 2001 From: Denis-Cosmin NUTIU Date: Sat, 6 Apr 2024 17:20:19 +0300 Subject: [PATCH] add application menu bar --- .../dev/nuculabs/imagetagger/ui/MainPage.kt | 8 +++- .../imagetagger/ui/MainPageController.kt | 1 + .../imagetagger/ui/alerts/AboutAlert.kt | 21 ++++++++++ .../ui/controls/ApplicationMenuBar.kt | 38 +++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/alerts/AboutAlert.kt create mode 100644 img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/controls/ApplicationMenuBar.kt 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 c4a4c70..4d7c51a 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 @@ -1,10 +1,12 @@ package dev.nuculabs.imagetagger.ui import dev.nuculabs.imagetagger.ai.ImageTagsPrediction +import dev.nuculabs.imagetagger.ui.controls.ApplicationMenuBar import javafx.application.Application 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 @@ -30,7 +32,8 @@ class MainPage : Application() { val scene = Scene(fxmlLoader.load(), 640.0, 760.0) // Initialize the controller. - fxmlLoader.getController().initialize() + val mainPageController = fxmlLoader.getController() + mainPageController.initialize() // Set up the stage. stage.title = "Image Tagger" @@ -38,6 +41,9 @@ class MainPage : Application() { stage.minWidth = 640.0 stage.minHeight = 760.0 + // Add menu bar + (scene.root as BorderPane).children.add(ApplicationMenuBar(mainPageController)) + stage.show() } diff --git a/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/MainPageController.kt b/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/MainPageController.kt index fc32f73..c31475e 100644 --- a/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/MainPageController.kt +++ b/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/MainPageController.kt @@ -75,6 +75,7 @@ class MainPageController { HBox.setHgrow(progressBar, Priority.ALWAYS) HBox.setHgrow(cancelButton, Priority.ALWAYS) } + /** * Prompts the user to select files then predicts tags for the selected image files. */ diff --git a/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/alerts/AboutAlert.kt b/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/alerts/AboutAlert.kt new file mode 100644 index 0000000..88897e6 --- /dev/null +++ b/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/alerts/AboutAlert.kt @@ -0,0 +1,21 @@ +package dev.nuculabs.imagetagger.ui.alerts + +import javafx.scene.control.Alert +import javafx.scene.layout.Region + +/** + * Represents the alert shown when the user clicks on About. + */ +class AboutAlert : Alert(AlertType.INFORMATION) { + init { + title = "About ImageTagger" + headerText = "" + contentText = "Image Tagger is an application that predicts an image's tags using deep-learning. " + + "It is useful for photographers who want to improve their workflow by auto-tagging images.\n\n" + + "Author: Denis-Cosmin Nutiu\n\n" + + "Website: blog.nuculabs.dev\n" + + "Github: https://github.com/dnutiu/ImageTagger" + dialogPane.minHeight = Region.USE_PREF_SIZE + show() + } +} \ No newline at end of file diff --git a/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/controls/ApplicationMenuBar.kt b/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/controls/ApplicationMenuBar.kt new file mode 100644 index 0000000..5b7a58d --- /dev/null +++ b/img-ui/src/main/kotlin/dev/nuculabs/imagetagger/ui/controls/ApplicationMenuBar.kt @@ -0,0 +1,38 @@ +package dev.nuculabs.imagetagger.ui.controls + +import dev.nuculabs.imagetagger.ui.MainPageController +import dev.nuculabs.imagetagger.ui.alerts.AboutAlert +import javafx.scene.control.Menu +import javafx.scene.control.MenuBar +import javafx.scene.control.MenuItem + +/** + * Used as the application menu bar. + */ +class ApplicationMenuBar(private val mainPageController: MainPageController) : MenuBar() { + private val fileMenu = Menu("File") + private val aboutMenu = Menu("About") + + init { + useSystemMenuBarProperty().set(true) + menus.addAll(fileMenu, aboutMenu) + setupFileMenu() + setupAboutMenu() + } + + private fun setupAboutMenu() { + val aboutMenuItem = MenuItem("About") + aboutMenuItem.setOnAction { + AboutAlert() + } + aboutMenu.items.add(aboutMenuItem) + } + + private fun setupFileMenu() { + val tagImagesMenuItem = MenuItem("Tag Images") + tagImagesMenuItem.setOnAction { + mainPageController.onTagImagesButtonClick() + } + fileMenu.items.add(tagImagesMenuItem) + } +} \ No newline at end of file