From 565b9f82546c30573e599b0252c61c658738f2d8 Mon Sep 17 00:00:00 2001 From: igor Date: Fri, 7 Mar 2025 16:54:27 -0800 Subject: [PATCH] Cleanup. --- src/AppDelegate.swift | 2 +- src/ProgramsTable.swift | 46 +++++++++++++++++++++++++++------- src/SearchViewController.swift | 45 ++++++++++++++++++++------------- 3 files changed, 66 insertions(+), 27 deletions(-) diff --git a/src/AppDelegate.swift b/src/AppDelegate.swift index 6e9c8ab..7fb14f1 100644 --- a/src/AppDelegate.swift +++ b/src/AppDelegate.swift @@ -82,7 +82,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate { public func fsEventTriggered(_ path: String, _ flags: Int) { if containsFlags(key: kFSEventStreamEventFlagItemCreated, in: flags) || containsFlags(key: kFSEventStreamEventFlagItemRemoved, in: flags) || - containsFlags(key: kFSEventStreamEventFlagItemCloned, in: flags) || + containsFlags(key: kFSEventStreamEventFlagItemCloned, in: flags) || containsFlags(key: kFSEventStreamEventFlagItemRenamed, in: flags) { for dir in PathManager.shared.paths { diff --git a/src/ProgramsTable.swift b/src/ProgramsTable.swift index c6dc1ec..87ee60d 100644 --- a/src/ProgramsTable.swift +++ b/src/ProgramsTable.swift @@ -19,6 +19,21 @@ class ProgramsTableViewCell: NSTableCellView { private(set) var isEditing = false + public var indexLabel: NSTextField = { + let field = NSTextField(labelWithString: "-") + field.alignment = .center + + // field.drawsBackground = true + // field.backgroundColor = NSColor.green.withAlphaComponent(0.2) + + field.textColor = NSColor.secondaryLabelColor + field.cell?.lineBreakMode = .byTruncatingTail + field.font = NSFont.systemFont(ofSize: NSFontDescriptor.preferredFontDescriptor(forTextStyle: .caption1).pointSize, weight: .bold) + field.translatesAutoresizingMaskIntoConstraints = false + return field + }() + + public var appIconImage: NSImageView = { let image = NSImageView() image.image = NSWorkspace.shared.icon(forFile: Bundle.main.bundlePath) @@ -28,38 +43,51 @@ class ProgramsTableViewCell: NSTableCellView { }() public var titleField: NSTextField = { - let field = NSTextField(labelWithString: "") - field.textColor = NSColor.secondaryLabelColor + let field = NSTextField() + field.isBordered = false + field.drawsBackground = false field.lineBreakMode = .byTruncatingTail + field.textColor = NSColor.secondaryLabelColor field.translatesAutoresizingMaskIntoConstraints = false return field }() public var progPathLabel: NSTextField = { - let textField = NSTextField(labelWithString: "") - textField.cell?.lineBreakMode = .byTruncatingTail - textField.font = NSFont.systemFont(ofSize: NSFontDescriptor.preferredFontDescriptor(forTextStyle: .caption1).pointSize, weight: .medium) - textField.translatesAutoresizingMaskIntoConstraints = false - return textField + let field = NSTextField() + field.isBordered = false + field.drawsBackground = false + field.lineBreakMode = .byTruncatingTail + field.font = NSFont.systemFont(ofSize: NSFontDescriptor.preferredFontDescriptor(forTextStyle: .caption1).pointSize, weight: .medium) + field.translatesAutoresizingMaskIntoConstraints = false + return field }() override init(frame frameRect: NSRect) { super.init(frame: frameRect) + // wantsLayer = true + // layer?.backgroundColor = NSColor.yellow.withAlphaComponent(0.2).cgColor + + addSubview(indexLabel) addSubview(appIconImage) addSubview(titleField) addSubview(progPathLabel) + // indexLabel.setContentHuggingPriority(.required, for: .horizontal) NSLayoutConstraint.activate([ + indexLabel.widthAnchor.constraint(equalToConstant: 25), + indexLabel.centerYAnchor.constraint(equalTo: centerYAnchor), + indexLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: ViewConstants.spacing5), + appIconImage.widthAnchor.constraint(equalToConstant: 40), appIconImage.heightAnchor.constraint(equalToConstant: 40), appIconImage.topAnchor.constraint(equalTo: topAnchor), appIconImage.bottomAnchor.constraint(equalTo: bottomAnchor), - appIconImage.leadingAnchor.constraint(equalTo: leadingAnchor, constant: ViewConstants.spacing5), + appIconImage.leadingAnchor.constraint(equalTo: indexLabel.trailingAnchor), titleField.topAnchor.constraint(equalTo: appIconImage.topAnchor, constant: ViewConstants.spacing2), titleField.leadingAnchor.constraint(equalTo: appIconImage.trailingAnchor, constant: ViewConstants.spacing5), - titleField.trailingAnchor.constraint(equalTo: trailingAnchor), + titleField.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -ViewConstants.spacing5), progPathLabel.topAnchor.constraint(equalTo: titleField.bottomAnchor), progPathLabel.leadingAnchor.constraint(equalTo: titleField.leadingAnchor), diff --git a/src/SearchViewController.swift b/src/SearchViewController.swift index 538d51c..53ebb1c 100644 --- a/src/SearchViewController.swift +++ b/src/SearchViewController.swift @@ -1,23 +1,17 @@ import AppKit import Carbon -// NOTE: This is the corner radius of the backgrounView view that acts as -// a window frame and an NSViewController's view that clips all +// NOTE: This is the corner radius of the backgrounView view that acts as a window frame and an NSViewController's view that clips all // elements inside of it. fileprivate let windowCornerRadius = 15.0 -struct ProgramWeighted { - let program: Program - let weight: Int -} - -fileprivate let maxItems = 10 +fileprivate let maxItems = 20 class SearchViewController: NSViewController, NSTextFieldDelegate, NSPopoverDelegate, NSTableViewDataSource, NSTableViewDelegate { private var keyboardEvents: EventMonitor? private var listIndex = 0 - private var programsList: [Program] = Array(repeating: Program(), count: maxItems) + private var programsList: [Program] = [] private var programsListCells: [ProgramsTableViewCell] = [] private var programsTableViewSelection = 0 @@ -99,7 +93,8 @@ class SearchViewController: NSViewController, NSTextFieldDelegate, NSPopoverDele private var programsTableView: ProgramsTableView = { let table = ProgramsTableView() - table.style = NSTableView.Style.plain + table.style = .plain + table.intercellSpacing = NSSize.zero table.backgroundColor = .clear table.usesAutomaticRowHeights = true @@ -166,15 +161,20 @@ class SearchViewController: NSViewController, NSTextFieldDelegate, NSPopoverDele override func viewDidLoad() { super.viewDidLoad() - // NOTE: This needs removeObserver on deinit? + // NOTE: This needs removeObserver on deinit? Well, technically we don't care because this view controller will exist throughout + // the whole life of the program. When the program gets killed, the OS will clear this. DistributedNotificationCenter.default.addObserver(self, selector: #selector(osThemeChanged(sender:)), name: NSNotification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil) - // Initialize an array of reusable cells. - for _ in 0..