[kit] WIP: Getting preference window to work
Rudimentary loading of colors from user defaults
This commit is contained in:
		
							parent
							
								
									4af251d1c8
								
							
						
					
					
						commit
						2ab34d99ea
					
				
					 3 changed files with 168 additions and 6 deletions
				
			
		| 
						 | 
				
			
			@ -7,6 +7,7 @@
 | 
			
		|||
	objects = {
 | 
			
		||||
 | 
			
		||||
/* Begin PBXBuildFile section */
 | 
			
		||||
		C09161701F3F5AE6009C4263 /* PreferencesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C091616F1F3F5AE6009C4263 /* PreferencesViewController.swift */; };
 | 
			
		||||
		C0BBE36B1F2E816500E68524 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0BBE36A1F2E816500E68524 /* AppDelegate.swift */; };
 | 
			
		||||
		C0BBE36D1F2E816500E68524 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0BBE36C1F2E816500E68524 /* ViewController.swift */; };
 | 
			
		||||
		C0BBE36F1F2E816500E68524 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C0BBE36E1F2E816500E68524 /* Assets.xcassets */; };
 | 
			
		||||
| 
						 | 
				
			
			@ -43,6 +44,7 @@
 | 
			
		|||
/* End PBXContainerItemProxy section */
 | 
			
		||||
 | 
			
		||||
/* Begin PBXFileReference section */
 | 
			
		||||
		C091616F1F3F5AE6009C4263 /* PreferencesViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferencesViewController.swift; sourceTree = "<group>"; };
 | 
			
		||||
		C0BBE3671F2E816500E68524 /* Metaballs.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Metaballs.app; sourceTree = BUILT_PRODUCTS_DIR; };
 | 
			
		||||
		C0BBE36A1F2E816500E68524 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
 | 
			
		||||
		C0BBE36C1F2E816500E68524 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
 | 
			
		||||
| 
						 | 
				
			
			@ -156,6 +158,7 @@
 | 
			
		|||
				C0BBE3A31F2E81C700E68524 /* Metaballs.swift */,
 | 
			
		||||
				C0CE7BFF1F362C3F001516B6 /* Geometry.swift */,
 | 
			
		||||
				C0DF1D781F3789DC0038B0A0 /* Memory.swift */,
 | 
			
		||||
				C091616F1F3F5AE6009C4263 /* PreferencesViewController.swift */,
 | 
			
		||||
			);
 | 
			
		||||
			path = MetaballsKit;
 | 
			
		||||
			sourceTree = "<group>";
 | 
			
		||||
| 
						 | 
				
			
			@ -377,6 +380,7 @@
 | 
			
		|||
				C0DF1D791F3789DC0038B0A0 /* Memory.swift in Sources */,
 | 
			
		||||
				C0BBE3A41F2E81C700E68524 /* Metaballs.swift in Sources */,
 | 
			
		||||
				C0CE7C001F362C3F001516B6 /* Geometry.swift in Sources */,
 | 
			
		||||
				C09161701F3F5AE6009C4263 /* PreferencesViewController.swift in Sources */,
 | 
			
		||||
			);
 | 
			
		||||
			runOnlyForDeploymentPostprocessing = 0;
 | 
			
		||||
		};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,9 @@
 | 
			
		|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
 | 
			
		||||
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="11134" systemVersion="15F34" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
			
		||||
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="12121" systemVersion="16F73" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
 | 
			
		||||
    <dependencies>
 | 
			
		||||
        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11134"/>
 | 
			
		||||
        <deployment identifier="macosx"/>
 | 
			
		||||
        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="12121"/>
 | 
			
		||||
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
 | 
			
		||||
    </dependencies>
 | 
			
		||||
    <scenes>
 | 
			
		||||
        <!--Application-->
 | 
			
		||||
| 
						 | 
				
			
			@ -21,7 +23,11 @@
 | 
			
		|||
                                            </connections>
 | 
			
		||||
                                        </menuItem>
 | 
			
		||||
                                        <menuItem isSeparatorItem="YES" id="VOq-y0-SEH"/>
 | 
			
		||||
                                        <menuItem title="Preferences…" keyEquivalent="," id="BOF-NM-1cW"/>
 | 
			
		||||
                                        <menuItem title="Preferences…" keyEquivalent="," id="BOF-NM-1cW">
 | 
			
		||||
                                            <connections>
 | 
			
		||||
                                                <segue destination="UFt-7I-hit" kind="show" id="5lj-Ty-4XS"/>
 | 
			
		||||
                                            </connections>
 | 
			
		||||
                                        </menuItem>
 | 
			
		||||
                                        <menuItem isSeparatorItem="YES" id="wFC-TO-SCJ"/>
 | 
			
		||||
                                        <menuItem title="Services" id="NMo-om-nkz">
 | 
			
		||||
                                            <modifierMask key="keyEquivalentModifierMask"/>
 | 
			
		||||
| 
						 | 
				
			
			@ -653,10 +659,10 @@
 | 
			
		|||
                        <outlet property="delegate" destination="Voe-Tx-rLC" id="PrD-fu-P6m"/>
 | 
			
		||||
                    </connections>
 | 
			
		||||
                </application>
 | 
			
		||||
                <customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModuleProvider="target"/>
 | 
			
		||||
                <customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Metaballs" customModuleProvider="target"/>
 | 
			
		||||
                <customObject id="Ady-hI-5gd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
 | 
			
		||||
            </objects>
 | 
			
		||||
            <point key="canvasLocation" x="75" y="0.0"/>
 | 
			
		||||
            <point key="canvasLocation" x="75" y="-102"/>
 | 
			
		||||
        </scene>
 | 
			
		||||
        <!--Window Controller-->
 | 
			
		||||
        <scene sceneID="R2V-B0-nI4">
 | 
			
		||||
| 
						 | 
				
			
			@ -676,6 +682,37 @@
 | 
			
		|||
            </objects>
 | 
			
		||||
            <point key="canvasLocation" x="75" y="250"/>
 | 
			
		||||
        </scene>
 | 
			
		||||
        <!--Window Controller-->
 | 
			
		||||
        <scene sceneID="wc9-ue-Eil">
 | 
			
		||||
            <objects>
 | 
			
		||||
                <windowController id="UFt-7I-hit" sceneMemberID="viewController">
 | 
			
		||||
                    <window key="window" title="Preferences" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" showsToolbarButton="NO" visibleAtLaunch="NO" frameAutosaveName="" animationBehavior="default" id="5zN-Bj-JVR" userLabel="Preferences">
 | 
			
		||||
                        <windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
 | 
			
		||||
                        <windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
 | 
			
		||||
                        <rect key="contentRect" x="196" y="240" width="480" height="270"/>
 | 
			
		||||
                        <rect key="screenRect" x="0.0" y="0.0" width="1440" height="878"/>
 | 
			
		||||
                    </window>
 | 
			
		||||
                    <connections>
 | 
			
		||||
                        <segue destination="9PC-bt-7aN" kind="relationship" relationship="window.shadowedContentViewController" id="b5v-pe-Pgt"/>
 | 
			
		||||
                    </connections>
 | 
			
		||||
                </windowController>
 | 
			
		||||
                <customObject id="qHK-Gc-Sjq" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
 | 
			
		||||
            </objects>
 | 
			
		||||
            <point key="canvasLocation" x="657" y="250"/>
 | 
			
		||||
        </scene>
 | 
			
		||||
        <!--Preferences View Controller-->
 | 
			
		||||
        <scene sceneID="Wgz-m9-9q7">
 | 
			
		||||
            <objects>
 | 
			
		||||
                <viewController id="9PC-bt-7aN" customClass="PreferencesViewController" customModule="MetaballsKit" sceneMemberID="viewController">
 | 
			
		||||
                    <view key="view" id="N8a-Ce-Lm4">
 | 
			
		||||
                        <rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
 | 
			
		||||
                        <autoresizingMask key="autoresizingMask"/>
 | 
			
		||||
                    </view>
 | 
			
		||||
                </viewController>
 | 
			
		||||
                <customObject id="HYe-ek-cRs" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
 | 
			
		||||
            </objects>
 | 
			
		||||
            <point key="canvasLocation" x="657" y="655"/>
 | 
			
		||||
        </scene>
 | 
			
		||||
        <!--View Controller-->
 | 
			
		||||
        <scene sceneID="hIz-AP-VOD">
 | 
			
		||||
            <objects>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										121
									
								
								MetaballsKit/PreferencesViewController.swift
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										121
									
								
								MetaballsKit/PreferencesViewController.swift
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,121 @@
 | 
			
		|||
//
 | 
			
		||||
//  PreferencesViewController.swift
 | 
			
		||||
//  Metaballs
 | 
			
		||||
//
 | 
			
		||||
//  Created by Eryn Wells on 8/12/17.
 | 
			
		||||
//  Copyright © 2017 Eryn Wells. All rights reserved.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
import Cocoa
 | 
			
		||||
 | 
			
		||||
class PreferencesViewController: NSViewController {
 | 
			
		||||
    public var defaults = UserDefaults.standard
 | 
			
		||||
 | 
			
		||||
    private var colorStackView = NSStackView()
 | 
			
		||||
    private var colorViews = [ColorView]()
 | 
			
		||||
 | 
			
		||||
    private lazy var styleMenu: NSPopUpButton = {
 | 
			
		||||
        let button = NSPopUpButton()
 | 
			
		||||
        button.translatesAutoresizingMaskIntoConstraints = false
 | 
			
		||||
 | 
			
		||||
        let menu = NSMenu()
 | 
			
		||||
        menu.addItem(withTitle: NSLocalizedString("Single Color", comment: "single color menu item"), action: nil, keyEquivalent: "")
 | 
			
		||||
        menu.addItem(withTitle: NSLocalizedString("Two Color Gradient — Horizontal", comment: "two color horizontal gradient menu item"), action: nil, keyEquivalent: "")
 | 
			
		||||
        button.menu = menu
 | 
			
		||||
 | 
			
		||||
        return button
 | 
			
		||||
    }()
 | 
			
		||||
 | 
			
		||||
    override func loadView() {
 | 
			
		||||
        let view = NSView()
 | 
			
		||||
        view.translatesAutoresizingMaskIntoConstraints = false
 | 
			
		||||
 | 
			
		||||
        colorStackView.setAccessibilityIdentifier("colorStackView")
 | 
			
		||||
        colorStackView.translatesAutoresizingMaskIntoConstraints = false
 | 
			
		||||
        colorStackView.orientation = .vertical
 | 
			
		||||
        colorStackView.alignment = .left
 | 
			
		||||
        colorStackView.distribution = .fillProportionally
 | 
			
		||||
        colorStackView.spacing = 8
 | 
			
		||||
        view.addSubview(colorStackView)
 | 
			
		||||
 | 
			
		||||
        let centerX = colorStackView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
 | 
			
		||||
        centerX.priority = 999
 | 
			
		||||
        let centerY = colorStackView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
 | 
			
		||||
        centerY.priority = 999
 | 
			
		||||
        NSLayoutConstraint.activate([
 | 
			
		||||
            centerX, centerY,
 | 
			
		||||
            colorStackView.topAnchor.constraint(greaterThanOrEqualTo: view.topAnchor, constant: 8),
 | 
			
		||||
            colorStackView.leftAnchor.constraint(greaterThanOrEqualTo: view.leftAnchor, constant: 8),
 | 
			
		||||
            colorStackView.bottomAnchor.constraint(lessThanOrEqualTo: view.bottomAnchor, constant: -8),
 | 
			
		||||
            colorStackView.rightAnchor.constraint(lessThanOrEqualTo: view.rightAnchor, constant: -8),
 | 
			
		||||
        ])
 | 
			
		||||
 | 
			
		||||
        colorStackView.addArrangedSubview(styleMenu)
 | 
			
		||||
        for i in 0..<4 {
 | 
			
		||||
            let colorView = ColorView()
 | 
			
		||||
            colorView.translatesAutoresizingMaskIntoConstraints = false
 | 
			
		||||
            colorView.label.stringValue = "Color \(i+1)"
 | 
			
		||||
            colorStackView.addArrangedSubview(colorView)
 | 
			
		||||
            colorViews.append(colorView)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        self.view = view
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    override func viewWillAppear() {
 | 
			
		||||
        super.viewWillAppear()
 | 
			
		||||
        prepareColorViews()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private func prepareColorViews() {
 | 
			
		||||
        guard let colors = defaults.array(forKey: "colors") else { return }
 | 
			
		||||
        for (idx, cv) in colorViews.enumerated() {
 | 
			
		||||
            if idx >= colors.count {
 | 
			
		||||
                continue
 | 
			
		||||
            }
 | 
			
		||||
            if let color = colors[idx] as? NSColor {
 | 
			
		||||
                cv.colorWell.color = color
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class ColorView: NSView {
 | 
			
		||||
    private let stackView = NSStackView()
 | 
			
		||||
    internal let colorWell = NSColorWell()
 | 
			
		||||
    internal let label = NSTextField(labelWithString: "Hello")
 | 
			
		||||
 | 
			
		||||
    override init(frame frameRect: NSRect) {
 | 
			
		||||
        super.init(frame: frameRect)
 | 
			
		||||
        commonInit()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    required init?(coder: NSCoder) {
 | 
			
		||||
        super.init(coder: coder)
 | 
			
		||||
        commonInit()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private func commonInit() {
 | 
			
		||||
        stackView.translatesAutoresizingMaskIntoConstraints = false
 | 
			
		||||
        stackView.orientation = .horizontal
 | 
			
		||||
        stackView.spacing = 8
 | 
			
		||||
        stackView.alignment = .centerY
 | 
			
		||||
        stackView.distribution = .equalSpacing
 | 
			
		||||
 | 
			
		||||
        colorWell.translatesAutoresizingMaskIntoConstraints = false
 | 
			
		||||
        colorWell.setContentHuggingPriority(251, for: .horizontal)
 | 
			
		||||
        stackView.addArrangedSubview(colorWell)
 | 
			
		||||
 | 
			
		||||
        label.translatesAutoresizingMaskIntoConstraints = false
 | 
			
		||||
        stackView.addArrangedSubview(label)
 | 
			
		||||
 | 
			
		||||
        addSubview(stackView)
 | 
			
		||||
        NSLayoutConstraint.activate([
 | 
			
		||||
            stackView.topAnchor.constraint(equalTo: topAnchor),
 | 
			
		||||
            stackView.leftAnchor.constraint(equalTo: leftAnchor),
 | 
			
		||||
            stackView.bottomAnchor.constraint(equalTo: bottomAnchor),
 | 
			
		||||
            stackView.rightAnchor.constraint(equalTo: rightAnchor),
 | 
			
		||||
        ])
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue