fix mas rejection - add touch bar support

This commit is contained in:
Bruce Liu 2020-08-07 17:11:26 +08:00
parent 2448663290
commit 79d5a5861e
8 changed files with 83 additions and 9 deletions

View File

@ -1,5 +1,6 @@
import { ipcRenderer } from "electron"
import { ImageCallbackTypes } from "../schema-types"
import { ImageCallbackTypes, TouchBarTexts } from "../schema-types"
import { IObjectWithKey } from "@fluentui/react"
const utilsBridge = {
platform: process.platform,
@ -108,6 +109,19 @@ const utilsBridge = {
callback(false)
})
},
addTouchBarEventsListener: (callback: (IObjectWithKey) => any) => {
ipcRenderer.removeAllListeners("touchbar-event")
ipcRenderer.on("touchbar-event", (_, key: string) => {
callback({ key: key} )
})
},
initTouchBar: (texts: TouchBarTexts) => {
ipcRenderer.invoke("touchbar-init", texts)
},
destroyTouchBar: () => {
ipcRenderer.invoke("touchbar-destroy")
},
}
declare global {

View File

@ -2,7 +2,7 @@ import * as React from "react"
import intl from "react-intl-universal"
import { Icon } from "@fluentui/react/lib/Icon"
import { AppState } from "../scripts/models/app"
import { ProgressIndicator } from "@fluentui/react"
import { ProgressIndicator, IObjectWithKey } from "@fluentui/react"
import { getWindowBreakpoint } from "../scripts/utils"
type NavProps = {
@ -34,7 +34,7 @@ class Nav extends React.Component<NavProps, NavState> {
this.setState({ maximized: state })
}
navShortcutsHandler = (e: KeyboardEvent) => {
navShortcutsHandler = (e: KeyboardEvent | IObjectWithKey) => {
if (!this.props.state.settings.display) {
switch (e.key) {
case "F1":
@ -66,6 +66,7 @@ class Nav extends React.Component<NavProps, NavState> {
componentDidMount() {
document.addEventListener("keydown", this.navShortcutsHandler)
if (window.utils.platform === "darwin") window.utils.addTouchBarEventsListener(this.navShortcutsHandler)
}
componentWillUnmount() {
document.removeEventListener("keydown", this.navShortcutsHandler)

View File

@ -9,6 +9,7 @@ import GroupsTabContainer from "../containers/settings/groups-container"
import AppTabContainer from "../containers/settings/app-container"
import RulesTabContainer from "../containers/settings/rules-container"
import ServiceTabContainer from "../containers/settings/service-container"
import { initTouchBarWithTexts } from "../scripts/utils"
type SettingsProps = {
display: boolean,
@ -22,6 +23,13 @@ class Settings extends React.Component<SettingsProps> {
super(props)
}
componentDidUpdate= (prevProps: SettingsProps) => {
if (this.props.display !== prevProps.display) {
if (this.props.display) window.utils.destroyTouchBar()
else initTouchBarWithTexts()
}
}
render = () => this.props.display && (
<div className="settings-container">
<div className="btn-group" style={{position: "absolute", top: 70, left: "calc(50% - 404px)"}}>

24
src/main/touchbar.ts Normal file
View File

@ -0,0 +1,24 @@
import { TouchBarTexts } from "../schema-types"
import { BrowserWindow, TouchBar } from "electron"
function createTouchBarFunctionButton(window: BrowserWindow, text: string, key: string) {
return new TouchBar.TouchBarButton({
label: text,
click: () => window.webContents.send("touchbar-event", key)
})
}
export function initMainTouchBar(texts: TouchBarTexts, window: BrowserWindow) {
const touchBar = new TouchBar({
items: [
createTouchBarFunctionButton(window, texts.menu, "F1"),
createTouchBarFunctionButton(window, texts.search, "F2"),
new TouchBar.TouchBarSpacer({ size: "small" }),
createTouchBarFunctionButton(window, texts.refresh, "F5"),
createTouchBarFunctionButton(window, texts.markAll, "F6"),
createTouchBarFunctionButton(window, texts.notifications, "F7"),
]
})
window.setTouchBar(touchBar)
}

View File

@ -1,7 +1,8 @@
import { ipcMain, shell, dialog, app, session, clipboard } from "electron"
import { ipcMain, shell, dialog, app, session, clipboard, TouchBar } from "electron"
import { WindowManager } from "./window"
import fs = require("fs")
import { ImageCallbackTypes } from "../schema-types"
import { ImageCallbackTypes, TouchBarTexts } from "../schema-types"
import { initMainTouchBar } from "./touchbar"
export function setUtilsListeners(manager: WindowManager) {
async function openExternal(url: string, background=false) {
@ -206,4 +207,11 @@ export function setUtilsListeners(manager: WindowManager) {
}
}
})
ipcMain.handle("touchbar-init", (_, texts: TouchBarTexts) => {
if (manager.hasWindow()) initMainTouchBar(texts, manager.mainWindow)
})
ipcMain.handle("touchbar-destroy", () => {
if (manager.hasWindow()) manager.mainWindow.setTouchBar(null)
})
}

View File

@ -44,6 +44,14 @@ export interface ServiceConfigs {
importGroups?: boolean
}
export interface TouchBarTexts {
menu: string
search: string
refresh: string
markAll: string
notifications: string
}
export type SchemaTypes = {
version: string
theme: ThemeSettings

View File

@ -1,7 +1,7 @@
import intl from "react-intl-universal"
import { INIT_SOURCES, SourceActionTypes, ADD_SOURCE, UPDATE_SOURCE, DELETE_SOURCE, initSources, SourceOpenTarget } from "./source"
import { RSSItem, ItemActionTypes, FETCH_ITEMS, fetchItems } from "./item"
import { ActionStatus, AppThunk, getWindowBreakpoint } from "../utils"
import { ActionStatus, AppThunk, getWindowBreakpoint, initTouchBarWithTexts } from "../utils"
import { INIT_FEEDS, FeedActionTypes, ALL, initFeeds } from "./feed"
import { SourceGroupActionTypes, UPDATE_SOURCE_GROUP, ADD_SOURCE_TO_GROUP, DELETE_SOURCE_GROUP, REMOVE_SOURCE_FROM_GROUP, REORDER_SOURCE_GROUPS } from "./group"
import { PageActionTypes, SELECT_PAGE, PageType, selectAllArticles, showItemFromId } from "./page"
@ -276,9 +276,10 @@ export function initIntl(): AppThunk<Promise<void>> {
export function initApp(): AppThunk {
return (dispatch) => {
document.body.classList.add(window.utils.platform)
dispatch(initIntl()).then(() =>
dispatch(initSources())
).then(() =>
dispatch(initIntl()).then(async () => {
if (window.utils.platform === "darwin") initTouchBarWithTexts()
await dispatch(initSources())
}).then(() =>
dispatch(initFeeds())
).then(() => {
dispatch(selectAllArticles())

View File

@ -193,3 +193,13 @@ export function validateRegex(regex: string, flags = ""): RegExp {
export function platformCtrl(e: React.MouseEvent | React.KeyboardEvent | MouseEvent | KeyboardEvent) {
return window.utils.platform === "darwin" ? e.metaKey : e.ctrlKey
}
export function initTouchBarWithTexts() {
window.utils.initTouchBar({
menu: intl.get("nav.menu"),
search: intl.get("search"),
refresh: intl.get("nav.refresh"),
markAll: intl.get("nav.markAllRead"),
notifications: intl.get("nav.notifications")
})
}