Skip to content

Commit

Permalink
chore: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
HouariZegai committed Apr 17, 2022
1 parent 25fb39b commit 81231ed
Show file tree
Hide file tree
Showing 15 changed files with 605 additions and 644 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# http://editorconfig.org/

root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
205 changes: 105 additions & 100 deletions src/main/java/com/houarizegai/prayertimes/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,115 +13,120 @@
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Optional;
import java.util.logging.Logger;

public class App extends Application {
public static Stage stage;

@Override
public void start(Stage stage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/views/PrayerTimes.fxml"));
stage.setScene(new Scene(root));
} catch(IOException ioe) {
ioe.printStackTrace();
}

// Make icon to the app
stage.getIcons().add(new Image("/images/icon-app-64px.png"));
stage.initStyle(StageStyle.UNDECORATED);
App.stage = stage;
public static Stage stage;
private static final Logger LOG = Logger.getLogger(App.class.getName());

// instructs the javafx system not to exit implicitly when the last application window is shut.
Platform.setImplicitExit(false);
@Override
public void start(Stage stage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/views/PrayerTimes.fxml"));
stage.setScene(new Scene(root));
} catch (IOException ioe) {
ioe.printStackTrace();
}

// sets up the tray icon (using awt code run on the swing thread).
javax.swing.SwingUtilities.invokeLater(this::addAppToTray);
// Make icon to the app
stage.getIcons().add(new Image("/images/icon-app-64px.png"));
stage.initStyle(StageStyle.UNDECORATED);
App.stage = stage;

showStage();
}
// instructs the javafx system not to exit implicitly when the last application window is shut.
Platform.setImplicitExit(false);

// Sets up a system tray icon for the application.
private void addAppToTray() {
try {
// ensure awt toolkit is initialized.
java.awt.Toolkit.getDefaultToolkit();

// app requires system tray support, just exit if there is no support.
if (!java.awt.SystemTray.isSupported()) {
System.out.println("No system tray support, application exiting.");
Platform.exit();
}

// set up a system tray icon.
java.awt.SystemTray tray = java.awt.SystemTray.getSystemTray();

java.awt.Image image = ImageIO.read(App.class.getResourceAsStream("/images/icon-app-16px.png"));

java.awt.TrayIcon trayIcon = new java.awt.TrayIcon(image);

// if the user clicks on the tray icon, show the main app stage.
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
Platform.runLater(() -> showStage());
}
}
});

// if the user selects the default menu item (which includes the app name),
// show the main app stage.
java.awt.MenuItem openItem = new java.awt.MenuItem("Open");
openItem.addActionListener(event -> Platform.runLater(this::showStage));

// hide the main app stage.
java.awt.MenuItem hideItem = new java.awt.MenuItem("Hide");
hideItem.addActionListener(event -> Platform.runLater(this::hideStage));


// the convention for tray icons seems to be to set the default icon for opening
// the application stage in a bold font.
java.awt.Font defaultFont = java.awt.Font.decode(null);
java.awt.Font boldFont = defaultFont.deriveFont(java.awt.Font.BOLD);
openItem.setFont(boldFont);

// to really exit the application, the user must go to the system tray icon
// and select the exit option, this will shutdown JavaFX and remove the
// tray icon (removing the tray icon will also shut down AWT).
java.awt.MenuItem exitItem = new java.awt.MenuItem("Exit");
exitItem.addActionListener(event -> {
Platform.exit();
tray.remove(trayIcon);
});

// setup the popup menu for the application.
final java.awt.PopupMenu popup = new java.awt.PopupMenu();
popup.add(openItem);
popup.add(hideItem);
popup.addSeparator();
popup.add(exitItem);
trayIcon.setPopupMenu(popup);

// add the application tray icon to the system tray.
tray.add(trayIcon);
} catch (java.awt.AWTException | IOException e) {
System.out.println("Unable to init system tray");
e.printStackTrace();
}
}
// sets up the tray icon (using awt code run on the swing thread).
javax.swing.SwingUtilities.invokeLater(this::addAppToTray);

private void showStage() {
if(stage != null) {
stage.show();
stage.toFront();
}
}
showStage();
}

private void hideStage() {
if(stage != null)
stage.hide();
}
// Sets up a system tray icon for the application.
private void addAppToTray() {
try {
// ensure awt toolkit is initialized.
java.awt.Toolkit.getDefaultToolkit();

// app requires system tray support, just exit if there is no support.
if (!java.awt.SystemTray.isSupported()) {
System.out.println("No system tray support, application exiting.");
Platform.exit();
}

// set up a system tray icon.
java.awt.SystemTray tray = java.awt.SystemTray.getSystemTray();

public static void main(String[] args) {
launch(args);
java.awt.Image image = ImageIO.read(App.class.getResourceAsStream("/images/icon-app-16px.png"));

java.awt.TrayIcon trayIcon = new java.awt.TrayIcon(image);

// if the user clicks on the tray icon, show the main app stage.
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
Platform.runLater(App.this::showStage);
}
}
});

// if the user selects the default menu item (which includes the app name),
// show the main app stage.
java.awt.MenuItem openItem = new java.awt.MenuItem("Open");
openItem.addActionListener(event -> Platform.runLater(this::showStage));

// hide the main app stage.
java.awt.MenuItem hideItem = new java.awt.MenuItem("Hide");
hideItem.addActionListener(event -> Platform.runLater(this::hideStage));

// the convention for tray icons seems to be to set the default icon for opening
// the application stage in a bold font.
java.awt.Font defaultFont = java.awt.Font.decode(null);
java.awt.Font boldFont = defaultFont.deriveFont(java.awt.Font.BOLD);
openItem.setFont(boldFont);

// to really exit the application, the user must go to the system tray icon
// and select the exit option, this will shutdown JavaFX and remove the
// tray icon (removing the tray icon will also shut down AWT).
java.awt.MenuItem exitItem = new java.awt.MenuItem("Exit");
exitItem.addActionListener(event -> {
Platform.exit();
tray.remove(trayIcon);
});

// setup the popup menu for the application.
final java.awt.PopupMenu popup = new java.awt.PopupMenu();
popup.add(openItem);
popup.add(hideItem);
popup.addSeparator();
popup.add(exitItem);
trayIcon.setPopupMenu(popup);

// add the application tray icon to the system tray.
tray.add(trayIcon);
} catch (java.awt.AWTException | IOException e) {
LOG.warning("Unable to init system tray");
e.printStackTrace();
}
}

private void showStage() {
Optional.ofNullable(stage).ifPresent(stg -> {
stage.show();
stage.toFront();
});
}

private void hideStage() {
Optional.ofNullable(stage).ifPresent(stg -> {
stage.hide();
});
}

public static void main(String[] args) {
launch(args);
}
}
Loading

0 comments on commit 81231ed

Please sign in to comment.