Skip to content

Commit

Permalink
working keyboard shorcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
vincelwt committed Dec 2, 2022
1 parent 0f4e66f commit f9ef102
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 64 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ You can use Cmd+Shift+G to quickly open it from anywhere.

Download:

- [Mac Arm64 .dmg](https://github.com/vincelwt/chatgpt-mac/releases/download/v0.0.3/ChatGPT-0.0.3-arm64.dmg)
- [Mac Intel .dmg](https://github.com/vincelwt/chatgpt-mac/releases/download/v0.0.3/ChatGPT-0.0.3-x64.dmg)
- [Mac Arm64 .dmg](https://github.com/vincelwt/chatgpt-mac/releases/download/v0.0.4/ChatGPT-0.0.4-arm64.dmg)
- [Mac Intel .dmg](https://github.com/vincelwt/chatgpt-mac/releases/download/v0.0.4/ChatGPT-0.0.4-x64.dmg)

<p align="center">
<img src="./images/screenshot.jpeg" width="500">
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</head>
<body class="myarrow">
<div class="page darwin">
<webview id="wv1" src="https://chat.openai.com/chat" autosize="on" style="width:100%; height:530px">
<webview id="wv1" src="https://chat.openai.com/chat" nodeintegration="on" autosize="on" style="width:100%; height:530px">
</webview>
</div>
<script>
Expand Down
65 changes: 45 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
const { menubar } = require("menubar");

const path = require("path");
const {
app,
nativeImage,
shell,
Tray,
Menu,
MenuItem,
globalShortcut,
} = require("electron");
const { app, nativeImage, Tray, Menu, globalShortcut } = require("electron");
const contextMenu = require("electron-context-menu");

const image = nativeImage.createFromPath(
path.join(__dirname, `images/newiconTemplate.png`)
);

const contextMenuTemplate = [
{ role: "about" },
{
label: "Quit",
accelerator: "Command+Q",
click: function () {
app.quit();
},
},
];

app.on("ready", () => {
const tray = new Tray(image);

const contextMenu = Menu.buildFromTemplate([
{ role: "about" },
{ type: "separator" },
{ role: "quit" },
]);

const mb = menubar({
browserWindow: {
icon: image,
Expand All @@ -36,6 +34,7 @@ app.on("ready", () => {
height: 550,
},
tray,
showOnAllWorkspaces: false,
preloadWindow: true,
showDockIcon: false,
icon: image,
Expand All @@ -45,15 +44,13 @@ app.on("ready", () => {
app.dock.hide();

tray.on("right-click", () => {
mb.tray.popUpContextMenu(contextMenu);
mb.tray.popUpContextMenu(Menu.buildFromTemplate(contextMenuTemplate));
});

const { window } = mb;

const menu = new Menu();

globalShortcut.register("CommandOrControl+Shift+g", () => {
console.log(window.isFocused(), window.isVisible());
if (window.isVisible()) {
mb.hideWindow();
} else {
Expand All @@ -71,11 +68,40 @@ app.on("ready", () => {
console.log("Menubar app is ready.");
});

app.on("web-contents-created", (e, contents) => {
if (contents.getType() == "webview") {
// open link with external browser in webview
contents.on("new-window", (e, url) => {
e.preventDefault();
shell.openExternal(url);
});
// set context menu in webview
contextMenu({
window: contents,
});

// we can't set the native app menu with "menubar" so need to manually register these events
// register cmd+c/cmd+v events
contents.on("before-input-event", (event, input) => {
const { control, meta, key } = input;
if (!control && !meta) return;
if (key === "c") contents.copy();
if (key === "v") contents.paste();
if (key === "a") contents.selectAll();
if (key === "z") contents.undo();
if (key === "y") contents.redo();
if (key === "q") app.quit();
if (key === "r") contents.reload();
});
}
});

// restore focus to previous app on hiding
mb.on("after-hide", () => {
mb.app.hide();
});

// open in new window
// open links in new window
// app.on("web-contents-created", (event, contents) => {
// contents.on("will-navigate", (event, navigationUrl) => {
// event.preventDefault();
Expand All @@ -89,7 +115,6 @@ app.on("ready", () => {
"true"
);
});
// restore focus to previous app on hiding

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
Expand Down
Loading

0 comments on commit f9ef102

Please sign in to comment.