From 8213c0d30e174dca391e7cda39db899bf9f875ef Mon Sep 17 00:00:00 2001 From: Nguyen Tri Tai Date: Sun, 19 Jun 2022 21:13:23 +0700 Subject: [PATCH] Replace onchange with before and after --- README.md | 29 +++++++++++++++++++++-------- lua/dark_notify.lua | 15 +++++++++++---- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index cf93736..85be341 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # dark-notify -It's a program for watching when macOS switches to dark mode. Useful for making -your text editor switch to a dark theme. Includes a Neovim (Lua) plugin to do +It's a program for watching when macOS switches to dark mode. Useful for making +your text editor switch to a dark theme. Includes a Neovim (Lua) plugin to do exactly that. ![Demo gif](demo.gif) @@ -30,7 +30,7 @@ require('dark_notify').run() EOF ``` -By default, this will just execute `:set bg=dark` or `:set bg=light` as soon as +By default, this will just execute `:set bg=dark` or `:set bg=light` as soon as the system appearance changes. ### Additional options @@ -59,11 +59,24 @@ dn.run({ -- example github = (vim.g.plug_home .. "/vim-colors-github/autoload/lightline/colorscheme/github.vim") }, - onchange = function(mode) - -- optional, you can configure your own things to react to changes. - -- this is called at startup and every time dark mode is switched, - -- either via the OS, or because you manually set/toggled the mode. - -- mode is either "light" or "dark" + -- optionally, you can configure what to do before/after changes. + -- this is called at startup and every time dark mode is switched, + -- either via the OS, or because you manually set/toggled the mode. + -- mode is either "light" or "dark" + before = function(mode) + -- your logic here + if mode == "dark" then + -- do something + else + -- do other thing + end + end, + after = function(mode) + if mode == "dark" then + -- do something + else + -- do other thing + end end, }) diff --git a/lua/dark_notify.lua b/lua/dark_notify.lua index 5a42730..9660e98 100644 --- a/lua/dark_notify.lua +++ b/lua/dark_notify.lua @@ -53,6 +53,10 @@ local function apply_mode(mode) local bg = sel.background or mode local lltheme = sel.lightline or nil + if config.before ~= nil then + config.before(mode) + end + vim.api.nvim_command('set background=' .. bg) if colorscheme ~= nil then vim.api.nvim_command('colorscheme ' .. colorscheme) @@ -79,10 +83,11 @@ local function apply_mode(mode) end end - if config.onchange ~= nil then - config.onchange(mode) + if config.after ~= nil then + config.after(mode) end + state.current_mode = mode end @@ -181,7 +186,8 @@ function M.configure(config) end local lightline_loaders = config.lightline_loaders or {} local schemes = config.schemes or {} - local onchange = config.onchange + local before = config.before + local after = config.after for _, mode in pairs({ "light", "dark" }) do if type(schemes[mode]) == "string" then @@ -192,7 +198,8 @@ function M.configure(config) edit_config(function (conf) conf.lightline_loaders = lightline_loaders conf.schemes = schemes - conf.onchange = onchange + conf.before = before + conf.after = after end) end