mirror of
https://github.com/tiddly-gittly/TidGi-Desktop.git
synced 2025-12-06 02:30:47 -08:00
refactor: dup logic
This commit is contained in:
parent
ee31957186
commit
313536a300
5 changed files with 77 additions and 63 deletions
|
|
@ -1,10 +1,34 @@
|
||||||
import { PageType } from '@/constants/pageTypes';
|
import { PageType } from '@/constants/pageTypes';
|
||||||
import { usePreferenceObservable } from '@services/preferences/hooks';
|
import { usePreferenceObservable } from '@services/preferences/hooks';
|
||||||
|
import type { IPreferences } from '@services/preferences/interface';
|
||||||
import { WindowNames } from '@services/windows/WindowProperties';
|
import { WindowNames } from '@services/windows/WindowProperties';
|
||||||
import { useWorkspacesListObservable } from '@services/workspaces/hooks';
|
import { useWorkspacesListObservable } from '@services/workspaces/hooks';
|
||||||
|
import type { IWorkspaceWithMetadata } from '@services/workspaces/interface';
|
||||||
import { useEffect, useRef } from 'react';
|
import { useEffect, useRef } from 'react';
|
||||||
import { useLocation } from 'wouter';
|
import { useLocation } from 'wouter';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to determine the target workspace for tidgi mini window based on preferences
|
||||||
|
*/
|
||||||
|
function getTidgiMiniWindowTargetWorkspace(
|
||||||
|
workspacesList: IWorkspaceWithMetadata[],
|
||||||
|
preferences: IPreferences,
|
||||||
|
): IWorkspaceWithMetadata | undefined {
|
||||||
|
const { tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId } = preferences;
|
||||||
|
// Default to sync (undefined means default to true, or explicitly true)
|
||||||
|
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
||||||
|
|
||||||
|
if (shouldSync) {
|
||||||
|
// Sync with main window - use active workspace
|
||||||
|
return workspacesList.find(workspace => workspace.active);
|
||||||
|
} else if (tidgiMiniWindowFixedWorkspaceId) {
|
||||||
|
// Use fixed workspace
|
||||||
|
return workspacesList.find(ws => ws.id === tidgiMiniWindowFixedWorkspaceId);
|
||||||
|
}
|
||||||
|
// No fixed workspace set - return undefined
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
export function useInitialPage() {
|
export function useInitialPage() {
|
||||||
const [location, setLocation] = useLocation();
|
const [location, setLocation] = useLocation();
|
||||||
const workspacesList = useWorkspacesListObservable();
|
const workspacesList = useWorkspacesListObservable();
|
||||||
|
|
@ -21,17 +45,7 @@ export function useInitialPage() {
|
||||||
|
|
||||||
// For tidgi mini window, determine which workspace to show based on preferences
|
// For tidgi mini window, determine which workspace to show based on preferences
|
||||||
if (windowName === WindowNames.tidgiMiniWindow && preferences) {
|
if (windowName === WindowNames.tidgiMiniWindow && preferences) {
|
||||||
const { tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId } = preferences;
|
targetWorkspace = getTidgiMiniWindowTargetWorkspace(workspacesList, preferences) || targetWorkspace;
|
||||||
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
|
||||||
|
|
||||||
if (!shouldSync && tidgiMiniWindowFixedWorkspaceId) {
|
|
||||||
// If not syncing with main window, use fixed workspace
|
|
||||||
const fixedWorkspace = workspacesList.find(ws => ws.id === tidgiMiniWindowFixedWorkspaceId);
|
|
||||||
if (fixedWorkspace) {
|
|
||||||
targetWorkspace = fixedWorkspace;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Otherwise, use the active workspace (sync with main window)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!targetWorkspace) {
|
if (!targetWorkspace) {
|
||||||
|
|
@ -57,17 +71,8 @@ export function useInitialPage() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId } = preferences;
|
// Determine target workspace using helper function
|
||||||
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
const targetWorkspace = getTidgiMiniWindowTargetWorkspace(workspacesList, preferences);
|
||||||
|
|
||||||
// Determine target workspace
|
|
||||||
let targetWorkspace = workspacesList.find(workspace => workspace.active);
|
|
||||||
if (!shouldSync && tidgiMiniWindowFixedWorkspaceId) {
|
|
||||||
const fixedWorkspace = workspacesList.find(ws => ws.id === tidgiMiniWindowFixedWorkspaceId);
|
|
||||||
if (fixedWorkspace) {
|
|
||||||
targetWorkspace = fixedWorkspace;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!targetWorkspace) return;
|
if (!targetWorkspace) return;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import { logger } from '@services/libs/log';
|
||||||
import type { INativeService } from '@services/native/interface';
|
import type { INativeService } from '@services/native/interface';
|
||||||
import { type IBrowserViewMetaData, WindowNames } from '@services/windows/WindowProperties';
|
import { type IBrowserViewMetaData, WindowNames } from '@services/windows/WindowProperties';
|
||||||
import { isWikiWorkspace, type IWorkspace } from '@services/workspaces/interface';
|
import { isWikiWorkspace, type IWorkspace } from '@services/workspaces/interface';
|
||||||
|
import { getTidgiMiniWindowTargetWorkspace } from '@services/workspacesView/utilities';
|
||||||
import debounce from 'lodash/debounce';
|
import debounce from 'lodash/debounce';
|
||||||
import { setViewEventName } from './constants';
|
import { setViewEventName } from './constants';
|
||||||
import { ViewLoadUrlError } from './error';
|
import { ViewLoadUrlError } from './error';
|
||||||
|
|
@ -412,22 +413,17 @@ export class View implements IViewService {
|
||||||
public async setActiveViewForAllBrowserViews(workspaceID: string): Promise<void> {
|
public async setActiveViewForAllBrowserViews(workspaceID: string): Promise<void> {
|
||||||
// Set main window workspace
|
// Set main window workspace
|
||||||
const mainWindowTask = this.setActiveView(workspaceID, WindowNames.main);
|
const mainWindowTask = this.setActiveView(workspaceID, WindowNames.main);
|
||||||
const [tidgiMiniWindow, tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId] = await Promise.all([
|
const tidgiMiniWindow = await this.preferenceService.get('tidgiMiniWindow');
|
||||||
this.preferenceService.get('tidgiMiniWindow'),
|
|
||||||
this.preferenceService.get('tidgiMiniWindowSyncWorkspaceWithMainWindow'),
|
|
||||||
this.preferenceService.get('tidgiMiniWindowFixedWorkspaceId'),
|
|
||||||
]);
|
|
||||||
|
|
||||||
// For tidgi mini window, decide which workspace to show based on preferences
|
// For tidgi mini window, decide which workspace to show based on preferences
|
||||||
let tidgiMiniWindowTask = Promise.resolve();
|
let tidgiMiniWindowTask = Promise.resolve();
|
||||||
if (tidgiMiniWindow) {
|
if (tidgiMiniWindow) {
|
||||||
// Default to sync (undefined or true), otherwise use fixed workspace ID (fallback to main if not set)
|
// Default to sync (undefined or true), otherwise use fixed workspace ID (fallback to main if not set)
|
||||||
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
const { targetWorkspaceId } = await getTidgiMiniWindowTargetWorkspace(workspaceID);
|
||||||
const tidgiMiniWindowWorkspaceId = shouldSync ? workspaceID : (tidgiMiniWindowFixedWorkspaceId || workspaceID);
|
const tidgiMiniWindowWorkspaceId = targetWorkspaceId || workspaceID;
|
||||||
|
|
||||||
logger.debug('setActiveViewForAllBrowserViews tidgi mini window decision', {
|
logger.debug('setActiveViewForAllBrowserViews tidgi mini window decision', {
|
||||||
function: 'setActiveViewForAllBrowserViews',
|
function: 'setActiveViewForAllBrowserViews',
|
||||||
shouldSync,
|
|
||||||
tidgiMiniWindowWorkspaceId,
|
tidgiMiniWindowWorkspaceId,
|
||||||
willSetActiveView: true,
|
willSetActiveView: true,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ import { container } from '@services/container';
|
||||||
import getViewBounds from '@services/libs/getViewBounds';
|
import getViewBounds from '@services/libs/getViewBounds';
|
||||||
import { logger } from '@services/libs/log';
|
import { logger } from '@services/libs/log';
|
||||||
import type { IThemeService } from '@services/theme/interface';
|
import type { IThemeService } from '@services/theme/interface';
|
||||||
|
import { getTidgiMiniWindowTargetWorkspace } from '@services/workspacesView/utilities';
|
||||||
import { handleAttachToTidgiMiniWindow } from './handleAttachToTidgiMiniWindow';
|
import { handleAttachToTidgiMiniWindow } from './handleAttachToTidgiMiniWindow';
|
||||||
import { handleCreateBasicWindow } from './handleCreateBasicWindow';
|
import { handleCreateBasicWindow } from './handleCreateBasicWindow';
|
||||||
import type { IWindowOpenConfig, IWindowService } from './interface';
|
import type { IWindowOpenConfig, IWindowService } from './interface';
|
||||||
|
|
@ -371,21 +372,12 @@ export class Window implements IWindowService {
|
||||||
logger.debug('TidGi mini window is already enabled, bring it to front', { function: 'openTidgiMiniWindow' });
|
logger.debug('TidGi mini window is already enabled, bring it to front', { function: 'openTidgiMiniWindow' });
|
||||||
if (showWindow) {
|
if (showWindow) {
|
||||||
// Before showing, get the target workspace
|
// Before showing, get the target workspace
|
||||||
const [tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId] = await Promise.all([
|
const { shouldSync, targetWorkspaceId } = await getTidgiMiniWindowTargetWorkspace();
|
||||||
this.preferenceService.get('tidgiMiniWindowSyncWorkspaceWithMainWindow'),
|
|
||||||
this.preferenceService.get('tidgiMiniWindowFixedWorkspaceId'),
|
|
||||||
]);
|
|
||||||
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
|
||||||
const targetWorkspaceId = shouldSync
|
|
||||||
? (await container.get<IWorkspaceService>(serviceIdentifier.Workspace).getActiveWorkspace())?.id
|
|
||||||
: tidgiMiniWindowFixedWorkspaceId;
|
|
||||||
|
|
||||||
logger.info('openTidgiMiniWindow: preparing to show window', {
|
logger.info('openTidgiMiniWindow: preparing to show window', {
|
||||||
function: 'openTidgiMiniWindow',
|
function: 'openTidgiMiniWindow',
|
||||||
shouldSync,
|
shouldSync,
|
||||||
targetWorkspaceId,
|
targetWorkspaceId,
|
||||||
tidgiMiniWindowSyncWorkspaceWithMainWindow,
|
|
||||||
tidgiMiniWindowFixedWorkspaceId,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Ensure view exists for the target workspace before realigning
|
// Ensure view exists for the target workspace before realigning
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ import { DELAY_MENU_REGISTER } from '@/constants/parameters';
|
||||||
import type { ISyncService } from '@services/sync/interface';
|
import type { ISyncService } from '@services/sync/interface';
|
||||||
import type { IInitializeWorkspaceOptions, IWorkspaceViewService } from './interface';
|
import type { IInitializeWorkspaceOptions, IWorkspaceViewService } from './interface';
|
||||||
import { registerMenu } from './registerMenu';
|
import { registerMenu } from './registerMenu';
|
||||||
|
import { getTidgiMiniWindowTargetWorkspace } from './utilities';
|
||||||
|
|
||||||
@injectable()
|
@injectable()
|
||||||
export class WorkspaceView implements IWorkspaceViewService {
|
export class WorkspaceView implements IWorkspaceViewService {
|
||||||
|
|
@ -207,22 +208,18 @@ export class WorkspaceView implements IWorkspaceViewService {
|
||||||
|
|
||||||
// For tidgi mini window, decide which workspace to show based on preferences
|
// For tidgi mini window, decide which workspace to show based on preferences
|
||||||
const tidgiMiniWindowTask = (async () => {
|
const tidgiMiniWindowTask = (async () => {
|
||||||
const [tidgiMiniWindow, tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId] = await Promise.all([
|
const tidgiMiniWindow = await this.preferenceService.get('tidgiMiniWindow');
|
||||||
this.preferenceService.get('tidgiMiniWindow'),
|
|
||||||
this.preferenceService.get('tidgiMiniWindowSyncWorkspaceWithMainWindow'),
|
|
||||||
this.preferenceService.get('tidgiMiniWindowFixedWorkspaceId'),
|
|
||||||
]);
|
|
||||||
if (!tidgiMiniWindow) {
|
if (!tidgiMiniWindow) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// If syncing with main window (undefined means default to true, or explicitly true), use the current workspace
|
const { shouldSync, targetWorkspaceId } = await getTidgiMiniWindowTargetWorkspace(workspace.id);
|
||||||
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
// If syncing with main window, use the current workspace
|
||||||
if (shouldSync) {
|
if (shouldSync) {
|
||||||
await container.get<IViewService>(serviceIdentifier.View).addView(workspace, WindowNames.tidgiMiniWindow);
|
await container.get<IViewService>(serviceIdentifier.View).addView(workspace, WindowNames.tidgiMiniWindow);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// If not syncing and a fixed workspace is set, only add view if this IS the fixed workspace
|
// If not syncing and a fixed workspace is set, only add view if this IS the fixed workspace
|
||||||
if (tidgiMiniWindowFixedWorkspaceId && workspace.id === tidgiMiniWindowFixedWorkspaceId) {
|
if (targetWorkspaceId && workspace.id === targetWorkspaceId) {
|
||||||
await container.get<IViewService>(serviceIdentifier.View).addView(workspace, WindowNames.tidgiMiniWindow);
|
await container.get<IViewService>(serviceIdentifier.View).addView(workspace, WindowNames.tidgiMiniWindow);
|
||||||
}
|
}
|
||||||
// If not syncing and no fixed workspace is set, don't add any view (user needs to select one)
|
// If not syncing and no fixed workspace is set, don't add any view (user needs to select one)
|
||||||
|
|
@ -592,13 +589,8 @@ export class WorkspaceView implements IWorkspaceViewService {
|
||||||
logger.info(`realignActiveWorkspaceView: no tidgiMiniWindowBrowserViewWebContent, skip tidgi mini window for ${workspaceToRealign.id}.`);
|
logger.info(`realignActiveWorkspaceView: no tidgiMiniWindowBrowserViewWebContent, skip tidgi mini window for ${workspaceToRealign.id}.`);
|
||||||
} else {
|
} else {
|
||||||
// For tidgi mini window, decide which workspace to show based on preferences
|
// For tidgi mini window, decide which workspace to show based on preferences
|
||||||
const [tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId] = await Promise.all([
|
const { targetWorkspaceId } = await getTidgiMiniWindowTargetWorkspace(workspaceToRealign.id);
|
||||||
this.preferenceService.get('tidgiMiniWindowSyncWorkspaceWithMainWindow'),
|
const tidgiMiniWindowWorkspaceId = targetWorkspaceId || workspaceToRealign.id;
|
||||||
this.preferenceService.get('tidgiMiniWindowFixedWorkspaceId'),
|
|
||||||
]);
|
|
||||||
// Default to sync (undefined or true), otherwise use fixed workspace ID (fallback to main if not set)
|
|
||||||
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
|
||||||
const tidgiMiniWindowWorkspaceId = shouldSync ? workspaceToRealign.id : (tidgiMiniWindowFixedWorkspaceId || workspaceToRealign.id);
|
|
||||||
tasks.push(container.get<IViewService>(serviceIdentifier.View).realignActiveView(tidgiMiniWindow, tidgiMiniWindowWorkspaceId, WindowNames.tidgiMiniWindow));
|
tasks.push(container.get<IViewService>(serviceIdentifier.View).realignActiveView(tidgiMiniWindow, tidgiMiniWindowWorkspaceId, WindowNames.tidgiMiniWindow));
|
||||||
}
|
}
|
||||||
await Promise.all(tasks);
|
await Promise.all(tasks);
|
||||||
|
|
@ -618,20 +610,15 @@ export class WorkspaceView implements IWorkspaceViewService {
|
||||||
logger.debug(`hideWorkspaceView: no tidgiMiniWindowBrowserWindow, skip tidgi mini window browserView.`);
|
logger.debug(`hideWorkspaceView: no tidgiMiniWindowBrowserWindow, skip tidgi mini window browserView.`);
|
||||||
} else {
|
} else {
|
||||||
// For tidgi mini window, only hide if syncing with main window OR if this is the fixed workspace being deactivated
|
// For tidgi mini window, only hide if syncing with main window OR if this is the fixed workspace being deactivated
|
||||||
const [tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId] = await Promise.all([
|
const { shouldSync, targetWorkspaceId } = await getTidgiMiniWindowTargetWorkspace(idToDeactivate);
|
||||||
this.preferenceService.get('tidgiMiniWindowSyncWorkspaceWithMainWindow'),
|
|
||||||
this.preferenceService.get('tidgiMiniWindowFixedWorkspaceId'),
|
|
||||||
]);
|
|
||||||
// Default to sync (undefined or true)
|
|
||||||
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
|
||||||
// Only hide tidgi mini window view if:
|
// Only hide tidgi mini window view if:
|
||||||
// 1. Syncing with main window (should hide when main window hides)
|
// 1. Syncing with main window (should hide when main window hides)
|
||||||
// 2. OR the workspace being hidden is the fixed workspace (rare case, but should be handled)
|
// 2. OR the workspace being hidden is the fixed workspace (rare case, but should be handled)
|
||||||
if (shouldSync || idToDeactivate === tidgiMiniWindowFixedWorkspaceId) {
|
if (shouldSync || idToDeactivate === targetWorkspaceId) {
|
||||||
logger.info(`hideWorkspaceView: hide tidgi mini window browserView.`);
|
logger.info(`hideWorkspaceView: hide tidgi mini window browserView.`);
|
||||||
tasks.push(container.get<IViewService>(serviceIdentifier.View).hideView(tidgiMiniWindow, WindowNames.tidgiMiniWindow, idToDeactivate));
|
tasks.push(container.get<IViewService>(serviceIdentifier.View).hideView(tidgiMiniWindow, WindowNames.tidgiMiniWindow, idToDeactivate));
|
||||||
} else {
|
} else {
|
||||||
logger.debug(`hideWorkspaceView: skip hiding tidgi mini window browserView (fixed workspace: ${tidgiMiniWindowFixedWorkspaceId || 'none'}).`);
|
logger.debug(`hideWorkspaceView: skip hiding tidgi mini window browserView (fixed workspace: ${targetWorkspaceId || 'none'}).`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await Promise.all(tasks);
|
await Promise.all(tasks);
|
||||||
|
|
|
||||||
34
src/services/workspacesView/utilities.ts
Normal file
34
src/services/workspacesView/utilities.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
import { container } from '@services/container';
|
||||||
|
import type { IPreferenceService } from '@services/preferences/interface';
|
||||||
|
import serviceIdentifier from '@services/serviceIdentifier';
|
||||||
|
import type { IWorkspaceService } from '@services/workspaces/interface';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to determine the target workspace for tidgi mini window based on preferences
|
||||||
|
* @param fallbackWorkspaceId - The workspace ID to use as fallback (usually the active/current workspace)
|
||||||
|
* @returns Object containing shouldSync flag and targetWorkspaceId
|
||||||
|
*/
|
||||||
|
export async function getTidgiMiniWindowTargetWorkspace(fallbackWorkspaceId?: string): Promise<{
|
||||||
|
shouldSync: boolean;
|
||||||
|
targetWorkspaceId: string | undefined;
|
||||||
|
}> {
|
||||||
|
const preferenceService = container.get<IPreferenceService>(serviceIdentifier.Preference);
|
||||||
|
const [tidgiMiniWindowSyncWorkspaceWithMainWindow, tidgiMiniWindowFixedWorkspaceId] = await Promise.all([
|
||||||
|
preferenceService.get('tidgiMiniWindowSyncWorkspaceWithMainWindow'),
|
||||||
|
preferenceService.get('tidgiMiniWindowFixedWorkspaceId'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Default to sync (undefined means default to true, or explicitly true)
|
||||||
|
const shouldSync = tidgiMiniWindowSyncWorkspaceWithMainWindow === undefined || tidgiMiniWindowSyncWorkspaceWithMainWindow;
|
||||||
|
|
||||||
|
let targetWorkspaceId: string | undefined;
|
||||||
|
if (shouldSync) {
|
||||||
|
// Sync with main window - use fallback or active workspace
|
||||||
|
targetWorkspaceId = fallbackWorkspaceId ?? (await container.get<IWorkspaceService>(serviceIdentifier.Workspace).getActiveWorkspace())?.id;
|
||||||
|
} else {
|
||||||
|
// Use fixed workspace
|
||||||
|
targetWorkspaceId = tidgiMiniWindowFixedWorkspaceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { shouldSync, targetWorkspaceId };
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue