Skip to content

Commit 72c3231

Browse files
committed
Refactor Neovim startup initialization
1 parent 98a7da6 commit 72c3231

File tree

5 files changed

+102
-88
lines changed

5 files changed

+102
-88
lines changed

editor/editor.go

Lines changed: 63 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ type Editor struct {
127127
font *Font
128128
fallbackfonts []*Font
129129
fontCh chan []*Font
130+
nvimErr error
131+
nvimSignal *neovimSignal
132+
redrawUpdates chan [][]interface{}
133+
guiUpdates chan []interface{}
134+
nvimCh chan *nvim.Nvim
135+
uiRemoteAttachedCh chan bool
136+
nvimErrCh chan error
137+
qAppStartedCh chan bool
130138
fontErrors []string
131139
notifications []*Notification
132140
workspaces []*Workspace
@@ -146,6 +154,7 @@ type Editor struct {
146154
doRestoreSessions bool
147155
initialColumns int
148156
initialLines int
157+
isSetWindowState bool
149158
isSetColumns bool
150159
isSetLines bool
151160
isSetGuiColor bool
@@ -159,6 +168,8 @@ type Editor struct {
159168
isHideMouse bool
160169
isBindNvimSizeToAppwin bool
161170
isUiPrepared bool
171+
firstPaintDone bool
172+
blurUpdateNeeded bool
162173
}
163174

164175
func (hl *Highlight) copy() Highlight {
@@ -217,8 +228,15 @@ func InitEditor(options Options, args []string) {
217228
notify: make(chan *Notify, 10),
218229
cbChan: make(chan *string, 240),
219230
chUiPrepared: make(chan bool, 1),
231+
nvimErrCh: make(chan error, 1),
220232
}
221233
e := editor
234+
if runtime.GOOS == "darwin" {
235+
e.qAppStartedCh = make(chan bool, 1)
236+
}
237+
if runtime.GOOS == "darwin" && editor.config.Editor.EnableBackgroundBlur {
238+
e.blurUpdateNeeded = true
239+
}
222240

223241
// Prepare debug log
224242
e.setDebuglog()
@@ -246,36 +264,35 @@ func InitEditor(options Options, args []string) {
246264
// set application working directory path
247265
e.setAppDirPath(e.homeDir)
248266

249-
// create qapplication
250-
e.putLog("start generating the application")
251-
core.QCoreApplication_SetAttribute(core.Qt__AA_EnableHighDpiScaling, true)
252-
e.app = widgets.NewQApplication(len(os.Args), os.Args)
253-
setMyApplicationDelegate()
254-
255-
e.app.SetDoubleClickInterval(0)
256-
e.putLog("finished generating the application")
257-
258-
e.initNotifications()
259-
260267
var cerr, lerr error
261268
e.initialColumns, cerr, e.initialLines, lerr = parseLinesAndColumns(args)
262-
if cerr == nil {
263-
editor.isSetColumns = true
264-
}
265-
if lerr == nil {
266-
editor.isSetLines = true
267-
}
269+
editor.isSetColumns, editor.isSetLines = cerr == nil, lerr == nil
268270

269271
// new nvim instance
270-
signal, redrawUpdates, guiUpdates, nvimCh, uiRCh, errCh := newNvim(
272+
e.nvimSignal, e.redrawUpdates, e.guiUpdates, e.nvimCh, e.uiRemoteAttachedCh, e.nvimErrCh = newNvim(
271273
e.initialColumns,
272274
e.initialLines,
273275
e.ctx,
274276
)
275277

276-
// e.setAppDirPath(home)
278+
// create qapplication
279+
e.putLog("start generating the application")
280+
281+
core.QCoreApplication_SetAttribute(core.Qt__AA_EnableHighDpiScaling, true)
282+
283+
e.app = widgets.NewQApplication(len(os.Args), os.Args)
284+
if runtime.GOOS == "darwin" {
285+
e.qAppStartedCh <- e.app != nil
286+
}
287+
288+
setMyApplicationDelegate()
289+
e.putLog("finished generating the application")
290+
291+
e.app.SetDoubleClickInterval(0)
292+
293+
e.initNotifications()
277294

278-
e.fontCh = make(chan []*Font, 100)
295+
e.fontCh = make(chan []*Font, 1)
279296
go func() {
280297
e.fontCh <- parseFont(
281298
e.config.Editor.FontFamily,
@@ -296,27 +313,14 @@ func InitEditor(options Options, args []string) {
296313
e.initSpecialKeys()
297314

298315
// application main window
299-
isSetWindowState := e.initAppWindow()
316+
e.isSetWindowState = e.initAppWindow()
317+
e.setWindowLayout()
318+
e.window.SetWindowOpacity(0.0)
300319
e.window.Show()
301320

302321
// Apply native title bar customization
303322
e.applyNativeTitlebarCustomization()
304323

305-
// window layout
306-
e.setWindowLayout()
307-
308-
// neovim workspaces
309-
310-
nvimErr := <-errCh
311-
if nvimErr != nil {
312-
fmt.Println(nvimErr)
313-
os.Exit(1)
314-
}
315-
316-
e.initWorkspaces(e.ctx, signal, redrawUpdates, guiUpdates, nvimCh, uiRCh, isSetWindowState)
317-
318-
e.connectAppSignals()
319-
320324
// go e.exitEditor(cancel, f, g)
321325
// go e.exitEditor(cancel, f, fgprofStop)
322326
go e.exitEditor(cancel)
@@ -603,6 +607,12 @@ func (e *Editor) initWorkspaces(ctx context.Context, signal *neovimSignal, redra
603607

604608
ws.initFont()
605609
e.initAppFont()
610+
611+
e.nvimErr = <-e.nvimErrCh
612+
if e.nvimErr != nil {
613+
fmt.Println(e.nvimErr)
614+
os.Exit(1)
615+
}
606616
ws.registerSignal(signal, redrawUpdates, guiUpdates)
607617
ws.updateSize()
608618

@@ -1056,7 +1066,10 @@ func (e *Editor) updateGUIColor() {
10561066
e.window.SetupTitleColor((uint16)(e.colors.fg.R), (uint16)(e.colors.fg.G), (uint16)(e.colors.fg.B))
10571067
}
10581068

1059-
// e.window.SetWindowOpacity(1.0)
1069+
if !e.firstPaintDone {
1070+
e.firstPaintDone = true
1071+
e.window.SetWindowOpacity(1.0)
1072+
}
10601073
e.putLog("finished updating GUI color")
10611074
}
10621075

@@ -1148,6 +1161,19 @@ func (e *Editor) connectWindowEvents() {
11481161
e.bindResizeEvent()
11491162
e.window.ConnectShowEvent(func(event *gui.QShowEvent) {
11501163
editor.putLog("show application window")
1164+
e.initWorkspaces(e.ctx, e.nvimSignal, e.redrawUpdates, e.guiUpdates, e.nvimCh, e.uiRemoteAttachedCh, e.isSetWindowState)
1165+
e.connectAppSignals()
1166+
1167+
// Set Transparent blue effect
1168+
if runtime.GOOS == "darwin" && editor.config.Editor.EnableBackgroundBlur {
1169+
if e.blurUpdateNeeded {
1170+
e.blurUpdateNeeded = false
1171+
ws := e.workspaces[e.active]
1172+
ws.getBG()
1173+
isLight := ws.screenbg == "light"
1174+
editor.window.SetBlurEffectForMacOS(isLight)
1175+
}
1176+
}
11511177
})
11521178

11531179
e.window.InstallEventFilter(e.window)

editor/imetooltip.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,8 @@ func (i *IMETooltip) parsePreeditString(preeditStr string) {
230230
return
231231
}
232232

233-
if i.s.ws.screenbg == "light" {
234-
currConvHl.foreground = warpColor(i.s.ws.background, -30)
235-
currConvHl.background = warpColor(i.s.ws.foreground, -30)
236-
} else {
237-
currConvHl.foreground = warpColor(i.s.ws.foreground, 30)
238-
currConvHl.background = warpColor(i.s.ws.background, 30)
239-
}
233+
currConvHl.foreground = warpColor(i.s.ws.background, -30)
234+
currConvHl.background = warpColor(i.s.ws.foreground, -30)
240235
currConvHl.underline = true
241236
}
242237

editor/nvim.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,23 @@ func newNvim(cols, rows int, ctx context.Context) (signal *neovimSignal, redrawU
4545
// }()
4646

4747
go func() {
48+
if runtime.GOOS == "darwin" {
49+
select {
50+
case qAppStarted := <-editor.qAppStartedCh:
51+
if !qAppStarted {
52+
return
53+
}
54+
}
55+
}
56+
4857
neovim, uiRemoteAttached, err = startNvim(signal, ctx)
4958
if err != nil {
5059
errCh <- err
5160
return
5261
} else {
5362
errCh <- nil
5463
}
55-
setVar(neovim)
64+
setChannelID(neovim)
5665
setGoneovim(neovim)
5766
setGoneovimCommands(neovim)
5867
registerHandler(neovim, signal, redrawUpdates, guiUpdates)
@@ -62,18 +71,14 @@ func newNvim(cols, rows int, ctx context.Context) (signal *neovimSignal, redrawU
6271
uiRemoteAttachedCh <- uiRemoteAttached
6372
}()
6473

65-
// // Suppress the problem that cmd.Start() hangs on MacOS.
66-
// time.Sleep(3 * time.Millisecond)
67-
6874
return
6975
}
7076

7177
func startNvim(signal *neovimSignal, ctx context.Context) (neovim *nvim.Nvim, uiRemoteAttached bool, err error) {
7278
editor.putLog("starting nvim")
7379

7480
option := []string{
75-
"--cmd",
76-
"let g:goneovim=1",
81+
"--cmd", "let g:goneovim=1",
7782
"--embed",
7883
}
7984

@@ -129,8 +134,10 @@ func startNvim(signal *neovimSignal, ctx context.Context) (neovim *nvim.Nvim, ui
129134
}
130135

131136
func registerHandler(neovim *nvim.Nvim, signal *neovimSignal, redrawUpdates chan [][]interface{}, guiUpdates chan []interface{}) {
137+
editor.putLog("start registerHandler()")
132138
handleRequest(neovim)
133139
handleNotification(neovim, signal, redrawUpdates, guiUpdates)
140+
editor.putLog("done registerHandler()")
134141
}
135142

136143
func handleRequest(neovim *nvim.Nvim) {
@@ -226,8 +233,6 @@ func handleNotification(neovim *nvim.Nvim, signal *neovimSignal, redrawUpdates c
226233
redrawUpdates <- updates
227234
signal.RedrawSignal()
228235
})
229-
230-
neovim.Subscribe("Gui")
231236
neovim.RegisterHandler("Gui", func(updates ...interface{}) {
232237
if !editor.isUiPrepared {
233238
select {
@@ -435,9 +440,11 @@ func setGoneovim(neovim *nvim.Nvim) {
435440
}
436441
registerScripts := fmt.Sprintf(`call execute(%s)`, util.SplitVimscript(gonvimAutoCmds))
437442
neovim.Command(registerScripts)
443+
editor.putLog("done setGoneovim()")
438444
}
439445

440446
func setGoneovimCommands(neovim *nvim.Nvim) {
447+
editor.putLog("start setGoneovimCommands()")
441448
// Definition of the commands that goneovim provides
442449
gonvimCommands := fmt.Sprintf(`
443450
command! -nargs=1 GonvimResize call rpcnotify(g:goneovim_channel_id, "Gui", "gonvim_resize", <args>)
@@ -474,6 +481,7 @@ func setGoneovimCommands(neovim *nvim.Nvim) {
474481
`
475482
registerScripts := fmt.Sprintf(`call execute(%s)`, util.SplitVimscript(gonvimCommands))
476483
neovim.Command(registerScripts)
484+
editor.putLog("done setGoneovimCommands()")
477485
}
478486

479487
func setGoneovimClipBoard(neovim *nvim.Nvim) {
@@ -513,8 +521,10 @@ func setGoneovimClipBoard(neovim *nvim.Nvim) {
513521
}
514522
}
515523

516-
func setVar(neovim *nvim.Nvim) {
517-
go neovim.SetVar("goneovim_channel_id", neovim.ChannelID())
524+
func setChannelID(neovim *nvim.Nvim) {
525+
editor.putLog("starting to set goneovim_channel_id")
526+
neovim.SetVar("goneovim_channel_id", neovim.ChannelID())
527+
editor.putLog("done setting goneovim_channel_id")
518528
}
519529

520530
func source(neovim *nvim.Nvim, file string) {

editor/scrollbar.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,11 @@ func newScrollBar() *ScrollBar {
4747
}
4848

4949
func (s *ScrollBar) thumbEnter(e *core.QEvent) {
50-
shift := -40
51-
if s.ws.screenbg == "light" {
52-
shift = 40
53-
}
54-
color := warpColor(s.fg, shift)
55-
s.thumb.SetStyleSheet(fmt.Sprintf(" * { background: %s;}", color))
50+
s.thumb.SetStyleSheet(fmt.Sprintf(" * { background: %s;}", s.fg.OverlayAccent(0.5)))
5651
}
5752

5853
func (s *ScrollBar) thumbLeave(e *core.QEvent) {
59-
color := s.fg
60-
s.thumb.SetStyleSheet(fmt.Sprintf(" * { background: %s;}", color))
54+
s.thumb.SetStyleSheet(fmt.Sprintf(" * { background: %s;}", s.fg.OverlayAccent(0.25)))
6155
}
6256

6357
func (s *ScrollBar) thumbPress(e *gui.QMouseEvent) {
@@ -156,7 +150,7 @@ func (s *ScrollBar) setColor() {
156150
if editor.config.ScrollBar.Color != "" {
157151
s.fg = hexToRGBA(editor.config.ScrollBar.Color)
158152
}
159-
s.thumb.SetStyleSheet(fmt.Sprintf(" * { background: %s;}", s.fg.String()))
153+
s.thumb.SetStyleSheet(fmt.Sprintf(" * { background: %s;}", s.fg.OverlayAccent(0.25)))
160154
s.widget.SetStyleSheet(" * { background: rgba(0, 0, 0, 0);}")
161155
}
162156

0 commit comments

Comments
 (0)