Skip to content

Commit

Permalink
Context menu validation
Browse files Browse the repository at this point in the history
  • Loading branch information
macmade committed Feb 18, 2020
1 parent 0219bd2 commit ec841de
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
33 changes: 25 additions & 8 deletions Xclean/Classes/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,28 +193,45 @@ public class MainViewController: NSViewController, NSMenuDelegate
}
}

public func menuWillOpen( _ menu: NSMenu )
@objc private func validateUserInterfaceItem( _ item: NSValidatedUserInterfaceItem ) -> Bool
{
let disable = { menu.items.forEach { $0.isEnabled = false } }
guard let _ = item as? NSMenuItem else
{
return true
}

if self.tableView.clickedRow < 0
{
disable()

return false
}

guard let arranged = self.arrayController.arrangedObjects as? [ DerivedData ] else
{
return false
}

if self.tableView.clickedRow >= arranged.count
{
return false
}

return true
}

public func menuWillOpen( _ menu: NSMenu )
{
if self.tableView.clickedRow < 0
{
return
}

guard let arranged = self.arrayController.arrangedObjects as? [ DerivedData ] else
{
disable()

return
}

if self.tableView.clickedRow >= arranged.count
{
disable()

return
}

Expand Down
6 changes: 3 additions & 3 deletions Xclean/Interface/Base.lproj/MainViewController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<tableColumns>
<tableColumn width="457" minWidth="40" maxWidth="1000" id="7GB-Gf-LER">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border">
<font key="font" metaFont="menu" size="11"/>
<font key="font" metaFont="controlContent" size="11"/>
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
</tableHeaderCell>
Expand Down Expand Up @@ -131,7 +131,7 @@
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" horizontalCompressionResistancePriority="1000" translatesAutoresizingMaskIntoConstraints="NO" id="DuA-Ou-jrg">
<rect key="frame" x="380" y="13" width="33" height="14"/>
<textFieldCell key="cell" controlSize="small" lineBreakMode="truncatingTail" title="Label" id="qNn-9b-jmT">
<font key="font" metaFont="menu" size="11"/>
<font key="font" metaFont="controlContent" size="11"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
Expand Down Expand Up @@ -210,6 +210,7 @@
<binding destination="IE3-7y-uvC" name="sortDescriptors" keyPath="sortDescriptors" previousBinding="jSs-k7-TKj" id="FrN-wL-lfT"/>
<outlet property="dataSource" destination="-2" id="wXJ-Vk-sdc"/>
<outlet property="delegate" destination="-2" id="PJ7-kz-rNk"/>
<outlet property="menu" destination="2sx-7B-nTu" id="J96-12-8hs"/>
</connections>
</tableView>
</subviews>
Expand All @@ -233,7 +234,6 @@
<integer key="NSNullPlaceholder" value="-1"/>
</dictionary>
</binding>
<outlet property="menu" destination="2sx-7B-nTu" id="CYc-di-j7Z"/>
</connections>
</scrollView>
<progressIndicator wantsLayer="YES" maxValue="100" indeterminate="YES" style="spinning" translatesAutoresizingMaskIntoConstraints="NO" id="0yJ-6i-qvJ">
Expand Down

0 comments on commit ec841de

Please sign in to comment.