Skip to content

Commit

Permalink
Translate old menu-handling code to Swift
Browse files Browse the repository at this point in the history
  • Loading branch information
kristopherjohnson committed Nov 9, 2015
1 parent 3a95e8a commit 7fe5edb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
5 changes: 3 additions & 2 deletions MenubarCountdown/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
/// Indicates whether the timer can be resumed
var canResume = false

var stopwatch = Stopwatch()
var stopwatch: Stopwatch!

var statusItem: NSStatusItem!

Expand All @@ -68,7 +68,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
statusItemView = StatusItemView()
statusItemView.statusItem = statusItem
statusItemView.menu = menu
statusItemView.toolTip = NSLocalizedString("Menubar Countdown", comment: "Status Item Tooltip")
statusItemView.toolTip = NSLocalizedString("Menubar Countdown",
comment: "Status Item Tooltip")
statusItem.view = statusItemView

updateStatusItemTitle(0)
Expand Down
69 changes: 68 additions & 1 deletion MenubarCountdown/StatusItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import Cocoa
///
/// The application can cause the title to blink by setting the `isTitleBlinking` property.
///
class StatusItemView: NSView {
class StatusItemView: NSView, NSMenuDelegate {
static let IconPaddingWidth = CGFloat(3)
static let TitlePaddingWidth = CGFloat(6)
static let TitlePaddingHeight = CGFloat(3)
Expand Down Expand Up @@ -229,4 +229,71 @@ class StatusItemView: NSView {

// MARK: Mouse and menu handling

override func mouseDown(theEvent: NSEvent) {
if let menu = menu {
menu.delegate = self
if let statusItem = statusItem {
statusItem.popUpStatusItemMenu(menu)
}
else {
Log.error("statusItem property not set")
}
}
else {
Log.error("menu property not set")
}
}

override func rightMouseDown(theEvent: NSEvent) {
// Treat right-click just like left-click
mouseDown(theEvent)
}

func menuWillOpen(menu: NSMenu) {
isMenuVisible = true

// Disable animation for the following changes
// (menu highlighting needs to be instantaneous)
CATransaction.begin()
CATransaction.disableActions()

// Need to highlight the background
backgroundLayer.setNeedsDisplay()
if isTitleVisible {
// The title's color will change
titleLayer.setNeedsDisplay()
}
else {
// Hide the normal icon and show the highlighted icon
iconLayer.hidden = true
highlightIconLayer.hidden = false
}

CATransaction.commit()
}

func menuDidClose(menu: NSMenu) {
isMenuVisible = false
menu.delegate = nil

// Disable animation for the following changes
// (menu highlighting needs to be instantaneous)
CATransaction.begin()
CATransaction.disableActions()

// Unhighlight the background
backgroundLayer.setNeedsDisplay()

if isTitleVisible {
// Restore title color
titleLayer.setNeedsDisplay()
}
else {
// Show the normal icon and hide the highlighted icon
iconLayer.hidden = false
highlightIconLayer.hidden = true
}

CATransaction.commit()
}
}

0 comments on commit 7fe5edb

Please sign in to comment.