change nstableview to view based

This commit is contained in:
yuanxue.yang 2018-10-11 16:06:59 +08:00
parent 2bf87d52dd
commit 5b5a9bd31d
3 changed files with 228 additions and 89 deletions

View File

@ -8,7 +8,7 @@
/* Begin PBXBuildFile section */
660D0E5A216E0158000C2922 /* Server.swift in Sources */ = {isa = PBXBuildFile; fileRef = 660D0E59216E0158000C2922 /* Server.swift */; };
6612ACC0216DB6DC00A8BF83 /* Config.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6612ACBF216DB6DC00A8BF83 /* Config.xib */; };
660D0E5C216EFEFA000C2922 /* Config.xib in Resources */ = {isa = PBXBuildFile; fileRef = 660D0E5B216EFEFA000C2922 /* Config.xib */; };
664EB375216C9A5E00B6AE0D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 664EB374216C9A5E00B6AE0D /* AppDelegate.swift */; };
664EB377216C9A5F00B6AE0D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 664EB376216C9A5F00B6AE0D /* Assets.xcassets */; };
664EB37A216C9A5F00B6AE0D /* Menu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 664EB378216C9A5F00B6AE0D /* Menu.xib */; };
@ -19,7 +19,7 @@
/* Begin PBXFileReference section */
660D0E59216E0158000C2922 /* Server.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Server.swift; sourceTree = "<group>"; };
6612ACBF216DB6DC00A8BF83 /* Config.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = Config.xib; sourceTree = "<group>"; };
660D0E5B216EFEFA000C2922 /* Config.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = Config.xib; sourceTree = "<group>"; };
664EB371216C9A5E00B6AE0D /* V2rayU.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = V2rayU.app; sourceTree = BUILT_PRODUCTS_DIR; };
664EB374216C9A5E00B6AE0D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
664EB376216C9A5F00B6AE0D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@ -63,7 +63,7 @@
children = (
660D0E59216E0158000C2922 /* Server.swift */,
664EB390216CA9E800B6AE0D /* Config.swift */,
6612ACBF216DB6DC00A8BF83 /* Config.xib */,
660D0E5B216EFEFA000C2922 /* Config.xib */,
664EB374216C9A5E00B6AE0D /* AppDelegate.swift */,
664EB376216C9A5F00B6AE0D /* Assets.xcassets */,
664EB378216C9A5F00B6AE0D /* Menu.xib */,
@ -134,7 +134,7 @@
buildActionMask = 2147483647;
files = (
664EB397216CB48400B6AE0D /* About.xib in Resources */,
6612ACC0216DB6DC00A8BF83 /* Config.xib in Resources */,
660D0E5C216EFEFA000C2922 /* Config.xib in Resources */,
664EB377216C9A5F00B6AE0D /* Assets.xcassets in Resources */,
664EB37A216C9A5F00B6AE0D /* Menu.xib in Resources */,
);

View File

@ -10,150 +10,162 @@ import Cocoa
import WebKit
class ConfigWindow: NSWindowController,NSTextFieldDelegate {
class ConfigWindow: NSWindowController {
let configServer = ConfigServer()
@IBAction func edit(_ sender: Any) {
print("edit")
}
override var windowNibName: String? {
return "Config" // no extension .xib here
}
@IBOutlet weak var tableView: NSTableView!
@IBOutlet weak var remark: NSTextFieldCell!
@IBOutlet weak var addRemoveButton: NSSegmentedControl!
@IBAction func addRemoveServer(_ sender: NSSegmentedCell) {
// 0 add,1 remove
let seg = addRemoveButton.indexOfSelectedItem
let appDelegate = NSApplication.shared.delegate as! AppDelegate
switch seg {
// add server config
// add server config
case 0:
// add
_ = configServer.add()
// reload data
self.tableView.reloadData()
// selected current row
self.tableView.selectRowIndexes(NSIndexSet(index: configServer.count()-1) as IndexSet, byExtendingSelection: false)
self.tableView.selectRowIndexes(NSIndexSet(index: configServer.count() - 1) as IndexSet, byExtendingSelection: false)
// refresh menu
// appDelegate.showServers(list:list)
// self.openEdit(tableView.NSTextFieldCell)
break
// delete server config
// delete server config
case 1:
// get seleted index
let idx = self.tableView.selectedRow
// remove
let list = configServer.remove(idx: idx)
// reload
self.tableView.reloadData()
// selected prev row
let cnt:Int = configServer.count()
var rowIndex:Int = idx-1
let cnt: Int = configServer.count()
var rowIndex: Int = idx - 1
if rowIndex < 0 {
rowIndex = cnt-1
rowIndex = cnt - 1
}
if rowIndex >= 0 {
self.tableView.selectRowIndexes(NSIndexSet(index: rowIndex) as IndexSet, byExtendingSelection: false)
}
// refresh menu
appDelegate.showServers(list:list)
// let serverItems : NSMenuItem = NSMenuItem()
appDelegate.showServers(list: list)
break
// unknown action
default :
// unknown action
default:
return
}
}
@IBAction func editCell(_ sender: NSTextFieldCell) {
print("open edit")
}
@IBAction func ok(_ sender: NSButton) {
// todo save
// self close
self.close()
// hide dock icon and close all opened windows
NSApp.setActivationPolicy(.accessory)
}
@IBAction func cancel(_ sender: NSButton) {
// self close
self.close()
// hide dock icon and close all opened windows
NSApp.setActivationPolicy(.accessory)
}
override func windowDidLoad() {
super.windowDidLoad()
self.tableView.delegate = self as? NSTableViewDelegate
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.reloadData()
self.tableView.action = #selector(onItemClicked)
self.tableView.doubleAction = #selector(onDoubleClicked)
// self.remark.action = #selector(onEdit)
// self.tableView.target
}
@objc private func onEdit() {
print("onEdit")
}
@objc private func onDoubleClicked() {
print("onDoubleClicked row \(tableView.clickedRow), col \(tableView.clickedColumn) clicked")
}
@objc private func onItemClicked() {
// print("row \(tableView.clickedRow), col \(tableView.clickedColumn) clicked")
@objc func onItemClicked() {
print("row \(tableView.clickedRow), col \(tableView.clickedColumn) clicked")
}
func windowWillClose(_ notification: Notification) {
// hide dock icon and close all opened windows
NSApp.setActivationPolicy(.accessory)
}
func controlTextDidChange(_ obj: Notification) {
print("controlTextDidChange")
// Get the data every time the user writes a character
}
func tableViewSelectionDidChange(_ notification: Notification) {
let tableView = notification.object as! NSTableView
if let identifier = tableView.identifier, identifier.rawValue == "remark" {
print("myTableView1")
}
let selectedRow = self.tableView.selectedRow
print("selectedRow",selectedRow)
// If the user selected a row. (When no row is selected, the index is -1)
if (selectedRow > -1) {
// let myCell = self.tableView.view(atColumn: self.tableView!.column(withIdentifier: "remark"), row: selectedRow, makeIfNecessary: true) as! NSTableCellView
//
// // Get the textField to detect and add it the delegate
// let textField = myCell.textField
// textField?.delegate = self
}
}
}
// NSTableViewDataSource
extension ConfigWindow: NSTableViewDataSource {
func numberOfRows(in tableView: NSTableView) -> Int {
return configServer.count()
}
}
// NSTableViewDelegate
extension ConfigWindow: NSTableViewDelegate {
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
// _ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let tableViewData = configServer.list()
return tableViewData[row][(tableColumn?.identifier.rawValue)!]
// get cell Identifier (name is "remark")
let cellIdentifier: NSUserInterfaceItemIdentifier = NSUserInterfaceItemIdentifier(rawValue: (tableColumn?.identifier)!.rawValue)
if let cell = tableView.makeView(withIdentifier: cellIdentifier, owner: nil) as? NSTableCellView {
// set cell val
cell.textField?.stringValue = tableViewData[row][cellIdentifier.rawValue]!
return cell
}
return nil
}
//
func tableView(_ tableView: NSTableView, didAdd rowView: NSTableRowView, forRow row: Int) {
print("addRow")
}
//
func tableView(_ tableView: NSTableView, didRemove rowView: NSTableRowView, forRow row: Int) {
print("removeRow")
}
func tableView(_ tableView: NSTableView, setObjectValue object: Any?, for tableColumn: NSTableColumn?, row: Int){
print("edit")
}
func selectionShouldChange(in tableView: NSTableView) -> Bool {
print("selectionShouldChange")
return true
}
func tableViewSelectionDidChange(_ notification: Notification) {
// print("changed")
// updateStatus()
}
}

View File

@ -5,42 +5,169 @@
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="ConfigWindowController">
<customObject id="-2" userLabel="File's Owner" customClass="ConfigWindow" customModule="V2rayU">
<connections>
<outlet property="window" destination="F0z-JX-Cv5" id="gIp-Ho-8D9"/>
<outlet property="addRemoveButton" destination="7rT-zk-KIa" id="wbT-dZ-Twc"/>
<outlet property="tableView" destination="iEj-cB-TDD" id="T94-2p-I2F"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<window title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" animationBehavior="default" id="F0z-JX-Cv5">
<window title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" animationBehavior="default" id="QvC-M9-y7g">
<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="contentRect" x="196" y="240" width="597" height="339"/>
<rect key="screenRect" x="0.0" y="0.0" width="1440" height="877"/>
<view key="contentView" wantsLayer="YES" id="se5-gp-TjO">
<rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
<view key="contentView" wantsLayer="YES" id="EiT-Mj-1SZ">
<rect key="frame" x="0.0" y="0.0" width="597" height="339"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<popUpButton verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Est-yz-g5g">
<rect key="frame" x="152" y="153" width="77" height="25"/>
<scrollView fixedFrame="YES" autohidesScrollers="YES" horizontalLineScroll="19" horizontalPageScroll="10" verticalLineScroll="19" verticalPageScroll="10" hasHorizontalScroller="NO" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Td1-fZ-rOR">
<rect key="frame" x="20" y="175" width="133" height="135"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<popUpButtonCell key="cell" type="push" title="Item 1" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="8Oc-87-5fq" id="eNa-zN-IbU">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="menu"/>
<menu key="menu" id="0A4-wx-R8r">
<items>
<menuItem title="Item 1" state="on" id="8Oc-87-5fq"/>
<menuItem title="Item 2" id="jYs-rx-sdi"/>
<menuItem title="Item 3" id="2Xg-iZ-WM4"/>
</items>
</menu>
</popUpButtonCell>
</popUpButton>
<clipView key="contentView" ambiguous="YES" copiesOnScroll="NO" id="aXS-ms-6Fb">
<rect key="frame" x="1" y="1" width="131" height="133"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" columnSelection="YES" multipleSelection="NO" autosaveColumns="NO" rowSizeStyle="automatic" viewBased="YES" id="iEj-cB-TDD">
<rect key="frame" x="0.0" y="0.0" width="131" height="133"/>
<autoresizingMask key="autoresizingMask"/>
<size key="intercellSpacing" width="3" height="2"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
<color key="gridColor" name="gridColor" catalog="System" colorSpace="catalog"/>
<tableColumns>
<tableColumn identifier="remark" width="128" minWidth="40" maxWidth="1000" id="2RG-jZ-YZ5">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
</tableHeaderCell>
<textFieldCell key="dataCell" lineBreakMode="truncatingTail" selectable="YES" editable="YES" title="Text Cell" id="olp-Xc-GTr">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
<prototypeCellViews>
<tableCellView id="Z47-0P-O6M">
<rect key="frame" x="1" y="1" width="128" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="pJd-jq-gtc">
<rect key="frame" x="0.0" y="0.0" width="128" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" title="Table View Cell" id="94f-0y-Glm">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<connections>
<binding destination="Z47-0P-O6M" name="alignment" keyPath="objectValue" id="MZB-1A-oRm"/>
<binding destination="Z47-0P-O6M" name="editable" keyPath="objectValue" id="yHm-tR-CJY"/>
</connections>
</textField>
</subviews>
<connections>
<outlet property="textField" destination="pJd-jq-gtc" id="dZY-hP-84i"/>
</connections>
</tableCellView>
</prototypeCellViews>
</tableColumn>
</tableColumns>
</tableView>
</subviews>
</clipView>
<scroller key="horizontalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="YES" id="CcG-vA-eQv">
<rect key="frame" x="-100" y="-100" width="223" height="15"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
<scroller key="verticalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="NO" id="aeH-wA-qiH">
<rect key="frame" x="224" y="17" width="15" height="102"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
</scrollView>
<segmentedControl verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="7rT-zk-KIa">
<rect key="frame" x="20" y="154" width="133" height="23"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<segmentedCell key="cell" borderStyle="border" alignment="left" style="smallSquare" trackingMode="selectOne" id="i7b-XT-FIs">
<font key="font" metaFont="system"/>
<segments>
<segment image="NSAddTemplate" width="32"/>
<segment image="NSRemoveTemplate" width="32" tag="1"/>
<segment width="65"/>
</segments>
<connections>
<action selector="addRemoveServer:" target="-2" id="mfS-2o-yqj"/>
</connections>
</segmentedCell>
</segmentedControl>
<button toolTip="Discard all the changes" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="2O3-hb-VOR">
<rect key="frame" x="178" y="24" width="135" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="Cancel" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="0xi-jg-IkE">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
<string key="keyEquivalent" base64-UTF8="YES">
Gw
</string>
</buttonCell>
<connections>
<action selector="cancel:" target="-2" id="Tyi-Un-od6"/>
</connections>
</button>
<button toolTip="Restart V2Ray and connected to the selected server above" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="iD8-Fu-9D9">
<rect key="frame" x="36" y="24" width="135" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="OK" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="qgX-1j-8WD">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
<string key="keyEquivalent" base64-UTF8="YES">
DQ
</string>
</buttonCell>
<connections>
<action selector="ok:" target="-2" id="VQa-fP-dxw"/>
</connections>
</button>
<scrollView fixedFrame="YES" borderType="none" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" translatesAutoresizingMaskIntoConstraints="NO" id="5yh-KN-ihD">
<rect key="frame" x="168" y="110" width="395" height="200"/>
<autoresizingMask key="autoresizingMask"/>
<clipView key="contentView" ambiguous="YES" drawsBackground="NO" copiesOnScroll="NO" id="0rS-HW-TeI">
<rect key="frame" x="0.0" y="0.0" width="395" height="200"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textView ambiguous="YES" importsGraphics="NO" richText="NO" verticallyResizable="YES" findStyle="bar" spellingCorrection="YES" smartInsertDelete="YES" id="dNK-7u-GUm">
<rect key="frame" x="0.0" y="0.0" width="395" height="200"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="textColor" name="textColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
<size key="minSize" width="395" height="200"/>
<size key="maxSize" width="399" height="10000000"/>
<color key="insertionPointColor" name="textColor" catalog="System" colorSpace="catalog"/>
</textView>
</subviews>
</clipView>
<scroller key="verticalScroller" wantsLayer="YES" verticalHuggingPriority="750" horizontal="NO" id="PcW-ea-yMr">
<rect key="frame" x="379" y="0.0" width="16" height="200"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
</scrollView>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" allowsCharacterPickerTouchBarItem="YES" translatesAutoresizingMaskIntoConstraints="NO" id="AJc-pc-iuU">
<rect key="frame" x="28" y="318" width="93" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="V2Ray Servers" id="clK-ga-tqY">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
</view>
<connections>
<outlet property="delegate" destination="-2" id="0bl-1N-AYu"/>
</connections>
<point key="canvasLocation" x="197.5" y="112.5"/>
</window>
</objects>
<resources>
<image name="NSAddTemplate" width="11" height="11"/>
<image name="NSRemoveTemplate" width="11" height="11"/>
</resources>
</document>