stage-browser 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,171 @@
1
+ 'use strict'
2
+ const { Menu } = require('electron')
3
+ const { execFile } = require('child_process')
4
+ const extensions = require('./extensions')
5
+
6
+ const SIZES = [
7
+ [1280, 720],
8
+ [1440, 900],
9
+ [1600, 900],
10
+ [1920, 1080],
11
+ ]
12
+
13
+ function buildMenu(ctx) {
14
+ const { tabs, reveal } = ctx
15
+ const hidden = (accelerator, click) => ({ label: accelerator, accelerator, click, visible: false })
16
+ const openInChrome = () => {
17
+ const t = tabs.active
18
+ if (t && t.url) execFile('open', ['-a', 'Google Chrome', t.url], () => {})
19
+ }
20
+ const withActive = (fn) => () => {
21
+ const t = tabs.active
22
+ if (t) fn(t.wc)
23
+ }
24
+
25
+ const extItems = extensions.installed(ctx).map((e) => ({
26
+ label: `${e.name} ${e.version}`,
27
+ submenu: [
28
+ {
29
+ label: 'Remove',
30
+ click: async () => {
31
+ await extensions.remove(ctx, e.id).catch((err) => console.error('[stage] remove failed', err.message))
32
+ ctx.rebuildMenu()
33
+ },
34
+ },
35
+ ],
36
+ }))
37
+
38
+ const template = [
39
+ {
40
+ label: 'Stage',
41
+ submenu: [
42
+ { role: 'about', label: 'About Stage' },
43
+ { type: 'separator' },
44
+ { role: 'hide', label: 'Hide Stage' },
45
+ { role: 'hideOthers' },
46
+ { role: 'unhide' },
47
+ { type: 'separator' },
48
+ { role: 'quit', label: 'Quit Stage' },
49
+ ],
50
+ },
51
+ {
52
+ label: 'File',
53
+ submenu: [
54
+ { label: 'New Tab', accelerator: 'CmdOrCtrl+T', click: () => reveal.openOverlay('new', '') },
55
+ {
56
+ label: 'Open Location…',
57
+ accelerator: 'CmdOrCtrl+L',
58
+ click: () => reveal.openOverlay('edit', tabs.active ? tabs.active.url : ''),
59
+ },
60
+ { label: 'Reopen Closed Tab', accelerator: 'CmdOrCtrl+Shift+T', click: () => tabs.reopenClosed() },
61
+ { label: 'Close Tab', accelerator: 'CmdOrCtrl+W', click: () => tabs.active && tabs.close(tabs.active.id) },
62
+ { type: 'separator' },
63
+ { label: 'Open in Google Chrome', accelerator: 'CmdOrCtrl+Shift+O', click: openInChrome },
64
+ ],
65
+ },
66
+ {
67
+ label: 'Edit',
68
+ submenu: [
69
+ { role: 'undo' },
70
+ { role: 'redo' },
71
+ { type: 'separator' },
72
+ { role: 'cut' },
73
+ { role: 'copy' },
74
+ { role: 'paste' },
75
+ { role: 'pasteAndMatchStyle' },
76
+ { role: 'selectAll' },
77
+ ],
78
+ },
79
+ {
80
+ label: 'View',
81
+ submenu: [
82
+ { label: 'Reload', accelerator: 'CmdOrCtrl+R', click: withActive((wc) => wc.reload()) },
83
+ { label: 'Hard Reload', accelerator: 'CmdOrCtrl+Shift+R', click: withActive((wc) => wc.reloadIgnoringCache()) },
84
+ { type: 'separator' },
85
+ {
86
+ label: 'Pin Tab Bar',
87
+ type: 'checkbox',
88
+ checked: ctx.bar.pinned,
89
+ accelerator: 'CmdOrCtrl+Shift+B',
90
+ click: () => reveal.setPinned(!ctx.bar.pinned),
91
+ },
92
+ { type: 'separator' },
93
+ { label: 'Zoom In', accelerator: 'CmdOrCtrl+=', click: () => tabs.zoom(0.1) },
94
+ hidden('CmdOrCtrl+Plus', () => tabs.zoom(0.1)),
95
+ { label: 'Zoom Out', accelerator: 'CmdOrCtrl+-', click: () => tabs.zoom(-0.1) },
96
+ { label: 'Actual Size', accelerator: 'CmdOrCtrl+0', click: () => tabs.zoom(0) },
97
+ { type: 'separator' },
98
+ {
99
+ label: 'Window Size',
100
+ submenu: SIZES.map(([w, h]) => ({
101
+ label: `${w} × ${h}`,
102
+ click: () => {
103
+ if (ctx.win.isFullScreen()) return
104
+ ctx.win.setContentSize(w, h)
105
+ ctx.win.center()
106
+ },
107
+ })),
108
+ },
109
+ { label: 'Enter Full Screen', accelerator: 'Ctrl+Command+F', click: () => ctx.win.setFullScreen(!ctx.win.isFullScreen()) },
110
+ { type: 'separator' },
111
+ {
112
+ label: 'Developer Tools',
113
+ accelerator: 'CmdOrCtrl+Alt+I',
114
+ click: withActive((wc) => (wc.isDevToolsOpened() ? wc.closeDevTools() : wc.openDevTools({ mode: 'detach' }))),
115
+ },
116
+ ],
117
+ },
118
+ {
119
+ label: 'History',
120
+ submenu: [
121
+ { label: 'Back', accelerator: 'CmdOrCtrl+[', click: withActive((wc) => wc.navigationHistory.goBack()) },
122
+ { label: 'Forward', accelerator: 'CmdOrCtrl+]', click: withActive((wc) => wc.navigationHistory.goForward()) },
123
+ ],
124
+ },
125
+ {
126
+ label: 'Tab',
127
+ submenu: [
128
+ { label: 'Next Tab', accelerator: 'Ctrl+Tab', click: () => tabs.step(1) },
129
+ { label: 'Previous Tab', accelerator: 'Ctrl+Shift+Tab', click: () => tabs.step(-1) },
130
+ hidden('CmdOrCtrl+Shift+]', () => tabs.step(1)),
131
+ hidden('CmdOrCtrl+Shift+[', () => tabs.step(-1)),
132
+ hidden('CmdOrCtrl+Alt+Right', () => tabs.step(1)),
133
+ hidden('CmdOrCtrl+Alt+Left', () => tabs.step(-1)),
134
+ { type: 'separator' },
135
+ ...[1, 2, 3, 4, 5, 6, 7, 8, 9].map((n) => ({
136
+ label: n === 9 ? 'Last Tab' : `Tab ${n}`,
137
+ accelerator: `CmdOrCtrl+${n}`,
138
+ click: () => tabs.goTo(n),
139
+ })),
140
+ ],
141
+ },
142
+ {
143
+ label: 'Extensions',
144
+ submenu: [
145
+ { label: 'Open Chrome Web Store', click: () => tabs.create({ url: extensions.WEB_STORE_URL, index: tabs.insertIndexAfterActive() }) },
146
+ {
147
+ label: 'Install Okta Browser Plugin',
148
+ click: async () => {
149
+ try {
150
+ await extensions.install(ctx, extensions.OKTA_ID)
151
+ } catch (e) {
152
+ console.error('[stage] okta install failed', e.message)
153
+ }
154
+ ctx.rebuildMenu()
155
+ },
156
+ },
157
+ { type: 'separator' },
158
+ ...(extItems.length ? extItems : [{ label: 'No extensions installed', enabled: false }]),
159
+ ],
160
+ },
161
+ { role: 'windowMenu' },
162
+ ]
163
+ return Menu.buildFromTemplate(template)
164
+ }
165
+
166
+ function installMenu(ctx) {
167
+ ctx.rebuildMenu = () => Menu.setApplicationMenu(buildMenu(ctx))
168
+ ctx.rebuildMenu()
169
+ }
170
+
171
+ module.exports = { installMenu, buildMenu }
@@ -0,0 +1,141 @@
1
+ 'use strict'
2
+ const { screen } = require('electron')
3
+ const { BAR_HEIGHT } = require('./window')
4
+
5
+ const POLL_MS = 60
6
+ const REVEAL_PX = 6
7
+ const HIDE_BELOW = BAR_HEIGHT + 24
8
+ const HIDE_DELAY = 350
9
+ const VIEW_HIDE_DELAY = 180
10
+
11
+ class Reveal {
12
+ constructor(ctx) {
13
+ this.ctx = ctx
14
+ this.timer = null
15
+ this.viewTimer = null
16
+ this.awaySince = null
17
+ const { win } = ctx
18
+ const sync = () => this.syncPolling()
19
+ for (const ev of ['show', 'hide', 'minimize', 'restore']) win.on(ev, sync)
20
+ }
21
+
22
+ syncPolling() {
23
+ const { win } = this.ctx
24
+ const should = !this.ctx.quitting && !win.isDestroyed() && win.isVisible() && !win.isMinimized()
25
+ if (should && !this.timer) this.timer = setInterval(() => this.tick(), POLL_MS)
26
+ else if (!should && this.timer) {
27
+ clearInterval(this.timer)
28
+ this.timer = null
29
+ }
30
+ }
31
+
32
+ keepOpen() {
33
+ const { ctx } = this
34
+ return ctx.bar.pinned || ctx.overlay || ctx.bar.hover || ctx.popupOpen
35
+ }
36
+
37
+ tick() {
38
+ const { ctx } = this
39
+ if (ctx.quitting || ctx.win.isDestroyed()) return this.stop()
40
+ const pt = screen.getCursorScreenPoint()
41
+ const b = ctx.win.getContentBounds()
42
+ const insideX = pt.x >= b.x && pt.x < b.x + b.width
43
+ const dy = pt.y - b.y
44
+ if (insideX && dy >= 0 && dy <= REVEAL_PX) {
45
+ this.awaySince = null
46
+ this.show()
47
+ return
48
+ }
49
+ if (!ctx.bar.visible) return
50
+ if (this.keepOpen()) {
51
+ this.awaySince = null
52
+ return
53
+ }
54
+ const away = !insideX || dy < 0 || dy > HIDE_BELOW
55
+ if (!away) {
56
+ this.awaySince = null
57
+ return
58
+ }
59
+ if (this.awaySince == null) this.awaySince = Date.now()
60
+ else if (Date.now() - this.awaySince >= HIDE_DELAY) this.hide()
61
+ }
62
+
63
+ show() {
64
+ const { ctx } = this
65
+ if (ctx.quitting || ctx.win.isDestroyed()) return
66
+ clearTimeout(this.viewTimer)
67
+ this.viewTimer = null
68
+ if (ctx.bar.visible && ctx.uiView.getVisible && ctx.uiView.getVisible()) return
69
+ ctx.bar.visible = true
70
+ ctx.layout()
71
+ ctx.flush() // state first, then the view
72
+ ctx.uiView.setVisible(true)
73
+ ctx.win.setWindowButtonVisibility(true)
74
+ }
75
+
76
+ hide() {
77
+ const { ctx } = this
78
+ if (ctx.quitting || ctx.win.isDestroyed()) return
79
+ if (!ctx.bar.visible) return
80
+ this.awaySince = null
81
+ ctx.bar.visible = false
82
+ ctx.flush()
83
+ ctx.win.setWindowButtonVisibility(false)
84
+ clearTimeout(this.viewTimer)
85
+ this.viewTimer = setTimeout(() => {
86
+ this.viewTimer = null
87
+ if (ctx.quitting || ctx.win.isDestroyed()) return
88
+ if (!ctx.bar.visible && !ctx.overlay) ctx.uiView.setVisible(false)
89
+ }, VIEW_HIDE_DELAY)
90
+ }
91
+
92
+ setPinned(pinned) {
93
+ const { ctx } = this
94
+ if (ctx.bar.pinned === pinned) return
95
+ ctx.bar.pinned = pinned
96
+ if (pinned) this.show()
97
+ ctx.layout()
98
+ ctx.push()
99
+ ctx.saveSession()
100
+ ctx.rebuildMenu && ctx.rebuildMenu()
101
+ }
102
+
103
+ setHover(h) {
104
+ this.ctx.bar.hover = !!h
105
+ if (h) this.awaySince = null
106
+ }
107
+
108
+ setPopupOpen(open) {
109
+ this.ctx.popupOpen = open
110
+ this.awaySince = null
111
+ }
112
+
113
+ openOverlay(mode, initial) {
114
+ const { ctx } = this
115
+ if (ctx.quitting || ctx.win.isDestroyed()) return
116
+ ctx.overlay = { type: 'omnibox', mode, initial: initial || '', nonce: Date.now() } // nonce: re-open selects again
117
+ this.show()
118
+ ctx.layout()
119
+ ctx.flush()
120
+ ctx.uiView.setVisible(true)
121
+ ctx.uiView.webContents.focus()
122
+ }
123
+
124
+ closeOverlay(refocus = true) {
125
+ const { ctx } = this
126
+ if (!ctx.overlay) return
127
+ ctx.overlay = null
128
+ ctx.layout()
129
+ ctx.flush()
130
+ if (refocus) ctx.tabs.focusActive()
131
+ }
132
+
133
+ stop() {
134
+ clearInterval(this.timer)
135
+ this.timer = null
136
+ clearTimeout(this.viewTimer)
137
+ this.viewTimer = null
138
+ }
139
+ }
140
+
141
+ module.exports = { Reveal }