source: launchers/macosx/I2PLauncher/userinterface/StatusBarController.swift @ 7a72049

Last change on this file since 7a72049 was 7a72049, checked in by meeh <meeh@…>, 2 years ago

Mac OS X Launcher:

  • Bugfixes as always
  • Added Sparkle (native updater, https://sparkle-project.org/ )
  • The launcher will now extract and overwrite older versions if found
  • Rewrite of the java extraction part (to enable overwrite)
  • Move more functionality to use EventManager? as it works quite well
  • Added check for updates menu item
  • Property mode set to 100644
File size: 3.4 KB
Line 
1//
2//  StatusBarController.swift
3//  I2PLauncher
4//
5//  Created by Mikal Villa on 13/03/2018.
6//  Copyright © 2018 I2P. All rights reserved.
7//
8
9import Foundation
10import Cocoa
11
12@objc class StatusBarController: NSObject, NSMenuDelegate {
13 
14  let popover = NSPopover()
15  let statusItem = NSStatusBar.system().statusItem(withLength: NSVariableStatusItemLength)
16  let storyboard = NSStoryboard(name: "Storyboard", bundle: Bundle.main)
17 
18  var updateObjectRef : SUUpdater?
19 
20  @objc func handleOpenConsole(_ sender: Any?) {
21    SwiftMainDelegate.openLink(url: "http://localhost:7657")
22  }
23 
24  @objc func constructMenu() -> NSMenu {
25    let menu = NSMenu()
26   
27    let updateMenuItem = NSMenuItem(title: "Check for updates", action: #selector(self.updateObjectRef?.checkForUpdates(_:)), keyEquivalent: "U")
28    updateMenuItem.isEnabled = true
29   
30    menu.addItem(NSMenuItem(title: "Open I2P Console", action: #selector(self.handleOpenConsole(_:)), keyEquivalent: "O"))
31    menu.addItem(NSMenuItem.separator())
32    menu.addItem(updateMenuItem)
33    menu.addItem(NSMenuItem.separator())
34    menu.addItem(NSMenuItem(title: "Quit I2P Launcher", action: #selector(SwiftMainDelegate.terminate(_:)), keyEquivalent: "q"))
35   
36    return menu
37  }
38
39 
40  override init() {
41    super.init()
42    popover.contentViewController = PopoverViewController.freshController()
43    updateObjectRef = SUUpdater.shared()
44    updateObjectRef?.checkForUpdatesInBackground()
45   
46   
47    if let button = statusItem.button {
48      button.image = NSImage(named:"StatusBarButtonImage")
49      button.toolTip = "I2P Launch Manager"
50      button.target = self
51      button.action = #selector(self.statusBarButtonClicked(sender:))
52      button.sendAction(on: [.leftMouseUp, .rightMouseUp])
53    }
54  }
55 
56  @IBAction func openConsoleClicked(_ sender: Any) {
57    NSLog("openConsoleClicked got clicked")
58    let realSender = sender as! NSMenuItem
59    NSWorkspace.shared().open(URL(string: "http://127.0.0.1:7657")!)
60    NSLog("Sender: @%", realSender)
61  }
62 
63  @IBAction func quitClicked(_ sender: NSMenuItem) {
64    NSApplication.shared().terminate(self)
65  }
66 
67  // Submenu
68 
69  @IBAction func startRouterClicked(_ sender: NSMenuItem) {
70   
71  }
72 
73  @IBAction func restartRouterClicked(_ sender: NSMenuItem) {
74   
75  }
76 
77  @IBAction func stopRouterClicked(_ sender: NSMenuItem) {
78   
79  }
80 
81  func statusBarButtonClicked(sender: NSStatusBarButton) {
82    let event = NSApp.currentEvent!
83   
84    if event.type == NSEventType.rightMouseUp {
85      closePopover(sender: nil)
86     
87      let ctxMenu = constructMenu()
88     
89      statusItem.menu = ctxMenu
90      statusItem.popUpMenu(ctxMenu)
91     
92      // This is critical, otherwise clicks won't be processed again
93      statusItem.menu = nil
94    } else {
95      togglePopover(sender: nil)
96    }
97  }
98 
99  func togglePopover(sender: AnyObject?) {
100    if popover.isShown {
101      closePopover(sender: sender)
102    } else {
103      RouterManager.shared().updateState()
104      showPopover(sender: sender)
105    }
106  }
107 
108  func showPopover(sender: AnyObject?) {
109    if let button = statusItem.button {
110      let inst = RouterStatusView.getInstance()
111      if (inst != nil) {
112        if (inst != Optional.none) { RouterStatusView.getInstance()?.setRouterStatusLabelText() }
113      }
114      popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
115    }
116  }
117 
118  func closePopover(sender: AnyObject?) {
119    popover.performClose(sender)
120  }
121 
122 
123}
124
Note: See TracBrowser for help on using the repository browser.