Skip to content

Commit feb2760

Browse files
committed
improve window handling
1 parent 2ad213c commit feb2760

File tree

6 files changed

+96
-24
lines changed

6 files changed

+96
-24
lines changed

src/components/editor.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -274,18 +274,21 @@ class AppEditor extends Tonic {
274274

275275
if (!this.projectNode) return
276276
const value = this.editor.getValue()
277-
const terminal = document.querySelector('app-terminal')
277+
const coTerminal = document.querySelector('app-terminal')
278+
const coProperties = document.querySelector('app-properties')
278279

279280
this.writeDebounce = setTimeout(() => {
280281
if (this.projectNode.label === 'settings.json' && this.projectNode.parent.id === 'root') {
281282

282283
try {
283284
this.props.parent.state.settings = JSON.parse(value)
284285
} catch (err) {
285-
terminal.error(`Unable to parse settings file (${err.message})`)
286+
coTerminal.error(`Unable to parse settings file (${err.message})`)
286287
return
287288
}
288-
terminal.info(`Settings file updated.`)
289+
coTerminal.info(`Settings file updated.`)
290+
coProperties.reRender()
291+
parent.activatePreviewWindows()
289292
}
290293

291294
this.writeToDisk(this.projectNode, value)

src/components/project.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,8 @@ class AppProject extends Tonic {
584584
child.icon = entry.isDirectory() ? 'folder' : 'file'
585585

586586
if (parent.id === 'root' && entry.isDirectory()) {
587-
if (!this.props.parent.currentProject) {
588-
this.props.parent.currentProject = child
587+
if (!this.props.parent.state.currentProject) {
588+
this.props.parent.state.currentProject = child
589589
}
590590

591591
child.icon = 'package'

src/components/properties.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ class AppProperties extends Tonic {
4747
if (previewWindow) {
4848
previewWindow.active = !previewWindow.active
4949

50+
const currentProject = app.state.currentProject
51+
52+
// if the user currently has the config file open in the editor...
53+
if (currentProject.label === 'settings.json' && currentProject.parent.id === 'root') {
54+
try {
55+
editor.value = JSON.stringify(app.state.settings, null, 2)
56+
} catch (err) {
57+
return notifications.create({
58+
type: 'error',
59+
title: 'Unable to save config file',
60+
message: err.message
61+
})
62+
}
63+
}
64+
5065
try {
5166
const str = JSON.stringify(app.state.settings)
5267
await fs.promises.writeFile(pathToSettingsFile, str)
@@ -131,7 +146,7 @@ class AppProperties extends Tonic {
131146
}
132147

133148
return this.html`
134-
<tonic-accordion id="options">
149+
<tonic-accordion id="options" selected="preview-windows">
135150
<tonic-accordion-section
136151
name="preview-windows"
137152
id="preview-windows"

src/index.js

+65-14
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import fs from 'socket:fs'
22
import path from 'socket:path'
33
import process from 'socket:process'
44
import application from 'socket:application'
5+
import { network, Encryption, sha256 } from 'socket:network'
56
import vm from 'socket:vm'
67
import { inspect, format } from 'socket:util'
78
import { spawn, exec } from 'socket:child_process'
89

910
import Tonic from '@socketsupply/tonic'
1011
import components from '@socketsupply/components'
1112

13+
import Database from './db/index.js'
14+
1215
import { AppTerminal } from './components/terminal.js'
1316
import { AppProject } from './components/project.js'
1417
import { AppProperties } from './components/properties.js'
@@ -30,7 +33,7 @@ class AppView extends Tonic {
3033
this.reloadPreviewWindows()
3134
})
3235

33-
window.addEventListener('close', async e => {
36+
window.addEventListener('window-closed', async e => {
3437
const data = e.detail.data
3538

3639
const previewWindowSettings = this.state.settings.previewWindows[data - 1]
@@ -142,11 +145,6 @@ class AppView extends Tonic {
142145
return
143146
}
144147

145-
for (const [k, v] of Object.entries(this.previewWindows)) {
146-
delete this.state.zoom[k]
147-
await v.close() // destroy any existing preview windows
148-
}
149-
150148
if (!this.state.userScript) {
151149
const res = await fetch('./preview.js')
152150
this.state.userScript = await res.text()
@@ -156,19 +154,27 @@ class AppView extends Tonic {
156154
const screenSize = await application.getScreenSize()
157155

158156
for (let i = 0; i < this.state.settings.previewWindows.length; i++) {
157+
const index = i + 1
159158
const preview = this.state.settings.previewWindows[i]
160-
if (!preview.active) continue
159+
160+
if (!preview.active) {
161+
const w = this.previewWindows[index]
162+
if (w) {
163+
delete this.state.zoom[index]
164+
await w.close()
165+
}
166+
continue
167+
}
161168

162169
let width = screenSize.width * 0.6
163170
let height = screenSize.height * 0.6
164-
const index = i + 1
165171
const scale = preview.scale || 1
166172
const platform = preview.platform || process.platform
167173

168174
if (/\d+x\d+/.test(preview.resolution)) {
169175
const size = preview.resolution.split('x')
170-
width = preview.resolution = size[0]
171-
height = preview.resolution = size[1]
176+
width = size[0]
177+
height = size[1]
172178
}
173179

174180
let hostOS = process.platform
@@ -243,13 +249,55 @@ class AppView extends Tonic {
243249
})
244250

245251
this.previewWindows[w.index] = w
246-
} catch {}
252+
} catch (err) {
253+
console.log(err)
254+
}
247255
}
248256
}
249257

250-
async init () {
251-
await navigator.serviceWorker.ready
258+
async initData () {
259+
if (process.env.DEBUG === '1') {
260+
const databases = await window.indexedDB.databases()
261+
for (const { name } of databases) await Database.drop(name)
262+
}
263+
264+
this.db = {
265+
channels: await Database.open('shares'),
266+
state: await Database.open('state')
267+
}
268+
269+
Database.onerror = err => {
270+
console.error(err)
252271

272+
const notifications = document.querySelector('#notifications')
273+
notifications?.create({
274+
type: 'error',
275+
title: 'Unhandled Database Error',
276+
message: err.message
277+
})
278+
}
279+
280+
//
281+
// Create a default clusterId (used as the default group)
282+
//
283+
const clusterId = await Encryption.createClusterId('socket-app-studio')
284+
285+
const { data: dataPeer } = await this.db.state.has('peer')
286+
287+
if (!dataPeer) {
288+
const signingKeys = await Encryption.createKeyPair()
289+
await this.db.state.put('peer', {
290+
config: {
291+
peerId: await Encryption.createId(),
292+
clusterId
293+
}
294+
})
295+
}
296+
297+
298+
}
299+
300+
async initApplication () {
253301
const notifications = document.querySelector('#notifications')
254302
const settingsFile = path.join(path.DATA, 'projects', 'settings.json')
255303

@@ -523,7 +571,10 @@ class AppView extends Tonic {
523571
}
524572

525573
async render () {
526-
await this.init()
574+
await navigator.serviceWorker.ready
575+
576+
await this.initData()
577+
await this.initApplication()
527578

528579
return this.html`
529580
<header>

src/template/demo-project/socket.ini

+2
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ codesign_paths = ""
255255
; default value: "13.0.0"
256256
; minimum_supported_version = "13.0.0"
257257

258+
trafficLightPosition = "10x24"
258259

259260
[native]
260261

@@ -320,6 +321,7 @@ width = 50%
320321
; default value: false
321322
; utility = false
322323

324+
; titleBarStyle = "hiddenInset"
323325

324326
[window.alert]
325327
; The title that appears in the 'alert', 'prompt', and 'confirm' dialogs. If this value is not present, then the application title is used instead. Currently only supported on iOS/macOS.

src/worker.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export default async function (req, env, ctx) {
3232
}
3333

3434
let css = `
35-
#SOCKET_NOTCH {
35+
body::after {
36+
content: ' ';
3637
position: fixed;
3738
transform: translateX(-50%);
3839
background: ${bgColor};
@@ -42,7 +43,7 @@ export default async function (req, env, ctx) {
4243

4344
if (params.get('device') === 'iphone-15') {
4445
css += `
45-
#SOCKET_NOTCH {
46+
body::after {
4647
top: 2%;
4748
left: 50%;
4849
width: 35%;
@@ -54,7 +55,7 @@ export default async function (req, env, ctx) {
5455

5556
if (params.get('device') === 'iphone-13') {
5657
css += `
57-
#SOCKET_NOTCH {
58+
body::after {
5859
top: 0;
5960
left: 50%;
6061
width: 45%;
@@ -66,7 +67,7 @@ export default async function (req, env, ctx) {
6667

6768
if (url.pathname.endsWith('index.html')) {
6869
data = data.replace(/<html(?:[^\n\r]*)>/, `<html style="zoom: ${params.get('zoom')}">`)
69-
data = data.replace('</body>', `<style>${css}</style><div id="SOCKET_NOTCH"></div></body>`)
70+
data = data.replace('</head>', `<style>${css}</style></head>`)
7071
}
7172

7273
const types = await lookup(path.extname(url.pathname).slice(1))

0 commit comments

Comments
 (0)