diff --git a/src/main/java/eu/m724/talkpages/IndexResource.java b/src/main/java/eu/m724/talkpages/IndexResource.java index 6ec0d1a..893f252 100644 --- a/src/main/java/eu/m724/talkpages/IndexResource.java +++ b/src/main/java/eu/m724/talkpages/IndexResource.java @@ -1,5 +1,6 @@ package eu.m724.talkpages; +import eu.m724.talkpages.theme.ThemeService; import io.quarkus.qute.CheckedTemplate; import io.quarkus.qute.TemplateInstance; import io.vertx.core.http.Cookie; @@ -15,6 +16,7 @@ import jakarta.ws.rs.core.Response; import org.jboss.resteasy.reactive.RestResponse.Status; import java.net.URI; +import java.util.List; @Path("/") public class IndexResource { @@ -24,36 +26,32 @@ public class IndexResource { @Inject HttpServerRequest request; + @Inject + ThemeService themeService; + @CheckedTemplate public static class Templates { - public static native TemplateInstance index(); + public static native TemplateInstance index(List themes, String currentTheme); } @GET @Produces(MediaType.TEXT_HTML) public TemplateInstance index() { - return Templates.index(); + + Cookie cookie = request.getCookie("theme"); + String currentTheme = cookie != null ? cookie.getValue() : null; + + return Templates.index(themeService.getThemes(), currentTheme); } @GET @Path("/theme") - public Response toggleDark() { - Cookie cookie = request.getCookie("dark"); - - NewCookie newCookie; - if (cookie == null) { - newCookie = new NewCookie.Builder("dark") + public Response toggleTheme(@QueryParam("theme") String theme) { + NewCookie newCookie = new NewCookie.Builder("theme") .path("/") - .value("1") + .value(theme) .maxAge(31560000) .build(); - } else { - newCookie = new NewCookie.Builder("dark") - .path("/") - .value("0") - .maxAge(0) - .build(); - } return Response.temporaryRedirect(URI.create("/")).cookie(newCookie).build(); } diff --git a/src/main/java/eu/m724/talkpages/Startup.java b/src/main/java/eu/m724/talkpages/Startup.java index 50d7020..c7c03ee 100644 --- a/src/main/java/eu/m724/talkpages/Startup.java +++ b/src/main/java/eu/m724/talkpages/Startup.java @@ -3,6 +3,7 @@ package eu.m724.talkpages; import eu.m724.talkpages.orm.entity.auth.Account; import eu.m724.talkpages.orm.entity.content.Page; import eu.m724.talkpages.orm.entity.content.PageRevision; +import eu.m724.talkpages.theme.ThemeService; import io.quarkus.runtime.LaunchMode; import io.quarkus.runtime.StartupEvent; import io.vertx.ext.web.Router; @@ -19,6 +20,9 @@ public class Startup { @Inject LaunchMode launchMode; + @Inject + ThemeService themeService; + @ConfigProperty(name = "talkpages.systemUser.name") private String username; @@ -28,8 +32,13 @@ public class Startup { .handler(StaticHandler.create("static/")); } - @Transactional public void examplePage(@Observes StartupEvent ignoredEvent) { + initDatabase(); + themeService.indexThemes(); + } + + @Transactional + public void initDatabase() { if (Account.findByName(username) != null) { // system user exists so assuming this is not the first run return; diff --git a/src/main/java/eu/m724/talkpages/template/ThemeExtension.java b/src/main/java/eu/m724/talkpages/template/ThemeExtension.java index 1be5d6f..6abae51 100644 --- a/src/main/java/eu/m724/talkpages/template/ThemeExtension.java +++ b/src/main/java/eu/m724/talkpages/template/ThemeExtension.java @@ -1,21 +1,27 @@ package eu.m724.talkpages.template; -import eu.m724.talkpages.RedirectService; +import eu.m724.talkpages.theme.ThemeService; import io.quarkus.arc.Arc; import io.quarkus.qute.TemplateExtension; -import io.vertx.core.Vertx; import io.vertx.core.http.Cookie; import io.vertx.core.http.HttpServerRequest; -import jakarta.inject.Inject; -import jakarta.ws.rs.core.Request; @TemplateExtension(namespace = "theme") public class ThemeExtension { - public static boolean darkTheme() { - // this is bad + public static String getTheme() { + // I don't like those arc containers HttpServerRequest request = Arc.container().instance(HttpServerRequest.class).get(); - Cookie cookie = request.getCookie("dark"); + ThemeService themeService = Arc.container().instance(ThemeService.class).get(); - return cookie == null || cookie.getValue().equals("0"); + Cookie cookie = request.getCookie("theme"); + + if (cookie != null) { + String theme = cookie.getValue(); + + if (themeService.getThemes().contains(theme)) + return theme; + } + + return themeService.getDefaultTheme(); } } diff --git a/src/main/java/eu/m724/talkpages/theme/ThemeService.java b/src/main/java/eu/m724/talkpages/theme/ThemeService.java new file mode 100644 index 0000000..d07788d --- /dev/null +++ b/src/main/java/eu/m724/talkpages/theme/ThemeService.java @@ -0,0 +1,45 @@ +package eu.m724.talkpages.theme; + +import jakarta.enterprise.context.ApplicationScoped; +import org.eclipse.microprofile.config.inject.ConfigProperty; + +import java.io.File; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +@ApplicationScoped +public class ThemeService { + @ConfigProperty(name = "talkpages.theme.default") + private String defaultTheme; + + private List themes = new ArrayList<>(); + + public List getThemes() { + return themes; + } + + public String getDefaultTheme() { + return defaultTheme; + } + + public void indexThemes() { + URL url = getClass().getClassLoader().getResource("static" + File.separator + "theme"); + if (url == null) return; + + File directory = new File(url.getPath()); + if (!directory.isDirectory()) return; + + File[] contents = directory.listFiles(); + if (contents == null) return; + + themes = Arrays.stream(contents) + .map(File::getName) + .filter(fn -> fn.endsWith(".css")) + .map(fn -> fn.substring(0, fn.length() - 4)) + .toList(); + + System.out.printf("Found %d themes: %s\n", themes.size(), String.join(", ", themes)); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index d0e7e4d..993aea0 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,6 @@ talkpages.homePage=/ talkpages.systemUser.name=System +talkpages.theme.default=light quarkus.http.auth.basic=true diff --git a/src/main/resources/static/dark.css b/src/main/resources/static/theme/dark.css similarity index 84% rename from src/main/resources/static/dark.css rename to src/main/resources/static/theme/dark.css index eaf9f0c..a43a4a0 100644 --- a/src/main/resources/static/dark.css +++ b/src/main/resources/static/theme/dark.css @@ -1,5 +1,5 @@ body { - background: #202020; + background: #222; color: white; } diff --git a/src/main/resources/static/theme/light.css b/src/main/resources/static/theme/light.css new file mode 100644 index 0000000..23d4fb4 --- /dev/null +++ b/src/main/resources/static/theme/light.css @@ -0,0 +1 @@ +/* stub */ \ No newline at end of file diff --git a/src/main/resources/templates/IndexResource/index.html b/src/main/resources/templates/IndexResource/index.html index 05cae4f..803d97f 100644 --- a/src/main/resources/templates/IndexResource/index.html +++ b/src/main/resources/templates/IndexResource/index.html @@ -15,15 +15,24 @@ Login or register {/if} -
  • - {#if theme:darkTheme} - Light mode - {#else} - Dark mode - {/if} -
  • -
  • Terms of Service
  • +
  • +
    + + + +
    +
  • + Terms of Service +
    Running TalkPages version {config:["quarkus.application.version"]} {/include} \ No newline at end of file diff --git a/src/main/resources/templates/layout.html b/src/main/resources/templates/layout.html index 5135d35..84ac542 100644 --- a/src/main/resources/templates/layout.html +++ b/src/main/resources/templates/layout.html @@ -5,8 +5,8 @@ - {#if theme:darkTheme} - + {#if theme:getTheme??} + {/if} {#insert pageTitle /}{#if !customTitle??} - TalkPages{/if}