Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/electron-chrome-extensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ To enable the element on a webpage, you must define a preload script which injec
- `partition` string (optional) - The `Electron.Session` partition which extensions are loaded in. Defaults to the session in which `<browser-action-list>` lives.
- `tab` string (optional) - The tab's `Electron.WebContents` ID to use for displaying
the relevant browser action state. Defaults to the active tab of the current browser window.
- `alignment` string (optional) - How the popup window should be aligned relative to the extension action. Defaults to `bottom left`. Use any assortment of `top`, `bottom`, `left`, and `right`.

#### Browser action example

Expand Down Expand Up @@ -250,6 +251,10 @@ Add the `<browser-action-list>` element with attributes appropriate for your app

<!-- Show actions for custom session and a specific tab of current window. -->
<browser-action-list partition="persist:custom" tab="1"></browser-action-list>

<!-- If extensions are displayed in the bottom left of your browser UI, then
you can align the popup to the top right of the extension action. -->
<browser-action-list alignment="top right"></browser-action-list>
```

##### Custom CSS
Expand Down
24 changes: 22 additions & 2 deletions packages/electron-chrome-extensions/src/browser-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const injectBrowserAction = () => {
extensionId: string
tabId: number
anchorRect: { x: number; y: number; width: number; height: number }
alignment?: string
}

const __browserAction__ = {
Expand Down Expand Up @@ -115,8 +116,16 @@ export const injectBrowserAction = () => {
}
}

get alignment(): string {
return this.getAttribute('alignment') || ''
}

set alignment(alignment: string) {
this.setAttribute('alignment', alignment)
}

static get observedAttributes() {
return ['id', 'tab', 'partition']
return ['id', 'tab', 'partition', 'alignment']
}

constructor() {
Expand Down Expand Up @@ -156,6 +165,7 @@ export const injectBrowserAction = () => {
eventType: event.type,
extensionId: this.id,
tabId: this.tab,
alignment: this.alignment,
anchorRect: {
x: rect.left,
y: rect.top,
Expand Down Expand Up @@ -280,8 +290,16 @@ export const injectBrowserAction = () => {
}
}

get alignment(): string {
return this.getAttribute('alignment') || ''
}

set alignment(alignment: string) {
this.setAttribute('alignment', alignment)
}

static get observedAttributes() {
return ['tab', 'partition']
return ['tab', 'partition', 'alignment']
}

constructor() {
Expand Down Expand Up @@ -413,12 +431,14 @@ export const injectBrowserAction = () => {
}) as BrowserActionElement
node.id = action.id
node.className = 'action'
;(node as any).alignment = this.alignment
;(node as any).part = 'action'
browserActionNode = node
this.shadowRoot?.appendChild(browserActionNode)
}

if (this.partition) browserActionNode.partition = this.partition
if (this.alignment) browserActionNode.alignment = this.alignment
browserActionNode.tab = tabId
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface ActivateDetails {
extensionId: string
tabId: number
anchorRect: { x: number; y: number; width: number; height: number }
alignment?: string
}

const getBrowserActionDefaults = (extension: Electron.Extension): ExtensionAction | undefined => {
Expand Down Expand Up @@ -390,7 +391,7 @@ export class BrowserActionAPI {
}

private activateClick(details: ActivateDetails) {
const { extensionId, tabId, anchorRect } = details
const { extensionId, tabId, anchorRect, alignment } = details

if (this.popup) {
const toggleExtension = !this.popup.isDestroyed() && this.popup.extensionId === extensionId
Expand Down Expand Up @@ -422,6 +423,7 @@ export class BrowserActionAPI {
parent: win,
url: popupUrl,
anchorRect,
alignment,
})

d(`opened popup: ${popupUrl}`)
Expand Down
9 changes: 8 additions & 1 deletion packages/electron-chrome-extensions/src/browser/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface PopupViewOptions {
parent: Electron.BaseWindow
url: string
anchorRect: PopupAnchorRect
alignment?: string
}

const supportsPreferredSize = () => {
Expand All @@ -40,6 +41,7 @@ export class PopupView {
private anchorRect: PopupAnchorRect
private destroyed: boolean = false
private hidden: boolean = true
private alignment?: string

/** Preferred size changes are only received in Electron v12+ */
private usingPreferredSize = supportsPreferredSize()
Expand All @@ -50,6 +52,7 @@ export class PopupView {
this.parent = opts.parent
this.extensionId = opts.extensionId
this.anchorRect = opts.anchorRect
this.alignment = opts.alignment

this.browserWindow = new BrowserWindow({
show: false,
Expand Down Expand Up @@ -200,10 +203,14 @@ export class PopupView {
const winBounds = this.parent.getBounds()
const viewBounds = this.browserWindow.getBounds()

// TODO: support more orientations than just top-right
let x = winBounds.x + this.anchorRect.x + this.anchorRect.width - viewBounds.width
let y = winBounds.y + this.anchorRect.y + this.anchorRect.height + PopupView.POSITION_PADDING

// If aligned to a differently then we need to offset the popup position
if (this.alignment?.includes('right')) x = winBounds.x + this.anchorRect.x
if (this.alignment?.includes('top'))
y = winBounds.y - viewBounds.height + this.anchorRect.y - PopupView.POSITION_PADDING

// Convert to ints
x = Math.floor(x)
y = Math.floor(y)
Expand Down
2 changes: 1 addition & 1 deletion packages/shell/browser/ui/webui.html
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
<div class="address-bar">
<input id="addressurl" spellcheck="false" />
</div>
<browser-action-list id="actions"></browser-action-list>
<browser-action-list id="actions" alignment="bottom left"></browser-action-list>
</div>
</div>
</div>
Expand Down