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

Configuration option to set theme to light/dark/auto #1917

Merged
merged 14 commits into from
Jul 4, 2023
6 changes: 6 additions & 0 deletions src/bridge/ui_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub enum ParallelCommand {
FocusLost,
FocusGained,
DisplayAvailableFonts(Vec<String>),
SetBackground(String),
#[cfg(windows)]
RegisterRightClick,
#[cfg(windows)]
Expand Down Expand Up @@ -148,6 +149,11 @@ impl ParallelCommand {
ParallelCommand::FileDrop(path) => {
nvim.command(format!("e {path}").as_str()).await.ok();
}
ParallelCommand::SetBackground(background) => {
nvim.command(format!("set background={}", background).as_str())
.await
.ok();
}
ParallelCommand::DisplayAvailableFonts(fonts) => {
let mut content: Vec<String> = vec![
"What follows are the font names available for guifont. You can try any of them with <CR> in normal mode.",
Expand Down
4 changes: 4 additions & 0 deletions src/settings/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct Config {
pub idle: Option<bool>,
pub neovim_bin: Option<PathBuf>,
pub frame: Option<Frame>,
pub theme: Option<String>,
}

impl Config {
Expand Down Expand Up @@ -72,6 +73,9 @@ impl Config {
if let Some(neovim_bin) = &self.neovim_bin {
env::set_var("NEOVIM_BIN", neovim_bin.to_string_lossy().to_string());
}
if let Some(theme) = &self.theme {
env::set_var("NEOVIDE_THEME", theme);
}
}

fn load_from_path(path: &Path) -> Result<Self, Option<String>> {
Expand Down
34 changes: 33 additions & 1 deletion src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use winit::{
dpi::PhysicalSize,
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{self, Fullscreen, Icon},
window::{self, Fullscreen, Icon, Theme},
};

#[cfg(target_os = "macos")]
Expand Down Expand Up @@ -85,6 +85,12 @@ pub struct WinitWindowWrapper {
window_command_receiver: UnboundedReceiver<WindowCommand>,
}

pub fn set_background(background: &str) {
EVENT_AGGREGATOR.send(UiCommand::Parallel(ParallelCommand::SetBackground(
background.to_string(),
)));
}

impl WinitWindowWrapper {
pub fn toggle_fullscreen(&mut self) {
let window = self.windowed_context.window();
Expand Down Expand Up @@ -195,6 +201,19 @@ impl WinitWindowWrapper {
self.handle_focus_lost();
}
}
Event::WindowEvent {
event: WindowEvent::ThemeChanged(theme),
..
} => {
let settings = SETTINGS.get::<WindowSettings>();
if settings.theme.as_str() == "auto" {
let background = match theme {
Theme::Light => "light",
Theme::Dark => "dark",
};
set_background(background);
}
}
Event::RedrawRequested(..) | Event::WindowEvent { .. } => {
REDRAW_SCHEDULER.queue_next_frame()
}
Expand Down Expand Up @@ -460,6 +479,19 @@ pub fn create_window() {
renderer.grid_renderer.font_dimensions,
);

println!("theme: {}", SETTINGS.get::<WindowSettings>().theme);

MultisampledNight marked this conversation as resolved.
Show resolved Hide resolved
match SETTINGS.get::<WindowSettings>().theme.as_str() {
"light" => set_background("light"),
"dark" => set_background("dark"),
"auto" => match window.theme() {
Some(Theme::Light) => set_background("light"),
Some(Theme::Dark) => set_background("dark"),
None => {}
},
_ => {}
}

let mut window_wrapper = WinitWindowWrapper {
windowed_context,
skia_renderer,
Expand Down
2 changes: 2 additions & 0 deletions src/window/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct WindowSettings {
pub padding_left: u32,
pub padding_right: u32,
pub padding_bottom: u32,
pub theme: String,
}

impl Default for WindowSettings {
Expand All @@ -43,6 +44,7 @@ impl Default for WindowSettings {
padding_left: 0,
padding_right: 0,
padding_bottom: 0,
theme: "".to_string(),
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions website/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,24 @@ sizes above 15.
**Note**: This is currently glitchy, and leads to some underlines being clipped by the line of text
below.

#### Theme

VimScript:

```vim
let g:neovide_theme = 'auto'
```

Lua:

```lua
vim.g.neovide_theme = 'auto'
```

Set the [`background`](https://neovim.io/doc/user/options.html#'background') option when Neovide
starts. Possible values: _light_, _dark_, _auto_. On systems that support it, _auto_ will mirror the
system theme, and will update `background` when the system theme changes.

### Functionality

#### Refresh Rate
Expand Down