-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
141 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
local Button = require('elements/Button') | ||
|
||
---@alias ManagedButtonProps {name: string; anchor_id?: string; render_order?: number} | ||
|
||
---@class ManagedButton : Button | ||
local ManagedButton = class(Button) | ||
|
||
---@param id string | ||
---@param props ManagedButtonProps | ||
function ManagedButton:new(id, props) return Class.new(self, id, props) --[[@as ManagedButton]] end | ||
---@param id string | ||
---@param props ManagedButtonProps | ||
function ManagedButton:init(id, props) | ||
---@type string | table | nil | ||
self.command = nil | ||
|
||
Button.init(self, id, table_assign({}, props, {on_click = function() execute_command(self.command) end})) | ||
|
||
self:register_disposer(buttons:subscribe(props.name, function(data) self:update(data) end)) | ||
end | ||
|
||
function ManagedButton:update(data) | ||
for _, prop in ipairs({'icon', 'active', 'badge', 'command', 'tooltip'}) do | ||
self[prop] = data[prop] | ||
end | ||
self.is_clickable = self.command ~= nil | ||
end | ||
|
||
return ManagedButton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
---@alias ButtonData {icon: string; active?: boolean; badge?: string; command?: string | string[]; tooltip?: string;} | ||
---@alias ButtonSubscriber fun(data: ButtonData) | ||
|
||
local buttons = { | ||
---@type ButtonData[] | ||
data = {}, | ||
---@type table<string, ButtonSubscriber[]> | ||
subscribers = {}, | ||
} | ||
|
||
---@param name string | ||
---@param callback fun(data: ButtonData) | ||
function buttons:subscribe(name, callback) | ||
local pool = self.subscribers[name] | ||
if not pool then | ||
pool = {} | ||
self.subscribers[name] = pool | ||
end | ||
pool[#pool + 1] = callback | ||
self:trigger(name) | ||
return function() buttons:unsubscribe(name, callback) end | ||
end | ||
|
||
---@param name string | ||
---@param callback? ButtonSubscriber | ||
function buttons:unsubscribe(name, callback) | ||
if self.subscribers[name] then | ||
if callback == nil then | ||
self.subscribers[name] = {} | ||
else | ||
itable_delete_value(self.subscribers[name], callback) | ||
end | ||
end | ||
end | ||
|
||
---@param name string | ||
function buttons:trigger(name) | ||
local pool = self.subscribers[name] | ||
local data = self.data[name] or {icon = 'help_center', tooltip = 'Uninitialized button "' .. name .. '"'} | ||
if pool then | ||
for _, callback in ipairs(pool) do callback(data) end | ||
end | ||
end | ||
|
||
---@param name string | ||
---@param data ButtonData | ||
function buttons:set(name, data) | ||
buttons.data[name] = data | ||
buttons:trigger(name) | ||
request_render() | ||
end | ||
|
||
mp.register_script_message('set-button', function(name, data) | ||
if type(name) ~= 'string' then | ||
msg.error('Invalid set-button message parameter: 1st parameter (name) has to be a string.') | ||
return | ||
end | ||
if type(data) ~= 'string' then | ||
msg.error('Invalid set-button message parameter: 2nd parameter (data) has to be a string.') | ||
return | ||
end | ||
|
||
local data = utils.parse_json(data) | ||
if type(data) == 'table' and type(data.icon) == 'string' then | ||
buttons:set(name, data) | ||
end | ||
end) | ||
|
||
return buttons |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters