Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (settings): Verification grid level default settings for spectrograms #226

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions src/components/axes/axes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ import axesStyles from "./css/style.css?inline";
// which are used in the axes rendering
// see: https://github.com/ecoacoustics/web-components/issues/85

export interface AxesOptions {
xTitle: string;
yTitle: string;
showXTitle: boolean;
showYTitle: boolean;
showXAxis: boolean;
showYAxis: boolean;
showXGrid: boolean;
showYGrid: boolean;
xStep?: Seconds;
yStep?: Hertz;
}

/**
* @description
* X and Y axis grid lines showing duration and frequency of a spectrogram
Expand Down Expand Up @@ -113,6 +126,34 @@ export class AxesComponent extends SignalWatcher(AbstractComponent(LitElement))
@query("#axes-svg")
private elementChrome!: Readonly<HTMLDivElement>;

public get axesOptions(): AxesOptions {
return {
xStep: this.xStepOverride,
yStep: this.yStepOverride,
xTitle: this.xTitle,
yTitle: this.yTitle,
showXTitle: this.showXTitle,
showYTitle: this.showYTitle,
showXAxis: this.showXAxis,
showYAxis: this.showYAxis,
showXGrid: this.showXGrid,
showYGrid: this.showYGrid,
};
}

public set axesOptions(value: AxesOptions) {
this.xStepOverride = value.xStep;
this.yStepOverride = value.yStep;
this.xTitle = value.xTitle;
this.yTitle = value.yTitle;
this.showXTitle = value.showXTitle;
this.showYTitle = value.showYTitle;
this.showXAxis = value.showXAxis;
this.showYAxis = value.showYAxis;
this.showXGrid = value.showXGrid;
this.showYGrid = value.showYGrid;
}

// if we do not know the text that we want to measure, we use one large
// character as an upperbound estimate of the size of characters
// using the default case should only ever be used for estimates and measuring
Expand Down
4 changes: 2 additions & 2 deletions src/components/media-controls/media-controls.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class TestPage {
public constructor(public readonly page: Page) {}

public component = () => this.page.locator("oe-media-controls").first();
public actionButton = this.page.locator("oe-media-controls #action-button").first();
public actionButton = () => this.page.locator("oe-media-controls #action-button").first();
public actionButtonSlot = () => this.page.locator("oe-media-controls #action-button > slot").first();
public spectrogram = () => this.page.locator("oe-spectrogram").first();

Expand All @@ -34,7 +34,7 @@ class TestPage {
}

public async toggleAudio() {
const actionButtonElement = this.actionButton;
const actionButtonElement = this.actionButton();
await actionButtonElement.click({ force: true });
}

Expand Down
234 changes: 24 additions & 210 deletions src/components/media-controls/media-controls.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { LitElement, PropertyValues, TemplateResult, html, nothing, unsafeCSS } from "lit";
import { LitElement, PropertyValues, html, nothing, unsafeCSS } from "lit";
import { customElement, property } from "lit/decorators.js";
import { ILogger, rootContext } from "../logger/logger";
import { provide } from "@lit/context";
import { AbstractComponent } from "../../mixins/abstractComponent";
import { SpectrogramComponent } from "../spectrogram/spectrogram";
import { SlMenuItem } from "@shoelace-style/shoelace";
import { SpectrogramOptions } from "../../helpers/audio/models";
import { AxesComponent } from "../axes/axes";
import { windowFunctions } from "../../helpers/audio/window";
import { colorScales } from "../../helpers/audio/colors";
import { AxesComponent, AxesOptions } from "../axes/axes";
import { SPACE_KEY } from "../../helpers/keyboard";
import { settingsTemplateFactory } from "../../templates/settings";
import { signal } from "@lit-labs/preact-signals";
import { SpectrogramOptions } from "../../helpers/audio/models";
import mediaControlsStyles from "./css/style.css?inline";

/**
Expand Down Expand Up @@ -181,225 +180,40 @@ export class MediaControlsComponent extends AbstractComponent(LitElement) {
`;
}

private discreteSettingsTemplate(
text: string,
values: string[] | number[] | ReadonlyArray<number | string>,
currentValue: string | number | boolean,
changeHandler: (event: CustomEvent<{ item: SlMenuItem }>) => void,
): TemplateResult {
return html`
<sl-menu-item>
${text}
<sl-menu @sl-select="${changeHandler}" slot="submenu">
${values.map(
(value) =>
html`<sl-menu-item
type="${value == currentValue ? "checkbox" : "normal"}"
value="${value}"
?checked=${value == currentValue}
>
${value}
</sl-menu-item>`,
)}
</sl-menu>
</sl-menu-item>
`;
}

private rangeSettingsTemplate(
text: string,
min: number,
max: number,
step: number,
currentValue: number,
changeHandler: any,
): TemplateResult {
return html`
<sl-menu-item>
${text}
<sl-menu slot="submenu">
<label>
<input
@change="${changeHandler}"
type="range"
min="${min}"
max="${max}"
step="${step}"
value="${currentValue}"
/>
</label>
</sl-menu>
</sl-menu-item>
`;
}

private settingsTemplate() {
if (!this.spectrogramElement) {
if (!this.spectrogramElement || !this.axesElement) {
return nothing;
}

const possibleWindowSizes = this.spectrogramElement.possibleWindowSizes;
const possibleWindowOverlaps = this.spectrogramElement.possibleWindowOverlaps;
const currentOptions = this.spectrogramElement.spectrogramOptions;

const discreteDropdownHandler = (key: keyof SpectrogramOptions) => {
return (event: CustomEvent<{ item: SlMenuItem }>) => {
if (!this.spectrogramElement) {
throw new Error("No spectrogram element found");
}

// TODO: remove this after demo
let newValue: string | number | boolean = ["windowSize", "windowOverlap"].includes(key)
? Number(event.detail.item.value)
: event.detail.item.value;

if (key === "melScale") {
newValue = newValue === "mel";
}

const oldOptions = this.spectrogramElement.spectrogramOptions;
if (key === "windowSize" && this.spectrogramElement) {
if (this.spectrogramElement.spectrogramOptions.windowOverlap >= (newValue as number)) {
oldOptions.windowOverlap = (newValue as number) / 2;
}
}

this.spectrogramElement.spectrogramOptions = {
...oldOptions,
[key]: newValue,
} as any;

this.requestUpdate();
};
};

const rangeInputHandler = (key: keyof SpectrogramOptions) => {
return (event: Event) => {
if (!this.spectrogramElement) {
throw new Error("No spectrogram element found");
}

const newValue = (event.target as HTMLInputElement).value;
const oldOptions = this.spectrogramElement.spectrogramOptions;

this.spectrogramElement.spectrogramOptions = {
...oldOptions,
[key]: Number(newValue),
} as any;
};
};

const axesChangeHandler = (event: CustomEvent<{ item: SlMenuItem }>) => {
if (!this.axesElement) {
throw new Error("No axes element found");
const spectrogramOptionsSignal = signal<SpectrogramOptions>(this.spectrogramElement.spectrogramOptions);
spectrogramOptionsSignal.subscribe((newOptions) => {
if (!this.spectrogramElement) {
throw new Error(
"No spectrogram element found. This might be because you forgot to unsubscribe from the options signal",
);
}
this.spectrogramElement.spectrogramOptions = newOptions;
});
Comment on lines +188 to +195
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to be a recursive definition.

but also, why is it the job of media controls to set the options on a spectrogram?


const menuItem = event.detail.item;
const checkboxElement = menuItem.querySelector<HTMLInputElement>("input[type=checkbox]");

if (!checkboxElement) {
throw new Error("No checkbox element found");
const axesOptionsSignal = signal<AxesOptions>(this.axesElement.axesOptions);
axesOptionsSignal.subscribe((newOptions) => {
if (!this.axesElement) {
throw new Error(
"No axes element found. This might be because you forgot to unsubscribe from the options signal",
);
}
this.axesElement.axesOptions = newOptions;
});

Comment on lines +198 to +206
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

const key = checkboxElement.name as keyof AxesComponent;
const value = checkboxElement.checked;

(this.axesElement as any)[key] = value;
};
const settingsTemplate = settingsTemplateFactory(spectrogramOptionsSignal, axesOptionsSignal);

return html`
<sl-dropdown hoist>
<a class="settings-menu-item" slot="trigger">
<sl-icon name="gear"></sl-icon>
</a>

<sl-menu>
${this.discreteSettingsTemplate(
"Colour",
Object.keys(colorScales),
currentOptions.colorMap,
discreteDropdownHandler("colorMap"),
)}
${this.rangeSettingsTemplate(
"Brightness",
-0.5,
0.5,
0.01,
currentOptions.brightness,
rangeInputHandler("brightness"),
)}
${this.rangeSettingsTemplate("Contrast", 0, 2, 0.01, currentOptions.contrast, rangeInputHandler("contrast"))}
${this.discreteSettingsTemplate(
"Window Function",
Array.from(windowFunctions.keys()),
currentOptions.windowFunction,
discreteDropdownHandler("windowFunction"),
)}
${this.discreteSettingsTemplate(
"Window Size",
possibleWindowSizes,
currentOptions.windowSize,
discreteDropdownHandler("windowSize"),
)}
${this.discreteSettingsTemplate(
"Window Overlap",
[0, ...possibleWindowOverlaps],
currentOptions.windowOverlap,
discreteDropdownHandler("windowOverlap"),
)}
${this.discreteSettingsTemplate(
"Scale",
["linear", "mel"],
currentOptions.melScale ? "mel" : "linear",
discreteDropdownHandler("melScale"),
)}
<sl-menu-item>
Axes
<sl-menu @sl-select="${axesChangeHandler}" slot="submenu">
<sl-menu-item>
<label>
<input type="checkbox" name="showXTitle" ?checked=${this.axesElement?.showXTitle} />
X-Axis Title
</label>
</sl-menu-item>

<sl-menu-item>
<label>
<input type="checkbox" name="showYTitle" ?checked=${this.axesElement?.showYTitle} />
Y-Axis Title
</label>
</sl-menu-item>

<sl-menu-item>
<label>
<input type="checkbox" name="showXAxis" ?checked=${this.axesElement?.showXAxis} />
X-Axis Labels
</label>
</sl-menu-item>

<sl-menu-item>
<label>
<input type="checkbox" name="showYAxis" ?checked=${this.axesElement?.showYAxis} />
Y-Axis Labels
</label>
</sl-menu-item>

<sl-menu-item>
<label>
<input type="checkbox" name="showXGrid" ?checked=${this.axesElement?.showXGrid} />
X-Axis Grid Lines
</label>
</sl-menu-item>

<sl-menu-item>
<label>
<input type="checkbox" name="showYGrid" ?checked=${this.axesElement?.showYGrid} />
Y-Axis Grid Lines
</label>
</sl-menu-item>
</sl-menu>
</sl-menu-item>
</sl-menu>
${settingsTemplate}
</sl-dropdown>
`;
}
Expand Down
Loading
Loading