Themes
Also moved stuff around in Startup and index.html, but that's minor
This commit is contained in:
parent
2f78b698fc
commit
e9361ece41
9 changed files with 105 additions and 36 deletions
|
@ -1,5 +1,6 @@
|
||||||
package eu.m724.talkpages;
|
package eu.m724.talkpages;
|
||||||
|
|
||||||
|
import eu.m724.talkpages.theme.ThemeService;
|
||||||
import io.quarkus.qute.CheckedTemplate;
|
import io.quarkus.qute.CheckedTemplate;
|
||||||
import io.quarkus.qute.TemplateInstance;
|
import io.quarkus.qute.TemplateInstance;
|
||||||
import io.vertx.core.http.Cookie;
|
import io.vertx.core.http.Cookie;
|
||||||
|
@ -15,6 +16,7 @@ import jakarta.ws.rs.core.Response;
|
||||||
import org.jboss.resteasy.reactive.RestResponse.Status;
|
import org.jboss.resteasy.reactive.RestResponse.Status;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Path("/")
|
@Path("/")
|
||||||
public class IndexResource {
|
public class IndexResource {
|
||||||
|
@ -24,36 +26,32 @@ public class IndexResource {
|
||||||
@Inject
|
@Inject
|
||||||
HttpServerRequest request;
|
HttpServerRequest request;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
ThemeService themeService;
|
||||||
|
|
||||||
@CheckedTemplate
|
@CheckedTemplate
|
||||||
public static class Templates {
|
public static class Templates {
|
||||||
public static native TemplateInstance index();
|
public static native TemplateInstance index(List<String> themes, String currentTheme);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Produces(MediaType.TEXT_HTML)
|
@Produces(MediaType.TEXT_HTML)
|
||||||
public TemplateInstance index() {
|
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
|
@GET
|
||||||
@Path("/theme")
|
@Path("/theme")
|
||||||
public Response toggleDark() {
|
public Response toggleTheme(@QueryParam("theme") String theme) {
|
||||||
Cookie cookie = request.getCookie("dark");
|
NewCookie newCookie = new NewCookie.Builder("theme")
|
||||||
|
|
||||||
NewCookie newCookie;
|
|
||||||
if (cookie == null) {
|
|
||||||
newCookie = new NewCookie.Builder("dark")
|
|
||||||
.path("/")
|
.path("/")
|
||||||
.value("1")
|
.value(theme)
|
||||||
.maxAge(31560000)
|
.maxAge(31560000)
|
||||||
.build();
|
.build();
|
||||||
} else {
|
|
||||||
newCookie = new NewCookie.Builder("dark")
|
|
||||||
.path("/")
|
|
||||||
.value("0")
|
|
||||||
.maxAge(0)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
return Response.temporaryRedirect(URI.create("/")).cookie(newCookie).build();
|
return Response.temporaryRedirect(URI.create("/")).cookie(newCookie).build();
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package eu.m724.talkpages;
|
||||||
import eu.m724.talkpages.orm.entity.auth.Account;
|
import eu.m724.talkpages.orm.entity.auth.Account;
|
||||||
import eu.m724.talkpages.orm.entity.content.Page;
|
import eu.m724.talkpages.orm.entity.content.Page;
|
||||||
import eu.m724.talkpages.orm.entity.content.PageRevision;
|
import eu.m724.talkpages.orm.entity.content.PageRevision;
|
||||||
|
import eu.m724.talkpages.theme.ThemeService;
|
||||||
import io.quarkus.runtime.LaunchMode;
|
import io.quarkus.runtime.LaunchMode;
|
||||||
import io.quarkus.runtime.StartupEvent;
|
import io.quarkus.runtime.StartupEvent;
|
||||||
import io.vertx.ext.web.Router;
|
import io.vertx.ext.web.Router;
|
||||||
|
@ -19,6 +20,9 @@ public class Startup {
|
||||||
@Inject
|
@Inject
|
||||||
LaunchMode launchMode;
|
LaunchMode launchMode;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
ThemeService themeService;
|
||||||
|
|
||||||
@ConfigProperty(name = "talkpages.systemUser.name")
|
@ConfigProperty(name = "talkpages.systemUser.name")
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
|
@ -28,8 +32,13 @@ public class Startup {
|
||||||
.handler(StaticHandler.create("static/"));
|
.handler(StaticHandler.create("static/"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public void examplePage(@Observes StartupEvent ignoredEvent) {
|
public void examplePage(@Observes StartupEvent ignoredEvent) {
|
||||||
|
initDatabase();
|
||||||
|
themeService.indexThemes();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void initDatabase() {
|
||||||
if (Account.findByName(username) != null) {
|
if (Account.findByName(username) != null) {
|
||||||
// system user exists so assuming this is not the first run
|
// system user exists so assuming this is not the first run
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -1,21 +1,27 @@
|
||||||
package eu.m724.talkpages.template;
|
package eu.m724.talkpages.template;
|
||||||
|
|
||||||
import eu.m724.talkpages.RedirectService;
|
import eu.m724.talkpages.theme.ThemeService;
|
||||||
import io.quarkus.arc.Arc;
|
import io.quarkus.arc.Arc;
|
||||||
import io.quarkus.qute.TemplateExtension;
|
import io.quarkus.qute.TemplateExtension;
|
||||||
import io.vertx.core.Vertx;
|
|
||||||
import io.vertx.core.http.Cookie;
|
import io.vertx.core.http.Cookie;
|
||||||
import io.vertx.core.http.HttpServerRequest;
|
import io.vertx.core.http.HttpServerRequest;
|
||||||
import jakarta.inject.Inject;
|
|
||||||
import jakarta.ws.rs.core.Request;
|
|
||||||
|
|
||||||
@TemplateExtension(namespace = "theme")
|
@TemplateExtension(namespace = "theme")
|
||||||
public class ThemeExtension {
|
public class ThemeExtension {
|
||||||
public static boolean darkTheme() {
|
public static String getTheme() {
|
||||||
// this is bad
|
// I don't like those arc containers
|
||||||
HttpServerRequest request = Arc.container().instance(HttpServerRequest.class).get();
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
45
src/main/java/eu/m724/talkpages/theme/ThemeService.java
Normal file
45
src/main/java/eu/m724/talkpages/theme/ThemeService.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
talkpages.homePage=/
|
talkpages.homePage=/
|
||||||
talkpages.systemUser.name=System
|
talkpages.systemUser.name=System
|
||||||
|
talkpages.theme.default=light
|
||||||
|
|
||||||
quarkus.http.auth.basic=true
|
quarkus.http.auth.basic=true
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
body {
|
body {
|
||||||
background: #202020;
|
background: #222;
|
||||||
color: white;
|
color: white;
|
||||||
}
|
}
|
||||||
|
|
1
src/main/resources/static/theme/light.css
Normal file
1
src/main/resources/static/theme/light.css
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/* stub */
|
|
@ -15,15 +15,24 @@
|
||||||
Login or register
|
Login or register
|
||||||
{/if}
|
{/if}
|
||||||
</a></li>
|
</a></li>
|
||||||
<li><a href="/theme">
|
<li>
|
||||||
{#if theme:darkTheme}
|
<form action="/theme">
|
||||||
Light mode
|
<label for="themePicker">Theme</label>
|
||||||
{#else}
|
<select id="themePicker" name="theme">
|
||||||
Dark mode
|
{#for theme in themes}
|
||||||
{/if}
|
{#if theme.equals(currentTheme)}
|
||||||
</a></li>
|
<option value="{theme}" selected>{theme}</option>
|
||||||
<li><a href="/page/TalkPages/Terms of Service">Terms of Service</a></li>
|
{#else}
|
||||||
|
<option value="{theme}">{theme}</option>
|
||||||
|
{/if}
|
||||||
|
{/for}
|
||||||
|
</select>
|
||||||
|
<input type="submit" value="Apply">
|
||||||
|
</form>
|
||||||
|
</li>
|
||||||
</ul>
|
</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>
|
<small>Running <a href="/page/TalkPages">TalkPages</a> version {config:["quarkus.application.version"]}</small>
|
||||||
{/include}
|
{/include}
|
|
@ -5,8 +5,8 @@
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
<!-- TODO opengraph tags and maybe some nice css. also add errors and stuff here -->
|
<!-- TODO opengraph tags and maybe some nice css. also add errors and stuff here -->
|
||||||
<link rel="stylesheet" href="/static/style.css">
|
<link rel="stylesheet" href="/static/style.css">
|
||||||
{#if theme:darkTheme}
|
{#if theme:getTheme??}
|
||||||
<link rel="stylesheet" href="/static/dark.css">
|
<link rel="stylesheet" href="/static/theme/{theme:getTheme}.css">
|
||||||
{/if}
|
{/if}
|
||||||
<title>{#insert pageTitle /}{#if !customTitle??} - TalkPages{/if}</title> <!-- I give up -->
|
<title>{#insert pageTitle /}{#if !customTitle??} - TalkPages{/if}</title> <!-- I give up -->
|
||||||
</head>
|
</head>
|
||||||
|
|
Loading…
Reference in a new issue