-
Notifications
You must be signed in to change notification settings - Fork 29
/
theme.fs
147 lines (119 loc) · 4.22 KB
/
theme.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
module FVim.theme
open ui
open def
open log
open Avalonia
open Avalonia.Media
#nowarn "0025"
let private trace fmt = trace "theme" fmt
// global theme configuration about colors, fonts, cursor, etc.
let mutable hi_defs = Array.create<HighlightAttr> 256 HighlightAttr.Default
let mutable mode_defs = Array.empty<ModeInfo>
let mutable semhl = Map.empty
let mutable guifont = DefaultFont
let mutable guifontwide = DefaultFontWide
let mutable fontsize = 16.0
let mutable cursor_enabled = true
let mutable default_fg = Colors.White
let mutable default_bg = Colors.Black
let mutable default_sp = Colors.Red
let hlchange_ev = Event<int>()
let fontconfig_ev = Event<unit>()
let cursoren_ev = Event<bool>()
let themeconfig_ev = Event<Color*Color*Color*Color*Color*Color*Color*Color>()
let fontConfig() =
fontsize <- max fontsize 1.0
//trace "fontConfig: guifont=%s guifontwide=%s" guifont guifontwide
fontconfig_ev.Trigger()
let setHighlight x =
if hi_defs.Length < x.id + 1 then
System.Array.Resize(&hi_defs, x.id + 100)
hi_defs.[x.id] <- x
if x.id = 0 then
default_fg <- x.rgb_attr.foreground.Value
default_bg <- x.rgb_attr.background.Value
default_sp <- x.rgb_attr.special.Value
hlchange_ev.Trigger(x.id)
let setModeInfo (cs_en: bool) (info: ModeInfo[]) =
mode_defs <- info
cursor_enabled <- cs_en
cursoren_ev.Trigger cs_en
let GetDrawAttrs hlid =
let attrs = hi_defs.[hlid].rgb_attr
let mutable fg = Option.defaultValue default_fg attrs.foreground
let mutable bg = Option.defaultValue default_bg attrs.background
let mutable sp = Option.defaultValue fg attrs.special
if attrs.reverse then
let tmp = fg
fg <- bg
bg <- tmp
let alpha =
if bg = default_bg then 0uy
else byte(states.background_altopacity * 255.0)
bg <- Avalonia.Media.Color(alpha, bg.R, bg.G, bg.B)
fg, bg, sp, attrs
do
let fg,bg,sp,_ = GetDrawAttrs 0
default_bg <- bg
default_fg <- fg
default_sp <- sp
let getSemanticHighlightGroup x =
semhl.TryFind x
|> Option.defaultValue 1
|> GetDrawAttrs
let setSemanticHighlightGroups grp =
semhl <- grp
// update themable elements color
let [ nfg, nbg, _, _
sfg, sbg, _, _
scfg, scbg, _, _
_, bbg, _, _
ifg, _, _, _] =
[
SemanticHighlightGroup.Pmenu
SemanticHighlightGroup.PmenuSel
SemanticHighlightGroup.PmenuSbar
SemanticHighlightGroup.VertSplit
SemanticHighlightGroup.Folded
]
|> List.map getSemanticHighlightGroup
themeconfig_ev.Trigger(nfg, nbg, sfg, sbg, scfg, scbg, bbg, ifg)
let setDefaultColors fg bg sp =
let bg =
if fg = bg && bg = sp then GetReverseColor bg
else bg
setHighlight {
id = 0
info = [||]
cterm_attr = RgbAttr.Empty
rgb_attr = {
foreground = Some fg
background = Some bg
special = Some sp
reverse = false
italic = false
bold = false
underline = false
undercurl = false
}
}
(*trace "setDefaultColors: %A %A %A" fg bg sp*)
let setOption (opt: UiOption) =
(*trace "setOption: %A" opt*)
let (|FN|_|) (x: string) =
// try to parse with 'font\ name:hNN'
match x.Split(':') with
| [|name; size|] when size.Length > 0 && size.[0] = 'h' -> Some(name.Trim('\'', '"'), size.Substring(1).TrimEnd('\'','"') |> float)
| _ -> None
let mutable config_font = true
match opt with
| Guifont(FN(name, sz)) -> guifont <- name; fontsize <- sz
| GuifontWide(FN(name, sz)) -> guifontwide <- name; fontsize <- sz
| Guifont("+") | GuifontWide("+") -> fontsize <- fontsize + 1.0
| Guifont("-") | GuifontWide("-") -> fontsize <- fontsize - 1.0
| Guifont(".+") | GuifontWide(".+") -> fontsize <- fontsize + 0.1
| Guifont(".-") | GuifontWide(".-") -> fontsize <- fontsize - 0.1
| _ -> config_font <- false
if config_font then fontConfig()
let hiattrDefine (hls: HighlightAttr[]) =
Array.iter setHighlight hls