Add ability to change Emacs meta key on Mac

This commit is contained in:
Jonatan Heyman 2023-01-25 10:11:51 +01:00
parent 659f853102
commit 97d4f6294a
5 changed files with 69 additions and 30 deletions

View File

@ -16,6 +16,7 @@ const schema = {
additionalProperties: false, additionalProperties: false,
}, },
keymap: { "enum": ["default", "emacs"] }, keymap: { "enum": ["default", "emacs"] },
emacsMetaKey: { "enum": [null, "alt", "meta"] },
} }
export default new Store({schema}) export default new Store({schema})

View File

@ -1,3 +1,4 @@
import CONFIG from "./config"
import { isMac } from "./detect-platform" import { isMac } from "./detect-platform"
@ -5,7 +6,7 @@ export function onBeforeInputEvent({win, event, input, currentKeymap}) {
//console.log("keyboard event", input) //console.log("keyboard event", input)
let metaKey = "alt" let metaKey = "alt"
if (isMac) { if (isMac) {
metaKey = "meta" metaKey = CONFIG.get("emacsMetaKey", "meta")
} }
if (currentKeymap === "emacs") { if (currentKeymap === "emacs") {
/** /**
@ -13,13 +14,13 @@ export function onBeforeInputEvent({win, event, input, currentKeymap}) {
* using Codemirror's bind function. Therefore we have to bind them in electron land, and send * using Codemirror's bind function. Therefore we have to bind them in electron land, and send
* cut, paste and copy to window.webContents * cut, paste and copy to window.webContents
*/ */
if (input.key === "y" && input.control) { if (input.code === "KeyY" && input.control) {
event.preventDefault() event.preventDefault()
win.webContents.paste() win.webContents.paste()
} else if (input.key === "w" && input.control) { } else if (input.code === "KeyW" && input.control) {
event.preventDefault() event.preventDefault()
win.webContents.cut() win.webContents.cut()
} else if (input.key === "w" && input[metaKey]) { } else if (input.code === "KeyW" && input[metaKey]) {
event.preventDefault() event.preventDefault()
win.webContents.copy() win.webContents.copy()
} }

View File

@ -5,14 +5,16 @@ import { ipcRenderer } from "electron"
import { WINDOW_CLOSE_EVENT, KEYMAP_CHANGE_EVENT, OPEN_SETTINGS_EVENT } from "../constants" import { WINDOW_CLOSE_EVENT, KEYMAP_CHANGE_EVENT, OPEN_SETTINGS_EVENT } from "../constants"
import CONFIG from "../config" import CONFIG from "../config"
contextBridge.exposeInMainWorld("platform", { //contextBridge.exposeInMainWorld("platform", )
isMac,
isWindows,
isLinux,
})
contextBridge.exposeInMainWorld('darkMode', darkMode) contextBridge.exposeInMainWorld('darkMode', darkMode)
contextBridge.exposeInMainWorld("heynote", { contextBridge.exposeInMainWorld("heynote", {
platform: {
isMac,
isWindows,
isLinux,
},
quit() { quit() {
console.log("quitting") console.log("quitting")
//ipcRenderer.invoke("app_quit") //ipcRenderer.invoke("app_quit")
@ -30,7 +32,7 @@ contextBridge.exposeInMainWorld("heynote", {
async load() { async load() {
return await ipcRenderer.invoke("buffer-content:load") return await ipcRenderer.invoke("buffer-content:load")
}, },
async save(content) { async save(content) {
return await ipcRenderer.invoke("buffer-content:save", content) return await ipcRenderer.invoke("buffer-content:save", content)
}, },
@ -44,7 +46,11 @@ contextBridge.exposeInMainWorld("heynote", {
set(keymap) { set(keymap) {
ipcRenderer.invoke("keymap:set", keymap); ipcRenderer.invoke("keymap:set", keymap);
}, },
setEmacsMetaKey(key) {
CONFIG.set("emacsMetaKey", key)
},
initial: CONFIG.get("keymap", "default"), initial: CONFIG.get("keymap", "default"),
getEmacsMetaKey: () => CONFIG.get("emacsMetaKey", isMac ? "meta" : "alt"),
onKeymapChange(callback) { onKeymapChange(callback) {
ipcRenderer.on(KEYMAP_CHANGE_EVENT, (event, keymap) => callback(keymap)) ipcRenderer.on(KEYMAP_CHANGE_EVENT, (event, keymap) => callback(keymap))
}, },

View File

@ -127,7 +127,7 @@
/> />
<Settings <Settings
v-if="showSettings" v-if="showSettings"
:keymap="keymap" :initialKeymap="keymap"
@closeSettings="closeSettings" @closeSettings="closeSettings"
/> />
</div> </div>

View File

@ -1,7 +1,19 @@
<script> <script>
export default { export default {
props: { props: {
keymap: String, initialKeymap: String,
},
data() {
return {
keymaps: [
{ name: "Default", value: "default" },
{ name: "Emacs", value: "emacs" },
],
keymap: this.initialKeymap,
metaKey: window.heynote.keymap.getEmacsMetaKey(),
isMac: window.heynote.platform.isMac,
}
}, },
mounted() { mounted() {
@ -12,11 +24,17 @@
window.removeEventListener("keydown", this.onKeyDown); window.removeEventListener("keydown", this.onKeyDown);
}, },
methods: { watch: {
onKeymapChange(event) { keymap(value) {
window.heynote.keymap.set(event.target.value) window.heynote.keymap.set(value)
}, },
metaKey(value) {
window.heynote.keymap.setEmacsMetaKey(value)
}
},
methods: {
onKeyDown(event) { onKeyDown(event) {
if (event.key === "Escape") { if (event.key === "Escape") {
this.$emit("closeSettings") this.$emit("closeSettings")
@ -31,12 +49,22 @@
<div class="dialog"> <div class="dialog">
<div> <div>
<h1>Settings</h1> <h1>Settings</h1>
<div class="entry"> <div class="row">
<h2>Keymap:</h2> <div class="entry">
<select ref="keymapSelector" @change="onKeymapChange"> <h2>Keymap</h2>
<option :selected="keymap==='default'" value="default">Default</option> <select ref="keymapSelector" v-model="keymap">
<option :selected="keymap==='emacs'" value="emacs">Emacs</option> <template v-for="km in keymaps" :key="km.value">
</select> <option :selected="km.value === keymap" :value="km.value">{{ km.name }}</option>
</template>
</select>
</div>
<div class="entry" v-if="keymap === 'emacs' && isMac">
<h2>Meta Key</h2>
<select v-model="metaKey">
<option :selected="metaKey === 'meta'" value="meta">Command</option>
<option :selected="metaKey === 'alt'" value="alt">Option</option>
</select>
</div>
</div> </div>
</div> </div>
<button <button
@ -95,15 +123,18 @@
font-weight: 600 font-weight: 600
margin-bottom: 20px margin-bottom: 20px
.entry .row
margin-bottom: 20px display: flex
h2 .entry
font-weight: 600 margin-bottom: 20px
margin-bottom: 10px margin-right: 20px
select h2
width: 200px font-weight: 600
&:focus margin-bottom: 10px
outline: none select
width: 200px
&:focus
outline: none
.close .close
height: 32px height: 32px