Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4486,6 +4486,7 @@ struct ContentView: View {
width: CGFloat.greatestFiniteMagnitude,
height: CGFloat.greatestFiniteMagnitude
)
textView.configureCmuxNaturalWritingDirectionForComposedText()
scrollView.documentView = textView

placeholderField.translatesAutoresizingMaskIntoConstraints = false
Expand Down Expand Up @@ -4768,6 +4769,7 @@ struct ContentView: View {
view.placeholder = placeholder
view.maximumHeight = maxHeight
view.textView.string = text
view.textView.applyCmuxNaturalWritingDirectionToComposedText()
view.textView.delegate = context.coordinator
view.textView.setAccessibilityLabel(accessibilityLabel)
view.textView.setAccessibilityIdentifier(accessibilityIdentifier)
Expand Down Expand Up @@ -4803,6 +4805,7 @@ struct ContentView: View {
if nsView.textView.string != text {
context.coordinator.isProgrammaticMutation = true
nsView.textView.string = text
nsView.textView.applyCmuxNaturalWritingDirectionToComposedText()
context.coordinator.isProgrammaticMutation = false
}
nsView.onMeasuredHeightChange = { [weak coordinator = context.coordinator] height in
Expand Down Expand Up @@ -12622,6 +12625,7 @@ private struct FeedbackComposerMessageEditor: NSViewRepresentable {
let view = FeedbackComposerMessageEditorView()
view.placeholder = placeholder
view.textView.string = text
view.textView.applyCmuxNaturalWritingDirectionToComposedText()
view.textView.delegate = context.coordinator
view.textView.setAccessibilityLabel(accessibilityLabel)
view.textView.setAccessibilityIdentifier(accessibilityIdentifier)
Expand All @@ -12632,6 +12636,7 @@ private struct FeedbackComposerMessageEditor: NSViewRepresentable {
func updateNSView(_ nsView: FeedbackComposerMessageEditorView, context: Context) {
if nsView.textView.string != text {
nsView.textView.string = text
nsView.textView.applyCmuxNaturalWritingDirectionToComposedText()
nsView.refreshTextLayout()
}
nsView.placeholder = placeholder
Expand Down Expand Up @@ -12730,6 +12735,7 @@ final class FeedbackComposerMessageEditorView: NSView {
width: CGFloat.greatestFiniteMagnitude,
height: CGFloat.greatestFiniteMagnitude
)
textView.configureCmuxNaturalWritingDirectionForComposedText()

scrollView.documentView = textView
addSubview(scrollView)
Expand Down
3 changes: 3 additions & 0 deletions Sources/Feed/FeedPanelView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3257,6 +3257,7 @@ private final class FeedInlineTextEditorView: NSView {
width: CGFloat.greatestFiniteMagnitude,
height: CGFloat.greatestFiniteMagnitude
)
textView.configureCmuxNaturalWritingDirectionForComposedText()
addSubview(textView)

placeholderField.textColor = .placeholderTextColor
Expand Down Expand Up @@ -3451,6 +3452,7 @@ private struct FeedInlineTextField: NSViewRepresentable {
let view = FeedInlineTextEditorView(frame: .zero)
view.textView.delegate = context.coordinator
view.textView.string = text
view.textView.applyCmuxNaturalWritingDirectionToComposedText()
view.textView.onActivate = { [weak coordinator = context.coordinator] in
coordinator?.activateField()
}
Expand Down Expand Up @@ -3478,6 +3480,7 @@ private struct FeedInlineTextField: NSViewRepresentable {
if nsView.textView.string != text, !nsView.textView.hasMarkedText() {
context.coordinator.isProgrammaticMutation = true
nsView.textView.string = text
nsView.textView.applyCmuxNaturalWritingDirectionToComposedText()
context.coordinator.isProgrammaticMutation = false
nsView.refreshMetrics()
}
Expand Down
33 changes: 33 additions & 0 deletions Sources/NaturalTextCompositionDirection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import AppKit

extension NSTextView {
func configureCmuxNaturalWritingDirectionForComposedText() {
baseWritingDirection = .natural
alignment = .natural

let paragraphStyle = cmuxNaturalComposedTextParagraphStyle()
defaultParagraphStyle = paragraphStyle

var attributes = typingAttributes
attributes[.paragraphStyle] = paragraphStyle
typingAttributes = attributes
}

func applyCmuxNaturalWritingDirectionToComposedText() {
configureCmuxNaturalWritingDirectionForComposedText()

let fullRange = NSRange(location: 0, length: (string as NSString).length)
guard fullRange.length > 0 else { return }

textStorage?.setAlignment(.natural, range: fullRange)
textStorage?.setBaseWritingDirection(.natural, range: fullRange)
}

func cmuxNaturalComposedTextParagraphStyle() -> NSParagraphStyle {
let paragraphStyle = (defaultParagraphStyle?.mutableCopy() as? NSMutableParagraphStyle)
?? NSMutableParagraphStyle()
paragraphStyle.alignment = .natural
paragraphStyle.baseWritingDirection = .natural
return paragraphStyle.copy() as! NSParagraphStyle
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial | ⚡ Quick win

Avoid the force cast flagged by SwiftLint.

copy() returns Any; while the cast is safe here, the force cast trips the force_cast rule. Use a safe cast with a fallback (an NSMutableParagraphStyle is itself an NSParagraphStyle, so it satisfies the return type).

♻️ Proposed fix
-        return paragraphStyle.copy() as! NSParagraphStyle
+        return paragraphStyle.copy() as? NSParagraphStyle ?? paragraphStyle
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return paragraphStyle.copy() as! NSParagraphStyle
return paragraphStyle.copy() as? NSParagraphStyle ?? paragraphStyle
🧰 Tools
🪛 SwiftLint (0.63.2)

[Error] 31-31: Force casts should be avoided

(force_cast)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Sources/NaturalTextCompositionDirection.swift` at line 31, Replace the force
cast on paragraphStyle.copy() with a safe cast and fallback: call
paragraphStyle.copy(), attempt to cast to NSParagraphStyle using as?, and return
the result or fall back to the original paragraphStyle; reference the
paragraphStyle variable, its copy() call, and NSParagraphStyle in this change to
satisfy SwiftLint's force_cast rule.

}
}
16 changes: 15 additions & 1 deletion Sources/TextBoxInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3727,6 +3727,7 @@ struct TextBoxInputView: NSViewRepresentable {
}
if textView.inlineAttachments().isEmpty && textView.plainText() != text {
textView.string = text
textView.applyCmuxNaturalWritingDirectionToComposedText()
}
updateTextView(textView, context: context)
}
Expand Down Expand Up @@ -3886,6 +3887,16 @@ final class TextBoxInputTextView: NSTextView {
attachmentPreviewPopover?.isShown == true
}

override init(frame frameRect: NSRect, textContainer container: NSTextContainer?) {
super.init(frame: frameRect, textContainer: container)
configureCmuxNaturalWritingDirectionForComposedText()
}

@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

deinit {
dismissMentionCompletions()
removeAttachmentKeyDownMonitor()
Expand Down Expand Up @@ -3997,6 +4008,7 @@ final class TextBoxInputTextView: NSTextView {
dismissMentionCompletions()
clearAttachmentFocus(dismissPreview: true)
textStorage?.setAttributedString(NSAttributedString(string: ""))
configureCmuxNaturalWritingDirectionForComposedText()
recenterSingleLineTextContainer()
didChangeText()
}
Expand All @@ -4019,6 +4031,7 @@ final class TextBoxInputTextView: NSTextView {
dismissMentionCompletions()
clearAttachmentFocus(dismissPreview: true)
textStorage?.setAttributedString(content)
applyCmuxNaturalWritingDirectionToComposedText()
refreshInlineAttachmentCells(
font: font ?? NSFont.systemFont(ofSize: NSFont.systemFontSize),
foregroundColor: textColor ?? .labelColor
Expand Down Expand Up @@ -5398,7 +5411,8 @@ final class TextBoxInputTextView: NSTextView {
[
.font: explicitFont ?? font ?? NSFont.systemFont(ofSize: NSFont.systemFontSize),
.foregroundColor: explicitForegroundColor ?? textColor ?? .labelColor,
.baselineOffset: textBaselineOffsetForCurrentContent()
.baselineOffset: textBaselineOffsetForCurrentContent(),
.paragraphStyle: cmuxNaturalComposedTextParagraphStyle()
]
}

Expand Down
4 changes: 4 additions & 0 deletions cmux.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@
E1000000A1B2C3D4E5F60718 /* MenuKeyEquivalentRoutingUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1000001A1B2C3D4E5F60718 /* MenuKeyEquivalentRoutingUITests.swift */; };
C0DE3150A00000000000001 /* MinimalModeSidebarControls.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0DE3150A00000000000002 /* MinimalModeSidebarControls.swift */; };
B9000015A1B2C3D4E5F60719 /* MultiWindowNotificationsUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9000016A1B2C3D4E5F60719 /* MultiWindowNotificationsUITests.swift */; };
C0DE50070000000000000001 /* NaturalTextCompositionDirection.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0DE50070000000000000002 /* NaturalTextCompositionDirection.swift */; };
734F49D37E543DD01C2F4FEF /* NotificationAndMenuBarTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2C075029771815DD5DA1332 /* NotificationAndMenuBarTests.swift */; };
A5001094 /* NotificationsPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5001091 /* NotificationsPage.swift */; };
4378399A7C0245EF8186F306 /* OmnibarAndToolsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B09C007F42697761B5F1A2AB /* OmnibarAndToolsTests.swift */; };
Expand Down Expand Up @@ -930,6 +931,7 @@
E1000001A1B2C3D4E5F60718 /* MenuKeyEquivalentRoutingUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuKeyEquivalentRoutingUITests.swift; sourceTree = "<group>"; };
C0DE3150A00000000000002 /* MinimalModeSidebarControls.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Update/MinimalModeSidebarControls.swift; sourceTree = "<group>"; };
B9000016A1B2C3D4E5F60719 /* MultiWindowNotificationsUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultiWindowNotificationsUITests.swift; sourceTree = "<group>"; };
C0DE50070000000000000002 /* NaturalTextCompositionDirection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NaturalTextCompositionDirection.swift; sourceTree = "<group>"; };
D2C075029771815DD5DA1332 /* NotificationAndMenuBarTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationAndMenuBarTests.swift; sourceTree = "<group>"; };
A5001091 /* NotificationsPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationsPage.swift; sourceTree = "<group>"; };
B09C007F42697761B5F1A2AB /* OmnibarAndToolsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OmnibarAndToolsTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1428,6 +1430,7 @@
C7934BB35B66491B1BCA8064 /* MenuBarExtraController.swift */,
2F0C07000000000000000001 /* CmuxMainWindow.swift */,
2F0C06000000000000000001 /* MainWindowVisibilityController.swift */,
C0DE50070000000000000002 /* NaturalTextCompositionDirection.swift */,
A500D011A1B2C3D4E5F60718 /* DebugLogging.swift */,
D35B71010000000000000002 /* StartupBreadcrumbLog.swift */,
A50016B0A1B2C3D4E5F60718 /* SessionSnapshotDebugBenchmark.swift */,
Expand Down Expand Up @@ -2319,6 +2322,7 @@
1A8BEE693C9E4C3190CB7F20 /* MenuBarExtraController.swift in Sources */,
3865A0033865A0033865A003 /* MenubarSearchPopover.swift in Sources */,
C0DE3150A00000000000001 /* MinimalModeSidebarControls.swift in Sources */,
C0DE50070000000000000001 /* NaturalTextCompositionDirection.swift in Sources */,
A5001094 /* NotificationsPage.swift in Sources */,
D0B1001CA1B2C3D4E5F60001 /* PaneDropRoutingSupport.swift in Sources */,
A5001400 /* Panel.swift in Sources */,
Expand Down
Loading