Skip to content

Commit

Permalink
Forgotten stuff and fix for big sur
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosefonseca committed Dec 22, 2020
1 parent aedc5cd commit 17676da
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 45 deletions.
Binary file modified Icons.pcvd
Binary file not shown.
22 changes: 22 additions & 0 deletions Statusfy/Images.xcassets/StatusIcon.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"images" : [
{
"filename" : "status_icon.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "status_icon@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 6 additions & 12 deletions Statusfy/SFYAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,12 @@ - (void)applicationDidFinishLaunching:(NSNotification * __unused)aNotification

- (void)setStatusItemTitle
{
NSString *trackName = [[self executeAppleScript:@"get name of current track"] stringValue];
NSString *artistName = [[self executeAppleScript:@"get artist of current track"] stringValue];

if (trackName && artistName) {
[self.customView updateWithLine1:trackName line2:artistName state:[self determinePlayerState]];
}
else {
NSImage *image = [NSImage imageNamed:@"status_icon"];
[image setTemplate:true];
self.statusItem.image = image;
self.statusItem.title = nil;
}
NSArray<NSString *> *trackArtist = [[[self executeAppleScript:@"(get name of current track) & \"\n\" & (get artist of current track)"] stringValue] componentsSeparatedByString:@"\n"];

NSString *trackName = trackArtist[0];
NSString *artistName = trackArtist[1];

[self.customView updateWithLine1:trackName line2:artistName state:[self determinePlayerState]];
}

#pragma mark - Executing AppleScript
Expand Down
88 changes: 55 additions & 33 deletions Statusfy/StatusItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ import Cocoa

@objcMembers public class StatusItemView: NSView {
var statusItem: NSStatusItem!

lazy var paragraph: NSParagraphStyle = {
var p = NSMutableParagraphStyle()
p.alignment = .center
return p
}()

lazy var attrs: [NSAttributedString.Key: Any] = {
if #available(OSX 10.10, *) {
return [
.font: NSFont.systemFont(ofSize: 10),
.foregroundColor: NSColor.labelColor,
.paragraphStyle: self.paragraph
.paragraphStyle: self.paragraph,
]
} else {
return [
Expand All @@ -35,38 +35,38 @@ import Cocoa
]
}
}()

lazy var highlightedAttrs: [NSAttributedString.Key: Any] = {
[
.font: NSFont.systemFont(ofSize: 10),
.foregroundColor: NSColor.white,
.paragraphStyle: self.paragraph
]
}()

var line1: String = "" {
willSet(newValue) {
if line1 != newValue {
self.line1attr = NSAttributedString(string: newValue, attributes: attrs)
line1attr = NSAttributedString(string: newValue, attributes: attrs)
}
}
}

var line2: String = "" {
willSet(newValue) {
if line2 != newValue {
self.line2attr = NSAttributedString(string: newValue, attributes: attrs)
line2attr = NSAttributedString(string: newValue, attributes: attrs)
}
}
}

var line1attr = NSAttributedString(string: "")

var line2attr = NSAttributedString(string: "")

var image: NSImage?
var state: PlayerState = .Hidden {

var state: PlayerState = .Other {
willSet(newState) {
if state != newState {
switch newState {
Expand All @@ -80,78 +80,100 @@ import Cocoa
}
}
}

var isHighlighted: Bool = false {
didSet {
setNeedsDisplay(bounds)
}
}

func update(line1: String, line2: String, state: PlayerState) {
if self.line1 != line1 || self.line2 != line2 || self.state != state {
var changed = false
if !line1.isEmpty, self.line1 != line1 {
self.line1 = line1
changed = true
}
if !line2.isEmpty, self.line2 != line2 {
self.line2 = line2
changed = true
}
if self.state != state {
self.state = state
changed = true
}
if changed {
update()
}
}

class func new(statusItem: NSStatusItem) -> StatusItemView {
return StatusItemView(statusItem: statusItem)
}

init(statusItem: NSStatusItem) {
let rect = CGRect(x: 0, y: 0, width: statusItem.length, height: NSStatusBar.system.thickness)
super.init(frame: rect)
self.statusItem = statusItem
statusItem.view = self
}


@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func update() {
let r1 = line1attr.boundingRect(with: NSSize(width: 1000, height: 100), options: NSString.DrawingOptions.usesLineFragmentOrigin)
let r2 = line2attr.boundingRect(with: NSSize(width: 1000, height: 100), options: NSString.DrawingOptions.usesLineFragmentOrigin)
let w = max(r1.width, r2.width)
statusItem.length = w + (image?.size.width ?? 0.0)

let w = max(ceil(r1.width), ceil(r2.width))

statusItem.length = max(w + (image?.size.width ?? 0.0), 15)
setNeedsDisplay(bounds)
}

public override func draw(_ dirtyRect: NSRect) {
statusItem.drawStatusBarBackground(in: dirtyRect, withHighlight: isHighlighted)


if image == nil, line1.isEmpty, line2.isEmpty {
let placeholder = NSImage(named: "StatusIcon")!
placeholder.isTemplate = true
let imgSize: CGFloat = 15

let vMargin = (dirtyRect.height - 15) / 2

placeholder.draw(in: NSRect(x: 0, y: vMargin, width: imgSize, height: imgSize))
return
}

if image != nil, isHighlighted, UserDefaults.standard.string(forKey: "AppleInterfaceStyle") != "Dark" {
let image = NSImage(named: "\(self.image!.name()!)-Highlighted")!
image.draw(in: NSRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
} else {
image?.draw(in: NSRect(x: 0, y: 0, width: image!.size.width, height: image!.size.height))
}

let imgWidth = image?.size.width ?? 0.0

let line1Rect = CGRect(x: imgWidth,
y: dirtyRect.height / 2,
width: dirtyRect.width - imgWidth,
height: dirtyRect.height / 2 + 1)

let line2Rect = CGRect(x: imgWidth,
y: 0,
width: line1Rect.width,
height: dirtyRect.height / 2 + 1)

if isHighlighted {
NSAttributedString(string: line1, attributes: highlightedAttrs).draw(in: line1Rect)
NSAttributedString(string: line2, attributes: highlightedAttrs).draw(in: line2Rect)
} else {
line1attr.draw(in: line1Rect)
line2attr.draw(in: line2Rect)
line1attr.draw(with: line1Rect, options: NSString.DrawingOptions.usesLineFragmentOrigin)
line2attr.draw(with: line2Rect, options: NSString.DrawingOptions.usesLineFragmentOrigin)
}
}

public override func mouseDown(with event: NSEvent) {
isHighlighted = true
statusItem.popUpMenu(statusItem.menu!)
Expand Down

0 comments on commit 17676da

Please sign in to comment.