A small menubar app that allows you to switch between R versions quickly (if you have multiple versions of R framework installed). https://rud.is/rswitch
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.5 KiB

// Menu.swift
// RSwitch
// Created by hrbrmstr on 8/24/19.
// Copyright © 2019 Bob Rudis. All rights reserved.
import Foundation
import Cocoa
extension AppDelegate: NSMenuDelegate {
@objc func toggle_dock_icon(_ sender: NSMenuItem) {
Preferences.showDockIcon = !Preferences.showDockIcon
if let menu = statusItem.menu, let item = menu.item(withTag: 99) {
item.state = Preferences.showDockIcon.stateValue
@objc func toggle_hourly_rstudio_check(_ sender: NSMenuItem) {
Preferences.hourlyRStudioCheck = !Preferences.hourlyRStudioCheck
if (Preferences.hourlyRStudioCheck) { performRStudioCheck(sender) }
if let menu = statusItem.menu, let item = menu.item(withTag: 98) {
item.state = Preferences.hourlyRStudioCheck.stateValue
func menuWillOpen(_ menu: NSMenu) {
if (menu != self.statusMenu) { return }
// clear the menu
// add selection to open frameworks dir in Finder
menu.addItem(NSMenuItem(title: "Open R Frameworks Directory", action: #selector(openFrameworksDir), keyEquivalent: ""))
menu.addItem(NSMenuItem(title: "Current R Version:", action: nil, keyEquivalent: ""))
// populate installed versions
RVersions.populateRVersionsMenu(menu: menu, handler: #selector(handleRSwitch))
// Add items to download latest r-devel tarball and latest macOS daily
let rdevelItem = NSMenuItem(title: NSLocalizedString("Download latest R-devel tarball", comment: "Download latest tarball item"), action: self.rdevel_enabled ? #selector(download_latest_tarball) : nil, keyEquivalent: "")
rdevelItem.isEnabled = self.rdevel_enabled
let rstudioItem = NSMenuItem(title: NSLocalizedString("Download latest RStudio daily build", comment: "Download latest RStudio item"), action: self.rstudio_enabled ? #selector(download_latest_rstudio) : nil, keyEquivalent: "")
rstudioItem.isEnabled = self.rstudio_enabled
let refsDropdown = NSMenuItem(title: "Reference Desk", action: nil, keyEquivalent: "")
let refsSub = NSMenu()
menu.setSubmenu(refsSub, for: refsDropdown)
// Add items to open various R for macOS pages
BrowseMenuAction.populateWebItems(menu: refsSub)
// Add links to local copies of the R Manuals
BrowseMenuAction.populateLocalRManualsItems(menu: refsSub)
// Add links to free R online books
BrowseMenuAction.populateRBooksItems(menu: refsSub)
// Add running apps
populateRunningApps(menu: menu)
// Add launchers
AppMenuAction.populateLaunchers(menu: menu)
RStudioServerMenuAction.populateRStudioServerSessions(menu: menu, manager: sess)
// Toggle Dock Icon
let prefsDropdown = NSMenuItem(title: "Preferences", action: nil, keyEquivalent: "")
let prefSub = NSMenu()
menu.setSubmenu(prefSub, for: prefsDropdown)
let dockItem = NSMenuItem(title: "Show Dock Icon", action: #selector(toggle_dock_icon), keyEquivalent: "")
dockItem.tag = 99
dockItem.target = self
dockItem.state = Preferences.showDockIcon.stateValue
let rstudioCheckItem = NSMenuItem(title: "Notify me when a new RStudio Daily is available", action: #selector(toggle_hourly_rstudio_check), keyEquivalent: "")
rstudioCheckItem.tag = 98
rstudioCheckItem.target = self
rstudioCheckItem.state = Preferences.hourlyRStudioCheck.stateValue
menu.addItem(NSMenuItem(title: "Check for update…", action: #selector(checkForUpdate), keyEquivalent: ""))
menu.addItem(NSMenuItem(title: "About RSwitch…", action: #selector(showAbout), keyEquivalent: ""))
menu.addItem(NSMenuItem(title: "RSwitch Help…", action: #selector(rswitch_help), keyEquivalent: ""))
// Add a Quit item
let quitItem = NSMenuItem(title: "Quit", action: #selector(NSApp.terminate), keyEquivalent: "q")
quitItem.keyEquivalentModifierMask = NSEvent.ModifierFlags.option