diff --git a/src/ui/generic-components/TableGroupView.swift b/src/ui/generic-components/TableGroupView.swift new file mode 100644 index 00000000..802cdfae --- /dev/null +++ b/src/ui/generic-components/TableGroupView.swift @@ -0,0 +1,311 @@ +import Cocoa + +class TableGroupView: NSStackView { + static let padding = CGFloat(10) + static let backgroundColor = NSColor.lightGray.withAlphaComponent(0.1).cgColor + static let borderColor = NSColor.lightGray.withAlphaComponent(0.2).cgColor + static let cornerRadius = CGFloat(10) + static let borderWidth = CGFloat(1) + + var title: String? + var subTitle: String? + + var width: CGFloat = 500 + private let titleLabel = NSTextField(labelWithString: "") + private let subTitleLabel = NSTextField(labelWithString: "") + private let tableView = NSStackView() + private var rows = [RowInfo]() + + struct Row { + let leftTitle: String + let subTitle: String? + let rightViews: [NSView] + + init(leftTitle: String, subTitle: String? = nil, rightViews: [NSView]) { + self.leftTitle = leftTitle + self.subTitle = subTitle + self.rightViews = rightViews + } + } + + struct RowInfo { + let id: Int + let view: NSView + var previousSeparator: NSBox? + var nextSeparator: NSBox? + + init(id: Int, view: NSView, previousSeparator: NSBox? = nil, nextSeparator: NSBox? = nil) { + self.id = id + self.view = view + self.previousSeparator = previousSeparator + self.nextSeparator = nextSeparator + } + } + + init(title: String? = nil, subTitle: String? = nil, width: CGFloat = 500) { + self.width = width + self.title = title + self.subTitle = subTitle + super.init(frame: .zero) + setupView() + } + + required init?(coder: NSCoder) { + super.init(coder: coder) + setupView() + } + + private func setupView() { + translatesAutoresizingMaskIntoConstraints = false + orientation = .vertical + spacing = 3 + + if let title = title { + titleLabel.stringValue = title + titleLabel.font = NSFont.boldSystemFont(ofSize: 13) + titleLabel.alignment = .left + titleLabel.lineBreakMode = .byWordWrapping + titleLabel.maximumNumberOfLines = 0 + + addArrangedSubview(titleLabel) + titleLabel.translatesAutoresizingMaskIntoConstraints = false + titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: TableGroupView.padding).isActive = true + titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -TableGroupView.padding).isActive = true + + // Ensure height adjusts to content by setting priorities + titleLabel.setContentHuggingPriority(.required, for: .vertical) + titleLabel.setContentCompressionResistancePriority(.required, for: .vertical) + } else { + titleLabel.isHidden = true + } + + if let subTitle = subTitle { + subTitleLabel.stringValue = subTitle + subTitleLabel.font = NSFont.systemFont(ofSize: 12) + subTitleLabel.textColor = .gray + subTitleLabel.alignment = .left + subTitleLabel.lineBreakMode = .byWordWrapping + subTitleLabel.maximumNumberOfLines = 0 + + addArrangedSubview(subTitleLabel) + subTitleLabel.translatesAutoresizingMaskIntoConstraints = false + subTitleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: TableGroupView.padding).isActive = true + subTitleLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -TableGroupView.padding).isActive = true + + // Ensure height adjusts to content by setting priorities + subTitleLabel.setContentHuggingPriority(.required, for: .vertical) + subTitleLabel.setContentCompressionResistancePriority(.required, for: .vertical) + // Calculate the fitting height for subLabel and activate the height constraint + let subLabelHeight = calculateHeightForLabel(subTitleLabel, width: self.width - 2 * TableGroupView.padding) + subTitleLabel.heightAnchor.constraint(greaterThanOrEqualToConstant: subLabelHeight).isActive = true + } else { + subTitleLabel.isHidden = true + } + + tableView.orientation = .vertical + tableView.spacing = 0 + tableView.wantsLayer = true + tableView.layer?.backgroundColor = TableGroupView.backgroundColor + tableView.layer?.cornerRadius = TableGroupView.cornerRadius + tableView.layer?.borderColor = TableGroupView.borderColor + tableView.layer?.borderWidth = TableGroupView.borderWidth + addArrangedSubview(tableView) + + // Add constraints to ensure tableView takes up the full width + tableView.translatesAutoresizingMaskIntoConstraints = false + tableView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true + tableView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true + tableView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true + widthAnchor.constraint(equalToConstant: self.width).isActive = true + } + + func addRow(_ row: Row, onMouseEntered: EventClosure? = nil, onMouseExited: EventClosure? = nil) -> RowInfo { + return addRow(row.leftTitle, row.rightViews, subText: row.subTitle, onMouseEntered: onMouseEntered, onMouseExited: onMouseExited) + } + + func addRow(_ leftText: String, _ rightViews: NSView, subText: String? = nil, + onMouseEntered: EventClosure? = nil, + onMouseExited: EventClosure? = nil) -> RowInfo { + return addRow(leftText, [rightViews], subText: subText, onMouseEntered: onMouseEntered, onMouseExited: onMouseExited) + } + + func addRow(_ leftText: String, _ rightViews: [NSView], subText: String? = nil, + onMouseEntered: EventClosure? = nil, + onMouseExited: EventClosure? = nil) -> RowInfo { + let rowView = MouseHoverStackView() + rowView.orientation = .vertical + rowView.spacing = 2 + + let mainRow = NSStackView() + mainRow.orientation = .horizontal + mainRow.spacing = 0 + +// rightViews.forEach { view in +// if let button = view as? NSButton { +// button.isBordered = false +// } +// } + + let rightStackView = NSStackView() + rightStackView.orientation = .horizontal + rightStackView.spacing = 2 + rightStackView.setViews(rightViews, in: .leading) + + let leftLabel = NSTextField(labelWithString: leftText) + leftLabel.alignment = .left + leftLabel.lineBreakMode = .byWordWrapping + leftLabel.maximumNumberOfLines = 0 + + let spacer = NSView() // Spacer to fill the middle space + + mainRow.addArrangedSubview(leftLabel) + mainRow.addArrangedSubview(spacer) + mainRow.addArrangedSubview(rightStackView) + + leftLabel.translatesAutoresizingMaskIntoConstraints = false + rightStackView.translatesAutoresizingMaskIntoConstraints = false + spacer.translatesAutoresizingMaskIntoConstraints = false + + leftLabel.leadingAnchor.constraint(equalTo: mainRow.leadingAnchor).isActive = true + rightStackView.trailingAnchor.constraint(equalTo: mainRow.trailingAnchor).isActive = true + spacer.widthAnchor.constraint(greaterThanOrEqualToConstant: 0).isActive = true + + rowView.addArrangedSubview(mainRow) + + mainRow.translatesAutoresizingMaskIntoConstraints = false + mainRow.topAnchor.constraint(equalTo: rowView.topAnchor, constant: TableGroupView.padding).isActive = true + mainRow.leadingAnchor.constraint(equalTo: rowView.leadingAnchor, constant: TableGroupView.padding).isActive = true + mainRow.trailingAnchor.constraint(equalTo: rowView.trailingAnchor, constant: -TableGroupView.padding).isActive = true + mainRow.heightAnchor.constraint(equalToConstant: mainRow.fittingSize.height).isActive = true + + if let subText = subText { + let subLabel = NSTextField(wrappingLabelWithString: subText) + subLabel.font = NSFont.systemFont(ofSize: 12) + subLabel.textColor = .gray + subLabel.alignment = .left + subLabel.lineBreakMode = .byWordWrapping + subLabel.maximumNumberOfLines = 0 // Allow unlimited lines + subLabel.setContentHuggingPriority(.required, for: .vertical) + subLabel.setContentCompressionResistancePriority(.required, for: .vertical) + + rowView.addArrangedSubview(subLabel) + subLabel.translatesAutoresizingMaskIntoConstraints = false + subLabel.leadingAnchor.constraint(equalTo: rowView.leadingAnchor, constant: TableGroupView.padding).isActive = true + subLabel.trailingAnchor.constraint(equalTo: rowView.trailingAnchor, constant: -rightStackView.fittingSize.width - 2 * TableGroupView.padding).isActive = true + subLabel.topAnchor.constraint(equalTo: mainRow.bottomAnchor, constant: 2).isActive = true + subLabel.bottomAnchor.constraint(equalTo: rowView.bottomAnchor, constant: -TableGroupView.padding).isActive = true + + // Calculate the fitting height for subLabel and activate the height constraint + let subLabelHeight = calculateHeightForLabel(subLabel, width: self.width - 2 * TableGroupView.padding) + subLabel.heightAnchor.constraint(greaterThanOrEqualToConstant: subLabelHeight).isActive = true + } else { + mainRow.bottomAnchor.constraint(equalTo: rowView.bottomAnchor, constant: -TableGroupView.padding).isActive = true + } + + let previousSeparator: NSBox? + if rows.count > 0 { + previousSeparator = NSBox() + previousSeparator?.translatesAutoresizingMaskIntoConstraints = false + previousSeparator?.boxType = .separator + // Add separator only if there are already rows present + tableView.addArrangedSubview(previousSeparator!) + previousSeparator?.heightAnchor.constraint(equalToConstant: TableGroupView.borderWidth).isActive = true + previousSeparator?.centerXAnchor.constraint(equalTo: tableView.centerXAnchor).isActive = true + self.adjustSeparatorWidth(separator: previousSeparator, isMouseInside: false) + } else { + previousSeparator = nil + } + + let rowId = rows.count + var rowInfo = RowInfo(id: rowId, view: rowView, previousSeparator: previousSeparator, nextSeparator: nil) + if rows.count > 0 { + // Update the nextSeparator of the previous row + rows[rows.count - 1].nextSeparator = previousSeparator + } + + rows.append(rowInfo) + tableView.addArrangedSubview(rowView) + + rowView.onMouseEntered = { (event, view) in + if let onMouseEntered = onMouseEntered { + if let rowInfo = self.rows.first(where: { $0.view === view }) { + rowView.layer?.backgroundColor = NSColor.lightGray.withAlphaComponent(0.2).cgColor + self.adjustSeparatorWidth(separator: rowInfo.previousSeparator, isMouseInside: true) + self.adjustSeparatorWidth(separator: rowInfo.nextSeparator, isMouseInside: true) + onMouseEntered(event, view) + } + } + } + + rowView.onMouseExited = { (event, view) in + if let rowInfo = self.rows.first(where: { $0.view === view }) { + rowView.layer?.backgroundColor = NSColor.clear.cgColor + self.adjustSeparatorWidth(separator: rowInfo.previousSeparator, isMouseInside: false) + self.adjustSeparatorWidth(separator: rowInfo.nextSeparator, isMouseInside: false) + onMouseExited?(event, view) + } + } + + return rowInfo + } + + private func adjustSeparatorWidth(separator: NSBox?, isMouseInside: Bool) { + let width = isMouseInside ? self.width : (self.width - 2 * TableGroupView.padding) + + if let separator = separator { + if let existingWidthConstraint = separator.constraints.first(where: { $0.firstAttribute == .width }) { + separator.removeConstraint(existingWidthConstraint) + } + separator.widthAnchor.constraint(equalToConstant: width).isActive = true + } + } + + func removeRow(byId id: Int) { + if let index = rows.firstIndex(where: { $0.id == id }) { + rows[index].view.removeFromSuperview() + rows.remove(at: index) + } + } + + private func calculateHeightForLabel(_ label: NSTextField, width: CGFloat) -> CGFloat { + let fittingSize = NSSize(width: width, height: CGFloat.greatestFiniteMagnitude) + let boundingRect = label.attributedStringValue.boundingRect(with: fittingSize, options: [.usesLineFragmentOrigin, .usesFontLeading]) + return ceil(boundingRect.height) + } + + // Helper function to get system default border color + func systemDefaultBorderColor() -> CGColor { + if #available(macOS 10.14, *) { + if NSApp.effectiveAppearance.name == .darkAqua { + return NSColor(calibratedWhite: 0.6, alpha: 1.0).cgColor // Dark mode color + } else { + return NSColor(calibratedWhite: 0.8, alpha: 1.0).cgColor // Light mode color + } + } else { + // Fallback color for older macOS versions + return NSColor(calibratedWhite: 0.8, alpha: 1.0).cgColor + } + } +} + +class MouseHoverStackView: NSStackView { + var onMouseEntered: EventClosure? + var onMouseExited: EventClosure? + + override func updateTrackingAreas() { + super.updateTrackingAreas() + for trackingArea in trackingAreas { + removeTrackingArea(trackingArea) + } + let newTrackingArea = NSTrackingArea(rect: bounds, options: [.mouseEnteredAndExited, .activeAlways], owner: self, userInfo: nil) + addTrackingArea(newTrackingArea) + } + + override func mouseEntered(with event: NSEvent) { + onMouseEntered?(event, self) + } + + override func mouseExited(with event: NSEvent) { + onMouseExited?(event, self) + } +} diff --git a/src/ui/preferences-window/LabelAndControl.swift b/src/ui/preferences-window/LabelAndControl.swift index 43065fb3..e5eca1de 100644 --- a/src/ui/preferences-window/LabelAndControl.swift +++ b/src/ui/preferences-window/LabelAndControl.swift @@ -178,7 +178,19 @@ class LabelAndControl: NSObject { return views } - static func makeInfoButton(width: CGFloat, height: CGFloat) -> ClickHoverImageView { + static func makeCheckbox(_ rawName: String, extraAction: ActionClosure? = nil) -> NSButton { + let checkbox = NSButton(checkboxWithTitle: "", target: nil, action: nil) + checkbox.translatesAutoresizingMaskIntoConstraints = false + checkbox.state = defaults.bool(rawName) ? .on : .off + _ = setupControl(checkbox, rawName, extraAction: extraAction) + return checkbox + } + + static func makeInfoButton(width: CGFloat = 15, + height: CGFloat = 15, + onClick: EventClosure? = nil, + onMouseEntered: EventClosure? = nil, + onMouseExited: EventClosure? = nil) -> ClickHoverImageView { let imageView = NSImageView(image: NSImage(named: "info_button")!) imageView.translatesAutoresizingMaskIntoConstraints = false imageView.imageScaling = .scaleProportionallyUpOrDown @@ -190,6 +202,9 @@ class LabelAndControl: NSObject { imageView.layer?.rasterizationScale = NSScreen.main?.backingScaleFactor ?? 1.0 let view = ClickHoverImageView(imageView: imageView) + view.onClick = onClick + view.onMouseEntered = onMouseEntered + view.onMouseExited = onMouseExited // Set constraints to add equal padding around the image NSLayoutConstraint.activate([ @@ -214,10 +229,7 @@ class LabelAndControl: NSObject { width: CGFloat = 15, height: CGFloat = 15) -> [NSView] { let labelCheckboxViews = makeLabelWithCheckbox(labelText, rawName, extraAction: extraAction, labelPosition: labelPosition) - let infoButtonView = makeInfoButton(width: width, height: height) - infoButtonView.onClick = onClick - infoButtonView.onMouseEntered = onMouseEntered - infoButtonView.onMouseExited = onMouseExited + let infoButtonView = makeInfoButton(width: width, height: height, onClick: onClick, onMouseEntered: onMouseEntered, onMouseExited: onMouseExited) var views: [NSView] = [] labelCheckboxViews.forEach { view in @@ -256,9 +268,9 @@ class LabelAndControl: NSObject { return popUp } - static func makeDropdown(_ rawName: String, _ macroPreferences: [MacroPreference]) -> NSControl { + static func makeDropdown(_ rawName: String, _ macroPreferences: [MacroPreference], extraAction: ActionClosure? = nil) -> NSControl { let dropdown = dropdown_(rawName, macroPreferences) - return setupControl(dropdown, rawName) + return setupControl(dropdown, rawName, extraAction: extraAction) } static func makeLabelWithRadioButtons(_ labelText: String, diff --git a/src/ui/preferences-window/tabs/AppearanceTab.swift b/src/ui/preferences-window/tabs/AppearanceTab.swift index a16f92f4..8ae18cac 100644 --- a/src/ui/preferences-window/tabs/AppearanceTab.swift +++ b/src/ui/preferences-window/tabs/AppearanceTab.swift @@ -4,8 +4,10 @@ struct ShowHideRowInfo { let rowId: String! var uncheckedImage: String! var checkedImage: String! - var components: [NSView]! // UI components associated with this item var supportedModels: [AppearanceModelPreference]! + var leftTitle: String! + var subTitle: String? + var rightViews = [NSView]() init() { self.rowId = UUID().uuidString @@ -13,6 +15,7 @@ struct ShowHideRowInfo { } class IllustratedImageThemeView: ClickHoverImageView { + static let padding = CGFloat(4) var model: AppearanceModelPreference! var theme: String! @@ -42,10 +45,10 @@ class IllustratedImageThemeView: ClickHoverImageView { NSLayoutConstraint.activate([ imageView.widthAnchor.constraint(equalToConstant: imageWidth), imageView.heightAnchor.constraint(equalToConstant: imageHeight), - imageView.topAnchor.constraint(equalTo: self.topAnchor, constant: 4), - imageView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -4), - imageView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 4), - imageView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -4), + imageView.topAnchor.constraint(equalTo: self.topAnchor, constant: IllustratedImageThemeView.padding), + imageView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -IllustratedImageThemeView.padding), + imageView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: IllustratedImageThemeView.padding), + imageView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -IllustratedImageThemeView.padding), ]) onClick = { event, view in imageView.image = NSImage(named: imageName) @@ -89,29 +92,22 @@ class ShowHideIllustratedView { illustratedImageView = IllustratedImageThemeView(model, ModelAdvancedSettingsWindow.illustratedImageWidth) } - func makeGridView() -> GridView { - // Add the illustrated image first - var settings: [[NSView]] = [ - [illustratedImageView], - ] - var modelToRows = [Int: ShowHideRowInfo]() - var index = 1 + func makeView() -> NSStackView { + let table = TableGroupView(width: ModelAdvancedSettingsWindow.width) for row in showHideRows { if row.supportedModels.contains(model) { - settings.append(row.components) - // match the row index of the grid - modelToRows[index] = row - index += 1 + table.addRow(row.leftTitle, row.rightViews, onMouseEntered: { event, view in + self.updateImageView(rowId: row.rowId) + }) } } - grid = GridView(settings) - setAlignment() - grid.column(at: 0).width = ModelAdvancedSettingsWindow.columnWidth - grid.rowSpacing = 0 - grid.row(at: 0).bottomPadding = TabView.padding - addMouseHoverEffects(modelToRows: modelToRows) - grid.fit() - return grid + table.fit() + + let view = NSStackView() + view.orientation = .vertical + view.spacing = 5 + view.setViews([illustratedImageView, table], in: .leading) + return view } private func setupItems() { @@ -119,154 +115,96 @@ class ShowHideIllustratedView { hideAppBadges.uncheckedImage = "show_app_badges" hideAppBadges.checkedImage = "hide_app_badges" hideAppBadges.supportedModels = [.thumbnails, .appIcons, .titles] - hideAppBadges.components = LabelAndControl.makeLabelWithCheckbox( - NSLocalizedString("Hide app badges", comment: ""), - "hideAppBadges", extraAction: { sender in - let button = sender as! NSButton - self.onCheckboxClicked(sender: button, rowId: hideAppBadges.rowId) - }, labelPosition: .right) + hideAppBadges.leftTitle = NSLocalizedString("Hide app badges", comment: "") + hideAppBadges.rightViews.append(LabelAndControl.makeCheckbox("hideAppBadges", extraAction: { sender in + self.onCheckboxClicked(sender: sender, rowId: hideAppBadges.rowId) + })) showHideRows.append(hideAppBadges) var hideStatusIcons = ShowHideRowInfo() hideStatusIcons.uncheckedImage = "show_status_icons" hideStatusIcons.checkedImage = "hide_status_icons" hideStatusIcons.supportedModels = [.thumbnails, .titles] - hideStatusIcons.components = LabelAndControl.makeLabelWithCheckboxAndInfoButton( - NSLocalizedString("Hide status icons", comment: ""), - "hideStatusIcons", extraAction: { sender in - let button = sender as! NSButton - self.onCheckboxClicked(sender: button, rowId: hideStatusIcons.rowId) - }, labelPosition: .right, onMouseEntered: { event, view in - Popover.shared.show(event: event, positioningView: view, message: NSLocalizedString("AltTab will show if the window is currently minimized or fullscreen with a status icon.", comment: "")) + hideStatusIcons.leftTitle = NSLocalizedString("Hide status icons", comment: "") + hideStatusIcons.subTitle = NSLocalizedString("AltTab will show if the window is currently minimized or fullscreen with a status icon.", comment: "") + hideStatusIcons.rightViews.append(LabelAndControl.makeInfoButton(width: 15, height: 15, onMouseEntered: { event, view in + Popover.shared.show(event: event, positioningView: view, message: hideStatusIcons.subTitle!) }, onMouseExited: { event, view in Popover.shared.hide() - }) + })) + hideStatusIcons.rightViews.append(LabelAndControl.makeCheckbox("hideStatusIcons", extraAction: { sender in + self.onCheckboxClicked(sender: sender, rowId: hideStatusIcons.rowId) + })) showHideRows.append(hideStatusIcons) var hideSpaceNumberLabels = ShowHideRowInfo() hideSpaceNumberLabels.uncheckedImage = "show_space_number_labels" hideSpaceNumberLabels.checkedImage = "hide_space_number_labels" hideSpaceNumberLabels.supportedModels = [.thumbnails, .titles] - hideSpaceNumberLabels.components = LabelAndControl.makeLabelWithCheckbox( - NSLocalizedString("Hide Space number labels", comment: ""), - "hideSpaceNumberLabels", extraAction: { sender in - let button = sender as! NSButton - self.onCheckboxClicked(sender: button, rowId: hideSpaceNumberLabels.rowId) - }, labelPosition: .right) + hideSpaceNumberLabels.leftTitle = NSLocalizedString("Hide Space number labels", comment: "") + hideSpaceNumberLabels.rightViews.append(LabelAndControl.makeCheckbox("hideSpaceNumberLabels", extraAction: { sender in + self.onCheckboxClicked(sender: sender, rowId: hideSpaceNumberLabels.rowId) + })) showHideRows.append(hideSpaceNumberLabels) var hideColoredCircles = ShowHideRowInfo() hideColoredCircles.uncheckedImage = "show_colored_circles" hideColoredCircles.checkedImage = "hide_colored_circles" hideColoredCircles.supportedModels = [.thumbnails] - hideColoredCircles.components = LabelAndControl.makeLabelWithCheckbox( - NSLocalizedString("Hide colored circles on mouse hover", comment: ""), - "hideColoredCircles", extraAction: { sender in - let button = sender as! NSButton - self.onCheckboxClicked(sender: button, rowId: hideColoredCircles.rowId) - }, labelPosition: .right) + hideColoredCircles.leftTitle = NSLocalizedString("Hide colored circles on mouse hover", comment: "") + hideColoredCircles.rightViews.append(LabelAndControl.makeCheckbox("hideColoredCircles", extraAction: { sender in + self.onCheckboxClicked(sender: sender, rowId: hideColoredCircles.rowId) + })) showHideRows.append(hideColoredCircles) var hideWindowlessApps = ShowHideRowInfo() hideWindowlessApps.uncheckedImage = "show_windowless_apps" hideWindowlessApps.checkedImage = "hide_windowless_apps" hideWindowlessApps.supportedModels = [.thumbnails, .appIcons, .titles] - hideWindowlessApps.components = LabelAndControl.makeLabelWithCheckbox( - NSLocalizedString("Hide apps with no open window", comment: ""), - "hideWindowlessApps", extraAction: { sender in - let button = sender as! NSButton - self.onCheckboxClicked(sender: button, rowId: hideWindowlessApps.rowId) - }, labelPosition: .right) + hideWindowlessApps.leftTitle = NSLocalizedString("Hide apps with no open window", comment: "") + hideWindowlessApps.rightViews.append(LabelAndControl.makeCheckbox("hideWindowlessApps", extraAction: { sender in + self.onCheckboxClicked(sender: sender, rowId: hideWindowlessApps.rowId) + })) showHideRows.append(hideWindowlessApps) var showTabsAsWindows = ShowHideRowInfo() showTabsAsWindows.uncheckedImage = "hide_tabs_as_windows" showTabsAsWindows.checkedImage = "show_tabs_as_windows" showTabsAsWindows.supportedModels = [.thumbnails, .appIcons, .titles] - showTabsAsWindows.components = LabelAndControl.makeLabelWithCheckboxAndInfoButton( - NSLocalizedString("Show standard tabs as windows", comment: ""), - "showTabsAsWindows", extraAction: { sender in - let button = sender as! NSButton - self.onCheckboxClicked(sender: button, rowId: showTabsAsWindows.rowId) - }, labelPosition: .right, onMouseEntered: { event, view in - Popover.shared.show(event: event, positioningView: view, message: NSLocalizedString("Some apps like Finder or Preview use standard tabs which act like independent windows. Some other apps like web browsers use custom tabs which act in unique ways and are not actual windows. AltTab can't list those separately.", comment: "")) + showTabsAsWindows.leftTitle = NSLocalizedString("Show standard tabs as windows", comment: "") + showTabsAsWindows.subTitle = NSLocalizedString("Some apps like Finder or Preview use standard tabs which act like independent windows. Some other apps like web browsers use custom tabs which act in unique ways and are not actual windows. AltTab can't list those separately.", comment: "") + showTabsAsWindows.rightViews.append(LabelAndControl.makeInfoButton(width: 15, height: 15, onMouseEntered: { event, view in + Popover.shared.show(event: event, positioningView: view, message: showTabsAsWindows.subTitle!) }, onMouseExited: { event, view in Popover.shared.hide() - }) + })) + showTabsAsWindows.rightViews.append(LabelAndControl.makeCheckbox("showTabsAsWindows", extraAction: { sender in + self.onCheckboxClicked(sender: sender, rowId: showTabsAsWindows.rowId) + })) showHideRows.append(showTabsAsWindows) var previewFocusedWindow = ShowHideRowInfo() previewFocusedWindow.uncheckedImage = "hide_preview_focused_window" previewFocusedWindow.checkedImage = "show_preview_focused_window" previewFocusedWindow.supportedModels = [.thumbnails, .appIcons, .titles] - previewFocusedWindow.components = LabelAndControl.makeLabelWithCheckbox( - NSLocalizedString("Preview selected window", comment: ""), - "previewFocusedWindow", extraAction: { sender in - let button = sender as! NSButton - self.onCheckboxClicked(sender: button, rowId: previewFocusedWindow.rowId) - }, labelPosition: .right) + previewFocusedWindow.leftTitle = NSLocalizedString("Preview selected window", comment: "") + previewFocusedWindow.rightViews.append(LabelAndControl.makeCheckbox("previewFocusedWindow", extraAction: { sender in + self.onCheckboxClicked(sender: sender, rowId: previewFocusedWindow.rowId) + })) showHideRows.append(previewFocusedWindow) } - private func addMouseHoverEffects(modelToRows: [Int: ShowHideRowInfo]) { - // Ignore the first row that stores the image - for rowIndex in 1.. Bool { - if let checkbox = view as? NSButton { - return checkbox.state == .on - } - for subview in view.subviews { - if findCheckboxState(in: subview) { - return true + private func updateImageView(rowId: String) { + let row = showHideRows.first { $0.rowId.elementsEqual(rowId) } + row?.rightViews.forEach { view in + if let checkbox = view as? NSButton { + let isChecked = checkbox.state == .on + let imageName = isChecked ? row?.checkedImage : row?.uncheckedImage + illustratedImageView.updateImage(imageName!) } } - return false } - /// Recursively finds all `NSButton` instances within a given `NSStackView` and its nested stack views. - /// - /// - Parameter stackView: The root `NSStackView` to search for buttons. - /// - Returns: An array of `NSButton` instances found within the stack view and its nested stack views. - private func findButtons(in stackView: NSStackView) -> [NSButton] { - var buttons: [NSButton] = [] - for subview in stackView.subviews { - if let button = subview as? NSButton { - buttons.append(button) - } else if let nestedStackView = subview as? NSStackView { - buttons.append(contentsOf: findButtons(in: nestedStackView)) - } - } - return buttons - } } class ModelAdvancedSettingsWindow: NSWindow { - static let columnWidth = CGFloat(450) - static let illustratedImageWidth = columnWidth - CGFloat(50) + static let width = CGFloat(512) + static let illustratedImageWidth = width - 50 + static let spacing = CGFloat(5) var model: AppearanceModelPreference = .thumbnails var illustratedImageView: IllustratedImageThemeView! - var alignThumbnails: [NSView]! - var titleTruncation: [NSView]! - var showAppsWindows: [NSView]! - var showAppNamesWindowTitles: [NSView]! + var alignThumbnails: TableGroupView.Row! + var titleTruncation: TableGroupView.Row! + var showAppsWindows: TableGroupView.Row! + var showAppNamesWindowTitles: TableGroupView.Row! var doneButton: NSButton! convenience init(_ model: AppearanceModelPreference) { @@ -329,21 +252,24 @@ class ModelAdvancedSettingsWindow: NSWindow { private func setupView() { illustratedImageView = IllustratedImageThemeView(model, ModelAdvancedSettingsWindow.illustratedImageWidth) - alignThumbnails = LabelAndControl.makeLabelWithDropdown(NSLocalizedString("Align windows:", comment: ""), - "alignThumbnails", AlignThumbnailsPreference.allCases, extraAction: { _ in + alignThumbnails = TableGroupView.Row(leftTitle: NSLocalizedString("Align windows:", comment: ""), + rightViews: [LabelAndControl.makeDropdown( + "alignThumbnails", AlignThumbnailsPreference.allCases, extraAction: { _ in self.showAlignThumbnailsIllustratedImage() - }) - titleTruncation = LabelAndControl.makeLabelWithDropdown(NSLocalizedString("Window title truncation:", comment: ""), - "titleTruncation", TitleTruncationPreference.allCases) - showAppsWindows = LabelAndControl.makeLabelWithRadioButtons(NSLocalizedString("Show running:", comment: ""), - "showAppsWindows", ShowAppsWindowsPreference.allCases, extraAction: { _ in + })]) + titleTruncation = TableGroupView.Row(leftTitle: NSLocalizedString("Window title truncation:", comment: ""), + rightViews: [LabelAndControl.makeDropdown("titleTruncation", TitleTruncationPreference.allCases)]) + showAppsWindows = TableGroupView.Row(leftTitle: NSLocalizedString("Show running:", comment: ""), + rightViews: LabelAndControl.makeRadioButtons(ShowAppsWindowsPreference.allCases, + "showAppsWindows", extraAction: { _ in self.toggleAppNamesWindowTitles() self.showAppsOrWindowsIllustratedImage() - }) - showAppNamesWindowTitles = LabelAndControl.makeLabelWithDropdown(NSLocalizedString("Show titles:", comment: ""), - "showAppNamesWindowTitles", ShowAppNamesWindowTitlesPreference.allCases, extraAction: { _ in + })) + showAppNamesWindowTitles = TableGroupView.Row(leftTitle: NSLocalizedString("Show titles:", comment: ""), + rightViews: [LabelAndControl.makeDropdown( + "showAppNamesWindowTitles", ShowAppNamesWindowTitlesPreference.allCases, extraAction: { _ in self.showAppsOrWindowsIllustratedImage() - }) + })]) doneButton = NSButton(title: NSLocalizedString("Done", comment: ""), target: self, action: #selector(onClicked(_:))) doneButton.translatesAutoresizingMaskIntoConstraints = false @@ -352,7 +278,7 @@ class ModelAdvancedSettingsWindow: NSWindow { doneButton.bezelColor = NSColor.controlAccentColor } - let showHideGrid = ShowHideIllustratedView(model).makeGridView() + let showHideView = ShowHideIllustratedView(model).makeView() var advancedView: NSView! if model == .thumbnails { @@ -363,21 +289,23 @@ class ModelAdvancedSettingsWindow: NSWindow { advancedView = makeTitlesView() } let tabView = TabView([ - (NSLocalizedString("Show & Hide", comment: ""), showHideGrid), + (NSLocalizedString("Show & Hide", comment: ""), showHideView), (NSLocalizedString("Advanced", comment: ""), advancedView), ]) tabView.translatesAutoresizingMaskIntoConstraints = false - tabView.widthAnchor.constraint(equalToConstant: tabView.maxIntrinsicContentSize().width + GridView.padding).isActive = true + tabView.widthAnchor.constraint(equalToConstant: ModelAdvancedSettingsWindow.width + 50).isActive = true - showHideGrid.translatesAutoresizingMaskIntoConstraints = false - showHideGrid.topAnchor.constraint(equalTo: tabView.tabViewItem(at: 0).view!.topAnchor).isActive = true - showHideGrid.bottomAnchor.constraint(equalTo: tabView.tabViewItem(at: 0).view!.bottomAnchor).isActive = true - showHideGrid.centerXAnchor.constraint(equalTo: tabView.tabViewItem(at: 0).view!.centerXAnchor).isActive = true + showHideView.translatesAutoresizingMaskIntoConstraints = false + showHideView.topAnchor.constraint(equalTo: tabView.tabViewItem(at: 0).view!.topAnchor).isActive = true + showHideView.bottomAnchor.constraint(equalTo: tabView.tabViewItem(at: 0).view!.bottomAnchor).isActive = true + showHideView.centerXAnchor.constraint(equalTo: tabView.tabViewItem(at: 0).view!.centerXAnchor).isActive = true + showHideView.centerYAnchor.constraint(equalTo: tabView.tabViewItem(at: 0).view!.centerYAnchor).isActive = true advancedView.translatesAutoresizingMaskIntoConstraints = false advancedView.topAnchor.constraint(equalTo: tabView.tabViewItem(at: 1).view!.topAnchor).isActive = true advancedView.bottomAnchor.constraint(equalTo: tabView.tabViewItem(at: 1).view!.bottomAnchor).isActive = true advancedView.centerXAnchor.constraint(equalTo: tabView.tabViewItem(at: 1).view!.centerXAnchor).isActive = true + advancedView.centerYAnchor.constraint(equalTo: tabView.tabViewItem(at: 1).view!.centerYAnchor).isActive = true let grid = GridView([ [tabView], @@ -389,71 +317,79 @@ class ModelAdvancedSettingsWindow: NSWindow { contentView = grid } - private func makeThumbnailsView() -> NSView { - let grid = GridView([ - [illustratedImageView], - alignThumbnails, - titleTruncation, - ]) - grid.row(at: 0).bottomPadding = TabView.padding - grid.cell(atColumnIndex: 0, rowIndex: 0).xPlacement = .center - grid.mergeCells(inHorizontalRange: NSRange(location: 0, length: 2), verticalRange: NSRange(location: 0, length: 1)) - grid.column(at: 0).xPlacement = .trailing - grid.column(at: 1).xPlacement = .leading - grid.column(at: 0).width = ModelAdvancedSettingsWindow.columnWidth * 0.5 - return grid + private func makeThumbnailsView() -> NSStackView { + let table = TableGroupView(width: ModelAdvancedSettingsWindow.width) + table.addRow(alignThumbnails, onMouseEntered: { event, view in + self.showAlignThumbnailsIllustratedImage() + }) + table.addRow(titleTruncation) + table.fit() + + let view = NSStackView() + view.orientation = .vertical + view.spacing = ModelAdvancedSettingsWindow.spacing + view.setViews([illustratedImageView, table], in: .leading) + return view } - private func makeAppIconsView() -> NSView { - let separator = AppearanceTab.makeSeparator() - let grid = GridView([ - [illustratedImageView], - showAppsWindows, - showAppNamesWindowTitles, - [separator], - alignThumbnails, - ]) - grid.row(at: 0).bottomPadding = TabView.padding - grid.cell(atColumnIndex: 0, rowIndex: 0).xPlacement = .center - grid.mergeCells(inHorizontalRange: NSRange(location: 0, length: 2), verticalRange: NSRange(location: 0, length: 1)) - grid.mergeCells(inHorizontalRange: NSRange(location: 0, length: 2), verticalRange: NSRange(location: 3, length: 1)) - grid.column(at: 0).xPlacement = .trailing - grid.column(at: 1).xPlacement = .leading - grid.column(at: 0).width = ModelAdvancedSettingsWindow.columnWidth * 0.3 - separator.widthAnchor.constraint(equalTo: illustratedImageView.widthAnchor).isActive = true + private func makeAppIconsView() -> NSStackView { + let table1 = TableGroupView(width: ModelAdvancedSettingsWindow.width) + table1.addRow(showAppsWindows, onMouseEntered: { event, view in + self.showAppsOrWindowsIllustratedImage() + }) + table1.addRow(showAppNamesWindowTitles, onMouseEntered: { event, view in + self.showAppsOrWindowsIllustratedImage() + }) + table1.fit() + + let table2 = TableGroupView(width: ModelAdvancedSettingsWindow.width) + table2.addRow(alignThumbnails, onMouseEntered: { event, view in + self.showAlignThumbnailsIllustratedImage() + }) + table2.fit() + + let view = NSStackView() + view.orientation = .vertical + view.spacing = ModelAdvancedSettingsWindow.spacing + view.setViews([illustratedImageView, table1, table2], in: .leading) + toggleAppNamesWindowTitles() - return grid + return view } - private func makeTitlesView() -> NSView { - let separator = AppearanceTab.makeSeparator() - let grid = GridView([ - [illustratedImageView], - showAppsWindows, - showAppNamesWindowTitles, - [separator], - titleTruncation, - ]) - grid.row(at: 0).bottomPadding = TabView.padding - grid.cell(atColumnIndex: 0, rowIndex: 0).xPlacement = .center - grid.mergeCells(inHorizontalRange: NSRange(location: 0, length: 2), verticalRange: NSRange(location: 0, length: 1)) - grid.mergeCells(inHorizontalRange: NSRange(location: 0, length: 2), verticalRange: NSRange(location: 3, length: 1)) - grid.column(at: 0).xPlacement = .trailing - grid.column(at: 1).xPlacement = .leading - grid.column(at: 0).width = ModelAdvancedSettingsWindow.columnWidth * 0.35 - separator.widthAnchor.constraint(equalTo: illustratedImageView.widthAnchor).isActive = true + private func makeTitlesView() -> NSStackView { + let table1 = TableGroupView(width: ModelAdvancedSettingsWindow.width) + table1.addRow(showAppsWindows, onMouseEntered: { event, view in + self.showAppsOrWindowsIllustratedImage() + }) + table1.addRow(showAppNamesWindowTitles, onMouseEntered: { event, view in + self.showAppsOrWindowsIllustratedImage() + }) + table1.fit() + + let table2 = TableGroupView(width: ModelAdvancedSettingsWindow.width) + table2.addRow(alignThumbnails, onMouseEntered: { event, view in + self.showAlignThumbnailsIllustratedImage() + }) + table2.fit() + + let view = NSStackView() + view.orientation = .vertical + view.spacing = ModelAdvancedSettingsWindow.spacing + view.setViews([illustratedImageView, table1, table2], in: .leading) + toggleAppNamesWindowTitles() - return grid + return view } private func toggleAppNamesWindowTitles() { - let label = showAppNamesWindowTitles[0] as? TextField - let button = showAppNamesWindowTitles[1] as? NSControl +// let label = showAppNamesWindowTitles[0] as? TextField + let button = showAppNamesWindowTitles.rightViews[0] as? NSControl if Preferences.showAppsWindows == .windows { - label?.textColor = NSColor.textColor +// label?.textColor = NSColor.textColor button?.isEnabled = true } else { - label?.textColor = NSColor.gray +// label?.textColor = NSColor.gray button?.isEnabled = false } } @@ -527,7 +463,7 @@ class Popover: NSPopover { let locationInPositioningView = positioningView.convert(locationInWindow, from: nil) let rect = CGRect(origin: locationInPositioningView, size: .zero) - show(relativeTo: rect, of: positioningView, preferredEdge: .minY) + show(relativeTo: rect, of: positioningView, preferredEdge: .minX) } }