Also moved stuff around in Startup and index.html, but that's minor
This commit is contained in:
Minecon724 2024-09-16 18:58:51 +02:00
parent 2f78b698fc
commit e9361ece41
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
9 changed files with 105 additions and 36 deletions

View file

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

View file

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

View file

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

View file

@ -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<String> themes = new ArrayList<>();
public List<String> 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));
}
}

View file

@ -1,5 +1,6 @@
talkpages.homePage=/
talkpages.systemUser.name=System
talkpages.theme.default=light
quarkus.http.auth.basic=true

View file

@ -1,5 +1,5 @@
body {
background: #202020;
background: #222;
color: white;
}

View file

@ -0,0 +1 @@
/* stub */

View file

@ -15,15 +15,24 @@
Login or register
{/if}
</a></li>
<li><a href="/theme">
{#if theme:darkTheme}
Light mode
<li>
<form action="/theme">
<label for="themePicker">Theme</label>
<select id="themePicker" name="theme">
{#for theme in themes}
{#if theme.equals(currentTheme)}
<option value="{theme}" selected>{theme}</option>
{#else}
Dark mode
<option value="{theme}">{theme}</option>
{/if}
</a></li>
<li><a href="/page/TalkPages/Terms of Service">Terms of Service</a></li>
{/for}
</select>
<input type="submit" value="Apply">
</form>
</li>
</ul>
<small><a href="/page/TalkPages/Terms of Service">Terms of Service</a></small>
<br>
<small>Running <a href="/page/TalkPages">TalkPages</a> version {config:["quarkus.application.version"]}</small>
{/include}

View file

@ -5,8 +5,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 -->
<link rel="stylesheet" href="/static/style.css">
{#if theme:darkTheme}
<link rel="stylesheet" href="/static/dark.css">
{#if theme:getTheme??}
<link rel="stylesheet" href="/static/theme/{theme:getTheme}.css">
{/if}
<title>{#insert pageTitle /}{#if !customTitle??} - TalkPages{/if}</title> <!-- I give up -->
</head>