如何更改侧边栏的选择颜色?

时间:2019-09-08 00:56:54

标签: applescript-objc

是否可以更改侧边栏项目的选择颜色? 例如,macOS 10.14.x深色主题的默认颜色是蓝色,是否可以更改此颜色? 我在这里看了一下:Cocoa osx NSTableview change row highlight color 但是我要先翻译成Applescript有点困难。 enter image description here

1 个答案:

答案 0 :(得分:1)

如果没有示例或MCVE项目,提供嵌入式解决方案将非常棘手。您链接到的主题和答案将覆盖 NSTableView NSTableRowView 类,因此我可以为您提供通用的解决方案。根据您需要引用的内容,AppleScriptObjC中的子类化可能会有些麻烦,但相当简单。本质上,您是在常规类和应用程序之间放置一个自定义类,在其中可以拦截各种标准方法调用。

对于基于单元格的表视图示例,请使用文件>新建>文件... 菜单项将新的空文件添加到项目中。 _file的名称并不重要(例如TableViewHighlight.applescript或其他名称),脚本的名称就是Xcode将使用的名称。在这里,我使用MyTableView作为类名,并从AppDelegate引用tableView出口属性:

script MyTableView -- the name of your custom class

    property parent : class "NSTableView" -- the parent class to override
    property tableView : a reference to current application's NSApp's delegate's tableView
    property highlightColor : a reference to current application's NSColor's greenColor -- whatever

    # set the row highlight color
    on drawRow:row clipRect:clipRect
        if tableView's selectedRowIndexes's containsIndex:row then -- filter as desired
            highlightColor's setFill()
            current application's NSRectFill(tableView's rectOfRow:row)
        end if
        continue drawRow:row clipRect:clipRect -- super
    end drawRow:clipRect:

    # set the highlight color of stuff behind the row (grid lines, etc)
    on drawBackgroundInClipRect:clipRect
        highlightColor's setFill()
        current application's NSRectFill(clipRect)
        continue drawBackgroundInClipRect:clipRect -- super
    end drawBackgroundInClipRect:

end script

界面编辑器中,使用身份检查器将表格视图的类设置为MyTableView类。最后,在表格视图设置中,将其突出显示设置为“ none”,因为它将由您的子类完成(同样,假设tableView插座已连接到表格视图):

    tableView's setSelectionHighlightStyle: current application's NSTableViewSelectionHighlightStyleNone

对于基于基于视图的表视图的示例,该过程是相似的,但是 NSTableRowView 是要子类化的过程。这里我正在使用的脚本/类的名称为MyTableRowView

script MyTableRowView -- the name of your custom class

    property parent : class "NSTableRowView" -- the parent class to override
    property highlightColor : a reference to current application's NSColor's redColor -- whatever

    # draw the selected row
    on drawSelectionInRect:dirtyRect
        continue drawSelectionInRect:dirtyRect
        highlightColor's setFill()
        current application's NSRectFill(dirtyRect)
    end drawSelectionInRect:

end script

界面编辑器中,使用 Attributes Inspector 将表视图的突出显示设置为常规,并向表视图的委托添加tableView:rowViewForRow:方法:< / p>

on tableView:tableView rowViewForRow:row
    set rowIdentifier to "MyTableRow"
    set myRowView to tableView's makeViewWithIdentifier:rowIdentifier owner:me
    if myRowView is missing value then
        set myRowView to current application's MyTableRowView's alloc's initWithFrame:current application's NSZeroRect
        myRowView's setIdentifier:rowIdentifier
    end if
    return myRowView
end tableView:rowViewForRow:

当然还有其他选择,但这应该可以帮助您入门,并帮助翻译一些Objective-C的答案/示例。

相关问题