Skip to content

Commit f238f03

Browse files
author
Stanislas Chevallier
committed
Fix subtitle text textField's autosizing
1 parent 12ba679 commit f238f03

File tree

6 files changed

+131
-11
lines changed

6 files changed

+131
-11
lines changed

Subtitler.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
97924989270374C700248C3D /* PlayerControlsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97924988270374C700248C3D /* PlayerControlsView.swift */; };
2323
9792498B2703802100248C3D /* Codable+SY.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9792498A2703802100248C3D /* Codable+SY.swift */; };
2424
97DF38302703F8FE002342CA /* SubtitleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97DF382F2703F8FE002342CA /* SubtitleView.swift */; };
25+
97DF3834270407F7002342CA /* SizingTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97DF3833270407F7002342CA /* SizingTextField.swift */; };
2526
97FAA54F270326DE00684B3F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 974FAA7B2703223C008B539B /* Main.storyboard */; };
2627
BE094BE218015E44F9AF9DA8 /* Pods_SubtitlerTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5557C5A516A61C7EE6592BFD /* Pods_SubtitlerTests.framework */; };
2728
E79833F0C546FA7BCAC3E406 /* Pods_Subtitler.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EF6E0DDF2787536935001AC8 /* Pods_Subtitler.framework */; };
@@ -62,6 +63,7 @@
6263
97924988270374C700248C3D /* PlayerControlsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerControlsView.swift; sourceTree = "<group>"; };
6364
9792498A2703802100248C3D /* Codable+SY.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Codable+SY.swift"; sourceTree = "<group>"; };
6465
97DF382F2703F8FE002342CA /* SubtitleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubtitleView.swift; sourceTree = "<group>"; };
66+
97DF3833270407F7002342CA /* SizingTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SizingTextField.swift; sourceTree = "<group>"; };
6567
DCFDB6466EDC7F8D71607D9F /* Pods-Subtitler.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Subtitler.release.xcconfig"; path = "Target Support Files/Pods-Subtitler/Pods-Subtitler.release.xcconfig"; sourceTree = "<group>"; };
6668
EF6E0DDF2787536935001AC8 /* Pods_Subtitler.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Subtitler.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6769
/* End PBXFileReference section */
@@ -138,6 +140,7 @@
138140
children = (
139141
97924988270374C700248C3D /* PlayerControlsView.swift */,
140142
97027088270339EC001B5785 /* PlayerView.swift */,
143+
97DF3833270407F7002342CA /* SizingTextField.swift */,
141144
97DF382F2703F8FE002342CA /* SubtitleView.swift */,
142145
972CAB6E27032591005E9C3D /* TimingButton.swift */,
143146
);
@@ -406,6 +409,7 @@
406409
9792497A2703473900248C3D /* SubRipParser.swift in Sources */,
407410
97924989270374C700248C3D /* PlayerControlsView.swift in Sources */,
408411
974FAA742703223B008B539B /* AppDelegate.swift in Sources */,
412+
97DF3834270407F7002342CA /* SizingTextField.swift in Sources */,
409413
972CAB7127032591005E9C3D /* NSTableView+SY.swift in Sources */,
410414
97DF38302703F8FE002342CA /* SubtitleView.swift in Sources */,
411415
9702708727033502001B5785 /* DocumentController.swift in Sources */,
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1300"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "974FAA6F2703223B008B539B"
18+
BuildableName = "Subtitler.app"
19+
BlueprintName = "Subtitler"
20+
ReferencedContainer = "container:Subtitler.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES">
30+
<Testables>
31+
<TestableReference
32+
skipped = "NO">
33+
<BuildableReference
34+
BuildableIdentifier = "primary"
35+
BlueprintIdentifier = "9792497E270351F900248C3D"
36+
BuildableName = "SubtitlerTests.xctest"
37+
BlueprintName = "SubtitlerTests"
38+
ReferencedContainer = "container:Subtitler.xcodeproj">
39+
</BuildableReference>
40+
</TestableReference>
41+
</Testables>
42+
</TestAction>
43+
<LaunchAction
44+
buildConfiguration = "Debug"
45+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
46+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
47+
launchStyle = "0"
48+
useCustomWorkingDirectory = "NO"
49+
ignoresPersistentStateOnLaunch = "NO"
50+
debugDocumentVersioning = "YES"
51+
debugServiceExtension = "internal"
52+
allowLocationSimulation = "YES">
53+
<BuildableProductRunnable
54+
runnableDebuggingMode = "0">
55+
<BuildableReference
56+
BuildableIdentifier = "primary"
57+
BlueprintIdentifier = "974FAA6F2703223B008B539B"
58+
BuildableName = "Subtitler.app"
59+
BlueprintName = "Subtitler"
60+
ReferencedContainer = "container:Subtitler.xcodeproj">
61+
</BuildableReference>
62+
</BuildableProductRunnable>
63+
</LaunchAction>
64+
<ProfileAction
65+
buildConfiguration = "Release"
66+
shouldUseLaunchSchemeArgsEnv = "YES"
67+
savedToolIdentifier = ""
68+
useCustomWorkingDirectory = "NO"
69+
debugDocumentVersioning = "YES">
70+
<BuildableProductRunnable
71+
runnableDebuggingMode = "0">
72+
<BuildableReference
73+
BuildableIdentifier = "primary"
74+
BlueprintIdentifier = "974FAA6F2703223B008B539B"
75+
BuildableName = "Subtitler.app"
76+
BlueprintName = "Subtitler"
77+
ReferencedContainer = "container:Subtitler.xcodeproj">
78+
</BuildableReference>
79+
</BuildableProductRunnable>
80+
</ProfileAction>
81+
<AnalyzeAction
82+
buildConfiguration = "Debug">
83+
</AnalyzeAction>
84+
<ArchiveAction
85+
buildConfiguration = "Release"
86+
revealArchiveInOrganizer = "YES">
87+
</ArchiveAction>
88+
</Scheme>

Subtitler/Base.lproj/Main.storyboard

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ CA
231231
<windowController storyboardIdentifier="Document Window Controller" showSeguePresentationStyle="single" id="B8D-0N-5wS" sceneMemberID="viewController">
232232
<window key="window" title="Subtitler" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" titlebarAppearsTransparent="YES" id="IQv-IB-iLA">
233233
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
234-
<rect key="contentRect" x="479" y="251" width="800" height="500"/>
234+
<rect key="contentRect" x="479" y="251" width="900" height="500"/>
235235
<rect key="screenRect" x="0.0" y="0.0" width="1680" height="1027"/>
236-
<value key="minSize" type="size" width="800" height="500"/>
236+
<value key="minSize" type="size" width="900" height="500"/>
237237
<connections>
238238
<outlet property="delegate" destination="B8D-0N-5wS" id="98r-iN-zZc"/>
239239
</connections>
@@ -251,24 +251,24 @@ CA
251251
<objects>
252252
<viewController id="XfG-lQ-9wD" customClass="ViewController" customModule="Subtitler" customModuleProvider="target" sceneMemberID="viewController">
253253
<view key="view" misplaced="YES" id="m2S-Jp-Qdl">
254-
<rect key="frame" x="0.0" y="0.0" width="800" height="574"/>
254+
<rect key="frame" x="0.0" y="0.0" width="800" height="549"/>
255255
<autoresizingMask key="autoresizingMask"/>
256256
<subviews>
257257
<splitView misplaced="YES" arrangesAllSubviews="NO" dividerStyle="thin" vertical="YES" translatesAutoresizingMaskIntoConstraints="NO" id="5Ph-wY-p8i">
258-
<rect key="frame" x="0.0" y="-26" width="800" height="600"/>
258+
<rect key="frame" x="0.0" y="-51" width="800" height="600"/>
259259
<subviews>
260260
<customView misplaced="YES" id="JCY-wC-yOm">
261261
<rect key="frame" x="0.0" y="0.0" width="380" height="600"/>
262262
<autoresizingMask key="autoresizingMask"/>
263263
<subviews>
264264
<scrollView autohidesScrollers="YES" horizontalLineScroll="18" horizontalPageScroll="10" verticalLineScroll="18" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Ob9-1O-CVB">
265-
<rect key="frame" x="0.0" y="46" width="380" height="78"/>
265+
<rect key="frame" x="0.0" y="46" width="380" height="403"/>
266266
<clipView key="contentView" id="bus-Px-5FW">
267-
<rect key="frame" x="1" y="1" width="378" height="76"/>
267+
<rect key="frame" x="1" y="1" width="378" height="401"/>
268268
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
269269
<subviews>
270270
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="firstColumnOnly" tableStyle="fullWidth" alternatingRowBackgroundColors="YES" columnReordering="NO" columnResizing="NO" multipleSelection="NO" autosaveColumns="NO" rowHeight="18" usesAutomaticRowHeights="YES" headerView="IK9-0e-Sxy" viewBased="YES" id="8su-3M-rp4">
271-
<rect key="frame" x="0.0" y="0.0" width="378" height="48"/>
271+
<rect key="frame" x="0.0" y="0.0" width="378" height="373"/>
272272
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
273273
<size key="intercellSpacing" width="17" height="0.0"/>
274274
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
@@ -290,7 +290,7 @@ CA
290290
<rect key="frame" x="8" y="0.0" width="146" height="17"/>
291291
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
292292
<subviews>
293-
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" verticalCompressionResistancePriority="1000" translatesAutoresizingMaskIntoConstraints="NO" id="XT2-sz-zwD">
293+
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" verticalCompressionResistancePriority="1000" translatesAutoresizingMaskIntoConstraints="NO" id="XT2-sz-zwD" customClass="SizingTextField" customModule="Subtitler" customModuleProvider="target">
294294
<rect key="frame" x="2" y="2" width="142" height="16"/>
295295
<textFieldCell key="cell" selectable="YES" editable="YES" allowsUndo="NO" title="Sample line with very long text" id="oNC-L2-X9N">
296296
<font key="font" usesAppearanceFont="YES"/>
@@ -444,7 +444,7 @@ CA
444444
<autoresizingMask key="autoresizingMask"/>
445445
<subviews>
446446
<avPlayerView controlsStyle="inline" translatesAutoresizingMaskIntoConstraints="NO" id="fFl-Ef-s8W" customClass="PlayerView" customModule="Subtitler" customModuleProvider="target">
447-
<rect key="frame" x="0.0" y="112" width="419" height="12"/>
447+
<rect key="frame" x="0.0" y="112" width="419" height="337"/>
448448
</avPlayerView>
449449
<button verticalHuggingPriority="750" springLoaded="YES" imageHugsTitle="YES" translatesAutoresizingMaskIntoConstraints="NO" id="EV0-6E-XRp" customClass="PressButton" customModule="Subtitler" customModuleProvider="target">
450450
<rect key="frame" x="2" y="2" width="415" height="40"/>
@@ -472,7 +472,7 @@ CA
472472
</customView>
473473
</subviews>
474474
<holdingPriorities>
475-
<real value="300"/>
475+
<real value="150"/>
476476
<real value="250"/>
477477
</holdingPriorities>
478478
</splitView>

Subtitler/Models/Subtitle.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,13 @@ class Subtitle: NSDocument {
123123
isTransient = false
124124
updateChangeCount(.changeDone)
125125

126+
var newText = text
127+
while newText.contains("\n\n") {
128+
newText = newText.replacingOccurrences(of: "\n\n", with: "\n")
129+
}
130+
126131
let previousText = lines[lineIndex].text
127-
lines[lineIndex].text = text.replacingOccurrences(of: "\n\n", with: "\n")
132+
lines[lineIndex].text = newText
128133

129134
undoManager?.registerUndo(withTarget: self, handler: { selfTarget in
130135
selfTarget.updateText(for: lineIndex, text: previousText)

Subtitler/VCs/ViewController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,11 @@ extension ViewController: NSTableViewDelegate {
269269
guard let field = sender as? NSTextField else { return }
270270
let row = tableView.row(for: field)
271271
guard row >= 0 else { return }
272+
272273
subtitle.updateText(for: row, text: field.stringValue)
274+
275+
// the commited text may differ from the input text, let's make sure the row is up to date
276+
tableView.reloadData(forRowIndexes: IndexSet(integer: tableView.selectedRow), columnIndexes: IndexSet(integersIn: 0...2))
273277
}
274278

275279
// https://samwize.com/2018/11/27/drag-and-drop-to-reorder-nstableview/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// SizingTextField.swift
3+
// Subtitler
4+
//
5+
// Created by Stanislas Chevallier on 29/09/2021.
6+
//
7+
8+
import Cocoa
9+
10+
class SizingTextField: NSTextField {
11+
12+
override var intrinsicContentSize: NSSize {
13+
let wasEditable = isEditable
14+
defer { isEditable = wasEditable }
15+
isEditable = false
16+
17+
return super.intrinsicContentSize
18+
}
19+
}

0 commit comments

Comments
 (0)