Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishiraj Sharma authored and Rishiraj Sharma committed Feb 8, 2018
1 parent 5bc555c commit 6d553e8
Show file tree
Hide file tree
Showing 53 changed files with 2,710 additions and 0 deletions.
416 changes: 416 additions & 0 deletions Swiftness.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 96 additions & 0 deletions Swiftness/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

// MARK: - Properties
let templateManager = TemplateManager()

private lazy var statusItem: NSStatusItem = {
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
statusItem.button?.action = #selector(togglePopover)
return statusItem
}()

private lazy var popover: NSPopover = {
let popover = NSPopover()
let statusBarViewController = NSStoryboard.statusBarViewController()
statusBarViewController?.delegate = self
popover.contentViewController = statusBarViewController
popover.behavior = .transient
return popover
}()

// When the popover is shown, click outside will close the popover
private lazy var eventMonitor: EventMonitor = {
let eventMonitor = EventMonitor(mask: [.leftMouseDown, .rightMouseDown] ) { [unowned self] event in
guard event?.window != self.popover.contentViewController?.view.window else { return }
if self.popover.isShown {
self.closePopover()
}
}
return eventMonitor
}()

// MARK: - Overrides
func applicationDidFinishLaunching(_ aNotification: Notification) {
hideMainWindow()
statusItem.button?.image = NSImage(named: NSImage.Name(rawValue: "link"))
}

}

// MARK: - MainWindowDelegate
extension AppDelegate: MainWindowDelegate {

func hideMainWindow() {
NSApp.hide(nil)
NSApp.setActivationPolicy(.accessory)
}

func showMainWindow() {
if NSApp.isHidden {
NSApp.setActivationPolicy(.regular)
}
closePopover()
NSApp.activate(ignoringOtherApps: true)
}

}

// MARK: - Public methods
extension AppDelegate {

@objc func togglePopover() {
if popover.isShown {
closePopover()
} else {
showPopover()
}
}

}

// MARK: - Private methods
private extension AppDelegate {

// MARK: Popover
func showPopover() {
guard let statusItemButton = statusItem.button else { return }

(popover.contentViewController as? StatusBarViewController)?.templateManager = templateManager

if !NSApp.isHidden {
NSApp.activate(ignoringOtherApps: true)
}

popover.show(relativeTo: .zero, of: statusItemButton, preferredEdge: NSRectEdge.minY)
eventMonitor.start()
}

func closePopover() {
eventMonitor.stop()
popover.performClose(self)
}

}
1,345 changes: 1,345 additions & 0 deletions Swiftness/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions Swiftness/CustomViews/EditorTableView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Cocoa

class EditorTableView: NSTableView {

override func menu(for event: NSEvent) -> NSMenu? {
let mouseLocation = event.locationInWindow

let viewLocation = convert(mouseLocation, from: nil)
let row = self.row(at: viewLocation)
return row == -1 ? nil : super.menu(for: event)
}

}
9 changes: 9 additions & 0 deletions Swiftness/CustomViews/EditorTemplateCellView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Cocoa

class EditorTemplateCellView: NSTableCellView {

static let identifier = NSUserInterfaceItemIdentifier("EditorTemplateCellView")
@IBOutlet weak var titleTextField: NSTextField!

}

21 changes: 21 additions & 0 deletions Swiftness/CustomViews/HoverButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Cocoa

class HoverButton: NSButton {

private var trackingArea: NSTrackingArea?

open override func cursorUpdate(with event: NSEvent) {
NSCursor.pointingHand.set()
}

override open func updateTrackingAreas() {
if let trackingArea = self.trackingArea {
self.removeTrackingArea(trackingArea)
}

let trackingOptions: NSTrackingArea.Options = [.cursorUpdate, .activeAlways]
trackingArea = NSTrackingArea(rect: bounds, options: trackingOptions, owner: self, userInfo: nil)
addTrackingArea(trackingArea!)
}

}
13 changes: 13 additions & 0 deletions Swiftness/CustomViews/RowView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Cocoa

class RowView: NSTableRowView {

override func drawSelection(in dirtyRect: NSRect) {
isEmphasized ? NSColor.white.set() : NSColor.white.set()
dirtyRect.fill()
}
override var isEmphasized: Bool {
set {}
get { return false }
}
}
13 changes: 13 additions & 0 deletions Swiftness/CustomViews/StatusBarTemplateCellView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Cocoa

class StatusBarTemplateCellView: NSTableCellView {

static let identifier = NSUserInterfaceItemIdentifier("StatusBarTemplateCellView")

@IBOutlet weak var titleTextField: NSTextField!
@IBOutlet weak var copyButton: NSButton!

func setSelected(selected: Bool) {
layer?.backgroundColor = selected ? CGColor.clear : CGColor.white
}
}
29 changes: 29 additions & 0 deletions Swiftness/EventMonitor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Cocoa

public class EventMonitor {

private var monitor: AnyObject?
private let mask: NSEvent.EventTypeMask
private let handler: (NSEvent?) -> Void

public init(mask: NSEvent.EventTypeMask, handler: @escaping (NSEvent?) -> Void) {
self.mask = mask
self.handler = handler
}

deinit {
stop()
}

public func start() {
monitor = NSEvent.addGlobalMonitorForEvents(matching: mask, handler: handler) as AnyObject?
}

public func stop() {
if monitor != nil {
NSEvent.removeMonitor(monitor!)
monitor = nil
}
}
}

15 changes: 15 additions & 0 deletions Swiftness/Protocols&Extensions/Copyable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Cocoa

protocol Copyable {
func copyToPasteboard(_ string: NSAttributedString)
}

extension Copyable {
func copyToPasteboard(_ string: NSAttributedString) {
let pasteboard = NSPasteboard.general
let documentAttributes: [NSAttributedString.DocumentAttributeKey: Any] = [.documentType: NSAttributedString.DocumentType.rtf]
let data = try! string.data(from: NSRange(location: 0, length: string.length), documentAttributes: documentAttributes)
pasteboard.clearContents()
pasteboard.setData(data, forType: .rtf)
}
}
6 changes: 6 additions & 0 deletions Swiftness/Protocols&Extensions/MainWindowDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation

protocol MainWindowDelegate: class {
func showMainWindow()
func hideMainWindow()
}
16 changes: 16 additions & 0 deletions Swiftness/Protocols&Extensions/NSButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Cocoa

extension NSButton {

@IBInspectable open var textColor: NSColor? {
get {
return attributedTitle.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? NSColor
}
set {
var attributes = attributedTitle.attributes(at: 0, effectiveRange: nil)
attributes[.foregroundColor] = newValue ?? NSColor.black
attributedTitle = NSMutableAttributedString(string: self.title, attributes: attributes)
}
}

}
22 changes: 22 additions & 0 deletions Swiftness/Protocols&Extensions/NSStoryboard.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Cocoa

extension NSStoryboard {

class func statusBarViewController() -> StatusBarViewController? {
return load(identifier: "StatusBarViewController") as? StatusBarViewController
}

class func editorWindowController() -> EditorWindowController? {
return load(identifier: "EditorWindowController") as? EditorWindowController
}

}

private extension NSStoryboard {

static func load(identifier: String) -> Any {
let storyboard = NSStoryboard(name: Name("Main"), bundle: nil)
return storyboard.instantiateController(withIdentifier: SceneIdentifier(identifier))
}

}
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.
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.
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.
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.
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"images" : [
{
"size" : "16x16",
"idiom" : "mac",
"filename" : "16px.png",
"scale" : "1x"
},
{
"size" : "16x16",
"idiom" : "mac",
"filename" : "16px@2x.png",
"scale" : "2x"
},
{
"size" : "32x32",
"idiom" : "mac",
"filename" : "32px.png",
"scale" : "1x"
},
{
"size" : "32x32",
"idiom" : "mac",
"filename" : "32px@2x.png",
"scale" : "2x"
},
{
"size" : "128x128",
"idiom" : "mac",
"filename" : "128px.png",
"scale" : "1x"
},
{
"size" : "128x128",
"idiom" : "mac",
"filename" : "128px@2x.png",
"scale" : "2x"
},
{
"size" : "256x256",
"idiom" : "mac",
"filename" : "256px.png",
"scale" : "1x"
},
{
"size" : "256x256",
"idiom" : "mac",
"filename" : "256px@2x.png",
"scale" : "2x"
},
{
"size" : "512x512",
"idiom" : "mac",
"filename" : "512px-1.png",
"scale" : "1x"
},
{
"size" : "512x512",
"idiom" : "mac",
"filename" : "512px@2x.png",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
6 changes: 6 additions & 0 deletions Swiftness/Resources/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "Copy Icon.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "Copy Icon@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
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.
22 changes: 22 additions & 0 deletions Swiftness/Resources/Assets.xcassets/Search.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "Search.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "Search@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
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.
Loading

0 comments on commit 6d553e8

Please sign in to comment.