Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make VimR tab colors match 1:1 with colorscheme. #1072

Merged
merged 1 commit into from
Jun 16, 2024
Merged
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
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