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

Fix media player & updater #1231

Merged
merged 5 commits into from
Dec 8, 2024
Merged
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
1 change: 0 additions & 1 deletion enjoy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@
"tslib": "^2.8.1",
"turndown": "^7.2.0",
"typescript": "^5.7.2",
"update-electron-app": "^3.0.0",
"vite": "^6.0.3",
"vite-plugin-static-copy": "^2.2.0",
"wavesurfer.js": "^7.8.10",
Expand Down
21 changes: 19 additions & 2 deletions enjoy/src/i18n/en.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
{
"menu": {
"about": "About",
"toggleFullScreen": "Toggle Full Screen",
"hide": "Hide",
"unhide": "Unhide",
"quit": "Quit",
"undo": "Undo",
"redo": "Redo",
"cut": "Cut",
"copy": "Copy",
"paste": "Paste",
"selectAll": "Select All",
"checkForUpdates": "Check for Updates",
"reportIssue": "Report Issue"
},
"models": {
"user": {
"id": "ID",
Expand Down Expand Up @@ -360,7 +375,6 @@
"currentVersion": "Current version",
"checkUpdate": "Check update",
"checkingLatestVersion": "Checking latest version",
"updateAvailable": "Update available",
"updateDownloaded": "A new version has been downloaded. Restart the application to apply the updates.",
"restart": "Restart",
"later": "Later",
Expand Down Expand Up @@ -938,5 +952,8 @@
"horizontal": "Horizontal",
"vertical": "Vertical",
"copied": "Copied",
"copyFullText": "Copy full text"
"copyFullText": "Copy full text",
"checkingForUpdate": "Checking for update",
"updateAvailable": "Update available",
"quitAndInstall": "Quit and install"
}
21 changes: 19 additions & 2 deletions enjoy/src/i18n/zh-CN.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
{
"menu": {
"about": "关于",
"toggleFullScreen": "全屏",
"hide": "隐藏",
"unhide": "显示",
"quit": "退出",
"undo": "撤销",
"redo": "重做",
"cut": "剪切",
"copy": "复制",
"paste": "粘贴",
"selectAll": "全选",
"checkForUpdates": "检查更新",
"reportIssue": "报告问题"
},
"models": {
"user": {
"id": "ID",
Expand Down Expand Up @@ -360,7 +375,6 @@
"currentVersion": "当前版本",
"checkUpdate": "检查更新",
"checkingLatestVersion": "正在检查最新版本",
"updateAvailable": "有新版本可用",
"updateDownloaded": "新版本已下载,点击重新启动以更新",
"restart": "Restart",
"later": "Later",
Expand Down Expand Up @@ -938,5 +952,8 @@
"horizontal": "水平",
"vertical": "垂直",
"copied": "已复制",
"copyFullText": "复制全文"
"copyFullText": "复制全文",
"checkingForUpdate": "正在检查更新",
"updateAvailable": "有新版本",
"quitAndInstall": "退出并安装新版本"
}
14 changes: 0 additions & 14 deletions enjoy/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,13 @@ import mainWindow from "@main/window";
import ElectronSquirrelStartup from "electron-squirrel-startup";
import contextMenu from "electron-context-menu";
import { t } from "i18next";
import { updateElectronApp, UpdateSourceType } from "update-electron-app";
import unhandled from "electron-unhandled";
import newGithubIssueUrl from "new-github-issue-url";

const logger = log.scope("main");

app.commandLine.appendSwitch("enable-features", "SharedArrayBuffer");

// config auto updater
if (!process.env.CI) {
updateElectronApp({
updateSource: {
type: UpdateSourceType.StaticStorage,
baseUrl: `https://dl.enjoy.bot/app/${process.platform}/${process.arch}`,
},
updateInterval: "1 hour",
logger: logger,
notifyUser: true,
});
}

if (!app.isPackaged) {
app.disableHardwareAcceleration();
app.commandLine.appendSwitch("disable-software-rasterizer");
Expand Down
88 changes: 74 additions & 14 deletions enjoy/src/main/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
dialog,
systemPreferences,
MenuItemConstructorOptions,
autoUpdater,
} from "electron";
import path from "path";
import db from "@main/db";
Expand All @@ -25,7 +26,9 @@ import dict from "./dict";
import mdict from "./mdict";
import decompresser from "./decompresser";
import { UserSetting } from "@main/db/models";
import { platform } from "os";
import { t } from "i18next";
import { format } from "util";
import pkg from "../../package.json" assert { type: "json" };

const __dirname = import.meta.dirname;

Expand All @@ -37,6 +40,25 @@ const youtubeProvider = new YoutubeProvider();
const ffmpeg = new Ffmpeg();
const waveform = new Waveform();

const FEED_BASE_URL = `https://dl.enjoy.bot/app/${process.platform}/${process.arch}`;
autoUpdater.setFeedURL({
url:
process.platform === "darwin"
? `${FEED_BASE_URL}/RELEASES.json`
: FEED_BASE_URL,
headers: {
"X-App-Version": app.getVersion(),
"User-Agent": format(
"%s/%s (%s: %s)",
pkg.name,
pkg.version,
process.platform,
process.arch
),
},
serverType: process.platform === "darwin" ? "json" : "default",
});

const main = {
win: null as BrowserWindow | null,
init: () => {},
Expand Down Expand Up @@ -386,6 +408,44 @@ main.init = async () => {
app.quit();
});

ipcMain.handle("app-check-for-updates", () => {
autoUpdater.checkForUpdates();
});

ipcMain.handle("app-quit-and-install", () => {
autoUpdater.quitAndInstall();
});

ipcMain.on("app-on-updater", () => {
autoUpdater.on("error", (error) => {
mainWindow.webContents.send("app-on-updater", "error", [error]);
});
autoUpdater.on("checking-for-update", () => {
mainWindow.webContents.send("app-on-updater", "checking-for-update", []);
});
autoUpdater.on("update-available", () => {
mainWindow.webContents.send("app-on-updater", "update-available", []);
});
autoUpdater.on(
"update-downloaded",
(_event, releaseNotes, releaseName, releaseDate, updateURL) => {
logger.info(
"update-downloaded",
releaseNotes,
releaseName,
releaseDate,
updateURL
);
mainWindow.webContents.send("app-on-updater", "update-downloaded", [
releaseNotes,
releaseName,
releaseDate,
updateURL,
]);
}
);
});

ipcMain.handle("app-open-dev-tools", () => {
mainWindow.webContents.openDevTools();
});
Expand Down Expand Up @@ -646,38 +706,38 @@ ${log}
{
label: app.name,
submenu: [
{ role: "about" },
{ role: "about", label: t("menu.about") },
{ type: "separator" },
{ role: "togglefullscreen" },
{ role: "hide" },
{ role: "unhide" },
{ role: "togglefullscreen", label: t("menu.toggleFullScreen") },
{ role: "hide", label: t("menu.hide") },
{ role: "unhide", label: t("menu.unhide") },
{ type: "separator" },
{ role: "quit" },
{ role: "quit", label: t("menu.quit") },
],
},
{
label: "Edit",
submenu: [
{ role: "undo" },
{ role: "redo" },
{ role: "undo", label: t("menu.undo") },
{ role: "redo", label: t("menu.redo") },
{ type: "separator" },
{ role: "cut" },
{ role: "copy" },
{ role: "paste" },
{ role: "selectAll" },
{ role: "cut", label: t("menu.cut") },
{ role: "copy", label: t("menu.copy") },
{ role: "paste", label: t("menu.paste") },
{ role: "selectAll", label: t("menu.selectAll") },
],
},
{
label: "Help",
submenu: [
{
label: "Check for Updates...",
label: t("menu.checkForUpdates"),
click: () => {
shell.openExternal("https://1000h.org/enjoy-app/install.html");
},
},
{
label: "Report Issue...",
label: t("menu.reportIssue"),
click: () => {
shell.openExternal(`${REPO_URL}/issues/new`);
},
Expand Down
16 changes: 16 additions & 0 deletions enjoy/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ contextBridge.exposeInMainWorld("__ENJOY_APP__", {
quit: () => {
ipcRenderer.invoke("app-quit");
},
checkForUpdates: () => {
ipcRenderer.invoke("app-check-for-updates");
},
quitAndInstall: () => {
ipcRenderer.invoke("app-quit-and-install");
},
onUpdater: (
callback: (
event: IpcRendererEvent,
eventType: string,
args: any[]
) => void
) => ipcRenderer.on("app-on-updater", callback),
removeUpdaterListeners: () => {
ipcRenderer.removeAllListeners("app-on-updater");
},
openDevTools: () => {
ipcRenderer.invoke("app-open-dev-tools");
},
Expand Down
5 changes: 4 additions & 1 deletion enjoy/src/renderer/components/layouts/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export const Layout = () => {
<ResizablePanel id="main-panel" order={1} minSize={50}>
<div className="flex flex-start">
<Sidebar />
<div className="flex-1 border-l overflow-x-hidden overflow-y-auto h-content">
<div
id="main-panel-content"
className="flex-1 border-l overflow-x-hidden overflow-y-auto h-content relative"
>
<Outlet />
</div>
</div>
Expand Down
55 changes: 46 additions & 9 deletions enjoy/src/renderer/components/layouts/title-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,38 @@ import {
LightbulbIcon,
LightbulbOffIcon,
MaximizeIcon,
MenuIcon,
MinimizeIcon,
MinusIcon,
SettingsIcon,
XIcon,
} from "lucide-react";
import { useContext, useEffect, useState } from "react";

const INSTALL_URL = "https://1000h.org/enjoy-app/install.html";

export const TitleBar = () => {
const [isMaximized, setIsMaximized] = useState(false);
const [isFullScreen, setIsFullScreen] = useState(false);
const [platform, setPlatform] = useState<"darwin" | "win32" | "linux">();
const [updaterState, setUpdaterState] = useState<
"checking-for-update" | "update-available" | "update-downloaded" | "error"
>();

const { EnjoyApp, setDisplayPreferences, initialized } = useContext(
AppSettingsProviderContext
);
const { active, setActive } = useContext(CopilotProviderContext);

const checkUpdate = () => {
if (platform === "linux") {
EnjoyApp.shell.openExternal(INSTALL_URL);
} else if (updaterState === "update-downloaded") {
EnjoyApp.app.quitAndInstall();
} else {
EnjoyApp.app.checkForUpdates();
}
};

const onWindowChange = (
_event: IpcRendererEvent,
state: { event: string }
Expand All @@ -65,14 +79,28 @@ export const TitleBar = () => {
}
};

const onUpdater = (
_event: IpcRendererEvent,
eventType:
| "checking-for-update"
| "update-available"
| "update-downloaded"
| "error",
args: any[]
) => {
setUpdaterState(eventType);
};

useEffect(() => {
EnjoyApp.window.onChange(onWindowChange);
EnjoyApp.app.getPlatformInfo().then((info) => {
setPlatform(info.platform as "darwin" | "win32" | "linux");
});
EnjoyApp.app.onUpdater(onUpdater);

return () => {
EnjoyApp.window.removeListener(onWindowChange);
EnjoyApp.app.removeUpdaterListeners();
};
}, []);

Expand Down Expand Up @@ -123,9 +151,12 @@ export const TitleBar = () => {
<Button
variant="ghost"
size="icon"
className="size-8 rounded-none non-draggable-region hover:bg-primary/10"
className="size-8 rounded-none non-draggable-region hover:bg-primary/10 relative"
>
<HelpCircleIcon className="size-4" />
{updaterState && (
<span className="absolute -top-1 -right-1 bg-red-500 rounded-full size-2"></span>
)}
</Button>
</DropdownMenuTrigger>

Expand Down Expand Up @@ -181,14 +212,20 @@ export const TitleBar = () => {
</DropdownMenuGroup>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={() =>
EnjoyApp.shell.openExternal(
"https://1000h.org/enjoy-app/install.html"
)
}
className="cursor-pointer"
onClick={checkUpdate}
className="cursor-pointer relative"
>
<span className="capitalize">{t("checkUpdate")}</span>
{updaterState && (
<span className="absolute -top-1 -right-1 bg-red-500 rounded-full size-2"></span>
)}
<span className="capitalize">
{updaterState === "checking-for-update" &&
t("checkingForUpdate")}
{updaterState === "update-available" && t("updateAvailable")}
{updaterState === "update-downloaded" && t("quitAndInstall")}
{!updaterState ||
(updaterState === "error" && t("checkUpdate"))}
</span>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
Expand Down
Loading
Loading