Skip to content

Commit

Permalink
Get 'file loading' in (framework for message sending main<->renderer)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcgrue committed Dec 14, 2023
1 parent 46d592b commit ba3e712
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 36 deletions.
32 changes: 31 additions & 1 deletion src/app/ReactDockableApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,20 @@ const ModalClickShield = () => {
);
};

let appDispatch: any = undefined;

function doAppDispatch(data:any) {

if(appDispatch === undefined) {
console.error('appDispatch was not set');
} else {
appDispatch(data);
}
}

function App() {
const [state, dispatch] = useReducer(breaditorAppReducer, getInitialState());
appDispatch = dispatch;

if (dispatch == undefined) {
console.info(state);
Expand Down Expand Up @@ -311,7 +323,7 @@ function App() {
}

function init() {
const domNode = document.getElementById('react-root') as Element;
const domNode = document.getElementById('react-root') as HTMLElement;

const root = createRoot(domNode);

Expand All @@ -320,6 +332,24 @@ function init() {
addDocument(spriteMaker('Sprite C'));
addDocument(mapMaker('Map D'));

/// MAIN<->RENDERER handlers
domNode.addEventListener('OPEN_FILE_SUCCESS', (event:any) => {
debugger;
doAppDispatch({
type: 'DOC_OPEN_FILE_FOUND',
thunkDispatch: doAppDispatch,
filePath: event.detail.filePath
});
});

domNode.addEventListener('OPEN_FILE_FAILURE', (event:any) => {
doAppDispatch({
type: 'DOC_OPEN_FILE_FAILURE',
thunkDispatch: doAppDispatch,
error: 'failed to load' + event.detail.filePath
});
});

root.render(<App />);
}

Expand Down
30 changes: 5 additions & 25 deletions src/app/state-management/in-memory/reducers/filemenu.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ const initialFileMenuState: FileMenuState = {};
const fileMenuReducer: Reducer<FileMenuState, FileMenuAction> = createReducer(
initialFileMenuState,
{
DOC_OPEN_FILE_FOUND: (state, action: OpenFileCancelledAction) => {
DOC_OPEN_FILE_FOUND: (state, action: OpenFileFoundAction) => {
action.thunkDispatch({type: 'SET_IS_LOADING', newState: false});
alert("now load: " + action.filePath);
return {...state};
},
DOC_OPEN_FILE_FAILURE: (state, action: OpenFileFoundAction) => {
DOC_OPEN_FILE_FAILURE: (state, action: OpenFileCancelledAction) => {
action.thunkDispatch({type: 'SET_IS_LOADING', newState: false});
alert("failed to load load: " + action.error);
return {...state};
},
DOC_OPEN_EXISTING: (state, action: OpenDocumentAction) => {
Expand All @@ -51,31 +53,9 @@ const fileMenuReducer: Reducer<FileMenuState, FileMenuAction> = createReducer(
};

console.log('INVOKING window.electronAPI.openFileDialog');

// @ts-ignore
window.electronAPI.openFileDialog(options);
console.log('bueller?');

/*
dialog
.showOpenDialog(options)
.then((result: any) => {
if (!result.canceled && result.filePaths.length > 0) {
const filePath = result.filePaths[0];
dispatch({
type: 'OPEN_FILE_FOUND',
payload: filePath,
thunkDispatch: dispatch,
});
}
})
.catch((error: any) => {
dispatch({
type: 'OPEN_FILE_FAILURE',
payload: error,
thunkDispatch: dispatch,
});
});
*/
};

thunk(action.thunkDispatch);
Expand Down
13 changes: 6 additions & 7 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,21 @@ function createWindow() {
mainWindow.close();
});

ipcMain.on('open-file-dialog', (_options: any) => {
const options: OpenDialogOptions = _options;
ipcMain.on('open-file-dialog', (event:Electron.IpcMainEvent, arg:any[]) => {
const options: OpenDialogOptions = arg[0];
const senderWebContents = event.sender;

console.log('MAIN open-file-dialog');
dialog
.showOpenDialog(options)
.then((result) => {
if (!result.canceled && result.filePaths.length > 0) {
const filePath = result.filePaths[0];
console.log('filePath', filePath);
//dispatch({type: 'OPEN_FILE_SUCCESS', payload: filePath});

senderWebContents.send('OPEN_FILE_SUCCESS', {type: 'OPEN_FILE_SUCCESS', payload: filePath});
}
})
.catch((error) => {
console.log('error', error);
//dispatch({type: 'OPEN_FILE_FAILURE', payload: error});
senderWebContents.send('OPEN_FILE_FAILURE', {type: 'OPEN_FILE_FAILURE', payload: error});
});
});

Expand Down
19 changes: 16 additions & 3 deletions src/main/preload/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
const {
contextBridge,
ipcRenderer,
OpenDialogOptions /*, ipcMain*/,
OpenDialogOptions
} = require('electron');

console.log('preload.ts!');

contextBridge.exposeInMainWorld('electronAPI', {
appClose: () => {
ipcRenderer.send('app-close');
Expand All @@ -21,3 +19,18 @@ contextBridge.exposeInMainWorld('electronAPI', {
ipcRenderer.send('open-file-dialog', options);
},
});


ipcRenderer.on('OPEN_FILE_SUCCESS', (e, msg) => {
document.getElementById('react-root').dispatchEvent(
new CustomEvent('OPEN_FILE_SUCCESS', {
detail: { filePath: msg.payload }
}));
});

ipcRenderer.on('OPEN_FILE_FAILURE', (e, msg) => {
document.getElementById('react-root').dispatchEvent(
new CustomEvent('OPEN_FILE_FAILURE', {
detail: { filePath: msg.payload }
}));
});

0 comments on commit ba3e712

Please sign in to comment.