3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JXAでメニューバーアプリ

Posted at

ウェブビューを仕込んだメニューバーアプリのサンプルです。

YouTubeApp
ObjC.import('Cocoa')
ObjC.import('WebKit')

var webViewRect = $.NSMakeRect(0, 0, 1024, 560)
var webViewConf = $.WKWebViewConfiguration.alloc.init
webViewConf.preferences.plugInsEnabled = true
var webView = $.WKWebView.alloc.initWithFrameConfiguration(webViewRect, webViewConf)

var url = $.NSURL.URLWithString('http://www.youtube.com/')
var req = $.NSURLRequest.requestWithURL(url)
webView.loadRequest(req)

var webViewItem = $.NSMenuItem.alloc.init
webViewItem.view = webView

var reloadItem = $.NSMenuItem.alloc.init
reloadItem.title = 'Reload'

var quitItem = $.NSMenuItem.alloc.init
quitItem.title = 'Quit'

var statusMenu = $.NSMenu.alloc.init
statusMenu.addItem(webViewItem)
statusMenu.addItem($.NSMenuItem.separatorItem)
statusMenu.addItem(reloadItem)
statusMenu.addItem($.NSMenuItem.separatorItem)
statusMenu.addItem(quitItem)

ObjC.registerSubclass({
	name: 'ActivateStatusItemAction',
	methods: {
		'popUpMenu:': {
			types: ['void', ['id']],
			implementation: function(sender) {
				if ($.NSApplication.sharedApplication.active) {
					statusItem.highlightMode = true
					statusItem.popUpStatusItemMenu(statusMenu)
					statusItem.highlightMode = false
				} else {
					$.NSApplication.sharedApplication.activateIgnoringOtherApps(true)
				}
			}
		},
		'becomeActive:': {
			types: ['void', ['id']],
			implementation: function(sender) {
				statusItem.button.performClick(this)
			}
		},
	}
})

var statusAction = $.ActivateStatusItemAction.alloc.init
var statusItem = $.NSStatusBar.systemStatusBar.statusItemWithLength($.NSVariableStatusItemLength)
statusItem.title = '📺'
statusItem.target = statusAction
statusItem.action = 'popUpMenu:'
statusItem.highlightMode = false

$.NSNotificationCenter.defaultCenter.addObserverSelectorNameObject(statusAction, 'becomeActive:', $.NSApplicationDidBecomeActiveNotification, $.NSApp)

ObjC.registerSubclass({
	name: 'Action',
	methods: {
		'reload:': {
			types: ['void', ['id']],
			implementation: function(sender) {
				webView.reloadFromOrigin
			}
		},
		'quit:': {
			types: ['void', ['id']],
			implementation: function(sender) {
				$.NSNotificationCenter.defaultCenter.removeObserver(statusAction)
				$.NSApp.terminate($())
			}
		},
	}
})

var action = $.Action.alloc.init

reloadItem.target = action
reloadItem.action = 'reload:'

quitItem.target = action
quitItem.action = 'quit:'

ポイントはオブザーバーを使ってクリックのタイミングでアプリを最前面に持ってくるところ。そうしないとウェブビューはキーボード入力を受け付けないので。

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?