-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from pioneers/develop/ui
Develop/UI
- Loading branch information
Showing
27 changed files
with
1,354 additions
and
65 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export default class AppConsoleMessage { | ||
#type: string; | ||
|
||
#text: string; | ||
|
||
constructor(type: string, text: string) { | ||
this.#type = type; | ||
this.#text = text; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import { dialog, ipcMain } from 'electron'; | ||
import type { BrowserWindow } from 'electron'; | ||
import fs from 'fs'; | ||
import { version as dawnVersion } from '../../package.json'; | ||
import AppConsoleMessage from '../common/AppConsoleMessage'; | ||
|
||
const WATCH_DEBOUNCE_MS = 3000; | ||
const CODE_FILE_FILTERS = [ | ||
{ name: 'Python Files', extensions: ['py'] }, | ||
{ name: 'All Files', extensions: ['*'] }, | ||
]; | ||
|
||
export default class MainApp { | ||
readonly #mainWindow: BrowserWindow; | ||
|
||
#savePath: string | null; | ||
|
||
#watcher: fs.FSWatcher | null; | ||
|
||
#watchDebounce: boolean; | ||
|
||
#preventQuit: boolean; | ||
|
||
constructor(mainWindow: BrowserWindow) { | ||
this.#mainWindow = mainWindow; | ||
this.#savePath = null; | ||
this.#watcher = null; | ||
this.#watchDebounce = true; | ||
this.#preventQuit = true; | ||
mainWindow.on('close', (e) => { | ||
if (this.#preventQuit) { | ||
e.preventDefault(); | ||
this.#sendToRenderer('renderer-quit-request'); | ||
} | ||
}); | ||
ipcMain.on('main-file-control', (_event, data) => { | ||
if (data.type === 'save') { | ||
this.#saveCodeFile(data.content as string, data.forceDialog as boolean); | ||
} else if (data.type === 'load') { | ||
this.#openCodeFile(); | ||
} else { | ||
// eslint-disable-next-line no-console | ||
console.error(`Unknown data.type for main-file-control ${data.type}`); | ||
} | ||
}); | ||
ipcMain.on('main-quit', () => { | ||
this.#preventQuit = false; | ||
this.#mainWindow.close(); | ||
}); | ||
} | ||
|
||
#sendToRenderer(...args) { | ||
this.#mainWindow.webContents.send(...args); | ||
} | ||
|
||
onPresent() { | ||
this.#watcher?.close(); | ||
this.#savePath = null; | ||
this.#sendToRenderer('renderer-init', { dawnVersion }); | ||
} | ||
|
||
promptSaveCodeFile(forceDialog: boolean) { | ||
this.#sendToRenderer('renderer-file-control', { | ||
type: 'promptSave', | ||
forceDialog, | ||
}); | ||
} | ||
|
||
promptLoadCodeFile() { | ||
this.#sendToRenderer('renderer-file-control', { type: 'promptLoad' }); | ||
} | ||
|
||
#saveCodeFile(code: string, forceDialog: boolean) { | ||
let success = true; | ||
if (this.#savePath === null || forceDialog) { | ||
success = this.#showCodePathDialog('save'); | ||
} | ||
if (success) { | ||
// Temporarily disable watcher | ||
this.#watcher?.close(); | ||
this.#watcher = null; | ||
fs.writeFile( | ||
this.#savePath, | ||
code, | ||
{ encoding: 'utf8', flag: 'w' }, | ||
(err) => { | ||
if (err) { | ||
this.#sendToRenderer( | ||
'renderer-post-console', | ||
new AppConsoleMessage( | ||
'dawn-err', | ||
`Failed to save code to ${this.#savePath}. ${err}`, | ||
), | ||
); | ||
} else { | ||
this.#sendToRenderer('renderer-file-control', { type: 'didSave' }); | ||
} | ||
this.#watchCodeFile(); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
#openCodeFile() { | ||
const success = this.#showCodePathDialog('load'); | ||
if (success) { | ||
this.#sendToRenderer('renderer-file-control', { | ||
type: 'didOpen', | ||
content: fs.readFileSync(this.#savePath, { | ||
encoding: 'utf8', | ||
flag: 'r', | ||
}), | ||
}); | ||
} | ||
} | ||
|
||
#showCodePathDialog(mode: 'save' | 'load') { | ||
let result: string | string[] | undefined; | ||
if (mode === 'save') { | ||
result = dialog.showSaveDialogSync(this.#mainWindow, { | ||
filters: CODE_FILE_FILTERS, | ||
...(this.#savePath === null ? {} : { defaultPath: this.#savePath }), | ||
}); | ||
} else { | ||
result = dialog.showOpenDialogSync(this.#mainWindow, { | ||
filters: CODE_FILE_FILTERS, | ||
properties: ['openFile'], | ||
}); | ||
} | ||
if (result && result.length) { | ||
this.#savePath = typeof result === 'string' ? result : result[0]; | ||
this.#sendToRenderer('renderer-file-control', { | ||
type: 'didChangePath', | ||
path: this.#savePath, | ||
}); | ||
if (mode === 'load') { | ||
this.#watchCodeFile(); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
#watchCodeFile() { | ||
this.#watcher?.close(); | ||
this.#watchDebounce = true; | ||
this.#watcher = fs.watch( | ||
this.#savePath, | ||
{ persistent: false, encoding: 'utf8' }, | ||
() => { | ||
// Don't care what the event type is | ||
if (this.#watchDebounce) { | ||
this.#watchDebounce = false; | ||
setTimeout(() => { | ||
this.#watchDebounce = true; | ||
}, WATCH_DEBOUNCE_MS); | ||
this.#sendToRenderer('renderer-file-control', { | ||
type: 'didExternalChange', | ||
}); | ||
} | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,27 @@ | ||
/* | ||
* @NOTE: Prepend a `~` to css file paths that are in your node_modules | ||
* See https://github.com/webpack-contrib/sass-loader#imports | ||
*/ | ||
body { | ||
margin: 0; | ||
} | ||
|
||
.App { | ||
display: flex; | ||
flex-direction: column; | ||
height: 100vh; | ||
} | ||
.App-cols { | ||
display: flex; | ||
flex-direction: row; | ||
flex-shrink: 0; | ||
} | ||
.App-modal-container { | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
background-color: rgb(0 0 0 / 50%); | ||
display: none; | ||
z-index: 4; | ||
} | ||
.App-modal-container:has(.modal-active) { | ||
display: block; | ||
} |
Oops, something went wrong.