2025-11-27 13:47:59 -08:00
|
|
|
/*
|
|
|
|
|
* Vencord, a Discord client mod
|
|
|
|
|
* Copyright (c) 2025 Vendicated and contributors
|
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import ErrorBoundary from "@components/ErrorBoundary";
|
2025-12-06 06:18:27 +01:00
|
|
|
import { Logger } from "@utils/Logger";
|
|
|
|
|
import { findComponentByCodeLazy } from "@webpack";
|
2025-11-27 13:47:59 -08:00
|
|
|
import { useEffect, useState } from "@webpack/common";
|
2025-12-10 15:35:21 +01:00
|
|
|
import type { ComponentType, JSX, MouseEventHandler, ReactNode } from "react";
|
2025-12-06 06:18:27 +01:00
|
|
|
|
|
|
|
|
const logger = new Logger("HeaderBarAPI");
|
2025-11-27 13:47:59 -08:00
|
|
|
|
2025-12-10 15:35:21 +01:00
|
|
|
const HeaderBarIcon = findComponentByCodeLazy(".HEADER_BAR_BADGE_TOP:", '.iconBadge,"top"') as ComponentType<HeaderBarButtonProps>;
|
2025-11-27 13:47:59 -08:00
|
|
|
|
2025-12-06 06:18:27 +01:00
|
|
|
export interface HeaderBarButtonProps {
|
2025-12-10 15:35:21 +01:00
|
|
|
/** The icon component to render inside the button */
|
2025-12-06 06:18:27 +01:00
|
|
|
icon: ComponentType<any>;
|
2025-12-10 15:35:21 +01:00
|
|
|
/** Tooltip text shown on hover. Pass null to disable tooltip */
|
2025-12-06 06:18:27 +01:00
|
|
|
tooltip: ReactNode;
|
2025-12-10 15:35:21 +01:00
|
|
|
/** Called when the button is clicked */
|
2025-12-06 06:18:27 +01:00
|
|
|
onClick?: MouseEventHandler<HTMLDivElement>;
|
2025-12-10 15:35:21 +01:00
|
|
|
/** Called when the button is right-clicked */
|
2025-12-06 06:18:27 +01:00
|
|
|
onContextMenu?: MouseEventHandler<HTMLDivElement>;
|
2025-12-10 15:35:21 +01:00
|
|
|
/** Additional CSS class names */
|
2025-12-06 06:18:27 +01:00
|
|
|
className?: string;
|
2025-12-10 15:35:21 +01:00
|
|
|
/** CSS class name for the icon element */
|
2025-12-06 06:18:27 +01:00
|
|
|
iconClassName?: string;
|
2025-12-10 15:35:21 +01:00
|
|
|
/** Tooltip position relative to the button */
|
2025-12-06 06:18:27 +01:00
|
|
|
position?: "top" | "bottom" | "left" | "right";
|
2025-12-10 15:35:21 +01:00
|
|
|
/** Whether the button appears in a selected/active state */
|
2025-12-06 06:18:27 +01:00
|
|
|
selected?: boolean;
|
2025-12-10 15:35:21 +01:00
|
|
|
/** Whether the button is disabled */
|
2025-12-06 06:18:27 +01:00
|
|
|
disabled?: boolean;
|
2025-12-10 15:35:21 +01:00
|
|
|
/** Whether to show a notification badge */
|
2025-12-06 06:18:27 +01:00
|
|
|
showBadge?: boolean;
|
2025-12-10 15:35:21 +01:00
|
|
|
/** Position of the notification badge */
|
2025-12-06 06:18:27 +01:00
|
|
|
badgePosition?: "top" | "bottom";
|
2025-12-10 15:35:21 +01:00
|
|
|
/** Size of the icon in pixels */
|
2025-12-10 14:35:29 +01:00
|
|
|
iconSize?: number;
|
2025-12-10 15:35:21 +01:00
|
|
|
/** Ref to the button element */
|
|
|
|
|
ref?: React.RefObject<any>;
|
2025-12-06 06:18:27 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-10 15:35:21 +01:00
|
|
|
export type HeaderBarButtonFactory = () => JSX.Element | null;
|
2025-12-06 06:18:27 +01:00
|
|
|
|
2025-12-10 15:35:21 +01:00
|
|
|
export interface HeaderBarButtonData {
|
|
|
|
|
/** Function that renders the button component */
|
|
|
|
|
render: HeaderBarButtonFactory;
|
|
|
|
|
/** Icon component used for settings UI display */
|
|
|
|
|
icon: ComponentType<any>;
|
|
|
|
|
/** Higher priority buttons appear further right. Default: 0 */
|
|
|
|
|
priority?: number;
|
|
|
|
|
/** Where to render the button. Default: "headerbar" */
|
|
|
|
|
location?: "headerbar" | "channeltoolbar";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ButtonEntry {
|
|
|
|
|
render: HeaderBarButtonFactory;
|
|
|
|
|
priority: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Button component for the top header bar (title bar area).
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* <HeaderBarButton
|
|
|
|
|
* icon={MyIcon}
|
|
|
|
|
* tooltip="My Button"
|
|
|
|
|
* onClick={() => console.log("clicked")}
|
|
|
|
|
* />
|
|
|
|
|
*/
|
|
|
|
|
export const HeaderBarButton = HeaderBarIcon;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Button component for the channel toolbar (below the search bar).
|
|
|
|
|
* Automatically handles selected state styling.
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* <ChannelToolbarButton
|
|
|
|
|
* icon={MyIcon}
|
|
|
|
|
* tooltip={isOpen ? null : "My Button"}
|
|
|
|
|
* onClick={() => setOpen(v => !v)}
|
|
|
|
|
* selected={isOpen}
|
|
|
|
|
* />
|
|
|
|
|
*/
|
|
|
|
|
export function ChannelToolbarButton(props: HeaderBarButtonProps) {
|
|
|
|
|
return <HeaderBarIcon {...props} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const headerBarButtons = new Map<string, ButtonEntry>();
|
|
|
|
|
const channelToolbarButtons = new Map<string, ButtonEntry>();
|
|
|
|
|
|
|
|
|
|
const headerBarListeners = new Set<() => void>();
|
|
|
|
|
const channelToolbarListeners = new Set<() => void>();
|
2025-12-10 14:35:29 +01:00
|
|
|
|
2025-11-27 13:47:59 -08:00
|
|
|
/**
|
2025-12-10 15:35:21 +01:00
|
|
|
* Adds a button to the header bar (title bar area).
|
|
|
|
|
*
|
|
|
|
|
* @param id - Unique identifier for the button (e.g., "my-plugin-button")
|
|
|
|
|
* @param render - Function that returns the button JSX
|
|
|
|
|
* @param priority - Higher values appear further right. Default: 0
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* addHeaderBarButton("my-button", () => (
|
|
|
|
|
* <HeaderBarButton
|
|
|
|
|
* icon={MyIcon}
|
|
|
|
|
* tooltip="My Button"
|
|
|
|
|
* onClick={handleClick}
|
|
|
|
|
* />
|
|
|
|
|
* ));
|
2025-11-27 13:47:59 -08:00
|
|
|
*/
|
|
|
|
|
export function addHeaderBarButton(id: string, render: HeaderBarButtonFactory, priority = 0) {
|
2025-12-10 15:35:21 +01:00
|
|
|
headerBarButtons.set(id, { render, priority });
|
|
|
|
|
headerBarListeners.forEach(listener => listener());
|
2025-11-27 13:47:59 -08:00
|
|
|
}
|
|
|
|
|
|
2025-12-10 15:35:21 +01:00
|
|
|
/**
|
|
|
|
|
* Removes a button from the header bar.
|
|
|
|
|
*
|
|
|
|
|
* @param id - The identifier used when adding the button
|
|
|
|
|
*/
|
2025-11-27 13:47:59 -08:00
|
|
|
export function removeHeaderBarButton(id: string) {
|
2025-12-10 15:35:21 +01:00
|
|
|
headerBarButtons.delete(id);
|
|
|
|
|
headerBarListeners.forEach(listener => listener());
|
2025-11-27 13:47:59 -08:00
|
|
|
}
|
|
|
|
|
|
2025-12-10 14:35:29 +01:00
|
|
|
/**
|
2025-12-10 15:35:21 +01:00
|
|
|
* Adds a button to the channel toolbar (below the search bar, next to pins/members).
|
|
|
|
|
*
|
|
|
|
|
* @param id - Unique identifier for the button (e.g., "my-plugin-toolbar")
|
|
|
|
|
* @param render - Function that returns the button JSX
|
|
|
|
|
* @param priority - Higher values appear further right. Default: 0
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* addChannelToolbarButton("my-toolbar", () => (
|
|
|
|
|
* <ChannelToolbarButton
|
|
|
|
|
* icon={MyIcon}
|
|
|
|
|
* tooltip="My Button"
|
|
|
|
|
* onClick={handleClick}
|
|
|
|
|
* />
|
|
|
|
|
* ));
|
2025-12-10 14:35:29 +01:00
|
|
|
*/
|
|
|
|
|
export function addChannelToolbarButton(id: string, render: HeaderBarButtonFactory, priority = 0) {
|
|
|
|
|
channelToolbarButtons.set(id, { render, priority });
|
2025-12-10 15:35:21 +01:00
|
|
|
channelToolbarListeners.forEach(listener => listener());
|
2025-12-10 14:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-10 15:35:21 +01:00
|
|
|
/**
|
|
|
|
|
* Removes a button from the channel toolbar.
|
|
|
|
|
*
|
|
|
|
|
* @param id - The identifier used when adding the button
|
|
|
|
|
*/
|
2025-12-10 14:35:29 +01:00
|
|
|
export function removeChannelToolbarButton(id: string) {
|
|
|
|
|
channelToolbarButtons.delete(id);
|
2025-12-10 15:35:21 +01:00
|
|
|
channelToolbarListeners.forEach(listener => listener());
|
2025-12-10 14:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-27 13:47:59 -08:00
|
|
|
function HeaderBarButtons() {
|
|
|
|
|
const [, forceUpdate] = useState(0);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const listener = () => forceUpdate(n => n + 1);
|
2025-12-10 15:35:21 +01:00
|
|
|
headerBarListeners.add(listener);
|
|
|
|
|
return () => { headerBarListeners.delete(listener); };
|
2025-11-27 13:47:59 -08:00
|
|
|
}, []);
|
|
|
|
|
|
2025-12-10 15:35:21 +01:00
|
|
|
return Array.from(headerBarButtons)
|
|
|
|
|
.sort(([, a], [, b]) => a.priority - b.priority)
|
|
|
|
|
.map(([id, { render: Button }]) => (
|
|
|
|
|
<ErrorBoundary noop key={id} onError={e => logger.error(`Failed to render header bar button: ${id}`, e.error)}>
|
|
|
|
|
<Button />
|
|
|
|
|
</ErrorBoundary>
|
|
|
|
|
));
|
2025-11-27 13:47:59 -08:00
|
|
|
}
|
|
|
|
|
|
2025-12-10 14:35:29 +01:00
|
|
|
function ChannelToolbarButtons() {
|
|
|
|
|
const [, forceUpdate] = useState(0);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const listener = () => forceUpdate(n => n + 1);
|
|
|
|
|
channelToolbarListeners.add(listener);
|
|
|
|
|
return () => { channelToolbarListeners.delete(listener); };
|
|
|
|
|
}, []);
|
|
|
|
|
|
2025-12-10 15:35:21 +01:00
|
|
|
return Array.from(channelToolbarButtons)
|
|
|
|
|
.sort(([, a], [, b]) => a.priority - b.priority)
|
|
|
|
|
.map(([id, { render: Button }]) => (
|
|
|
|
|
<ErrorBoundary noop key={id} onError={e => logger.error(`Failed to render channel toolbar button: ${id}`, e.error)}>
|
|
|
|
|
<Button />
|
|
|
|
|
</ErrorBoundary>
|
|
|
|
|
));
|
2025-12-10 14:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-10 15:35:21 +01:00
|
|
|
/** @internal Injected by HeaderBarAPI patch (do NOT call directly) */
|
|
|
|
|
export function _addHeaderBarButtons() {
|
2025-11-27 13:47:59 -08:00
|
|
|
return [<HeaderBarButtons key="vc-header-bar-buttons" />];
|
|
|
|
|
}
|
2025-12-10 14:35:29 +01:00
|
|
|
|
2025-12-10 15:35:21 +01:00
|
|
|
/** @internal Injected by HeaderBarAPI patch (do NOT call directly) */
|
2025-12-10 14:35:29 +01:00
|
|
|
export function _addChannelToolbarButtons(toolbar: ReactNode[]) {
|
|
|
|
|
toolbar.push(<ChannelToolbarButtons key="vc-channel-toolbar-buttons" />);
|
|
|
|
|
}
|