Skip to content

Commit

Permalink
Merge pull request #87 from zenangst/feature/list-mode
Browse files Browse the repository at this point in the history
Feature list mode
  • Loading branch information
zenangst authored Feb 11, 2019
2 parents 0d2c3f7 + 4c186e9 commit 57bda84
Show file tree
Hide file tree
Showing 23 changed files with 680 additions and 140 deletions.
28 changes: 28 additions & 0 deletions Gray.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
BDB20F2B2167A9DB00A72623 /* SearchField.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDB20F2A2167A9DB00A72623 /* SearchField.swift */; };
BDBFB09421D0DDB20086F697 /* ApplicationsLoadingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDBFB09321D0DDB20086F697 /* ApplicationsLoadingViewController.swift */; };
BDC14C6F21B4502900610983 /* AlertsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDC14C6E21B4502900610983 /* AlertsController.swift */; };
BDE5736022105FDF0065597B /* ApplicationListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDE5735F22105FDF0065597B /* ApplicationListView.swift */; };
BDE57364221093440065597B /* Component.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDE57363221093440065597B /* Component.swift */; };
BDE57366221098170065597B /* ViewToolbarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDE57365221098170065597B /* ViewToolbarItem.swift */; };
BDE573682210A2CB0065597B /* AppearanceAware.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDE573672210A2CB0065597B /* AppearanceAware.swift */; };
BDE5736D2210A9850065597B /* UserDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDE5736C2210A9850065597B /* UserDefaults.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -86,6 +91,11 @@
BDB20F2A2167A9DB00A72623 /* SearchField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchField.swift; sourceTree = "<group>"; };
BDBFB09321D0DDB20086F697 /* ApplicationsLoadingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApplicationsLoadingViewController.swift; sourceTree = "<group>"; };
BDC14C6E21B4502900610983 /* AlertsController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertsController.swift; sourceTree = "<group>"; };
BDE5735F22105FDF0065597B /* ApplicationListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApplicationListView.swift; sourceTree = "<group>"; };
BDE57363221093440065597B /* Component.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Component.swift; sourceTree = "<group>"; };
BDE57365221098170065597B /* ViewToolbarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewToolbarItem.swift; sourceTree = "<group>"; };
BDE573672210A2CB0065597B /* AppearanceAware.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppearanceAware.swift; sourceTree = "<group>"; };
BDE5736C2210A9850065597B /* UserDefaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserDefaults.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -122,6 +132,7 @@
children = (
BDC14C6D21B4501B00610983 /* Alerts */,
BD30ED4C216C011F00FD2D91 /* Application */,
BDE5736B2210A9780065597B /* Extensions */,
BD30ED4B216C010400FD2D91 /* Features */,
BDAAC490216A2A810093B27D /* Shared */,
BD6F243621620A570032338F /* Utilities */,
Expand Down Expand Up @@ -246,11 +257,14 @@
BD9D3D79215C2D3F00233333 /* Applications */ = {
isa = PBXGroup;
children = (
BDE573672210A2CB0065597B /* AppearanceAware.swift */,
BD750FDB215C1AC60024E70A /* Application.swift */,
BD1BA9BF215E6DBB0052633B /* ApplicationGridView.swift */,
BDE5735F22105FDF0065597B /* ApplicationListView.swift */,
BD9D3D7E215C30B000233333 /* ApplicationsFeatureViewController.swift */,
BDBFB09321D0DDB20086F697 /* ApplicationsLoadingViewController.swift */,
BD308449215C198D002A9349 /* ApplicationsLogicController.swift */,
BDE57363221093440065597B /* Component.swift */,
);
path = Applications;
sourceTree = "<group>";
Expand Down Expand Up @@ -288,6 +302,7 @@
BDB20F252167A64100A72623 /* Toolbar.swift */,
BDB20F282167A73C00A72623 /* SearchToolbarItem.swift */,
BDB20F2A2167A9DB00A72623 /* SearchField.swift */,
BDE57365221098170065597B /* ViewToolbarItem.swift */,
);
path = Toolbar;
sourceTree = "<group>";
Expand All @@ -300,6 +315,14 @@
path = Alerts;
sourceTree = "<group>";
};
BDE5736B2210A9780065597B /* Extensions */ = {
isa = PBXGroup;
children = (
BDE5736C2210A9850065597B /* UserDefaults.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -450,6 +473,7 @@
buildActionMask = 2147483647;
files = (
BD2349312207446D0047F910 /* StatefulItem.swift in Sources */,
BDE57364221093440065597B /* Component.swift in Sources */,
BDB20F2B2167A9DB00A72623 /* SearchField.swift in Sources */,
BDB20F292167A73C00A72623 /* SearchToolbarItem.swift in Sources */,
BD23492D220744330047F910 /* ViewControllerFactory-macOS.generated.swift in Sources */,
Expand All @@ -461,13 +485,17 @@
BD67CE1C215BF4FE00216FDB /* MainContainerViewController.swift in Sources */,
BD23492E220744330047F910 /* CollectionViewItemComponent-macOS.generated.swift in Sources */,
BD67CE0E215BF15F00216FDB /* AppDelegate.swift in Sources */,
BDE57366221098170065597B /* ViewToolbarItem.swift in Sources */,
BD30844A215C198D002A9349 /* ApplicationsLogicController.swift in Sources */,
BDAAC492216A2A920093B27D /* CollectionViewHeader.swift in Sources */,
BD1D9545215E435F003ABBCF /* VersionController.swift in Sources */,
BDE5736022105FDF0065597B /* ApplicationListView.swift in Sources */,
BD9F0C462166764E00608FD9 /* SystemPreference.swift in Sources */,
BDC14C6F21B4502900610983 /* AlertsController.swift in Sources */,
BDB20F262167A64100A72623 /* Toolbar.swift in Sources */,
BDE5736D2210A9850065597B /* UserDefaults.swift in Sources */,
BD9F0C4C2166773300608FD9 /* SystemPreferenceView.swift in Sources */,
BDE573682210A2CB0065597B /* AppearanceAware.swift in Sources */,
BD9F0C43216675F300608FD9 /* SystemPreferenceFeatureViewController.swift in Sources */,
BD23492F220744330047F910 /* StatefulItem-macOS.generated.swift in Sources */,
BD2349302207446D0047F910 /* CollectionViewItemComponent.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 9a4a4208e8595d7fa51cbc758521284dec9359cc

COCOAPODS: 1.6.0.rc.2
COCOAPODS: 1.6.0
17 changes: 17 additions & 0 deletions Resources/Assets.xcassets/Grid.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "Grid.pdf"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
},
"properties" : {
"template-rendering-intent" : "template",
"preserves-vector-representation" : true,
"auto-scaling" : "auto"
}
}
Binary file added Resources/Assets.xcassets/Grid.imageset/Grid.pdf
Binary file not shown.
17 changes: 17 additions & 0 deletions Resources/Assets.xcassets/List.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "List.pdf"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
},
"properties" : {
"template-rendering-intent" : "template",
"preserves-vector-representation" : true,
"auto-scaling" : "auto"
}
}
Binary file added Resources/Assets.xcassets/List.imageset/List.pdf
Binary file not shown.
14 changes: 12 additions & 2 deletions Resources/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14313.18"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14460.31"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
Expand Down Expand Up @@ -470,6 +470,16 @@
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="hB3-LF-h0Y"/>
<menuItem title="as Grid" keyEquivalent="1" id="QN9-Xa-3J4">
<connections>
<action selector="switchToGrid:" target="Voe-Tx-rLC" id="VEp-5v-aFR"/>
</connections>
</menuItem>
<menuItem title="as List" keyEquivalent="2" id="oUf-cU-ksv">
<connections>
<action selector="switchToList:" target="Voe-Tx-rLC" id="h8R-9S-Ea8"/>
</connections>
</menuItem>
<menuItem title="Show Sidebar" keyEquivalent="s" id="kIP-vf-haE">
<modifierMask key="keyEquivalentModifierMask" control="YES" option="YES" command="YES"/>
<connections>
Expand Down
16 changes: 15 additions & 1 deletion Sources/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let dependencyContainer = DependencyContainer()
let contentViewController = MainContainerViewController(iconStore: dependencyContainer)
let toolbar = Toolbar(identifier: .init("MainApplicationWindowToolbar"))
toolbar.searchDelegate = contentViewController
toolbar.toolbarDelegate = contentViewController
let windowSize = CGSize(width: 400, height: 640)
let window = NSWindow(contentViewController: contentViewController)
window.setFrameAutosaveName(NSWindow.FrameAutosaveName.init("MainApplicationWindow"))
Expand Down Expand Up @@ -64,6 +64,20 @@ class AppDelegate: NSObject, NSApplicationDelegate {

// MARK: - Actions

@IBAction func switchToGrid(_ sender: Any?) {
guard let toolbar = window?.toolbar as? Toolbar else { return }
(window?.contentViewController as? MainContainerViewController)?.toolbar(toolbar,
didChangeMode: ApplicationsFeatureViewController.Mode.grid.rawValue)
NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "featureViewControllerMode"), object: nil)
}

@IBAction func switchToList(_ sender: Any?) {
guard let toolbar = window?.toolbar as? Toolbar else { return }
(window?.contentViewController as? MainContainerViewController)?.toolbar(toolbar,
didChangeMode: ApplicationsFeatureViewController.Mode.list.rawValue)
NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "featureViewControllerMode"), object: nil)
}

@IBAction func search(_ sender: Any?) {
toolbar?.searchField?.becomeFirstResponder()
}
Expand Down
11 changes: 11 additions & 0 deletions Sources/Application/LayoutFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,15 @@ class LayoutFactory {
animator: DefaultLayoutAnimator(animation: .fade))
return layout
}

func createListLayout() -> VerticalBlueprintLayout {
let layout = VerticalBlueprintLayout(
itemsPerRow: 1.0,
height: 50,
minimumInteritemSpacing: 10,
minimumLineSpacing: 10,
sectionInset: .init(top: 0, left: 10, bottom: 20, right: 10),
animator: DefaultLayoutAnimator(animation: .fade))
return layout
}
}
23 changes: 18 additions & 5 deletions Sources/Application/Toolbar/Toolbar.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Cocoa

protocol ToolbarSearchDelegate: class {
protocol ToolbarDelegate: class {
func toolbar(_ toolbar: Toolbar, didSearchFor string: String)
func toolbar(_ toolbar: Toolbar, didChangeMode mode: String)
}

class Toolbar: NSToolbar, NSToolbarDelegate {
weak var searchDelegate: ToolbarSearchDelegate?
class Toolbar: NSToolbar, NSToolbarDelegate, ViewToolbarItemDelegate {
weak var toolbarDelegate: ToolbarDelegate?
weak var searchField: SearchField?

override init(identifier: NSToolbar.Identifier) {
Expand All @@ -19,19 +20,25 @@ class Toolbar: NSToolbar, NSToolbarDelegate {
return [
NSToolbarItem.Identifier.space,
NSToolbarItem.Identifier.flexibleSpace,
ViewToolbarItem.itemIdentifier,
SearchToolbarItem.itemIdentifier
]
}

func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
return [
ViewToolbarItem.itemIdentifier,
NSToolbarItem.Identifier.flexibleSpace,
SearchToolbarItem.itemIdentifier
SearchToolbarItem.itemIdentifier,
]
}

func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
switch itemIdentifier {
case ViewToolbarItem.itemIdentifier:
let viewToolbarItem = ViewToolbarItem()
viewToolbarItem.delegate = self
return viewToolbarItem
case SearchToolbarItem.itemIdentifier:
let searchToolbarItem = SearchToolbarItem(text: "")
searchToolbarItem.titleLabel.target = self
Expand All @@ -46,6 +53,12 @@ class Toolbar: NSToolbar, NSToolbarDelegate {
}

@objc func search(_ label: SearchField) {
searchDelegate?.toolbar(self, didSearchFor: label.stringValue)
toolbarDelegate?.toolbar(self, didSearchFor: label.stringValue)
}

// MARK: - ViewToolbarItemDelegate

func viewToolbarItem(_ toolbarItem: ViewToolbarItem, didChange mode: String) {
toolbarDelegate?.toolbar(self, didChangeMode: mode)
}
}
63 changes: 63 additions & 0 deletions Sources/Application/Toolbar/ViewToolbarItem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import Cocoa

protocol ViewToolbarItemDelegate: class {
func viewToolbarItem(_ toolbarItem: ViewToolbarItem, didChange mode: String)
}

class ViewToolbarItem: NSToolbarItem {
static var itemIdentifier: NSToolbarItem.Identifier = .init("View")

weak var delegate: ViewToolbarItemDelegate?

lazy var segmentedControl = NSSegmentedControl.init(images: ApplicationsFeatureViewController.Mode.allCases.compactMap({ $0.image }),
trackingMode: .selectOne,
target: self,
action: #selector(didChangeView(_:)))
lazy var customView = NSView()

init() {
super.init(itemIdentifier: ViewToolbarItem.itemIdentifier)
view = customView
view?.addSubview(segmentedControl)
minSize = .init(width: 80, height: 25)
maxSize = .init(width: 80, height: 25)
segmentedControl.setToolTip("Grid", forSegment: 0)
segmentedControl.setTag(0, forSegment: 0)
segmentedControl.setToolTip("List", forSegment: 1)
segmentedControl.setTag(1, forSegment: 1)
configureSegmentControl()
setupConstraints()

NotificationCenter.default.addObserver(self, selector: #selector(configureSegmentControl),
name: NSNotification.Name.init(rawValue: "featureViewControllerMode"), object: nil)
}

@objc func configureSegmentControl() {
if let mode = UserDefaults.standard.featureViewControllerMode {
switch mode {
case .grid:
segmentedControl.selectSegment(withTag: 0)
case .list:
segmentedControl.selectSegment(withTag: 1)
}
} else {
segmentedControl.selectSegment(withTag: 0)
}
}

func setupConstraints() {
segmentedControl.translatesAutoresizingMaskIntoConstraints = false
segmentedControl.centerXAnchor.constraint(equalTo: customView.centerXAnchor).isActive = true
segmentedControl.centerYAnchor.constraint(equalTo: customView.centerYAnchor).isActive = true
segmentedControl.widthAnchor.constraint(equalTo: customView.widthAnchor).isActive = true
segmentedControl.heightAnchor.constraint(equalToConstant: 25).isActive = true
}

@objc func didChangeView(_ segmentControl: NSSegmentedControl) {
guard let label = segmentedControl.toolTip(forSegment: segmentedControl.indexOfSelectedItem) else {
return
}

delegate?.viewToolbarItem(self, didChange: label)
}
}
13 changes: 13 additions & 0 deletions Sources/Extensions/UserDefaults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

extension UserDefaults {
var featureViewControllerMode: ApplicationsFeatureViewController.Mode? {
get {
let rawValue = UserDefaults.standard.string(forKey: #function) ?? ""
return ApplicationsFeatureViewController.Mode.init(rawValue: rawValue)
}
set {
UserDefaults.standard.set(newValue?.rawValue, forKey: #function)
}
}
}
Loading

0 comments on commit 57bda84

Please sign in to comment.