import Dropdown from "../react/Dropdown"; import "./global_menu.css"; import { useStaticTooltip, useStaticTooltipWithKeyboardShortcut, useTriliumOption, useTriliumOptionBool } from "../react/hooks"; import { useContext, useEffect, useRef, useState } from "preact/hooks"; import { t } from "../../services/i18n"; import { FormDropdownDivider, FormDropdownSubmenu, FormListItem } from "../react/FormList"; import { CommandNames } from "../../components/app_context"; import KeyboardShortcut from "../react/KeyboardShortcut"; import { KeyboardActionNames } from "@triliumnext/commons"; import { ComponentChildren } from "preact"; import Component from "../../components/component"; import { ParentComponent } from "../react/react_utils"; import utils, { dynamicRequire, isElectron, isMobile } from "../../services/utils"; interface MenuItemProps { icon: string, text: ComponentChildren, title?: string, command: T, disabled?: boolean active?: boolean; outsideChildren?: ComponentChildren; } export default function GlobalMenu({ isHorizontalLayout }: { isHorizontalLayout: boolean }) { const isVerticalLayout = !isHorizontalLayout; const parentComponent = useContext(ParentComponent); const { isUpdateAvailable, latestVersion } = useTriliumUpdateStatus(); return ( {isVerticalLayout && } {isUpdateAvailable && } } noDropdownListStyle > {isUpdateAvailable && window.open("https://github.com/TriliumNext/Trilium/releases/latest")} icon="bx bx-sync" text={`Version ${latestVersion} is available, click to download.`} /> } {!isElectron() && } ) } function AdvancedMenu() { return ( {isElectron() && } ) } function BrowserOnlyOptions() { return <> ; } function SwitchToOptions() { if (isElectron()) { return; } else if (!isMobile()) { return } else { return } } function MenuItem({ icon, text, title, command, disabled, active }: MenuItemProps void)>) { return {text} } function KeyboardActionMenuItem({ text, command, ...props }: MenuItemProps) { return {text} } /> } function VerticalLayoutIcon() { const logoRef = useRef(null); useStaticTooltip(logoRef); return ( ) } function ZoomControls({ parentComponent }: { parentComponent?: Component | null }) { const [ zoomLevel, setZoomLevel ] = useState(100); function updateZoomState() { if (!isElectron()) { return; } const zoomFactor = dynamicRequire("electron").webFrame.getZoomFactor(); setZoomLevel(Math.round(zoomFactor * 100)); } useEffect(updateZoomState, []); function ZoomControlButton({ command, title, icon, children }: { command: KeyboardActionNames, title: string, icon?: string, children?: ComponentChildren }) { const linkRef = useRef(null); useStaticTooltipWithKeyboardShortcut(linkRef, title, command); return ( { parentComponent?.triggerCommand(command); setTimeout(() => updateZoomState(), 300) e.stopPropagation(); }} className={`dropdown-item-button ${icon}`} >{children} ) } return isElectron() ? ( {t("global_menu.zoom")} <>
  {zoomLevel}{t("units.percentage")}
) : ( ); } function ToggleWindowOnTop() { const focusedWindow = isElectron() ? dynamicRequire("@electron/remote").BrowserWindow.getFocusedWindow() : null; const [ isAlwaysOnTop, setIsAlwaysOnTop ] = useState(focusedWindow?.isAlwaysOnTop()); return (isElectron() && { const newState = !isAlwaysOnTop; focusedWindow?.setAlwaysOnTop(newState); setIsAlwaysOnTop(newState); }} /> ) } function useTriliumUpdateStatus() { const [ latestVersion, setLatestVersion ] = useState(); const [ checkForUpdates ] = useTriliumOptionBool("checkForUpdates"); const isUpdateAvailable = utils.isUpdateAvailable(latestVersion, glob.triliumVersion); async function updateVersionStatus() { const RELEASES_API_URL = "https://api.github.com/repos/TriliumNext/Trilium/releases/latest"; const resp = await fetch(RELEASES_API_URL); const data = await resp.json(); const latestVersion = data?.tag_name?.substring(1); setLatestVersion(latestVersion); } useEffect(() => { if (!checkForUpdates) { setLatestVersion(undefined); return; } updateVersionStatus(); const interval = setInterval(() => updateVersionStatus(), 8 * 60 * 60 * 1000); return () => clearInterval(interval); }, [ checkForUpdates ]); return { isUpdateAvailable, latestVersion }; }