toggleable dark theme

looks bad but that's because I don't want to spend much time on it because it looks bad (dark themes in general usually)
I also plan to add more themes and user generated themes
This commit is contained in:
Minecon724 2024-09-15 10:09:36 +02:00
parent abc294dcc1
commit 6a11f7ec6c
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
7 changed files with 78 additions and 2 deletions

View file

@ -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) {

View file

@ -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) {

View file

@ -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;

View file

@ -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");
}
}

View file

@ -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; }

View file

@ -9,10 +9,11 @@
<ul>
<li><a href="/edit">Create a page</a></li>
{#if !user:loggedIn}
<li><a href="/auth">Login or register</a></li>
<li><a href="/auth">Login or register</a></li>
{#else}
<li><a href="/auth/logout">Logout ({user:name})</a></li>
<li><a href="/auth/logout">Logout ({user:name})</a></li>
{/if}
<li><a href="/theme">{#if theme:darkTheme}Light mode{#else}Dark mode{/if}</a></li>
</ul>
<small>Running <a href="/page/TalkPages">TalkPages</a> version {config:["quarkus.application.version"]}</small>

View file

@ -4,6 +4,9 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- TODO opengraph tags and maybe some nice css. also add errors and stuff here -->
{#if theme:darkTheme}
<link rel="stylesheet" href="/static/dark.css">
{/if}
<title>{#insert pageTitle /}{#if !customTitle??} - TalkPages{/if}</title> <!-- I give up -->
</head>
<body>