Skip to content
Open
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
7 changes: 7 additions & 0 deletions src/glimpse.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ class GlimpseWindow extends EventEmitter {
case 'click':
this.emit('click');
break;
case 'menu':
this.emit('menu', msg.id);
break;
case 'closed':
if (!this.#closed) {
this.#closed = true;
Expand Down Expand Up @@ -257,6 +260,10 @@ class GlimpseStatusItem extends GlimpseWindow {
this._write({ type: 'title', title });
}

setMenu(items) {
this._write({ type: 'status-menu', items });
}

resize(width, height) {
this._write({ type: 'resize', width, height });
}
Expand Down
31 changes: 31 additions & 0 deletions src/glimpse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, WKNavigationDelegate, WKScri
var nsStatusItem: NSStatusItem?
var popover: NSPopover?
var popoverViewController: StatusItemViewController?
var statusMenu: NSMenu?

nonisolated init(config: Config) {
self.config = config
Expand Down Expand Up @@ -467,6 +468,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, WKNavigationDelegate, WKScri
button.title = config.title == "Glimpse" ? "G" : config.title
button.action = #selector(statusItemClicked(_:))
button.target = self
button.sendAction(on: [.leftMouseUp, .rightMouseUp])
}

// Load blank page to trigger first ready
Expand All @@ -475,6 +477,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, WKNavigationDelegate, WKScri

@objc func statusItemClicked(_ sender: Any?) {
guard let button = nsStatusItem?.button, let popover = popover else { return }
if let event = NSApp.currentEvent,
(event.type == .rightMouseUp || (event.type == .leftMouseUp && event.modifierFlags.contains(.control))),
let statusMenu {
NSMenu.popUpContextMenu(statusMenu, with: event, for: button)
return
}
if popover.isShown {
popover.performClose(sender)
} else {
Expand All @@ -483,6 +491,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, WKNavigationDelegate, WKScri
writeToStdout(["type": "click"])
}

@objc func statusMenuItemSelected(_ sender: NSMenuItem) {
if let id = sender.representedObject as? String {
writeToStdout(["type": "menu", "id": id])
}
}

// MARK: - Follow Cursor

func computeTargetPosition(mouse: NSPoint) -> NSPoint {
Expand Down Expand Up @@ -766,6 +780,23 @@ class AppDelegate: NSObject, NSApplicationDelegate, WKNavigationDelegate, WKScri
} else {
window.setContentSize(size)
}
case "status-menu":
guard config.statusItem else {
log("status-menu not supported outside status-item mode")
return
}
let items = json["items"] as? [[String: Any]] ?? []
let menu = NSMenu()
for item in items {
guard let id = item["id"] as? String, let title = item["title"] as? String else {
continue
}
let menuItem = NSMenuItem(title: title, action: #selector(statusMenuItemSelected(_:)), keyEquivalent: "")
menuItem.target = self
menuItem.representedObject = id
menu.addItem(menuItem)
}
statusMenu = menu.items.isEmpty ? nil : menu
case "close":
closeAndExit()
default:
Expand Down