feat: inform wiki of titlebar change

This commit is contained in:
lin onetwo 2022-11-23 14:45:51 +08:00
parent d7ba2eeca9
commit 18cda646ed
6 changed files with 65 additions and 11 deletions

View file

@ -57,6 +57,7 @@ export enum WikiChannel {
setTiddlerTextDone = 'wiki-set-tiddler-text-done',
/** show message inside tiddlywiki to show git sync progress */
syncProgress = 'wiki-sync-progress',
setState = "setState"
}
export enum WikiGitWorkspaceChannel {
name = 'WikiGitWorkspaceChannel',

3
src/constants/wiki.ts Normal file
View file

@ -0,0 +1,3 @@
export enum WikiStateKey {
titleBarOpened = 'titleBarOpened',
}

View file

@ -3,10 +3,11 @@
/**
* Call tiddlywiki api from electron
* This file should be required by view.ts preload script to work
*
* You can use wrapped method in `services/wiki/index.ts` 's `wikiOperation()` instead. Available operations are registered in `src/services/wiki/wikiOperations.ts`
*/
import { ipcRenderer, webFrame } from 'electron';
import { WikiChannel } from '@/constants/channels';
import { native } from './common/services';
/**
* Execute statement with $tw when idle, so there won't be significant lagging.
@ -86,6 +87,13 @@ ipcRenderer.on(WikiChannel.syncProgress, async (event, message: string) => {
{ onlyWhenVisible: true },
);
});
ipcRenderer.on(WikiChannel.setState, async (event, stateKey: string, content: string) => {
await executeTWJavaScriptWhenIdle(
`
$tw.wiki.addTiddler({ title: '$:/state/${stateKey}', text: '${content}' });
`,
);
});
ipcRenderer.on(WikiChannel.generalNotification, async (event, message: string) => {
await executeTWJavaScriptWhenIdle(`
$tw.wiki.addTiddler({ title: '$:/state/notification/${WikiChannel.generalNotification}', text: '${message}' });

View file

@ -14,6 +14,10 @@ import { IPreferences, IPreferenceService } from './interface';
import { defaultPreferences } from './defaultPreferences';
import { lazyInject } from '@services/container';
import { requestChangeLanguage } from '@services/libs/i18n/requestChangeLanguage';
import { IWikiService } from '@services/wiki/interface';
import { IWorkspaceService } from '@services/workspaces/interface';
import { WikiChannel } from '@/constants/channels';
import { WikiStateKey } from '@/constants/wiki';
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const debouncedSetPreferenceFile = debounce(async (newPreferences: IPreferences) => await settings.set(`preferences`, { ...newPreferences } as any), 500);
@ -22,6 +26,8 @@ const debouncedSetPreferenceFile = debounce(async (newPreferences: IPreferences)
export class Preference implements IPreferenceService {
@lazyInject(serviceIdentifier.Window) private readonly windowService!: IWindowService;
@lazyInject(serviceIdentifier.NotificationService) private readonly notificationService!: INotificationService;
@lazyInject(serviceIdentifier.Wiki) private readonly wikiService!: IWikiService;
@lazyInject(serviceIdentifier.Workspace) private readonly workspaceService!: IWorkspaceService;
private cachedPreferences: IPreferences;
public preference$: BehaviorSubject<IPreferences>;
@ -96,11 +102,30 @@ export class Preference implements IPreferenceService {
if (key.startsWith('pauseNotifications')) {
await this.notificationService.updatePauseNotificationsInfo();
}
if (key === 'themeSource') {
nativeTheme.themeSource = value as IPreferences['themeSource'];
}
if (key === 'language') {
await requestChangeLanguage(value as string);
switch (key) {
case 'themeSource': {
nativeTheme.themeSource = value as IPreferences['themeSource'];
break;
}
case 'language': {
await requestChangeLanguage(value as string);
break;
}
case 'titleBar': {
/**
* Tell wiki titleBar is on/off, so opened-tiddlers-bar plugin can react to it.
* This also happened in reactWhenSetActiveWorkspaceView in src/services/workspacesView/index.ts
*/
const activeWorkspaceID = this.workspaceService.getActiveWorkspaceSync()?.id;
if (activeWorkspaceID !== undefined) {
this.wikiService.wikiOperation(WikiChannel.setState, [
activeWorkspaceID,
WikiStateKey.titleBarOpened,
(value as IPreferences['titleBar']) ? 'yes' : 'no',
]);
}
break;
}
}
}

View file

@ -1,14 +1,15 @@
import { WikiChannel } from '@/constants/channels';
import { WikiStateKey } from '@/constants/wiki';
import { container } from '@services/container';
import serviceIdentifier from '@services/serviceIdentifier';
import { IViewService } from '@services/view/interface';
import { IWindowService } from '@services/windows/interface';
import { WindowNames } from '@services/windows/WindowProperties';
function sendToMainWindow(type: WikiChannel, workspaceID: string, message: string): void {
function sendToMainWindow(type: WikiChannel, workspaceID: string, messages: string[]): void {
const viewService = container.get<IViewService>(serviceIdentifier.View);
const browserView = viewService.getView(workspaceID, WindowNames.main);
browserView?.webContents?.send?.(type, message);
browserView?.webContents?.send?.(type, ...messages);
}
/**
* Handle sending message to trigger operations defined in `src/preload/wikiOperation.ts`
@ -19,9 +20,11 @@ export const wikiOperations = {
const createWorkspaceWindow = windowService.get(WindowNames.addWorkspace);
createWorkspaceWindow?.webContents?.send(WikiChannel.createProgress, message);
},
[WikiChannel.syncProgress]: (workspaceID: string, message: string): void => sendToMainWindow(WikiChannel.syncProgress, workspaceID, message),
[WikiChannel.generalNotification]: (workspaceID: string, message: string): void => sendToMainWindow(WikiChannel.generalNotification, workspaceID, message),
[WikiChannel.openTiddler]: (workspaceID: string, tiddlerName: string): void => sendToMainWindow(WikiChannel.openTiddler, workspaceID, tiddlerName),
[WikiChannel.syncProgress]: (workspaceID: string, message: string): void => sendToMainWindow(WikiChannel.syncProgress, workspaceID, [message]),
[WikiChannel.generalNotification]: (workspaceID: string, message: string): void => sendToMainWindow(WikiChannel.generalNotification, workspaceID, [message]),
[WikiChannel.openTiddler]: (workspaceID: string, tiddlerName: string): void => sendToMainWindow(WikiChannel.openTiddler, workspaceID, [tiddlerName]),
[WikiChannel.setState]: (workspaceID: string, stateKey: WikiStateKey, content: string): void =>
sendToMainWindow(WikiChannel.setState, workspaceID, [stateKey, content]),
// TODO: add more operations here from `src/preload/wikiOperation.ts`
};
export type IWikiOperations = typeof wikiOperations;

View file

@ -23,6 +23,7 @@ import { SupportedStorageServices } from '@services/types';
import { WorkspaceFailedToLoadError } from './error';
import { WikiChannel } from '@/constants/channels';
import { tiddlywikiLanguagesMap } from '@/constants/languages';
import { WikiStateKey } from '@/constants/wiki';
@injectable()
export class WorkspaceView implements IWorkspaceViewService {
@ -323,6 +324,19 @@ export class WorkspaceView implements IWorkspaceViewService {
if (oldActiveWorkspace !== undefined && oldActiveWorkspace.id !== nextWorkspaceID && oldActiveWorkspace.hibernateWhenUnused) {
await this.hibernateWorkspaceView(oldActiveWorkspace.id);
}
await this.reactWhenSetActiveWorkspaceView(newWorkspace);
}
/**
* Apply things to active workspace's view when workspace become active
*/
private async reactWhenSetActiveWorkspaceView(newWorkspace: IWorkspace): Promise<void> {
/**
* Tell wiki titleBar is on/off, so opened-tiddlers-bar plugin can react to it.
* This also happened in reactWhenPreferencesChanged in src/services/preferences/index.ts
*/
const titleBar = await this.preferenceService.get('titleBar');
this.wikiService.wikiOperation(WikiChannel.setState, [newWorkspace.id, WikiStateKey.titleBarOpened, titleBar ? 'yes' : 'no']);
}
public async removeWorkspaceView(workspaceID: string): Promise<void> {