Skip to content

Commit

Permalink
feat: adds callbacks for modes
Browse files Browse the repository at this point in the history
Now callbacks for each mode can be used:
from - triggered when switching from a mode
to - triggered when switching to a mode
on - triggered when switching from or to a mode
  • Loading branch information
rasulomaroff committed Sep 30, 2023
1 parent 1a3b245 commit 7936cab
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lua/reactive/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function M:init()

Snapshot:set_modes(from, to)

Highlight:apply(Snapshot:gen())
Highlight:apply(Snapshot:gen { callbacks = true })
end,
})

Expand All @@ -77,7 +77,7 @@ function M:init()
group = group,
desc = 'Reactive: applies inactive window highlights',
callback = function()
Highlight:apply(Snapshot:gen(true))
Highlight:apply(Snapshot:gen { inactive_win = true })
end,
})
end
Expand Down
50 changes: 42 additions & 8 deletions lua/reactive/snapshot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,23 @@ function M:set_opfunc(opfunc)
self.current_opfunc = opfunc
end

---@param inactive? boolean
---@param opts? { inactive_win?: boolean, callbacks?: boolean }
---@return table<any>
function M:gen(inactive)
function M:gen(opts)
local State = require 'reactive.state'

self.snapshot = { winhl = {}, hl = {} }

-- if we're leaving a window, then we just color that window with `inactive` colors, if present
if inactive then
if opts and opts.inactive_win then
State:iterate_presets(function(preset)
local constraints = {}

if preset.static and not vim.tbl_isempty(preset.static) and not self:process_skip(preset.skip, constraints) then
if
preset.static
and not vim.tbl_isempty(preset.static)
and not self:process_constraints(preset.skip, constraints)
then
self:form_snapshot(preset.name, {
winhl = preset.static.winhl and preset.static.winhl.inactive,
hl = preset.static.hl,
Expand Down Expand Up @@ -94,7 +98,7 @@ function M:gen(inactive)
end

-- we check `skip` method/table only on a first iteration
if self:process_skip(preset.skip, scope[preset.name].constraints) then
if self:process_constraints(preset.skip, scope[preset.name].constraints) then
dropped_presets[preset.name] = true

return
Expand Down Expand Up @@ -159,14 +163,44 @@ function M:gen(inactive)
end)
end)

if has_static then
if has_static or (opts and opts.callbacks) then
State:iterate_presets(function(preset)
if preset.static and not vim.tbl_isempty(preset.static) then
self:form_snapshot(preset.name, {
winhl = preset.static.winhl and preset.static.winhl.active,
hl = preset.static.hl,
}, '@static.active', scope[preset.name].constraints)
end

if
not opts
or not opts.callbacks
or not preset.modes
or scope[preset.name].constraints.callbacks
or dropped_presets[preset.name]
then
return
end

if preset.modes[self.from] then
if preset.modes[self.from].from then
preset.modes[self.from].from(self.from, self.to)
end

if preset.modes[self.from].on then
preset.modes[self.from].on(self.from, self.to)
end
end

if preset.modes[self.to] then
if preset.modes[self.to].to then
preset.modes[self.to].to(self.from, self.to)
end

if preset.modes[self.to].on then
preset.modes[self.to].on(self.from, self.to)
end
end
end)
end

Expand Down Expand Up @@ -262,7 +296,7 @@ end
---@param skip table | fun(): boolean
---@param constraints? table<string, boolean>
---@return boolean is_dropped
function M:process_skip(skip, constraints)
function M:process_constraints(skip, constraints)
if type(skip) == 'function' then
return skip()
end
Expand All @@ -273,7 +307,7 @@ function M:process_skip(skip, constraints)

local escaped = true

for _, field in ipairs(shape_fields) do
for _, field in ipairs { 'winhl', 'hl', 'callbacks' } do
if skip[field] and skip[field]() then
if constraints then
constraints[field] = true
Expand Down
5 changes: 4 additions & 1 deletion lua/reactive/state.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local Preset = require 'reactive.preset'
local Util = require 'reactive.util'

---@class TriggerConstraints<V>: { winhl?: V, hl?: V }
---@class TriggerConstraints<V>: { winhl?: V, hl?: V, callbacks?: V }

---@class Reactive.TriggerConfig
---@field winhl? table<string, string | table<string, any>>
Expand All @@ -10,6 +10,9 @@ local Util = require 'reactive.util'
---@field opfuncs? table<string, Reactive.TriggerConfig>
---@field exact? boolean | TriggerConstraints<boolean>
---@field frozen? boolean | TriggerConstraints<boolean>
---@field from? fun(from: string, to: string)
---@field to? fun(from: string, to: string)
---@field on? fun(from: string, to: string)

---@class Reactive.Preset
---@field name string
Expand Down

0 comments on commit 7936cab

Please sign in to comment.