Create Files preference pane with code (using NSGridView)

This commit is contained in:
Allan Odgaard
2020-04-18 23:02:15 +07:00
parent 5aef1c5c8a
commit bdedf08bd7
4 changed files with 57 additions and 217 deletions

View File

@@ -1,204 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="9059" systemVersion="14F1021" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none">
<dependencies>
<deployment version="101200" identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="9059"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="FilesPreferences">
<connections>
<outlet property="encodingPopUp" destination="22" id="66"/>
<outlet property="newDocumentTypesMenu" destination="6" id="93"/>
<outlet property="newDocumentTypesPopUp" destination="4" id="91"/>
<outlet property="unknownDocumentTypesMenu" destination="87" id="94"/>
<outlet property="unknownDocumentTypesPopUp" destination="85" id="92"/>
<outlet property="view" destination="1" id="2"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application"/>
<customView id="1">
<rect key="frame" x="0.0" y="0.0" width="480" height="242"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<box verticalHuggingPriority="750" title="Box" boxType="separator" titlePosition="noTitle" id="47">
<rect key="frame" x="12" y="137" width="456" height="5"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<color key="borderColor" white="0.0" alpha="0.41999999999999998" colorSpace="calibratedWhite"/>
<color key="fillColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<font key="titleFont" metaFont="system"/>
</box>
<popUpButton verticalHuggingPriority="750" id="4" userLabel="New Document Type Pop Up">
<rect key="frame" x="202" y="107" width="206" height="26"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<popUpButtonCell key="cell" type="push" title="Plain text" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="7" id="5">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
<menu key="menu" title="New Document Type" id="6">
<items>
<menuItem title="Plain text" state="on" id="7"/>
</items>
</menu>
</popUpButtonCell>
</popUpButton>
<popUpButton verticalHuggingPriority="750" id="85" userLabel="Unknown Document Type Pop Up">
<rect key="frame" x="202" y="77" width="206" height="26"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<popUpButtonCell key="cell" type="push" title="Plain text" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="88" id="86">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
<menu key="menu" title="Unknown Document Type" id="87">
<items>
<menuItem title="Plain text" state="on" id="88"/>
</items>
</menu>
</popUpButtonCell>
</popUpButton>
<popUpButton verticalHuggingPriority="750" id="22" userLabel="Encodings Pop Up" customClass="OakEncodingPopUpButton">
<rect key="frame" x="202" y="47" width="206" height="26"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<popUpButtonCell key="cell" type="push" title="UTF-8 (recommended)" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="27" id="23">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
<menu key="menu" title="OtherViews" id="24">
<items>
<menuItem title="UTF-8 (recommended)" state="on" id="27"/>
</items>
</menu>
</popUpButtonCell>
</popUpButton>
<popUpButton verticalHuggingPriority="750" id="28" userLabel="Line Endings Pop Up">
<rect key="frame" x="202" y="17" width="206" height="26"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<popUpButtonCell key="cell" type="push" title="LF (recommended)" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="33" id="29">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
<menu key="menu" title="OtherViews" id="30">
<items>
<menuItem title="LF (recommended)" state="on" id="33"/>
<menuItem title="CR (Mac Classic)" tag="1" id="32"/>
<menuItem title="CRLF (Windows)" tag="2" id="31"/>
</items>
</menu>
</popUpButtonCell>
<connections>
<binding destination="-2" name="selectedTag" keyPath="lineEndings" id="65">
<dictionary key="options">
<string key="NSValueTransformerName">OakLineEndingsSettingsTransformer</string>
</dictionary>
</binding>
</connections>
</popUpButton>
<textField verticalHuggingPriority="750" id="34">
<rect key="frame" x="66" y="113" width="134" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="New document type:" id="35">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" id="89">
<rect key="frame" x="34" y="83" width="166" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Unknown document type:" id="90">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" id="36">
<rect key="frame" x="134" y="53" width="66" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Encoding:" id="37">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" id="38">
<rect key="frame" x="111" y="23" width="89" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Line endings:" id="39">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" id="67">
<rect key="frame" x="128" y="205" width="72" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="At startup:" id="68">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<button id="69">
<rect key="frame" x="203" y="204" width="242" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Open documents from last session" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="70">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<binding destination="-2" name="value" keyPath="disableSessionRestore" id="80">
<dictionary key="options">
<string key="NSValueTransformerName">NSNegateBoolean</string>
</dictionary>
</binding>
</connections>
</button>
<button id="73">
<rect key="frame" x="203" y="166" width="156" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Create one at startup" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="74">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<binding destination="-2" name="value" keyPath="disableDocumentAtStartup" id="82">
<dictionary key="options">
<string key="NSValueTransformerName">NSNegateBoolean</string>
</dictionary>
</binding>
</connections>
</button>
<button id="75">
<rect key="frame" x="203" y="146" width="209" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Create one when re-activated" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="76">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<binding destination="-2" name="value" keyPath="disableDocumentAtReactivation" id="84">
<dictionary key="options">
<string key="NSValueTransformerName">NSNegateBoolean</string>
</dictionary>
</binding>
</connections>
</button>
<textField verticalHuggingPriority="750" id="71">
<rect key="frame" x="34" y="167" width="166" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="With no open documents:" id="72">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" id="77">
<rect key="frame" x="222" y="188" width="133" height="14"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Hold shift (⇧) to bypass" id="78">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
</customView>
<userDefaultsController representsSharedInstance="YES" id="40"/>
</objects>
</document>

View File

@@ -1,12 +1,4 @@
#import "PreferencesPane.h"
#import <OakAppKit/OakEncodingPopUpButton.h>
@interface FilesPreferences : PreferencesPane
{
IBOutlet NSPopUpButton* newDocumentTypesPopUp;
IBOutlet NSPopUpButton* unknownDocumentTypesPopUp;
IBOutlet OakEncodingPopUpButton* encodingPopUp;
IBOutlet NSMenu* newDocumentTypesMenu;
IBOutlet NSMenu* unknownDocumentTypesMenu;
}
@end

View File

@@ -1,8 +1,11 @@
#import "FilesPreferences.h"
#import "Keys.h"
#import <OakAppKit/NSMenu Additions.h>
#import <OakAppKit/OakEncodingPopUpButton.h>
#import <OakAppKit/OakUIConstructionFunctions.h>
#import <OakFoundation/NSString Additions.h>
#import <OakFoundation/OakStringListTransformer.h>
#import <MenuBuilder/MenuBuilder.h>
#import <settings/settings.h>
#import <bundles/bundles.h>
#import <ns/ns.h>
@@ -12,7 +15,7 @@
@implementation FilesPreferences
- (id)init
{
if(self = [super initWithNibName:@"FilesPreferences" label:@"Files" image:[NSImage imageNamed:NSImageNameMultipleDocuments]])
if(self = [super initWithNibName:nil label:@"Files" image:[NSImage imageNamed:NSImageNameMultipleDocuments]])
{
[OakStringListTransformer createTransformerWithName:@"OakLineEndingsSettingsTransformer" andObjectsArray:@[ @"\\n", @"\\r", @"\\r\\n" ]];
@@ -42,7 +45,58 @@
- (void)loadView
{
[super loadView];
NSButton* restoreDocumentsCheckBox = OakCreateCheckBox(@"Open documents from last session");
NSButton* createAtStartupCheckBox = OakCreateCheckBox(@"Create one at startup");
NSButton* createOnActivationCheckBox = OakCreateCheckBox(@"Create one when re-activated");
NSPopUpButton* newDocumentTypesPopUp = OakCreatePopUpButton();
NSPopUpButton* unknownDocumentTypesPopUp = OakCreatePopUpButton();
OakEncodingPopUpButton* encodingPopUp = [[OakEncodingPopUpButton alloc] init];
NSPopUpButton* lineEndingsPopUp = OakCreatePopUpButton();
MBMenu const items = {
{ @"LF (recommended)", .tag = 0 },
{ @"CR (Mac Classic)", .tag = 1 },
{ @"CRLF (Windows)", .tag = 2 },
};
MBCreateMenu(items, lineEndingsPopUp.menu);
NSFont* smallFont = [NSFont messageFontOfSize:[NSFont systemFontSizeForControlSize:NSControlSizeSmall]];
NSGridView* gridView = [NSGridView gridViewWithViews:@[
@[ OakCreateLabel(@"At startup:"), restoreDocumentsCheckBox ],
@[ NSGridCell.emptyContentView, OakCreateLabel(@"Hold shift (⇧) to bypass", smallFont) ],
@[ OakCreateLabel(@"With no open documents:"), createAtStartupCheckBox ],
@[ NSGridCell.emptyContentView, createOnActivationCheckBox ],
@[ ],
@[ OakCreateLabel(@"New document type:"), newDocumentTypesPopUp ],
@[ OakCreateLabel(@"Unknown document type:"), unknownDocumentTypesPopUp ],
@[ OakCreateLabel(@"Encoding:"), encodingPopUp ],
@[ OakCreateLabel(@"Line endings:"), lineEndingsPopUp ],
]];
NSView* label = [gridView cellAtColumnIndex:1 rowIndex:0].contentView;
NSGridCell* sublabel = [gridView cellAtColumnIndex:1 rowIndex:1];
sublabel.xPlacement = NSGridCellPlacementNone;
sublabel.customPlacementConstraints = @[ [sublabel.contentView.leadingAnchor constraintEqualToAnchor:label.leadingAnchor constant:19] ];
for(NSView* popUpButton in @[ unknownDocumentTypesPopUp, encodingPopUp, lineEndingsPopUp ])
[popUpButton.widthAnchor constraintEqualToAnchor:newDocumentTypesPopUp.widthAnchor].active = YES;
self.view = OakSetupGridViewWithSeparators(gridView, { 4 });
[restoreDocumentsCheckBox bind:NSValueBinding toObject:self withKeyPath:@"disableSessionRestore" options:@{ NSValueTransformerNameBindingOption: NSNegateBooleanTransformerName }];
[createAtStartupCheckBox bind:NSValueBinding toObject:self withKeyPath:@"disableDocumentAtStartup" options:@{ NSValueTransformerNameBindingOption: NSNegateBooleanTransformerName }];
[createOnActivationCheckBox bind:NSValueBinding toObject:self withKeyPath:@"disableDocumentAtReactivation" options:@{ NSValueTransformerNameBindingOption: NSNegateBooleanTransformerName }];
[encodingPopUp bind:@"encoding" toObject:self withKeyPath:@"encoding" options:nil];
[lineEndingsPopUp bind:NSSelectedTagBinding toObject:self withKeyPath:@"lineEndings" options:@{ NSValueTransformerNameBindingOption: @"OakLineEndingsSettingsTransformer" }];
// ================================
// = Create Language Pop-up Menus =
// ================================
NSMenu* newDocumentTypesMenu = newDocumentTypesPopUp.menu;
NSMenu* unknownDocumentTypesMenu = unknownDocumentTypesPopUp.menu;
[newDocumentTypesMenu removeAllItems];
[unknownDocumentTypesMenu removeAllItems];
@@ -85,7 +139,5 @@
[unknownDocumentTypesPopUp selectItem:item];
}
}
[encodingPopUp bind:@"encoding" toObject:self withKeyPath:@"encoding" options:nil];
}
@end

View File

@@ -1,6 +1,6 @@
SOURCES = src/*.mm
EXPORT = src/{Preferences,Keys,TerminalPreferences,PreferencesPane}.h
CP_Resources = resources/* icons/*
LINK = BundlesManager MASPreferences OakAppKit OakFoundation SoftwareUpdate bundles io ns regexp settings text version
LINK = BundlesManager MASPreferences OakAppKit OakFoundation MenuBuilder SoftwareUpdate bundles io ns regexp settings text version
IMPORT = OakTabBarView
FRAMEWORKS = Cocoa