Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' into electron5
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrazee committed Apr 26, 2019
2 parents 303f3a0 + ca18809 commit 31911aa
Show file tree
Hide file tree
Showing 16 changed files with 157 additions and 30 deletions.
4 changes: 2 additions & 2 deletions app/background-process/ui/subwindows/shell-menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export function reposition (parentWindow) {
win.setBounds({
x: parentBounds.x + win.boundsOpt.right - 320,
y: parentBounds.y + win.boundsOpt.top,
width: 320,
height: 88
width: 350,
height: 90
})
} else if (win.menuId === 'local-path') {
win.setBounds({
Expand Down
81 changes: 68 additions & 13 deletions app/background-process/ui/view-manager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, dialog, BrowserView, BrowserWindow, Menu, clipboard } from 'electron'
import { app, dialog, BrowserView, BrowserWindow, Menu, clipboard, ipcMain } from 'electron'
import * as beakerCore from '@beaker/core'
import errorPage from '@beaker/core/lib/error-page'
import path from 'path'
Expand Down Expand Up @@ -94,6 +94,7 @@ var activeViews = {} // map of {[win.id]: Array<View>}
var closedURLs = {} // map of {[win.id]: Array<string>}
var windowEvents = {} // mapof {[win.id]: Events}
var noRedirectHostnames = new Set() // set of hostnames which have dat-redirection disabled
var nextViewIsScriptCloseable = false // will the next view created be "script closable"?

// classes
// =
Expand Down Expand Up @@ -131,6 +132,7 @@ class View {
this.isInpageFindActive = false // is the inpage-finder UI active?
this.currentInpageFindString = undefined // what's the current inpage-finder query string?
this.currentInpageFindResults = undefined // what's the current inpage-finder query results?
this.isScriptClosable = takeIsScriptClosable() // can this view be closed by `window.close` ?

// helper state
this.peers = 0 // how many peers does the site have?
Expand All @@ -149,6 +151,7 @@ class View {
this.webContents.on('did-stop-loading', this.onDidStopLoading.bind(this))
this.webContents.on('did-fail-load', this.onDidFailLoad.bind(this))
this.webContents.on('update-target-url', this.onUpdateTargetUrl.bind(this))
this.webContents.on('page-title-updated', this.onPageTitleUpdated.bind(this)) // NOTE page-title-updated isn't documented on webContents but it is supported
this.webContents.on('page-favicon-updated', this.onPageFaviconUpdated.bind(this))
this.webContents.on('new-window', this.onNewWindow.bind(this))
this.webContents.on('media-started-playing', this.onMediaChange.bind(this))
Expand Down Expand Up @@ -219,6 +222,13 @@ class View {
this.browserView.webContents.loadURL(url)
}

resize () {
const win = this.browserWindow
var {width, height} = win.getContentBounds()
this.browserView.setBounds({x: 0, y: Y_POSITION, width, height: height - Y_POSITION})
this.browserView.setAutoResize({width: true, height: true})
}

activate () {
this.isActive = true

Expand All @@ -227,10 +237,7 @@ class View {
permPrompt.show(this.browserView)
modals.show(this.browserView)

var {width, height} = win.getBounds()
this.browserView.setBounds({x: 0, y: Y_POSITION, width, height: height - Y_POSITION})
this.browserView.setAutoResize({width: true, height: true})

this.resize()
this.webContents.focus()
}

Expand Down Expand Up @@ -414,7 +421,9 @@ class View {
.markdown code { padding: 3px 5px; }
.markdown pre > code { display: block; }
`)
this.webContents.executeJavaScript(await fs.readFile(path.join(app.getAppPath(), 'markdown-renderer.build.js'), 'utf8'))
let mdpath = path.join(app.getAppPath(), 'markdown-renderer.build.js')
mdpath = mdpath.replace('app.asar', 'app.asar.unpacked') // fetch from unpacked dir
this.webContents.executeJavaScript(await fs.readFile(mdpath, 'utf8'))
}

// json rendering
Expand Down Expand Up @@ -446,7 +455,9 @@ class View {
background: #ddd;
}
`)
this.webContents.executeJavaScript(await fs.readFile(path.join(app.getAppPath(), 'json-renderer.build.js'), 'utf8'))
let jsonpath = path.join(app.getAppPath(), 'json-renderer.build.js')
jsonpath = jsonpath.replace('app.asar', 'app.asar.unpacked') // fetch from unpacked dir
this.webContents.executeJavaScript(await fs.readFile(jsonpath, 'utf8'))
}
}

Expand Down Expand Up @@ -501,6 +512,7 @@ class View {
// update state
this.isLoading = true
this.loadingURL = null
this.favicons = null
this.isReceivingAssets = false
this.wasDatTimeout = false

Expand Down Expand Up @@ -592,6 +604,10 @@ class View {
statusBar.set(this.browserWindow, url)
}

onPageTitleUpdated (e, title) {
this.emitUpdateState()
}

onPageFaviconUpdated (e, favicons) {
this.favicons = favicons && favicons[0] ? favicons : null
this.emitUpdateState()
Expand Down Expand Up @@ -625,6 +641,24 @@ class View {
// =

export function setup () {
// listen for webContents messages
ipcMain.on('BEAKER_MARK_NEXT_VIEW_SCRIPTCLOSEABLE', e => {
nextViewIsScriptCloseable = true
e.returnValue = true
})
ipcMain.on('BEAKER_SCRIPTCLOSE_SELF', e => {
var browserView = BrowserView.fromWebContents(e.sender)
if (browserView) {
var view = findView(browserView)
if (view && view.isScriptClosable) {
remove(view.browserWindow, view)
e.returnValue = true
return
}
}
e.returnValue = false
})

// track peer-counts
beakerCore.dat.library.createEventStream().on('data', ([evt, {details}]) => {
if (evt !== 'network-changed') return
Expand Down Expand Up @@ -670,10 +704,20 @@ export function getActive (win) {
return getAll(win).find(view => view.isActive)
}

export function findContainingWindow (view) {
export function findView (browserView) {
for (let winId in activeViews) {
for (let v of activeViews[winId]) {
if (v.browserView === browserView) {
return v
}
}
}
}

export function findContainingWindow (browserView) {
for (let winId in activeViews) {
for (let v of activeViews[winId]) {
if (v.browserView === view) {
if (v.browserView === browserView) {
return v.browserWindow
}
}
Expand Down Expand Up @@ -798,6 +842,11 @@ export function setActive (win, view) {
emitReplaceState(win)
}

export function resize (win) {
var active = getActive(win)
if (active) active.resize()
}

export function initializeFromSnapshot (win, snapshot) {
win = getTopWindow(win)
for (let url of snapshot) {
Expand Down Expand Up @@ -840,9 +889,7 @@ export async function loadPins (win) {
win = getTopWindow(win)
var json = await settingsDb.get('pinned_tabs')
try { JSON.parse(json).forEach(url => create(win, url, {isPinned: true})) }
catch (e) {
console.log('Failed to load pins', e)
}
catch (e) {}
}

export function reopenLastRemoved (win) {
Expand Down Expand Up @@ -1174,7 +1221,7 @@ function addToNoRedirects (url) {

async function fireBeforeUnloadEvent (wc) {
try {
if (wc.isWaitingForResponse()) {
if (wc.isLoading() || wc.isWaitingForResponse()) {
return // dont bother
}
return await wc.executeJavaScript(`
Expand All @@ -1187,4 +1234,12 @@ async function fireBeforeUnloadEvent (wc) {
} catch (e) {
// ignore
}
}

// `nextViewIsScriptCloseable` is set by a message received prior to window.open() being called
// we capture the state of the flag on the next created view, then reset it
function takeIsScriptClosable () {
var b = nextViewIsScriptCloseable
nextViewIsScriptCloseable = false
return b
}
12 changes: 11 additions & 1 deletion app/background-process/ui/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,19 @@ export function createShellWindow (windowState) {
registerGlobalKeybinding(win, 'CmdOrCtrl+[', onGoBack(win))
registerGlobalKeybinding(win, 'CmdOrCtrl+]', onGoForward(win))
registerGlobalKeybinding(win, 'Alt+D', onFocusLocation(win))
registerGlobalKeybinding(win, 'F5', onReload(win))
registerGlobalKeybinding(win, 'F6', onFocusLocation(win))

// register event handlers
win.on('browser-backward', onGoBack(win))
win.on('browser-forward', onGoForward(win))
win.on('scroll-touch-begin', sendScrollTouchBegin)
win.on('scroll-touch-end', sendToWebContents('scroll-touch-end'))
win.on('focus', sendToWebContents('focus'))
win.on('blur', sendToWebContents('blur'))
win.on('blur', e => {
statusBarSubwindow.set(win, false) // hide the statusbar on blur
sendToWebContents('blur')(e)
})
win.on('app-command', (e, cmd) => { onAppCommand(win, e, cmd) })
win.on('enter-full-screen', e => {
// update UI
Expand All @@ -251,6 +256,7 @@ export function createShellWindow (windowState) {
// sendToWebContents('leave-full-screen')(e)
})
win.on('resize', () => {
viewManager.resize(win)
for (let k in subwindows) {
subwindows[k].reposition(win)
}
Expand Down Expand Up @@ -402,6 +408,10 @@ function onGoForward (win) {
return () => viewManager.getActive(win).webContents.goForward()
}

function onReload (win) {
return () => viewManager.getActive(win).webContents.reload()
}

function onFocusLocation (win) {
return () => win.webContents.send('command', 'focus-location')
}
Expand Down
19 changes: 16 additions & 3 deletions app/builtin-pages/views/library-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,14 @@ async function gotoFileEditor (filePath) {
onOpenFileEditor()
}

function resolvePaymentLink (archiveUrl, paymentLink) {
if (paymentLink.indexOf('://') === -1) {
const shouldAddSlash = !archiveUrl.endsWith('/') && !paymentLink.startsWith('/')
return `${archiveUrl}${shouldAddSlash ? '/' : ''}${paymentLink}`
}
return paymentLink
}

// rendering
// =

Expand Down Expand Up @@ -835,6 +843,11 @@ function renderReadmeHint () {
</div>`
}

function renderDonationLink (archiveUrl, paymentLink) {
const url = resolvePaymentLink(archiveUrl, paymentLink)
return yo`<a href=${url}>${url}</a>`
}

function renderSettingsView () {
const isOwner = _get(archive, 'info.isOwner')

Expand Down Expand Up @@ -1013,13 +1026,13 @@ function renderSettingsView () {
? yo`
<p>
Enter a link to your donation page and Beaker will show
a <span class="fa fa-heart"></span> icon in your page's URL bar.
a <span class="fa fa-donate"></span> icon in your page's URL bar.
${renderSettingsField({key: 'paymentLink', value: paymentLink, placeholder: 'Example: https://opencollective.com/beaker', onUpdate: setManifestValue})}
</p>
`
: paymentLink
? yo`<p><a href=${paymentLink}>${paymentLink}</a></p>`
? yo`<p>${renderDonationLink(archive.url, paymentLink)}</p>`
: yo`<p><em>No link provided.</em></p>`
}
</div>
Expand Down Expand Up @@ -1156,7 +1169,7 @@ function renderNetworkView () {
</div>`
] : yo`
<div class="hint">
<i class="far fa-heart"></i>
<i class="fa fa-hand-holding-heart"></i>
<strong>Give back!</strong> Seed this project${"'"}s files to help keep them online.
<a href="https://beakerbrowser.com/docs/how-beaker-works/peer-to-peer-websites#keeping-a-peer-to-peer-website-online" target="_blank" class="learn-more-link">Learn more</a>
</div>`
Expand Down
4 changes: 4 additions & 0 deletions app/dat-daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ process.on('uncaughtException', (err) => {
console.error('Uncaught exception:', err)
})

process.on('disconnect', () => {
process.exit()
})

process.once('message', firstMsg => {
beakerCoreDatDaemon.setup({
rpcAPI,
Expand Down
3 changes: 3 additions & 0 deletions app/markdown-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ if (!document.querySelector('main')) {
var navReq = await fetch('/nav.md')
if (!navReq.ok) return
var navMD = await navReq.text()
if (navMD.includes('<!doctype') || navMD.includes('<html')) {
return // dont render if we get HTML - this can indicate a 404 page
}
navHTML = md.render(navMD)
document.querySelector('nav').innerHTML = navHTML
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion app/new-shell-window/navbar/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class NavbarLocation extends LitElement {
var cls = classMap({donate: true, pressed: this.isDonateMenuOpen})
return html`
<button class="${cls}" @click=${this.onClickDonateMenu}>
<i class="far fa-heart"></i>
<i class="fa fa-donate"></i>
</button>
`
}
Expand Down
2 changes: 1 addition & 1 deletion app/new-shell-window/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ ${spinnerCSS}
}
.tab.pinned {
width: 45px;
flex: 0 0 45px;
}
.tab-favicon {
Expand Down
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"productName": "Beaker Browser",
"description": "An Experimental Peer-to-Peer Web Browser.",
"homepage": "https://beakerbrowser.com/",
"version": "0.8.6",
"version": "0.8.8",
"author": "Paul Frazee <[email protected]>",
"copyright": "© 2019, Blue Link Labs",
"main": "background-process.build.js",
Expand Down
2 changes: 1 addition & 1 deletion app/shell-menus/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class BrowserMenu extends LitElement {
</div>
<div class="menu-item" @click=${e => this.onOpenPage(e, 'https://opencollective.com/beaker')}>
<i class="far fa-heart"></i>
<i class="fa fa-donate"></i>
<span class="label">Support Beaker</span>
</div>
</div>
Expand Down
Loading

0 comments on commit 31911aa

Please sign in to comment.