diff --git a/src/main/java/eu/m724/talkpages/IndexResource.java b/src/main/java/eu/m724/talkpages/IndexResource.java index 0854a4f..6ec0d1a 100644 --- a/src/main/java/eu/m724/talkpages/IndexResource.java +++ b/src/main/java/eu/m724/talkpages/IndexResource.java @@ -2,20 +2,28 @@ package eu.m724.talkpages; import io.quarkus.qute.CheckedTemplate; import io.quarkus.qute.TemplateInstance; +import io.vertx.core.http.Cookie; +import io.vertx.core.http.HttpServerRequest; import jakarta.inject.Inject; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.QueryParam; import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.NewCookie; import jakarta.ws.rs.core.Response; import org.jboss.resteasy.reactive.RestResponse.Status; +import java.net.URI; + @Path("/") public class IndexResource { @Inject RedirectService redirectService; + @Inject + HttpServerRequest request; + @CheckedTemplate public static class Templates { public static native TemplateInstance index(); @@ -27,6 +35,29 @@ public class IndexResource { return Templates.index(); } + @GET + @Path("/theme") + public Response toggleDark() { + Cookie cookie = request.getCookie("dark"); + + NewCookie newCookie; + if (cookie == null) { + newCookie = new NewCookie.Builder("dark") + .path("/") + .value("1") + .maxAge(31560000) + .build(); + } else { + newCookie = new NewCookie.Builder("dark") + .path("/") + .value("0") + .maxAge(0) + .build(); + } + + return Response.temporaryRedirect(URI.create("/")).cookie(newCookie).build(); + } + @GET @Path("/search") public Response search(@QueryParam("query") String query) { diff --git a/src/main/java/eu/m724/talkpages/Startup.java b/src/main/java/eu/m724/talkpages/Startup.java index 687a9a9..ca0cf0c 100644 --- a/src/main/java/eu/m724/talkpages/Startup.java +++ b/src/main/java/eu/m724/talkpages/Startup.java @@ -5,6 +5,8 @@ import eu.m724.talkpages.orm.entity.content.Page; import eu.m724.talkpages.orm.entity.content.PageRevision; import io.quarkus.runtime.LaunchMode; import io.quarkus.runtime.StartupEvent; +import io.vertx.ext.web.Router; +import io.vertx.ext.web.handler.StaticHandler; import jakarta.enterprise.event.Observes; import jakarta.inject.Inject; import jakarta.inject.Singleton; @@ -23,6 +25,12 @@ public class Startup { @ConfigProperty(name = "talkpages.createExamplePage") private boolean createExamplePage; + void installStaticRoute(@Observes StartupEvent startupEvent, Router router) { + router.route() + .path("/static/*") + .handler(StaticHandler.create("static/")); + } + @Transactional public void examplePage(@Observes StartupEvent ignoredEvent) { if (Account.findById(1) != null) { diff --git a/src/main/java/eu/m724/talkpages/template/TemplateExtensions.java b/src/main/java/eu/m724/talkpages/template/TemplateExtensions.java index a77da61..86a38f4 100644 --- a/src/main/java/eu/m724/talkpages/template/TemplateExtensions.java +++ b/src/main/java/eu/m724/talkpages/template/TemplateExtensions.java @@ -1,7 +1,10 @@ package eu.m724.talkpages.template; import io.quarkus.qute.TemplateExtension; +import io.vertx.core.http.Cookie; +import io.vertx.core.http.HttpServerRequest; import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; import org.jsoup.Jsoup; import org.jsoup.safety.Safelist; diff --git a/src/main/java/eu/m724/talkpages/template/ThemeExtension.java b/src/main/java/eu/m724/talkpages/template/ThemeExtension.java new file mode 100644 index 0000000..1be5d6f --- /dev/null +++ b/src/main/java/eu/m724/talkpages/template/ThemeExtension.java @@ -0,0 +1,21 @@ +package eu.m724.talkpages.template; + +import eu.m724.talkpages.RedirectService; +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 + HttpServerRequest request = Arc.container().instance(HttpServerRequest.class).get(); + Cookie cookie = request.getCookie("dark"); + + return cookie == null || cookie.getValue().equals("0"); + } +} diff --git a/src/main/resources/static/dark.css b/src/main/resources/static/dark.css new file mode 100644 index 0000000..eaf9f0c --- /dev/null +++ b/src/main/resources/static/dark.css @@ -0,0 +1,9 @@ +body { + background: #202020; + color: white; +} + +a:link { color: #e0d0ff; } +a:visited { color: #d0c0ff; } +a:hover { color: #e0e0ff; } +a:active { color: #e0d0ff; } \ 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 606b024..a7e5923 100644 --- a/src/main/resources/templates/IndexResource/index.html +++ b/src/main/resources/templates/IndexResource/index.html @@ -9,10 +9,11 @@ Running TalkPages version {config:["quarkus.application.version"]} diff --git a/src/main/resources/templates/layout.html b/src/main/resources/templates/layout.html index 9af0d96..0b6038d 100644 --- a/src/main/resources/templates/layout.html +++ b/src/main/resources/templates/layout.html @@ -4,6 +4,9 @@ + {#if theme:darkTheme} + + {/if} {#insert pageTitle /}{#if !customTitle??} - TalkPages{/if}