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

Last change on this file since e3ab9f8e was e3ab9f8e, checked in by meeh <meeh@…>, 20 months ago

OSX Launcher: Swift GUI code updates, most related to the new launch method.

  • Property mode set to 100644
File size: 3.8 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
20  @IBOutlet var routerStatusTabView: RouterStatusView?
21 
22  //var updateObjectRef : SUUpdater?
23 
24  @objc func handleOpenConsole(_ sender: Any?) {
25    SwiftMainDelegate.openLink(url: "http://localhost:7657")
26  }
27 
28  @objc func constructMenu() -> NSMenu {
29    let menu = NSMenu()
30   
31    /*let updateMenuItem = NSMenuItem(title: "Check for updates", action: #selector(self.updateObjectRef?.checkForUpdates(_:)), keyEquivalent: "U")
32    updateMenuItem.isEnabled = true
33    */
34    menu.addItem(NSMenuItem(title: "Open I2P Console", action: #selector(self.handleOpenConsole(_:)), keyEquivalent: "O"))
35    menu.addItem(NSMenuItem.separator())
36    //menu.addItem(updateMenuItem)
37    menu.addItem(NSMenuItem.separator())
38    menu.addItem(NSMenuItem(title: "Quit I2P Launcher", action: #selector(SwiftMainDelegate.terminate(_:)), keyEquivalent: "q"))
39   
40    return menu
41  }
42 
43  func pidReaction(information:Any?){
44    let pidStr = information as! String
45    NSLog("PID! %@", pidStr)
46    showPopover(sender: nil)
47    //self.ctrl?.getRouterStatusView()?.handlerRouterStart(information: pidStr)
48    self.ctrl?.getRouterStatusView()?.needsDisplay = true
49  }
50
51 
52  override init() {
53    super.init()
54    self.ctrl = PopoverViewController.freshController()
55    popover.contentViewController = self.ctrl
56    //updateObjectRef = SUUpdater.shared()
57    //updateObjectRef?.checkForUpdatesInBackground()
58   RouterManager.shared().eventManager.listenTo(eventName: "router_pid", action: pidReaction)
59   
60   
61    if let button = statusItem.button {
62      button.image = NSImage(named:"StatusBarButtonImage")
63      button.toolTip = "I2P Launch Manager"
64      button.target = self
65      button.action = #selector(self.statusBarButtonClicked(sender:))
66      button.sendAction(on: [.leftMouseUp, .rightMouseUp])
67    }
68  }
69 
70  @IBAction func openConsoleClicked(_ sender: Any) {
71    NSLog("openConsoleClicked got clicked")
72    let realSender = sender as! NSMenuItem
73    NSWorkspace.shared().open(URL(string: "http://127.0.0.1:7657")!)
74    NSLog("Sender: @%", realSender)
75  }
76 
77  @IBAction func quitClicked(_ sender: NSMenuItem) {
78    NSApplication.shared().terminate(self)
79  }
80 
81  // Submenu
82 
83  @IBAction func startRouterClicked(_ sender: NSMenuItem) {
84   
85  }
86 
87  @IBAction func restartRouterClicked(_ sender: NSMenuItem) {
88   
89  }
90 
91  @IBAction func stopRouterClicked(_ sender: NSMenuItem) {
92   
93  }
94 
95  func statusBarButtonClicked(sender: NSStatusBarButton) {
96    let event = NSApp.currentEvent!
97   
98    if event.type == NSEventType.rightMouseUp {
99      closePopover(sender: nil)
100     
101      let ctxMenu = constructMenu()
102     
103      statusItem.menu = ctxMenu
104      statusItem.popUpMenu(ctxMenu)
105     
106      // This is critical, otherwise clicks won't be processed again
107      statusItem.menu = nil
108    } else {
109      togglePopover(sender: nil)
110    }
111  }
112 
113  func togglePopover(sender: AnyObject?) {
114    if popover.isShown {
115      closePopover(sender: sender)
116    } else {
117      showPopover(sender: sender)
118    }
119  }
120 
121  func showPopover(sender: AnyObject?) {
122    if let button = statusItem.button {
123      let inst = RouterStatusView.getInstance()
124      if (inst != nil) {
125        if (inst != Optional.none) { RouterStatusView.getInstance()?.setRouterStatusLabelText() }
126      }
127      popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
128    }
129  }
130 
131  func closePopover(sender: AnyObject?) {
132    popover.performClose(sender)
133  }
134 
135 
136}
137
Note: See TracBrowser for help on using the repository browser.