Skip to content

Commit

Permalink
Configuration option to set theme to light/dark/auto (#1917)
Browse files Browse the repository at this point in the history
* Create `--theme` command-line option

* Set background according to theme option

* Handle winit ThemeChanged events

* Config option "theme"

* Documentation

* Call `.as_str()` for consistency

* Clean up

* Make `--theme` truly optional

* `required` is not required

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

* Remove unnecessary &

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

* Intermediate variable for match

* Use a setting instead of a command-line option

* Remove debugging println!

* Add note clarifying theme not being released yet

---------

Co-authored-by: multisn8 <contact@multisamplednight.com>
  • Loading branch information
mgax and MultisampledNight committed Jul 4, 2023
1 parent b4161a5 commit bc94189
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
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
32 changes: 31 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,17 @@ pub fn create_window() {
renderer.grid_renderer.font_dimensions,
);

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
20 changes: 20 additions & 0 deletions website/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,26 @@ 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'
```

**Unreleased yet.**

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

0 comments on commit bc94189

Please sign in to comment.