Skip to content

Commit

Permalink
Improve job control
Browse files Browse the repository at this point in the history
  • Loading branch information
richinfante committed Feb 16, 2019
1 parent 3112cf4 commit f4dde79
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 20 deletions.
4 changes: 4 additions & 0 deletions shbar2.xcodeproj/project.pbxproj
Expand Up @@ -350,6 +350,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "Mac Developer";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = 7A264T7MNQ;
Expand All @@ -360,6 +361,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.richinfante.shbar2;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 4.2;
};
name = Debug;
Expand All @@ -368,6 +370,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "Mac Developer";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = 7A264T7MNQ;
Expand All @@ -378,6 +381,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.richinfante.shbar2;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 4.2;
};
name = Release;
Expand Down
77 changes: 72 additions & 5 deletions shbar2/AppDelegate.swift
Expand Up @@ -82,6 +82,13 @@ class ItemConfig : Codable {
var jobStatusItem: NSMenuItem?
var jobExitStatus: Int32?
var currentJob: Process?
var isPaused : Bool = false

var startMenuItem: NSMenuItem?
var stopMenuItem: NSMenuItem?
var restartMenuItem: NSMenuItem?
var suspendMenuItem: NSMenuItem?
var resumeMenuItem: NSMenuItem?

private enum CodingKeys: String, CodingKey {
case mode
Expand Down Expand Up @@ -117,6 +124,28 @@ class ItemConfig : Codable {
self.autostartJob = autostartJob
}

@objc func suspendJob() {
if let process = self.currentJob {
process.suspend()
self.isPaused = true

if let title = self.title {
self.updateTitle(title: title)
}
}
}

@objc func resumeJob() {
if let process = self.currentJob {
process.resume()
self.isPaused = false

if let title = self.title {
self.updateTitle(title: title)
}
}
}

@objc func startJob() {
self.currentJob?.terminate()

Expand All @@ -129,12 +158,9 @@ class ItemConfig : Codable {
if let title = self.title {
self.updateTitle(title: title)
}
self.jobStatusItem?.title = "running - pid:\(process.processIdentifier)"
}, completed: {
status, result in

self.jobStatusItem?.title = "exited: \(status)"


if let title = self.title {
self.updateTitle(title: title)
}
Expand All @@ -157,11 +183,36 @@ class ItemConfig : Codable {

// Update job's status label
if let currentJob = self.currentJob {
if currentJob.isRunning {
if self.isPaused {
color = NSColor.blue
} else if currentJob.isRunning {
color = NSColor.green
} else if currentJob.terminationStatus != 0 {
color = NSColor.red
}

if self.isPaused {
// self.startMenuItem?.isHidden = true
// self.stopMenuItem?.isHidden = false
// self.restartMenuItem?.isHidden = false
// self.suspendMenuItem?.isHidden = true
// self.resumeMenuItem?.isHidden = false
self.jobStatusItem?.title = "suspended - pid:\(currentJob.processIdentifier)"
} else if currentJob.isRunning {
// self.startMenuItem?.isHidden = true
// self.stopMenuItem?.isHidden = false
// self.restartMenuItem?.isHidden = false
// self.suspendMenuItem?.isHidden = false
// self.resumeMenuItem?.isHidden = true
self.jobStatusItem?.title = "running - pid:\(currentJob.processIdentifier)"
} else {
// self.startMenuItem?.isHidden = false
// self.stopMenuItem?.isHidden = true
// self.restartMenuItem?.isHidden = true
// self.suspendMenuItem?.isHidden = true
// self.resumeMenuItem?.isHidden = false
self.jobStatusItem?.title = "exited: \(currentJob.terminationStatus)"
}
}

let mutableAttributedString = NSMutableAttributedString(string: "", attributes: [
Expand Down Expand Up @@ -195,10 +246,26 @@ class ItemConfig : Codable {
restartItem.action = #selector(ItemConfig.startJob)
restartItem.target = self

let suspendItem = NSMenuItem(title: "suspend", action: nil, keyEquivalent: "")
suspendItem.action = #selector(ItemConfig.suspendJob)
suspendItem.target = self

let resumeItem = NSMenuItem(title: "resume", action: nil, keyEquivalent: "")
resumeItem.action = #selector(ItemConfig.resumeJob)
resumeItem.target = self

subMenu.addItem(statusItem)
subMenu.addItem(startItem)
subMenu.addItem(stopItem)
subMenu.addItem(restartItem)
subMenu.addItem(suspendItem)
subMenu.addItem(resumeItem)

self.startMenuItem = startItem
self.stopMenuItem = stopItem
self.restartMenuItem = restartItem
self.suspendMenuItem = suspendItem
self.resumeMenuItem = resumeItem

menuItem.submenu = subMenu

Expand Down
14 changes: 0 additions & 14 deletions shbar2/Base.lproj/Main.storyboard
Expand Up @@ -2,7 +2,6 @@
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14460.31"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Application-->
Expand Down Expand Up @@ -680,18 +679,5 @@
</objects>
<point key="canvasLocation" x="75" y="0.0"/>
</scene>
<!--View Controller-->
<scene sceneID="hIz-AP-VOD">
<objects>
<viewController id="XfG-lQ-9wD" customClass="ViewController" customModule="shbar2" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" wantsLayer="YES" id="m2S-Jp-Qdl">
<rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
<autoresizingMask key="autoresizingMask"/>
</view>
</viewController>
<customObject id="rPt-NT-nkU" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="75" y="655"/>
</scene>
</scenes>
</document>
2 changes: 1 addition & 1 deletion shbar2/Info.plist
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>0.0.1</string>
<key>CFBundleVersion</key>
<string>2</string>
<string>3</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.developer-tools</string>
<key>LSMinimumSystemVersion</key>
Expand Down

0 comments on commit f4dde79

Please sign in to comment.