source: launchers/macosx/I2PLauncher/userinterface/PopoverViewController.swift @ 281c5f57

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

Bugfixes, improvements, more error handling and stability to the osx launcher.

  • Property mode set to 100644
File size: 3.7 KB
Line 
1//
2//  PopoverViewController.swift
3//  I2PLauncher
4//
5//  Created by Mikal Villa on 18/09/2018.
6//  Copyright © 2018 The I2P Project. All rights reserved.
7//
8
9import Cocoa
10
11class PopoverViewController: NSViewController {
12 
13  required init?(coder: NSCoder) {
14    super.init(coder: coder)
15    //super.init(nibName: "UserInterfaces", bundle: Bundle.main)!
16    //let nib = NSNib(nibNamed: "UserInterfaces", bundle: Bundle.main)
17   
18  }
19 
20 
21  override func viewDidLoad() {
22    super.viewDidLoad()
23    // Do view setup here.
24  }
25 
26 
27 
28}
29
30@objc class RouterStatusView : NSView {
31  static var instance: RouterStatusView?
32 
33  static func getInstance() -> RouterStatusView? {
34    if (self.instance != Optional.none) {
35      return RouterStatusView.instance
36    }
37    return Optional.none
38  }
39 
40  @IBOutlet var routerStatusLabel: NSTextField?
41  @IBOutlet var routerVersionLabel: NSTextField?
42  @IBOutlet var routerStartedByLabel: NSTextField?
43  @IBOutlet var routerUptimeLabel: NSTextField?
44 
45  @IBOutlet var quickControlView: NSView?
46  @IBOutlet var routerStartStopButton: NSButton?
47 
48 
49  @objc func actionBtnStartRouter(_ sender: Any?) {
50    NSLog("START ROUTER")
51    (sender as! NSButton).cell?.stringValue = "Stop Router"
52    let timeWhenStarted = Date()
53    RouterProcessStatus.routerStartedAt = timeWhenStarted
54    SwiftMainDelegate.objCBridge.startupI2PRouter(RouterProcessStatus.i2pDirectoryPath, javaBinPath: RouterProcessStatus.knownJavaBinPath!)
55  }
56  @objc func actionBtnStopRouter(_ sender: Any?) {
57    NSLog("STOP ROUTER")
58  }
59  @objc func actionBtnRestartRouter(sender: Any?) {}
60 
61  override func viewWillDraw() {
62    super.viewWillDraw()
63    if (RouterStatusView.instance == Optional.none) {
64      RouterStatusView.instance = self
65    }
66    self.setRouterStatusLabelText()
67  }
68 
69  func setRouterStatusLabelText() {
70    if (RouterProcessStatus.isRouterRunning) {
71      routerStatusLabel?.cell?.stringValue = "Router status: Running"
72      routerStartStopButton?.action = #selector(self.actionBtnStopRouter(_:))
73    } else {
74      routerStatusLabel?.cell?.stringValue = "Router status: Not running"
75      routerStartStopButton?.action = #selector(self.actionBtnStartRouter(_:))
76    }
77    routerStartStopButton?.needsDisplay = true
78    routerStartStopButton?.target = self
79    quickControlView?.needsDisplay = true
80   
81    if (RouterProcessStatus.routerVersion == Optional.none) {
82      routerVersionLabel?.cell?.stringValue = "Router version: Still unknown"
83      // trigger a read to ensure values
84      let tmp = SwiftMainDelegate()
85      tmp.findInstalledI2PVersion()
86    } else {
87      routerVersionLabel?.cell?.stringValue = "Router version: " + RouterProcessStatus.routerVersion!
88    }
89    if (RouterProcessStatus.routerStartedAt != Optional.none) {
90      routerUptimeLabel?.cell?.stringValue = "Router has runned for " + DateTimeUtils.timeAgoSinceDate(date: NSDate(date: RouterProcessStatus.routerStartedAt!), numericDates: false)
91    }
92  }
93 
94  init() {
95    let c = NSCoder()
96    super.init(coder: c)!
97    self.setRouterStatusLabelText()
98  }
99 
100  required init?(coder decoder: NSCoder) {
101    super.init(coder: decoder)
102    self.setRouterStatusLabelText()
103  }
104 
105}
106
107
108extension PopoverViewController {
109  static func freshController() -> PopoverViewController {
110    let storyboard = NSStoryboard(name: "Storyboard", bundle: Bundle.main)
111    //2.
112    let identifier = NSStoryboard.SceneIdentifier(stringLiteral: "PopoverView")
113    //3.
114    guard let viewcontroller = storyboard.instantiateController(withIdentifier: identifier as String) as? PopoverViewController else {
115      fatalError("Why cant i find PopoverViewController? - Check PopoverViewController.storyboard")
116    }
117    //let viewcontroller = PopoverViewController()
118    return viewcontroller
119  }
120}
121
Note: See TracBrowser for help on using the repository browser.