Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/live2d/src/components/Live2dChatWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
} from "@/live2d/api/agent-navigation-intent";
import { ChatApi, type ChatMessage } from "@/live2d/api/chat-api";
import { UnoLitElement } from "@/live2d/common/UnoLitElement";
import { normalizeLive2dZIndex } from "@/live2d/config/z-index";
import {
type Live2dConfig,
configContext,
Expand Down Expand Up @@ -79,7 +80,7 @@ export class Live2dChatWindow extends DraggableUnoLitElement {

render(): TemplateResult {
const panelClasses = [
"fixed z-[10000] overflow-hidden rounded-full border border-[#eadfce] bg-[#fffaf4]/96 shadow-[0_10px_24px_rgba(15,23,42,0.08)] backdrop-blur-sm will-change-[opacity]",
"fixed overflow-hidden rounded-full border border-[#eadfce] bg-[#fffaf4]/96 shadow-[0_10px_24px_rgba(15,23,42,0.08)] backdrop-blur-sm will-change-[opacity]",
this._isShow
? "pointer-events-auto opacity-100"
: "pointer-events-none opacity-0",
Expand All @@ -95,7 +96,7 @@ export class Live2dChatWindow extends DraggableUnoLitElement {
id="live2d-chat-model"
popover="manual"
class=${panelClasses}
style="inset: auto auto ${CHAT_PANEL_BOTTOM} 50%; margin: 0; width: ${CHAT_PANEL_WIDTH}; transform: translateX(-50%); transition: opacity ${CHAT_PANEL_TRANSITION_MS}ms ease;"
style="inset: auto auto ${CHAT_PANEL_BOTTOM} 50%; margin: 0; width: ${CHAT_PANEL_WIDTH}; transform: translateX(-50%); transition: opacity ${CHAT_PANEL_TRANSITION_MS}ms ease; z-index: ${normalizeLive2dZIndex(this.config?.zIndex) + 2};"
>
<div class="flex items-center gap-1.5 px-1 py-1">
<input
Expand Down
5 changes: 3 additions & 2 deletions packages/live2d/src/components/Live2dToggle.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { UnoLitElement } from "@/live2d/common/UnoLitElement";
import { normalizeLive2dZIndex } from "@/live2d/config/z-index";
import {
type Live2dConfig,
configContext,
Expand Down Expand Up @@ -69,11 +70,11 @@ export class Live2dToggle extends DraggableUnoLitElement {
: "pointer-events-none opacity-0 scale-90";
return html`<div
id="live2d-toggle"
class="fixed bottom-16 ${positionClass} z-9999 inline-flex cursor-grab select-none items-center gap-1.5 rounded-full border border-[#f3d7b8] bg-[#fffaf4]/96 px-2.5 py-1.5 text-3.25 color-[#8b5e34] shadow-[0_8px_22px_rgba(139,94,52,0.18)] backdrop-blur-sm transition-[opacity,transform,box-shadow] duration-300 hover:scale-105 hover:shadow-[0_10px_26px_rgba(139,94,52,0.24)] active:cursor-grabbing ${visibilityClass}"
class="fixed bottom-16 ${positionClass} inline-flex cursor-grab select-none items-center gap-1.5 rounded-full border border-[#f3d7b8] bg-[#fffaf4]/96 px-2.5 py-1.5 text-3.25 color-[#8b5e34] shadow-[0_8px_22px_rgba(139,94,52,0.18)] backdrop-blur-sm transition-[opacity,transform,box-shadow] duration-300 hover:scale-105 hover:shadow-[0_10px_26px_rgba(139,94,52,0.24)] active:cursor-grabbing ${visibilityClass}"
role="button"
tabindex="0"
aria-label="打开看板娘"
style="bottom: calc(4rem + env(safe-area-inset-bottom, 0px));"
style="bottom: calc(4rem + env(safe-area-inset-bottom, 0px)); z-index: ${normalizeLive2dZIndex(this.config?.zIndex) + 1};"
@keydown=${this.handleKeydown}
>
<span
Expand Down
5 changes: 3 additions & 2 deletions packages/live2d/src/components/Live2dWidget.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { UnoLitElement } from "@/live2d/common/UnoLitElement";
import { normalizeLive2dZIndex } from "@/live2d/config/z-index";
import {
type Live2dConfig,
configContext,
Expand Down Expand Up @@ -102,8 +103,8 @@ export class Live2dWidget extends DraggableUnoLitElement {
const drawerClass = this._isShow ? "translate-y-0" : "translate-y-full";
return html`<div
id="live2d-plugin"
class="fixed z-9998 inline-block ${positionClass} ${visibilityClass}"
style=${drawerBoundaryStyle}
class="fixed inline-block ${positionClass} ${visibilityClass}"
style="${drawerBoundaryStyle} z-index: ${normalizeLive2dZIndex(this.config?.zIndex)};"
>
<div
class="linear transition-transform ${drawerClass}"
Expand Down
72 changes: 72 additions & 0 deletions packages/live2d/src/components/__tests__/stacking.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { createDefaultLive2dConfig } from "@/live2d/config/default-config";
import { DEFAULT_LIVE2D_Z_INDEX } from "@/live2d/config/z-index";
import type { Live2dConfig } from "@/live2d/context/config-context";
import { ToggleCanvasEvent } from "@/live2d/events/toggle-canvas";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

vi.mock("@/live2d/components/Live2dCanvas", () => ({}));
vi.mock("@/live2d/components/Live2dTools", () => ({}));
vi.mock("@/live2d/components/Live2dTips", () => ({}));

import "../Live2dWidget";

type ConfiguredElement = HTMLElement & {
config?: Live2dConfig;
updateComplete: Promise<boolean>;
};

describe("Live2D stacking layers", () => {
beforeEach(() => {
localStorage.clear();
vi.stubGlobal("matchMedia", () => ({
matches: false,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
}));
});

afterEach(() => {
document.body.replaceChildren();
vi.unstubAllGlobals();
});

it.each([undefined, 0, 30])(
"keeps the widget, restore control and chat in order for layer %s",
async (zIndex) => {
const config = { ...createDefaultLive2dConfig(), zIndex };
const widget = document.createElement(
"live2d-widget",
) as ConfiguredElement;
const toggle = document.createElement(
"live2d-toggle",
) as ConfiguredElement;
const chat = document.createElement(
"live2d-chat-window",
) as ConfiguredElement;
for (const element of [widget, toggle, chat]) {
element.config = config;
document.body.append(element);
}
window.dispatchEvent(new ToggleCanvasEvent({ isShow: true }));
await Promise.all([
widget.updateComplete,
toggle.updateComplete,
chat.updateComplete,
]);

const base = zIndex ?? DEFAULT_LIVE2D_Z_INDEX;
expect(
widget.shadowRoot?.querySelector<HTMLElement>("#live2d-plugin")?.style
.zIndex,
).toBe(String(base));
expect(
toggle.shadowRoot?.querySelector<HTMLElement>("#live2d-toggle")?.style
.zIndex,
).toBe(String(base + 1));
expect(
chat.shadowRoot?.querySelector<HTMLElement>("#live2d-chat-model")?.style
.zIndex,
).toBe(String(base + 2));
},
);
});
36 changes: 36 additions & 0 deletions packages/live2d/src/config/__tests__/z-index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { normalizeLive2dConfig } from "@/live2d/config/normalize-config";
import {
DEFAULT_LIVE2D_Z_INDEX,
MAX_LIVE2D_Z_INDEX,
} from "@/live2d/config/z-index";
import { describe, expect, it } from "vitest";

describe("Live2D stacking configuration", () => {
it("preserves the existing layer when an older configuration has no setting", () => {
expect(normalizeLive2dConfig("/assets/", {}).zIndex).toBe(
DEFAULT_LIVE2D_Z_INDEX,
);
});

it.each([0, 10, MAX_LIVE2D_Z_INDEX])(
"accepts the configured integer %s, including zero",
(zIndex) => {
expect(normalizeLive2dConfig("/assets/", { zIndex }).zIndex).toBe(zIndex);
},
);

it("accepts a numeric string from the saved Halo JSON", () => {
const input = JSON.parse('{"zIndex":"25"}');
expect(normalizeLive2dConfig("/assets/", input).zIndex).toBe(25);
});

it.each([null, "", -1, 1.5, "invalid", MAX_LIVE2D_Z_INDEX + 1])(
"falls back for an invalid saved value %s",
(zIndex) => {
const input = JSON.parse(JSON.stringify({ zIndex }));
expect(normalizeLive2dConfig("/assets/", input).zIndex).toBe(
DEFAULT_LIVE2D_Z_INDEX,
);
},
);
});
2 changes: 2 additions & 0 deletions packages/live2d/src/config/default-config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DEFAULT_LIVE2D_Z_INDEX } from "@/live2d/config/z-index";
import type { Live2dConfig } from "@/live2d/context/config-context";

export const DEFAULT_TOOL_NAMES = [
Expand All @@ -13,6 +14,7 @@ export const DEFAULT_TOOL_NAMES = [
export const createDefaultLive2dConfig = (): Live2dConfig => ({
apiPath: "https://live2d.fghrsh.net/api/",
live2dLocation: "left",
zIndex: DEFAULT_LIVE2D_Z_INDEX,
consoleShowStatus: import.meta.env.DEV ?? false,
isForceUseDefaultConfig: false,
modelId: 1,
Expand Down
2 changes: 2 additions & 0 deletions packages/live2d/src/config/normalize-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
pickNumber,
pickString,
} from "@/live2d/config/normalize-helpers";
import { normalizeLive2dZIndex } from "@/live2d/config/z-index";
import type { Live2dConfig } from "@/live2d/context/config-context";
import { isNotEmptyString } from "@/live2d/utils/isString";

Expand Down Expand Up @@ -94,6 +95,7 @@ export const normalizeLive2dConfig = (
...input,
apiPath: ensureTrailingSlash(normalizedApiPath),
live2dLocation: input.live2dLocation === "right" ? "right" : "left",
zIndex: normalizeLive2dZIndex(input.zIndex),
consoleShowStatus: pickBoolean(
input.consoleShowStatus,
input.consoleShowStatu,
Expand Down
14 changes: 14 additions & 0 deletions packages/live2d/src/config/z-index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { pickNumber } from "@/live2d/config/normalize-helpers";

export const DEFAULT_LIVE2D_Z_INDEX = 9998;
export const MAX_LIVE2D_Z_INDEX = 2147483645;

export const normalizeLive2dZIndex = (value: unknown): number => {
const zIndex = pickNumber(value);
return zIndex !== undefined &&
Number.isInteger(zIndex) &&
zIndex >= 0 &&
zIndex <= MAX_LIVE2D_Z_INDEX
? zIndex
: DEFAULT_LIVE2D_Z_INDEX;
};
2 changes: 2 additions & 0 deletions packages/live2d/src/context/config-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ export interface Live2dConfig extends Live2dToolsConfig {
apiPath: string;
// Live2d 定位
live2dLocation: "left" | "right";
// 看板娘和工具栏的显示层级
zIndex?: number;
// 是否在控制台显示状态
consoleShowStatus?: boolean;
// 兼容旧版控制台状态字段
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/run/halo/live2d/Live2dSettingProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
private static final List<String> TIPS_FIELDS = List.of(
"firstOpenSite", "backSite", "backSiteTip", "copyContent", "copyContentTip",
"openConsole", "openConsoleTip", "selectorTips", "tipsPath");
private static final List<String> ADVANCED_FIELDS = List.of("consoleShowStatu", "photoName");
private static final List<String> ADVANCED_FIELDS = List.of("consoleShowStatu", "photoName", "zIndex");
private static final List<String> AI_CHAT_PUBLIC_FIELDS = List.of(
"chunkTimeout", "showChatMessageTimeout", "autoContinuationMessageMinVisibleMs",
"requestAcceptedMessage", "reasoningMessages", "reasoningMessageInterval",
Expand All @@ -44,7 +44,7 @@

@Override
public Mono<JsonNode> getGroup(String groupName) {
return this.settingFetcher.get(groupName);

Check warning on line 47 in src/main/java/run/halo/live2d/Live2dSettingProcess.java

View workflow job for this annotation

GitHub Actions / ci / ci

[removal] get(String) in ReactiveSettingFetcher has been deprecated and marked for removal
}

@Override
Expand All @@ -54,7 +54,7 @@

@Override
public Mono<JsonNode> getPublicConfig(String themeTipsPath) {
return Mono.zip(settingFetcher.getValues(), haloAiFoundationAvailability.isEnabled())

Check warning on line 57 in src/main/java/run/halo/live2d/Live2dSettingProcess.java

View workflow job for this annotation

GitHub Actions / ci / ci

[removal] getValues() in ReactiveSettingFetcher has been deprecated and marked for removal
.map(tuple -> {
var data = tuple.getT1();
var aiFoundationEnabled = tuple.getT2();
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/extensions/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,15 @@ spec:
- group: advanced
label: 高级设置
formSchema:
- $formkit: number
name: zIndex
label: 显示层级
help: 看板娘和工具栏的显示层级,值越大越靠前;遮挡音乐按钮等控件时可调低。
value: 9998
min: 0
max: 2147483645
step: 1
validation: 'required|number|min:0|max:2147483645|matches:/^[0-9]+$/'
- $formkit: switch
name: consoleShowStatu
key: consoleShowStatu
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/run/halo/live2d/Live2dStackingSettingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package run.halo.live2d;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import java.util.Map;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import reactor.core.publisher.Mono;
import run.halo.app.plugin.ReactiveSettingFetcher;
import run.halo.live2d.agent.AgentToolNormalizer;
import run.halo.live2d.ai.HaloAiFoundationAvailability;

class Live2dStackingSettingTest {

@ParameterizedTest
@ValueSource(ints = {0, 25, 9998})
void includesSavedStackingLayerInPublicConfiguration(int zIndex) {
var settingFetcher = mock(ReactiveSettingFetcher.class);
var availability = mock(HaloAiFoundationAvailability.class);
var advanced = JsonNodeFactory.instance.objectNode().put("zIndex", zIndex);
when(settingFetcher.getValues()).thenReturn(Mono.just(Map.of("advanced", advanced)));

Check warning on line 24 in src/test/java/run/halo/live2d/Live2dStackingSettingTest.java

View workflow job for this annotation

GitHub Actions / ci / ci

[removal] getValues() in ReactiveSettingFetcher has been deprecated and marked for removal
when(availability.isEnabled()).thenReturn(Mono.just(false));
var settings = new Live2dSettingProcess(settingFetcher,
mock(AgentToolNormalizer.class), availability);

var config = settings.getPublicConfig(null).block();

assertThat(config).isNotNull();
assertThat(config.get("zIndex").intValue()).isEqualTo(zIndex);
assertThat(config.has("advanced")).isFalse();
}
}
Loading