source: launchers/macosx/I2PLauncher/userinterface/StatusBarController.swift @ eca5805

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

Mac OS X Launcher: Adding selectors for Preferences window launch, and for view of experimental console webview.

  • Property mode set to 100644
File size: 5.7 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 ctrl : PopoverViewController?
19  private static var preferencesController: NSWindowController?
20  private static var experimentalConsoleViewController: NSWindowController?
21
22  @IBOutlet var routerStatusTabView: RouterStatusView?
23 
24  //var updateObjectRef : SUUpdater?
25 
26  @objc func handleOpenConsole(_ sender: Any?) {
27    SwiftMainDelegate.openLink(url: "http://localhost:7657")
28  }
29 
30  @objc func constructMenu() -> NSMenu {
31    let menu = NSMenu()
32   
33    /*let updateMenuItem = NSMenuItem(title: "Check for updates", action: #selector(self.updateObjectRef?.checkForUpdates(_:)), keyEquivalent: "U")
34    updateMenuItem.isEnabled = true
35    */
36   
37    let preferencesMenuItem = NSMenuItem(title: "Preferences", action: #selector(StatusBarController.launchPreferences(_:)), keyEquivalent: "P")
38    preferencesMenuItem.isEnabled = true
39   
40    menu.addItem(NSMenuItem(title: "Open I2P Console", action: #selector(self.handleOpenConsole(_:)), keyEquivalent: "O"))
41    menu.addItem(NSMenuItem.separator())
42    //menu.addItem(updateMenuItem)
43    menu.addItem(preferencesMenuItem)
44    menu.addItem(NSMenuItem.separator())
45    menu.addItem(NSMenuItem(title: "Quit I2P Launcher", action: #selector(SwiftMainDelegate.terminate(_:)), keyEquivalent: "q"))
46   
47    return menu
48  }
49 
50  static func onExperimentalConsoleViewClick(_ sender: NSButton) {
51    if #available(OSX 10.12, *) {
52      print("Clicked for Experimental Console WebView")
53      if !(experimentalConsoleViewController != nil) {
54        let storyboard = NSStoryboard(name: "ConsoleWebView", bundle: Bundle.main)
55        experimentalConsoleViewController = storyboard.instantiateInitialController() as? NSWindowController
56        print("created experimental console webview controller")
57      }
58      if (experimentalConsoleViewController != nil) {
59        experimentalConsoleViewController!.showWindow(sender)
60        print("trying to view: Console WebView")
61      }
62    } else {
63      // Sorry, only OSX >= 10.12
64    }
65  }
66 
67  static func launchPreferences(_ sender: Any) {
68    print("Preferences clicked")
69    if !(preferencesController != nil) {
70      let storyboard = NSStoryboard(name: "Preferences", bundle: Bundle.main)
71      preferencesController = storyboard.instantiateInitialController() as? NSWindowController
72      print("created preferences controller")
73    }
74    if (preferencesController != nil) {
75      NSApp.activate(ignoringOtherApps: true)
76      preferencesController!.showWindow(sender)
77      print("trying to view: Preferences")
78    }
79  }
80 
81  static func launchRouterConsole(_ sender: Any) {
82    if (!Preferences.shared().featureToggleExperimental) {
83      // The normal...
84      NSWorkspace.shared().open(URL(string: "http://127.0.0.1:7657")!)
85    } else {
86      // Experimental
87    }
88  }
89 
90  func pidReaction(information:Any?){
91    let pidStr = information as! String
92    NSLog("PID! %@", pidStr)
93    showPopover(sender: nil)
94    RouterManager.shared().lastRouterPid = pidStr
95    self.ctrl?.getRouterStatusView()?.needsDisplay = true
96  }
97 
98  func event_toggle(information:Any?) {
99    self.togglePopover(sender: self)
100  }
101
102 
103  override init() {
104    super.init()
105    self.ctrl = PopoverViewController.freshController()
106    popover.contentViewController = self.ctrl
107    RouterManager.shared().eventManager.listenTo(eventName: "router_pid", action: pidReaction)
108   
109    RouterManager.shared().eventManager.listenTo(eventName: "toggle_popover", action: event_toggle)
110   
111    if let button = statusItem.button {
112      button.image = NSImage(named:"StatusBarButtonImage")
113      button.toolTip = "I2P Launch Manager"
114      button.target = self
115      button.action = #selector(self.statusBarButtonClicked(sender:))
116      button.sendAction(on: [.leftMouseUp, .rightMouseUp])
117    }
118  }
119 
120  @IBAction func openConsoleClicked(_ sender: Any) {
121    NSLog("openConsoleClicked got clicked")
122    let realSender = sender as! NSMenuItem
123    NSLog("Sender: @%", realSender)
124  }
125 
126  @IBAction func quitClicked(_ sender: NSMenuItem) {
127    NSApplication.shared().terminate(self)
128  }
129 
130  // Submenu
131 
132  @IBAction func startRouterClicked(_ sender: NSMenuItem) {
133   
134  }
135 
136  @IBAction func restartRouterClicked(_ sender: NSMenuItem) {
137   
138  }
139 
140  @IBAction func stopRouterClicked(_ sender: NSMenuItem) {
141   
142  }
143 
144  func statusBarButtonClicked(sender: NSStatusBarButton) {
145    let event = NSApp.currentEvent!
146   
147    if event.type == NSEventType.rightMouseUp {
148      closePopover(sender: nil)
149     
150      let ctxMenu = constructMenu()
151     
152      statusItem.menu = ctxMenu
153      statusItem.popUpMenu(ctxMenu)
154     
155      // This is critical, otherwise clicks won't be processed again
156      statusItem.menu = nil
157    } else {
158      togglePopover(sender: nil)
159    }
160  }
161 
162  func togglePopover(sender: AnyObject?) {
163    if popover.isShown {
164      closePopover(sender: sender)
165    } else {
166      showPopover(sender: sender)
167    }
168  }
169 
170  func showPopover(sender: AnyObject?) {
171    if let button = statusItem.button {
172      let inst = RouterStatusView.getInstance()
173      if (inst != nil) {
174        if (inst != Optional.none) { RouterStatusView.getInstance()?.setRouterStatusLabelText() }
175      }
176      popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
177    }
178  }
179 
180  func closePopover(sender: AnyObject?) {
181    popover.performClose(sender)
182  }
183 
184 
185}
186
Note: See TracBrowser for help on using the repository browser.