Skip to content

Commit

Permalink
Fix font setting issue
Browse files Browse the repository at this point in the history
Fonts for time labels was not correctly registered as resources. Registered fonts at App starting.
  • Loading branch information
tsathis committed Sep 19, 2020
1 parent 38d3814 commit 17ef7e8
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 50 deletions.
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@
</additionalClasspathElements>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
<configuration>
<mainClass>com.github.tharindusathis.jomodoro.App</mainClass>
<launcher>launch</launcher>
<compress>2</compress>
<jlinkZipName>jomodoro-timer</jlinkZipName>
<noManPages>true</noManPages>
<noHeaderFiles>true</noHeaderFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
58 changes: 37 additions & 21 deletions src/main/java/com/github/tharindusathis/jomodoro/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.github.tharindusathis.jomodoro.controller.FullScreenController;
import com.github.tharindusathis.jomodoro.controller.MainController;
import com.github.tharindusathis.jomodoro.controller.NotifyFlashScreenController;
import com.github.tharindusathis.jomodoro.util.Loggers;
import com.github.tharindusathis.jomodoro.util.Resources;
import javafx.application.Application;
import javafx.beans.binding.DoubleBinding;
import javafx.fxml.FXMLLoader;
Expand All @@ -13,20 +15,23 @@
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.IOException;
import java.util.logging.Level;

/**
* @author tharindusathis
*/
public class App extends Application
{
private static final double INIT_UI_SCALE_FACTOR = .4;
private Stage mainViewStage;
private Stage fullscreenViewStage;
private static final double INIT_UI_SCALE_FACTOR = .4;
private Stage notifyFlashScreenStage;

public static void main( String[] args )
{
Expand Down Expand Up @@ -89,12 +94,12 @@ private MainController createMainView() throws IOException
Scene scene = new Scene( rootPane, origW, origH );

// bind the scene's width and height to the scaling parameters on the group
DoubleBinding width = scene.widthProperty().divide( origW );
DoubleBinding width = scene.widthProperty().divide( origW );
group.scaleXProperty().bind( width );
group.scaleYProperty().bind( width );

mainViewStage = new Stage();
mainViewStage.initOwner(getUtilityStage());
mainViewStage.initOwner( getUtilityStage() );
mainViewStage.initStyle( StageStyle.TRANSPARENT );
scene.setFill( Color.TRANSPARENT );
mainViewStage.setScene( scene );
Expand All @@ -110,7 +115,6 @@ private MainController createMainView() throws IOException
return loader.getController();
}

private Stage notifyFlashScreenStage;
private NotifyFlashScreenController createNotifyFlashScreenView() throws IOException
{
FXMLLoader loader = new FXMLLoader(
Expand All @@ -134,9 +138,37 @@ private NotifyFlashScreenController createNotifyFlashScreenView() throws IOExcep
return loader.getController();
}

/**
* Get a utility styled stage.
*
* @return Utility styled stage
*/
private Stage getUtilityStage()
{
Stage utilityStage = new Stage();
utilityStage.initStyle( StageStyle.UTILITY );
utilityStage.setOpacity( 0 );
utilityStage.setHeight( 0 );
utilityStage.setWidth( 0 );
utilityStage.show();
return utilityStage;
}

@Override
public void start( Stage stage )
{
try
{
String fontRobotoPath = getClass().getResource( "/com/github/tharindusathis/jomodoro/fonts/Roboto-Medium.ttf" )
.toExternalForm();
Resources.addFont( Resources.CustomFont.ROBOTO_250, Font.loadFont( fontRobotoPath , 250 ) );
Resources.addFont( Resources.CustomFont.ROBOTO_87, Font.loadFont( fontRobotoPath, 87 ) );
}
catch( Exception e )
{
Loggers.COMMON_LOGGER.log( Level.SEVERE, e::getMessage );
}

MainController mainController = null;
FullScreenController fullscreenController = null;
NotifyFlashScreenController notifyFlashScreenController = null;
Expand All @@ -148,7 +180,7 @@ public void start( Stage stage )
}
catch( IOException e )
{
e.printStackTrace();
Loggers.COMMON_LOGGER.log( Level.SEVERE, e::getMessage );
}

final ControllerManager controllerManager = new ControllerManager();
Expand All @@ -160,22 +192,6 @@ public void start( Stage stage )
ControllerManager.View.NOTIFY_FLASH, notifyFlashScreenController, notifyFlashScreenStage );

mainViewStage.show();

}

/**
* Get a utility styled stage.
*
* @return Utility styled stage
*/
private Stage getUtilityStage(){
Stage utilityStage = new Stage();
utilityStage.initStyle(StageStyle.UTILITY);
utilityStage.setOpacity(0);
utilityStage.setHeight(0);
utilityStage.setWidth(0);
utilityStage.show();
return utilityStage;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.tharindusathis.jomodoro.timer.CountdownTask;
import com.github.tharindusathis.jomodoro.util.Constants;
import com.github.tharindusathis.jomodoro.util.Loggers;
import com.github.tharindusathis.jomodoro.util.Resources;
import javafx.animation.FadeTransition;
import javafx.application.Platform;
import javafx.beans.InvalidationListener;
Expand Down Expand Up @@ -90,6 +91,18 @@ void addTime( @SuppressWarnings( "SameParameterValue" ) int seconds )
void afterInitialize()
{
playSound();
try
{
Resources.getFont( Resources.CustomFont.ROBOTO_250 )
.ifPresent( font -> timeLabelTimerView.setFont( font ));

Resources.getFont( Resources.CustomFont.ROBOTO_87 )
.ifPresent( font -> timeLabelControlView.setFont( font ) );
}
catch( Exception e )
{
Loggers.COMMON_LOGGER.log(Level.WARNING, "Error Setting Fonts");
}
}

@FXML
Expand Down Expand Up @@ -269,7 +282,7 @@ else if( currentState.isRunning() && remainingSeconds > 0 )
{
setView( MainStageViews.TIMER );
}
else if( !currentState.isRunning() )
else if( !currentState.isRunning() && remainingSeconds == 0)
{
btnTimerPlayBreak.setVisible( currentState == State.WORK_STOP );
setView( MainStageViews.TIMER_STOP );
Expand Down Expand Up @@ -557,6 +570,9 @@ public void startTimer()
btnStart.getStyleClass().remove( "btn-start" );
btnStart.getStyleClass().add( "btn-pause" );

getControllerManager().flatMap( controllerManager -> controllerManager.getController( NotifyFlashScreenController.class ) )
.ifPresent( NotifyFlashScreenController::flashReset );

/* TODO: check whether this if this code needed: `setView( MainStageViews.TIMER );` */
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.tharindusathis.jomodoro.util;

import javafx.scene.text.Font;

import java.util.EnumMap;
import java.util.Map;
import java.util.Optional;

public final class Resources
{
private static final Map<CustomFont,Font> FONTS = new EnumMap<>( CustomFont.class );

private Resources()
{
}

public static Optional<Font> getFont( CustomFont name )
{
return Optional.ofNullable( FONTS.get( name ) );
}

public static void addFont( CustomFont name, Font font )
{
FONTS.put( name, font );
}

public enum CustomFont
{
ROBOTO_250, ROBOTO_87
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@

.lbl-timer {
-fx-text-fill: rgba(115,214,148,1.0);
-fx-font-size: 180;
}


Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');

.main-window-container{
-fx-max-width: 1440;
-fx-max-height: 1080;
Expand Down Expand Up @@ -206,15 +204,15 @@

.timer-label-timer-view{
-fx-text-fill: white;
-fx-font-size: 250;
-fx-padding: 0;
-fx-font-family: 'Roboto';
/*-fx-font-size: 250;*/
/*-fx-font-family: 'Roboto';*/
}
.time-label-control-view{
-fx-text-fill: white;
-fx-font-size: 87;
-fx-padding: 0;
-fx-font-family: 'Roboto';
/*-fx-font-size: 87;*/
/*-fx-font-family: 'Roboto';*/
}

.control-area-stack-pane{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,12 @@
<String fx:value="txt-input-small"/>
</styleClass>
<center>
<TextField fx:id="timeTextBox" alignment="CENTER_RIGHT" prefHeight="80.0" prefWidth="126.0"
<TextField fx:id="timeTextBox" alignment="CENTER_RIGHT" prefHeight="80.0" prefWidth="126.0"
styleClass="txt-input" stylesheets="@../styles/fullscreen-stage.css"
text="999" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0"/>
</BorderPane.margin>
<font>
<Font name="Webdings" size="17.0"/>
</font>
</TextField>
</center>
<left>
Expand Down Expand Up @@ -131,9 +128,6 @@
<BorderPane.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0"/>
</BorderPane.margin>
<font>
<Font name="Webdings" size="17.0"/>
</font>
</TextField>
</center>
<left>
Expand All @@ -158,14 +152,11 @@
</styleClass>
<center>
<TextField fx:id="tagTextBox" alignment="CENTER_RIGHT" prefHeight="80.0" prefWidth="126.0"
promptText="task label..." styleClass="txt-input"
promptText="I will focus on ..." styleClass="txt-input"
stylesheets="@../styles/fullscreen-stage.css" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0"/>
</BorderPane.margin>
<font>
<Font name="Webdings" size="17.0"/>
</font>
</TextField>
</center>
<left>
Expand Down Expand Up @@ -214,9 +205,6 @@
</HBox.margin>
<Label fx:id="lblTimer" styleClass="lbl-timer"
stylesheets="@../styles/fullscreen-stage.css" text="00:00">
<font>
<Font name="Webdings" size="180.0"/>
</font>
</Label>
<ProgressBar fx:id="breakProgressBar" prefHeight="18.0" prefWidth="456.0"
progress="0.0"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import java.lang.String?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tooltip?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>

<StackPane fx:id="parentContainer" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="100.0" styleClass="parent-container" stylesheets="@../styles/main-stage.css" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.github.tharindusathis.jomodoro.controller.MainController">
<StackPane>
<BorderPane fx:id="timerView" prefHeight="200.0" prefWidth="200.0" styleClass="timer-view" stylesheets="@../styles/main-stage.css">
<bottom>
<AnchorPane maxHeight="92.0" minHeight="92.0" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<Button fx:id="tagBtnTimerView" mnemonicParsing="false" styleClass="tag-btn-timer-view" stylesheets="@../styles/main-stage.css" text="..." AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="40.0" />
<Button fx:id="tagBtnTimerView" mnemonicParsing="false" styleClass="tag-btn-timer-view" stylesheets="@../styles/main-stage.css" text="My task" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="40.0" />
</AnchorPane>
</bottom>
<top>
Expand Down Expand Up @@ -68,16 +75,21 @@
<StackPane fx:id="controlView">
<BorderPane styleClass="timer-view" stylesheets="@../styles/main-stage.css">
<top>
<AnchorPane BorderPane.alignment="CENTER">
<AnchorPane prefHeight="115.0" prefWidth="720.0" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets />
</BorderPane.margin>
<Label fx:id="timeLabelControlView" layoutX="21.0" layoutY="-12.0" styleClass="time-label-control-view" stylesheets="@../styles/main-stage.css" text="26:48" AnchorPane.leftAnchor="40.0" AnchorPane.topAnchor="-3.0">
<Label fx:id="timeLabelControlView" contentDisplay="BOTTOM" layoutX="42.0" layoutY="-1.0" prefHeight="106.0" prefWidth="227.0" styleClass="time-label-control-view" stylesheets="@../styles/main-stage.css" text="26:48" AnchorPane.leftAnchor="40.0">
<opaqueInsets>
<Insets left="30.0" right="30.0" />
</opaqueInsets>
</Label>
<Button fx:id="tagBtnControlView" maxWidth="422.0" mnemonicParsing="false" prefHeight="70.0" prefWidth="422.0" styleClass="tag-btn-control-view" stylesheets="@../styles/main-stage.css" text="My Task" AnchorPane.rightAnchor="40.0" AnchorPane.topAnchor="25.0" />
<StackPane layoutX="270.0" layoutY="25.0" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="40.0"
AnchorPane.topAnchor="0.0">
<Button fx:id="tagBtnControlView" maxWidth="422.0" mnemonicParsing="false" prefHeight="70.0"
prefWidth="422.0" styleClass="tag-btn-control-view" stylesheets="@../styles/main-stage.css"
text="My Task"/>
</StackPane>
</AnchorPane>
</top>
<bottom>
Expand Down

0 comments on commit 17ef7e8

Please sign in to comment.