mirror of
https://github.com/jackyzha0/quartz.git
synced 2026-03-21 21:45:42 -05:00
2.5 KiB
2.5 KiB
| tags | session | duration | status | |||||
|---|---|---|---|---|---|---|---|---|
|
3 | 2 hours | ready |
Session 3 — Windows, Menus & Native Features
🎯 Objectives
- Configure BrowserWindow with different options
- Build a native application menu
- Open and save files using system dialogs
- Read and write files with Node.js
fs
🧠 Concepts Covered
BrowserWindow Options
const win = new BrowserWindow({
width: 1000,
height: 700,
minWidth: 400,
title: "My App",
backgroundColor: '#1e1e1e',
frame: true, // set false for frameless window
resizable: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
App Lifecycle Events
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
Native Menus
const { Menu, MenuItem } = require('electron')
const menu = Menu.buildFromTemplate([
{
label: 'File',
submenu: [
{ label: 'Open', accelerator: 'CmdOrCtrl+O', click: openFile },
{ label: 'Save', accelerator: 'CmdOrCtrl+S', click: saveFile },
{ type: 'separator' },
{ role: 'quit' }
]
},
{ role: 'editMenu' },
{ role: 'viewMenu' }
])
Menu.setApplicationMenu(menu)
File Dialogs & fs
const { dialog } = require('electron')
const fs = require('fs')
async function openFile() {
const result = await dialog.showOpenDialog({
filters: [{ name: 'Text Files', extensions: ['txt', 'md'] }]
})
if (!result.canceled) {
const content = fs.readFileSync(result.filePaths[0], 'utf-8')
// send content to renderer via IPC (next session!)
}
}
🛠 Hands-on Exercise — Markdown Notepad (60 min)
Build a simple text editor:
Features to implement:
- A
<textarea>that fills the window - File → Open loads a
.txtfile into the textarea - File → Save writes the textarea content back to disk
- Title bar shows the filename
Starter structure:
notepad/
├── main.js
├── preload.js
├── index.html
└── package.json
⚠️ Note: Full IPC wiring comes in Session 4 — for now, students can use
remotemodule or handle everything in main.js as a simplified demo
📦 Resources
- BrowserWindow Docs
- dialog Docs
- Session 4 - IPC Communication ← next (this is where it all clicks)