Skip to content

Commit

Permalink
fix!: Improve render loop (#1977)
Browse files Browse the repository at this point in the history
* Split the grid into separate lines

* Move scroll_region to the grid

* Add some unit tests for grid scrolling

* Use a ringbuffer to represent the grid

This optimizes the scrolling a bit, especially pure up/down.

* Draw each line as a skia picture

This currently disables smooth scrolling. Which will be re-implemented
using the scroll events and the skia pictures instead of the old
scrolling snapshots.

* Reimplement scrolling

* Use simple pd controller for scrolling

* Implement floating window transparency

This also optimizes the blurring step, to not be done unless needed.
Also removes the neovide_floating_opacity setting, since it's not clear
how that should interact with neovim's default way of dealing with
transparncy.

* Optimize the foreground drawing a bit

* Use pixel scroll offsets

* Use scroll_delta

* Draw directly without any surface

Also speed up rendering of transparent surfaces

* Remove win_viewport hack

* Remove the undocumented floating_opacity setting

* Split rendering and animation

The animate function returs a boolean that tells if the animation should
continue or not.

* Improve the event loop

All events are now processed before allowing rendering.

* Remove the redraw_scheduler

The various update functions, which are cheap to run, are always run and
return true when rendering is needed.

* Run the animation with equal and maximum step size

* Filter the frame dt by a moving average

* Use gr_context.flush_and_submit

This properly submits everything to the GPU

* Add a render thread

The default event loop on winit, at least on Windows is too slow. See
rust-windowing/winit#2782

* Implement vsync sources

Windows using DwmFlush, Wayland using frame callbacks, a timer based
vsync when --novsync is given, and standard swap_frame otherwise.

Note on Windows, the there's some kind of race condition in the
opengl implementation and waiting for DwmFlush before swapping fixes that.

* Add some profiling blocks

* Optimize font metrics

This is done by caching the info, instead of re-calculating each time

* Fix animations only running on one Window at a time

* Document neovide far scroll lines

* Improve performance, shape only the visible lines

* Improve the scroll animation length documentation

* Document neovide_refresh_rate

* Update the actual rendered window on Flush rather than WinViewport

* Require Neovim 0.9.2

* Rename neovide_scroll_animation_far_scroll_lines to neovide_scroll_animation_far_lines

Also fix the documentation

* Remove the far scroll configuration in favor of always scrolling like normal which looks better

* Make multigrid default

* Update multigrid docs

* Review fixes

* Fix lint errors

* Fix reversed custom background condition

* refactor: removing of nestings and early returns where possible

* codestyle: calm rustfmt

* fix: remove debugging leftover override of swap interval

* feat: rename no_multigrid -> nomultigrid only on cmdline

* Revert "Remove the far scroll configuration in favor of always scrolling like normal which looks better"

This reverts commit b2ec34d.

* Fix far scroll direction

* feat(config)!: rename all config option sources to kebab-case

* feat!: make frame naming in config file kebab-case like cmdline

* refactor: fix typos in test names

* codestyle: calm clippy in cmdline tests

* Remove unneccessary flush command

* Add a ringbuffer struct

* Convert grid to use ringbuffer

* Implement clone_from_iter for the RingBuffer

* Make scrollback_lines use RingBuffer

* Convert actual_lines to RingBuffer

* Add iter_range to RingBuffer

* Clean up the code using iter_range

* Add size_hint to RingBuffer iterator

* fix: Improve windows creation and sizing (#2027)

* Move the window reposition code from WindowWrapper to creation

* Handle columns and lines options

* Create the window with the correct size at startup

* Support the geometry parameter

* Support setting lines and columns from init.lua/vim

* Fix the neovide channel id

* Split window creation and main loop

* Don't show the window until the final size is determined

* Improve the logging

* Make the code easier to read

* Set neovide_channel_id on all platforms

* Cleanup the grid size calculation

* docs: add unreleased yet note

* Fix new clippy warnings from rust 1.72

* Fix clippy warnings

* Try to position the floating windows inside the grid

* Reposition to messages inside the window

* Make sure that a redraw is performed when a floating window is moved

* Fix windows positioning with padding

* Refactor window padding (store it only once)

* Fix some problems with the padding

Ensure that it's always dynamically calculated. Also fix some cases
where it was calculated wrong.

* Update to Winit 0.29 master (#2035)

* Update dependencies

Winit master 0.29.1-beta#2422ea39d0e97fd43698391f84d9300cd169d8cd
Glutin 0.4.1-beta

* Fix winit breaking changes

* Do a clean exit, correctly waiting for Neovim to shut down

* Call neovide.quit at VimLeavePre instead of VimLeave

This is recommended here neovim/neovim#7727 (comment).
But I don't think it has any practical difference, other than perhaps
slightly speed up the exit process.

* Fix macOS build

* Use winit throttling on Wayland instead of a custom solution

Also always redraw on RedrawRequested. This fixes the windows not
getting updated when moved in from off-screen for example.

* Fix clippy warnings

* Split neovim bridge into launch and attach phases

This makes it possible to create the window with the correct size after
it's launched, and ginit.vim/lua is processed.

* codestyle: use explicit Arc::clone

---------

Co-authored-by: MultisampledNight <contact@multisamplednight.com>

* Show the window on any grid line modification and WinViewport

* Remove UIReady callback

* Ensure that the IME is updated

* Group geometry related command line settings and update documentation

* Support resizing the window using --geometry

Both with a size from the command line and reading the size from
init.vim/lua

* Rename geometry to grid size

* Fix cmd_line tests

* Fix the unreleased yet tag that was lost during the rebase

* Move initial setup to lua (#2062)

This cleans up the code and makes it slightly faster

* fix: Fix some crashes that can happen during exit (#2076)

* Basic structure for handling errors at startup

* Special case for clap errors

* Change the startup order a bit to support building an error window

* Move startup error handling to error_handling.rs

* Create an error window

But it still does not draw anything

* Simple error text drawing

* More complex error window with scrolling

* Support copying to clipboard

* Add help text

* Use target_os instead of cfg(target) and install textlayout

For some reason cfg(target) seemed to use the wrong configuration on
WSL in some cases. It also seemed to not work when specified only for
skia, so now skia is has been move to the same location as other Os
dependent dependencies.

Also make sure that textlayout is installed on all platforms, from
packages with binaries.

* Refactor the help message

* Cleanup the code

* Handle mouse scroll

* Set default and minimum size

* Return errors from command.rs

* Handle launch errors

* Enable backtraces

* Report errors from nvim_attach

The launch and attach functions are now combined, since there's no need
to keep them splitted after some re-organization that was done.

* Report errors during setup of the neovim state

* Report errors when reading and setting the initial setting values

* Log the error messages

* Fix the tests

* Use gl and textlayout for Skia on all platforms

Egl, X11 and Wayland are not needed in our use cases.

* Cleanup and fix error reporting

Errors from Neovide were not reported correctly.

* Fix clippy warning

* Clean up the error window rendering

Don't call layout twice

* Move maybe_disown to just after command-line parsing

It's not safe to fork after threads have been created, but it still
feels more natural to output command line help in the terminal than in a
separate window, so it's delayed a little bit.

The starting of the profiler is also moved to after the fork, because it
creates threads.

* Properly fork the process on Linux and Mac

On Windows the subsystem is set to Windows, so no console is created.
The errors are always reported through the GUI.

* Fix exit crash on Windows

* Fix mixed min and default size

* Improve the crash message

* Don't panic when the event aggregator channel is closed.

Closing is a normal behaviour during shutdown, and those are the only
possible errors, so the error can safely be ignored.

* Don't panic on ParallelCommand errors

Instead log the errors to the logfile

* Don't panic when the serial and parallel send loops exit in the wrong order

* Don't panic on serial command failures

* Fix confirm quit

Neovim might exit before the response is received, so ignore all errors

* Revert "Properly fork the process on Linux and Mac"

This reverts commit 8ecc899.

* Don't detach/attach to the console on Windows

* Update to winit 0.29.2

---------

Co-authored-by: Fred Sundvik <fsundvik@gmail.com>
Co-authored-by: MultisampledNight <contact@multisamplednight.com>
  • Loading branch information
3 people committed Nov 1, 2023
1 parent c041cc6 commit 5743356
Show file tree
Hide file tree
Showing 51 changed files with 4,229 additions and 1,957 deletions.
1,102 changes: 743 additions & 359 deletions Cargo.lock

Large diffs are not rendered by default.

38 changes: 18 additions & 20 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ profiling = ["dep:tracy-client-sys"]
gpu_profiling = ["profiling"]

[dependencies]
anyhow = { version = "1.0.75", features = ["backtrace"] }
async-trait = "0.1.53"
backtrace = "0.3.67"
cfg-if = "1.0.0"
Expand All @@ -29,14 +30,15 @@ euclid = "0.22.7"
flexi_logger = { version = "0.22.3", default-features = false }
futures = "0.3.21"
gl = "0.14.0"
glutin = "0.30.7"
glutin-winit = "0.4.0-beta.0"
glutin = "0.31.0"
glutin-winit = "0.4.2"
image = { version = "0.24.1", default-features = false, features = ["ico"] }
itertools = "0.10.5"
lazy_static = "1.4.0"
log = "0.4.16"
lru = "0.7.5"
neovide-derive = { path = "neovide-derive" }
num = "0.4.1"
nvim-rs = { version = "0.6.0", features = ["use_tokio"] }
parking_lot = "0.12.0"
pin-project = "1.0.10"
Expand All @@ -46,6 +48,10 @@ rmpv = "1.0.0"
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.79"
shlex = "1.1.0"
simple_moving_average = "0.1.2"
skia-safe = { version = "0.62.0", features = ["gl", "textlayout"] }
spin_sleep = "1.1.1"
strum = { version = "0.25.0", features = ["derive"] }
swash = "0.1.8"
time = "0.3.9"
tokio = { version = "1.25.0", features = ["full"] }
Expand All @@ -54,40 +60,31 @@ toml = "0.7.3"
tracy-client-sys = { version = "0.19.0", optional = true }
unicode-segmentation = "1.9.0"
which = "4.2.5"
winit = { version = "=0.29.0-beta.0", features = ["serde"] }
winit = { version = "=0.29.2", features = ["serde"] }
xdg = "2.4.1"

[dev-dependencies]
mockall = "0.11.0"
scoped-env = "2.1.0"
serial_test = "2.0.0"

[target.'cfg(windows)'.dependencies]
[target.'cfg(target_os = "windows")'.dependencies]
# NOTE: winerror is only needed because the indirect dependency parity-tokio-ipc does not set it even if it uses it
winapi = { version = "0.3.9", features = ["winuser", "wincon", "winerror"] }
winapi = { version = "0.3.9", features = ["winuser", "wincon", "winerror", "dwmapi"] }

[target.'cfg(target_os = "macos")'.dependencies]
cocoa = "0.24.0"
objc = "0.2.7"

[target.'cfg(not(windows))'.dependencies]
[target.'cfg(target_os = "linux")'.dependencies]
nix = { version = "0.26.2", features = ["poll"] }
wayland-client = "0.30.2"
wayland-sys = "0.30.1"
wayland-backend = "0.1.2"

[target.'cfg(windows)'.build-dependencies]
[target.'cfg(target_os = "windows")'.build-dependencies]
winres = "0.1.12"

[target.'cfg(windows)'.dependencies.skia-safe]
# NOTE: Including textlayout for pre-built binaries
features = ["gl", "textlayout"]
version = "0.62.0"

[target.'cfg(linux)'.dependencies.skia-safe]
features = ["egl", "gl", "textlayout", "wayland", "x11"]
version = "0.62.0"

[target.'cfg(not(linux))'.dependencies.skia-safe]
features = ["gl"]
version = "0.62.0"

[profile.release]
lto = true
incremental = true
Expand All @@ -111,3 +108,4 @@ long_description = """
This is a simple graphical user interface for Neovim. Where possible there are some graphical improvements, but it should act functionally like the terminal UI.
"""
osx_minimum_system_version = "10.11"

91 changes: 91 additions & 0 deletions lua/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
---@class Args
---@field neovide_channel_id integer
---@field register_clipboard boolean
---@field register_right_click boolean
---@field enable_focus_command boolean

---@type Args
local args = ...


vim.g.neovide_channel_id = args.neovide_channel_id

-- Set some basic rendering options.
vim.o.lazyredraw = false
vim.o.termguicolors = true

local function rpcnotify(method, ...)
vim.rpcnotify(vim.g.neovide_channel_id, method, ...)
end

local function rpcrequest(method, ...)
vim.rpcrequest(vim.g.neovide_channel_id, method, ...)
end

local function set_clipboard(register)
return function(lines, regtype)
rpcrequest("neovide.set_clipboard", lines)
end
end

local function get_clipboard(register)
return function()
return rpcrequest("neovide.get_clipboard", register)
end
end

if args.register_clipboard and not vim.g.neovide_no_custom_clipboard then
vim.g.clipboard = {
name = "neovide",
copy = {
["+"] = set_clipboard("+"),
["*"] = set_clipboard("*"),
},
paste = {
["+"] = get_clipboard("+"),
["*"] = get_clipboard("*"),
},
cache_enabled = 0
}
end

if args.register_right_click then
vim.api.nvim_create_user_command("NeovideRegisterRightClick", function()
rpcnotify("neovide.register_right_click")
end, {})
vim.api.nvim_create_user_command("NeovideUnregisterRightClick", function()
rpcnotify("neovide.unregister_right_click")
end, {})
end

vim.api.nvim_create_user_command("NeovideFocus", function()
rpcnotify("neovide.focus_window")
end, {})

vim.api.nvim_create_autocmd({ "OptionSet" }, {
pattern = "columns",
once = false,
nested = true,
callback = function()
rpcnotify("neovide.columns", tonumber(vim.v.option_new))
end
})

vim.api.nvim_create_autocmd({ "OptionSet" }, {
pattern = "lines",
once = false,
nested = true,
callback = function()
rpcnotify("neovide.lines", tonumber(vim.v.option_new))
end
})

-- Create auto command for retrieving exit code from neovim on quit.
vim.api.nvim_create_autocmd({ "VimLeavePre" }, {
pattern = "*",
once = true,
nested = true,
callback = function()
rpcnotify("neovide.quit", vim.v.exiting)
end
})
63 changes: 33 additions & 30 deletions src/bridge/command.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use std::{
env, eprintln,
env,
process::{Command as StdCommand, Stdio},
};

use anyhow::{bail, Result};
use log::debug;
use tokio::process::Command as TokioCommand;

use crate::{cmd_line::CmdLineSettings, settings::*};

pub fn create_nvim_command() -> TokioCommand {
let mut cmd = build_nvim_cmd();
pub fn create_nvim_command() -> Result<TokioCommand> {
let mut cmd = build_nvim_cmd()?;

debug!("Starting neovim with: {:?}", cmd);

Expand All @@ -22,29 +23,27 @@ pub fn create_nvim_command() -> TokioCommand {
#[cfg(windows)]
set_windows_creation_flags(&mut cmd);

cmd
Ok(cmd)
}

#[cfg(target_os = "windows")]
fn set_windows_creation_flags(cmd: &mut TokioCommand) {
cmd.creation_flags(0x0800_0000); // CREATE_NO_WINDOW
}

fn build_nvim_cmd() -> TokioCommand {
fn build_nvim_cmd() -> Result<TokioCommand> {
if let Some(cmdline) = SETTINGS.get::<CmdLineSettings>().neovim_bin {
if let Some((bin, args)) = lex_nvim_cmdline(&cmdline) {
return build_nvim_cmd_with_args(bin, args);
if let Some((bin, args)) = lex_nvim_cmdline(&cmdline)? {
return Ok(build_nvim_cmd_with_args(bin, args));
}

eprintln!("ERROR: NEOVIM_BIN='{}' was not found.", cmdline);
std::process::exit(1);
bail!("ERROR: NEOVIM_BIN='{}' was not found.", cmdline);
} else if let Some(path) = platform_which("nvim") {
if neovim_ok(&path, &[]) {
return build_nvim_cmd_with_args(path, vec![]);
if neovim_ok(&path, &[])? {
return Ok(build_nvim_cmd_with_args(path, vec![]));
}
}
eprintln!("ERROR: nvim not found!");
std::process::exit(1);
bail!("ERROR: nvim not found!")
}

// Creates a shell command if needed on this platform (wsl or macos)
Expand Down Expand Up @@ -79,7 +78,7 @@ fn create_platform_shell_command(command: &str, args: &[&str]) -> StdCommand {
}
}

fn neovim_ok(bin: &str, args: &[String]) -> bool {
fn neovim_ok(bin: &str, args: &[String]) -> Result<bool> {
let is_wsl = SETTINGS.get::<CmdLineSettings>().wsl;
let mut args = args.iter().map(String::as_str).collect::<Vec<_>>();
args.push("-v");
Expand All @@ -106,29 +105,33 @@ fn neovim_ok(bin: &str, args: &[String]) -> bool {
);

if is_wsl {
eprintln!("{error_message_prefix}wsl '$SHELL' -lc '{bin} -v'");
bail!("{error_message_prefix}wsl '$SHELL' -lc '{bin} -v'");
} else {
eprintln!("{error_message_prefix}$SHELL -lc '{bin} -v'");
bail!("{error_message_prefix}$SHELL -lc '{bin} -v'");
}
std::process::exit(1);
}
return true;
return Ok(true);
}
}
false
Ok(false)
}

fn lex_nvim_cmdline(cmdline: &str) -> Option<(String, Vec<String>)> {
let mut tokens = shlex::split(cmdline).filter(|t| !t.is_empty())?;
let (mut bin, args) = (tokens.remove(0), tokens);

// if neovim_bin contains a path separator, then try to launch it directly
// otherwise use which to find the fully path
if !bin.contains('/') && !bin.contains('\\') {
bin = platform_which(&bin)?;
}

neovim_ok(&bin, &args).then_some((bin, args))
fn lex_nvim_cmdline(cmdline: &str) -> Result<Option<(String, Vec<String>)>> {
shlex::split(cmdline)
.filter(|t| !t.is_empty())
.map(|mut tokens| (tokens.remove(0), tokens))
.and_then(|(bin, args)| {
// if neovim_bin contains a path separator, then try to launch it directly
// otherwise use which to find the full path
if !bin.contains('/') && !bin.contains('\\') {
platform_which(&bin).map(|bin| (bin, args))
} else {
Some((bin, args))
}
})
.map_or(Ok(None), |(bin, args)| {
neovim_ok(&bin, &args).map(|res| res.then_some((bin, args)))
})
}

fn platform_which(bin: &str) -> Option<String> {
Expand Down
14 changes: 11 additions & 3 deletions src/bridge/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ use rmpv::Value;
use crate::bridge::ui_commands::{ParallelCommand, UiCommand};
use crate::{
bridge::clipboard::{get_clipboard_contents, set_clipboard_contents},
window::WindowCommand,
};
use crate::{
bridge::{events::parse_redraw_event, NeovimWriter},
editor::EditorCommand,
error_handling::ResultPanicExplanation,
event_aggregator::EVENT_AGGREGATOR,
running_tracker::*,
settings::SETTINGS,
window::WindowCommand,
};

#[derive(Clone)]
Expand Down Expand Up @@ -88,6 +86,16 @@ impl Handler for NeovimHandler {
.expect("Could not parse error code from neovim");
RUNNING_TRACKER.quit_with_code(error_code as i32, "Quit from neovim");
}
"neovide.columns" => {
if let Some(columns) = arguments[0].as_u64() {
EVENT_AGGREGATOR.send(WindowCommand::Columns(columns));
}
}
"neovide.lines" => {
if let Some(lines) = arguments[0].as_u64() {
EVENT_AGGREGATOR.send(WindowCommand::Lines(lines));
}
}
#[cfg(windows)]
"neovide.register_right_click" => {
EVENT_AGGREGATOR.send(UiCommand::Parallel(ParallelCommand::RegisterRightClick));
Expand Down

0 comments on commit 5743356

Please sign in to comment.