Skip to content

Commit

Permalink
Make VimR tab colors match 1:1 with colorscheme.
Browse files Browse the repository at this point in the history
Additionally: Pick up system accent color properly when no colors set.

NvimView+Resize.swift:
  - Now transmitting Tabline, TablineFill  colors to the VimR
    UI, as well as TablineSel
  - Replace the built-in get() + nvim_get_hl() method because they don't
   have handle 'link' highlights properly (they return -1 for color code)
  - defined a new GetHiColor vimscript method that behaves like get()
       but also reads `link` type highlight groups by following the
       link.

NvimView+Types.swift:
  - visualForegroundColor is picked up from user accent color in System
    Settings using NSColor.Name("controlAccentColor")
  - Updated to include all additional colors

NvimView+UiBridge.swift:
 - Updated MessagePackUtils value code to accomodate new color code fields

Tabs/Tab.swift:
 - Get Selected tab color from TablineSel

Tabs/TabBar.swift:
 - Set tab bar color to TablineFill

Tabs/Theme.swift:
- Add new fields for TablineFill and TablineSel color corresponding to
  tab bar background and selected tab colors
- Minor rearranging of field order

VimR/Theme.swift:
- Add new fields for the tab bar, tab, and selected tab colors
  • Loading branch information
s-daveb committed Jun 10, 2024
1 parent ef0b105 commit 6396944
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 44 deletions.
30 changes: 20 additions & 10 deletions NvimView/Sources/NvimView/NvimView+Resize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,26 @@ extension NvimView {
throw RxNeovimApi.Error.exception(message: "Incompatible neovim version.")
}

// swiftformat:disable all
return self.api.nvimExec2(src: """
let g:gui_vimr = 1
autocmd VimLeave * call rpcnotify(\(channel), 'autocommand', 'vimleave')
autocmd VimEnter * call rpcnotify(\(channel), 'autocommand', 'vimenter')
autocmd ColorScheme * call rpcnotify(\(channel), 'autocommand', 'colorscheme', get(nvim_get_hl(0, {'id': hlID('Normal')}), 'fg', -1), get(nvim_get_hl(0, {'id': hlID('Normal')}), 'bg', -1), get(nvim_get_hl(0, {'id': hlID('Visual')}), 'fg', -1), get(nvim_get_hl(0, {'id': hlID('Visual')}), 'bg', -1), get(nvim_get_hl(0, {'id': hlID('Directory')}), 'fg', -1), get(nvim_get_hl(0, {'id': hlID('TablineSel')}), 'bg', -1), get(nvim_get_hl(0, {'id': hlID('TablineSel')}), 'fg', -1))
autocmd VimEnter * call rpcrequest(\(channel), 'vimenter')
""", opts: [:], errWhenBlocked: false)
// swiftformat:enable all
.asCompletable()
// swiftformat:disable all
let vimscript = """
function! GetHiColor(hlID, component)
let color = synIDattr(synIDtrans(hlID(a:hlID)), a:component)
if empty(color)
return -1
else
return str2nr(color[1:], 16)
endif
endfunction
let g:gui_vimr = 1
autocmd VimLeave * call rpcnotify(\(channel), 'autocommand', 'vimleave')
autocmd VimEnter * call rpcnotify(\(channel), 'autocommand', 'vimenter')
autocmd ColorScheme * call rpcnotify(\(channel), 'autocommand', 'colorscheme', GetHiColor('Normal', 'fg'), GetHiColor('Normal', 'bg'), GetHiColor('Visual', 'fg'), GetHiColor('Visual', 'bg'), GetHiColor('Directory', 'fg'), GetHiColor('TablineFill', 'bg'), GetHiColor('TablineFill', 'fg'), GetHiColor('Tabline', 'bg'), GetHiColor('Tabline', 'fg'), GetHiColor('TablineSel', 'bg'), GetHiColor('TablineSel', 'fg'))
autocmd VimEnter * call rpcrequest(\(channel), 'vimenter')
"""

return self.api.nvimExec2(src: vimscript, opts: [:], errWhenBlocked: false)
.asCompletable()
// swiftformat:enable all
}
)
.andThen(
Expand Down
29 changes: 23 additions & 6 deletions NvimView/Sources/NvimView/NvimView+Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,28 @@ public extension NvimView {
public var foreground = NSColor.textColor
public var background = NSColor.textBackgroundColor

public var visualForeground = NSColor.selectedMenuItemTextColor
public var visualForeground: NSColor = NSColor(named: NSColor.Name("controlAccentColor")) ?? .selectedMenuItemTextColor
// NSColor.selectedMenuItemTextColor
// NSColor.selectedMenuItemColor is deprecated. The doc says that
// NSVisualEffectView.Material.selection should be used instead, but I don't know how to get
// an NSColor from it.
public var visualBackground = NSColor.selectedContentBackgroundColor

public var directoryForeground = NSColor.textColor

public var tabForeground = NSColor.textColor
public var tabBackground = NSColor.textBackgroundColor
public var tabForeground = NSColor.controlColor
public var tabBackground = NSColor.controlBackgroundColor

public var tabBarForeground = NSColor.textColor
public var tabBarBackground = NSColor.windowBackgroundColor

public var selectedTabForeground = NSColor.selectedTextColor
public var selectedTabBackground = NSColor.selectedTextBackgroundColor

public init() {}

public init(_ values: [Int]) {
if values.count < 7 { preconditionFailure("We need 7 colors!") }
if values.count < 11 { preconditionFailure("We need 11 colors!") }

let color = ColorUtils.colorIgnoringAlpha

Expand All @@ -121,15 +128,25 @@ public extension NvimView {
self.directoryForeground = values[4] < 0
? Theme.default.directoryForeground
: color(values[4])
self.tabBackground = values[5] < 0 ? Theme.default.background : color(values[5])
self.tabForeground = values[6] < 0 ? Theme.default.foreground : color(values[6])

self.tabBarBackground = values[5] < 0 ? Theme.default.tabBarBackground : color(values[5])
self.tabBarForeground = values[6] < 0 ? Theme.default.tabBarForeground : color(values[6])

self.tabBackground = values[7] < 0 ? Theme.default.tabBackground : color(values[7])
self.tabForeground = values[8] < 0 ? Theme.default.tabForeground : color(values[8])

self.selectedTabBackground = values[9] < 0 ? Theme.default.selectedTabBackground : color(values[9])
self.selectedTabForeground = values[10] < 0 ? Theme.default.selectedTabForeground : color(values[10])

}

public var description: String {
"NVV.Theme<" +
"fg: \(self.foreground.hex), bg: \(self.background.hex), " +
"visual-fg: \(self.visualForeground.hex), visual-bg: \(self.visualBackground.hex)" +
"tab-fg: \(self.tabForeground.hex), tab-bg: \(self.tabBackground.hex)" +
"tabfill-fg: \(self.tabBarForeground.hex), tabfill-bg: \(self.tabBarBackground.hex)" +
"tabsel-fg: \(self.selectedTabForeground.hex), tabsel-bg: \(self.selectedTabBackground.hex)" +
">"
}
}
Expand Down
2 changes: 1 addition & 1 deletion NvimView/Sources/NvimView/NvimView+UiBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ extension NvimView {

private func colorSchemeChanged(_ value: MessagePackValue) {
guard let values = MessagePackUtils.array(
from: value, ofSize: 7, conversion: { $0.intValue }
from: value, ofSize: 11, conversion: { $0.intValue }
) else {
self.bridgeLogger.error("Could not convert \(value)")
return
Expand Down
4 changes: 2 additions & 2 deletions Tabs/Sources/Tabs/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ extension Tab {

private func adjustColors(_ newIsSelected: Bool) {
if newIsSelected {
self.layer?.backgroundColor = self.theme.tabBackgroundColor.cgColor
self.titleView.textColor = self.theme.tabForegroundColor
self.layer?.backgroundColor = self.theme.selectedBackgroundColor.cgColor
self.titleView.textColor = self.theme.selectedForegroundColor
self.closeButton.image = self.theme.selectedCloseButtonImage
} else {
self.layer?.backgroundColor = self.theme.backgroundColor.cgColor
Expand Down
4 changes: 2 additions & 2 deletions Tabs/Sources/Tabs/TabBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public final class TabBar<Rep: TabRepresentative>: NSView {
super.init(frame: .zero)
self.configureForAutoLayout()
self.wantsLayer = true
self.layer?.backgroundColor = theme.backgroundColor.cgColor
self.layer?.backgroundColor = theme.tabBarBackgroundColor.cgColor

self.addViews()
}

public func update(theme: Theme) {
self._theme = theme
self.layer?.backgroundColor = theme.backgroundColor.cgColor
self.layer?.backgroundColor = theme.tabBarBackgroundColor.cgColor

self.needsDisplay = true
self.tabs.forEach { $0.updateTheme() }
Expand Down
23 changes: 10 additions & 13 deletions Tabs/Sources/Tabs/Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import MaterialIcons
public struct Theme {
public static let `default` = Self()

public var separatorColor = NSColor.gridColor
public var backgroundColor = NSColor.textBackgroundColor
public var foregroundColor = NSColor.textColor {
didSet {
self.closeButtonImage = Icon.close.asImage(
Expand All @@ -17,25 +19,21 @@ public struct Theme {
)
}
}

public var backgroundColor = NSColor.textBackgroundColor
public var separatorColor = NSColor.gridColor


public var selectedBackgroundColor = NSColor.selectedTextBackgroundColor
public var selectedForegroundColor = NSColor.selectedTextColor {
didSet {
self.selectedCloseButtonImage = Icon.close.asImage(
dimension: self.iconDimension.width,
color: self.tabForegroundColor
color: self.selectedForegroundColor
)
}
}

public var selectedBackgroundColor = NSColor.selectedTextBackgroundColor
public var tabSelectedIndicatorColor = NSColor.selectedTextColor

public var tabBackgroundColor = NSColor.selectedTextBackgroundColor
public var tabForegroundColor = NSColor.selectedTextColor

public var tabBarBackgroundColor = NSColor.windowBackgroundColor
public var tabBarForegroundColor = NSColor.textColor
public var titleFont = NSFont.systemFont(ofSize: 11)
public var selectedTitleFont = NSFont.boldSystemFont(ofSize: 11)

Expand All @@ -60,12 +58,11 @@ public struct Theme {
public init() {
self.closeButtonImage = Icon.close.asImage(
dimension: self.iconDimension.width,
color: self.tabForegroundColor

color: self.foregroundColor
)
self.selectedCloseButtonImage = Icon.close.asImage(
dimension: self.iconDimension.width,
color: self.tabForegroundColor
color: self.foregroundColor
)
}
}
12 changes: 6 additions & 6 deletions VimR/VimR/MainWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -470,16 +470,16 @@ final class MainWindow: NSObject,
private func set(tabsThemeWith _: Theme) {
var tabsTheme = Tabs.Theme.default

tabsTheme.foregroundColor = self.theme.foreground
tabsTheme.backgroundColor = self.theme.background
tabsTheme.foregroundColor = self.theme.tabForeground
tabsTheme.backgroundColor = self.theme.tabBackground

tabsTheme.separatorColor = self.theme.background.brightening(by: 0.75)

tabsTheme.selectedForegroundColor = self.theme.highlightForeground
tabsTheme.selectedBackgroundColor = self.theme.highlightBackground
tabsTheme.tabBarBackgroundColor = self.theme.tabBarBackground
tabsTheme.tabBarForegroundColor = self.theme.tabBarForeground

tabsTheme.tabBackgroundColor = self.theme.tabBackground
tabsTheme.tabForegroundColor = self.theme.tabForeground
tabsTheme.selectedForegroundColor = self.theme.selectedTabForeground
tabsTheme.selectedBackgroundColor = self.theme.selectedTabBackground

tabsTheme.tabSelectedIndicatorColor = self.theme.highlightForeground

Expand Down
22 changes: 18 additions & 4 deletions VimR/VimR/Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ struct Theme: CustomStringConvertible {
var tabForeground = NSColor.selectedMenuItemTextColor
var tabBackground = NSColor.selectedContentBackgroundColor

var tabBarForeground = NSColor.selectedMenuItemTextColor
var tabBarBackground = NSColor.selectedContentBackgroundColor

var selectedTabForeground = NSColor.selectedMenuItemTextColor
var selectedTabBackground = NSColor.selectedContentBackgroundColor

var cssColor = NSColor(hex: "24292e")!
var cssBackgroundColor = NSColor.white
var cssA = NSColor(hex: "0366d6")!
Expand All @@ -61,12 +67,14 @@ struct Theme: CustomStringConvertible {
var cssCodeColor = NSColor(hex: "24292e")!
var cssCodeBackgroundColor = NSColor(hex: "1b1f23")!

public var description: String {
public var description: String {
"Theme<" +
"fg: \(self.foreground.hex), bg: \(self.background.hex), " +
"hl-fg: \(self.highlightForeground.hex), hl-bg: \(self.highlightBackground.hex)" +
"dir-fg: \(self.directoryForeground.hex)," +
"tab-bg: \(self.tabBackground.hex), tab-fg: \(self.tabForeground.hex)" +
"hl-fg: \(self.highlightForeground.hex), hl-bg: \(self.highlightBackground.hex), " +
"dir-fg: \(self.directoryForeground.hex), " +
"tab-fg: \(self.tabForeground.hex), tab-bg: \(self.tabBackground.hex), " +
"tabfill-fg: \(self.tabBarForeground.hex), tabfill-bg: \(self.tabBarBackground.hex), " +
"tabsel-bg: \(self.selectedTabBackground.hex), tabsel-fg: \(self.selectedTabForeground.hex)" +
">"
}

Expand All @@ -84,6 +92,12 @@ struct Theme: CustomStringConvertible {
self.tabBackground = nvimTheme.tabBackground
self.tabForeground = nvimTheme.tabForeground

self.tabBarBackground = nvimTheme.tabBarBackground
self.tabBarForeground = nvimTheme.tabBarForeground

self.selectedTabBackground = nvimTheme.selectedTabBackground
self.selectedTabForeground = nvimTheme.selectedTabForeground

self.updateCssColors(additionalColorDict)
}

Expand Down

0 comments on commit 6396944

Please sign in to comment.