From 8470f16de46c5e0aaeff66e66d7ed59c05adac5c Mon Sep 17 00:00:00 2001 From: dyphire Date: Sun, 18 Aug 2024 18:19:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=84=9A=E6=9C=AC=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script-opts/adevice_list.conf | 2 - script-opts/chapter_list.conf | 2 - script-opts/edition_list.conf | 2 - scripts/adevice-list.lua | 83 +++++++++++++------------------- scripts/chapter-list.lua | 89 ++++++++++++++--------------------- scripts/edition-list.lua | 78 ++++++++++++------------------ 6 files changed, 97 insertions(+), 159 deletions(-) diff --git a/script-opts/adevice_list.conf b/script-opts/adevice_list.conf index cb6163a4..4791c0c7 100644 --- a/script-opts/adevice_list.conf +++ b/script-opts/adevice_list.conf @@ -16,8 +16,6 @@ indent=\h\h\h\h num_entries=16 ##是否循环滚动 wrap=yes -##打开菜单时重置光标导航 -reset_cursor_on_close=yes ##菜单打开时绑定的按键 key_move_begin=HOME key_move_end=END diff --git a/script-opts/chapter_list.conf b/script-opts/chapter_list.conf index 74bf9ae5..b1689e52 100644 --- a/script-opts/chapter_list.conf +++ b/script-opts/chapter_list.conf @@ -18,8 +18,6 @@ num_entries=16 max_title_length=100 ##是否循环滚动 wrap=yes -##打开菜单时重置光标导航 -reset_cursor_on_close=yes ##菜单打开时绑定的按键 key_move_begin=HOME key_move_end=END diff --git a/script-opts/edition_list.conf b/script-opts/edition_list.conf index 5a23c386..6af7062f 100644 --- a/script-opts/edition_list.conf +++ b/script-opts/edition_list.conf @@ -20,8 +20,6 @@ num_entries=16 max_title_length=100 ##是否循环滚动 wrap=yes -##打开菜单时重置光标导航 -reset_cursor_on_close=yes ##菜单打开时绑定的按键 key_move_begin=HOME key_move_end=END diff --git a/scripts/adevice-list.lua b/scripts/adevice-list.lua index beebc61e..0d712cd4 100644 --- a/scripts/adevice-list.lua +++ b/scripts/adevice-list.lua @@ -1,5 +1,5 @@ --[[ - * adevice-list.lua v.2024-01-07 + * adevice-list.lua v.2024-08-18 * * AUTHORS: dyphire * License: MIT @@ -36,8 +36,6 @@ local o = { num_entries = 16, -- wrap the cursor around the top and bottom of the list wrap = true, - -- reset cursor navigation when open the list - reset_cursor_on_close = true, -- set dynamic keybinds to bind when the list is open key_move_begin = "HOME", key_move_end = "END", @@ -56,7 +54,6 @@ package.path = mp.command_native({"expand-path", "~~/script-modules/?.lua;"}) .. local list = require "scroll-list" --modifying the list settings -local original_open = list.open list.header = o.header list.cursor = o.cursor list.indent = o.indent @@ -80,53 +77,45 @@ end --update the list when the current audio-device changes local function adevice_list() - list.list = {} - local adeviceList = mp.get_property_native('audio-device-list', {}) - for i = 1, #adeviceList do - local item = {} - if (i == list.selected) then - current_name = adeviceList[i].name - item.style = o.active_style - item.ass = "■ " .. list.ass_escape(adeviceList[i].description) - else - item.ass = "□ " .. list.ass_escape(adeviceList[i].description) - end - list.list[i] = item - end - list:update() + mp.observe_property('audio-device-list', 'native', function(_, adevice_list) + mp.observe_property('audio-device', 'string', function(_, current_device) + list.list = {} + if adevice_list == nil then + list:update() + return + end + if list.selected > 0 and string.find(adevice_list[list.selected].name, current_device) == nil then + list.selected = 0 + end + for i = 1, #adevice_list do + local item = {} + if current_device ~= nil then + if adevice_list[i].name == current_device then + list.selected = i + end + end + if (i == list.selected) then + item.style = o.active_style + item.ass = "■ " .. list.ass_escape(adevice_list[i].description) + else + item.ass = "□ " .. list.ass_escape(adevice_list[i].description) + end + list.list[i] = item + end + list:update() + end) + end) + list:toggle() end --open to the selected audio-device local function open_adevice() - local adeviceList = mp.get_property_native('audio-device-list', {}) + local adevice_list = mp.get_property_native('audio-device-list', {}) if list.list[list.selected] then - mp.set_property("audio-device", adeviceList[list.selected].name) + mp.set_property("audio-device", adevice_list[list.selected].name) end end ---reset cursor navigation when open the list -local function reset_cursor() - local adeviceList = mp.get_property_native('audio-device-list', {}) - if current_name ~= nil and list.selected > 0 then - if string.find(adeviceList[list.selected].name, current_name) == nil then - list.selected = 0 - end - end - for s = 1, #adeviceList do - if current_name ~= nil then - if adeviceList[s].name == current_name then list.selected = s end - end - end - if o.reset_cursor_on_close and list.selected > 0 then - list:update() - end -end - -function list:open() - reset_cursor() - original_open(self) -end - --dynamic keybinds to bind when the list is open list.keybinds = {} @@ -147,13 +136,7 @@ add_keys(o.key_move_end, 'move_end', function() list:move_end() end, {}) add_keys(o.key_open_adevice, 'open_adevice', open_adevice, {}) add_keys(o.key_close_browser, 'close_browser', function() list:close() end, {}) -mp.observe_property('audio-device', 'string', adevice_list) -mp.observe_property('audio-device-list', 'string', function() - reset_cursor() - adevice_list() -end) - -mp.register_script_message("toggle-adevice-browser", function() list:toggle() end) +mp.register_script_message("toggle-adevice-browser", adevice_list) mp.register_event('end-file', function() list:close() diff --git a/scripts/chapter-list.lua b/scripts/chapter-list.lua index d109ff2f..6554afb1 100644 --- a/scripts/chapter-list.lua +++ b/scripts/chapter-list.lua @@ -32,8 +32,6 @@ local o = { max_title_length = 100, -- wrap the cursor around the top and bottom of the list wrap = true, - -- reset cursor navigation when open the list - reset_cursor_on_close = true, -- set dynamic keybinds to bind when the list is open key_move_begin = "HOME", key_move_end = "END", @@ -65,7 +63,6 @@ if not success then end --modifying the list settings -local original_open = list.open list.header = o.header list.cursor = o.cursor list.indent = o.indent @@ -110,32 +107,40 @@ local function utf8_sub(s, i, j) end --update the list when the current chapter changes -function chapter_list(curr_chapter) - list.list = {} - local chapter_list = mp.get_property_native('chapter-list', {}) - for i = 1, #chapter_list do - local item = {} - if (i - 1 == curr_chapter) then - if reset_curr then list.selected = curr_chapter + 1 end - item.style = o.active_style - end - - local time = chapter_list[i].time - local title = chapter_list[i].title - if not title or title == '(unnamed)' or title == '' then - title = "Chapter " .. string.format("%02.f", i) - end - local title_clip = utf8_sub(title, 1, o.max_title_length) - if title ~= title_clip then - title = title_clip .. "..." - end - if time < 0 then time = 0 - else time = math.floor(time) end - item.ass = string.format("[%02d:%02d:%02d]", math.floor(time / 60 / 60), math.floor(time / 60) % 60, time % 60) - item.ass = item.ass .. '\\h\\h\\h' .. list.ass_escape(title) - list.list[i] = item - end - list:update() +local function chapter_list() + mp.observe_property('chapter-list', 'native', function(_, chapter_list) + mp.observe_property('chapter', 'number', function(_, curr_chapter) + list.list = {} + if chapter_list == nil then + list:update() + return + end + for i = 1, #chapter_list do + local item = {} + if (i - 1 == curr_chapter) then + if reset_curr then list.selected = curr_chapter + 1 end + item.style = o.active_style + end + + local time = chapter_list[i].time + local title = chapter_list[i].title + if not title or title == '(unnamed)' or title == '' then + title = "Chapter " .. string.format("%02.f", i) + end + local title_clip = utf8_sub(title, 1, o.max_title_length) + if title ~= title_clip then + title = title_clip .. "..." + end + if time < 0 then time = 0 + else time = math.floor(time) end + item.ass = string.format("[%02d:%02d:%02d]", math.floor(time / 60 / 60), math.floor(time / 60) % 60, time % 60) + item.ass = item.ass .. '\\h\\h\\h' .. list.ass_escape(title) + list.list[i] = item + end + list:update() + end) + end) + list:toggle() end local function change_title_callback(user_input, err, chapter_index) @@ -238,21 +243,6 @@ local function open_chapter() end end ---reset cursor navigation when open the list -local function reset_cursor() - if o.reset_cursor_on_close then - if mp.get_property('chapter') then - list.selected = mp.get_property_number('chapter') + 1 - list:update() - end - end -end - -function list:open() - reset_cursor() - original_open(self) -end - --dynamic keybinds to bind when the list is open list.keybinds = {} @@ -275,16 +265,7 @@ add_keys(o.key_close_browser, 'close_browser', function() list:close() end, {}) add_keys(o.key_remove_chapter, 'remove_chapter', remove_chapter, {}) add_keys(o.key_edit_chapter, 'edit_chapter', edit_chapter, {}) -mp.register_script_message("toggle-chapter-browser", function() list:toggle() end) - -mp.observe_property('chapter', 'number', function(_, curr_chapter) - chapter_list(curr_chapter) -end) - -mp.observe_property('chapter-list', 'native', function() - local curr_chapter = mp.get_property_number("chapter") - if curr_chapter then chapter_list(curr_chapter) end -end) +mp.register_script_message("toggle-chapter-browser", chapter_list) if user_input_module then mp.add_hook("on_unload", 50, function() input.cancel_user_input() end) diff --git a/scripts/edition-list.lua b/scripts/edition-list.lua index 07ad07ff..a6713a54 100644 --- a/scripts/edition-list.lua +++ b/scripts/edition-list.lua @@ -41,8 +41,6 @@ local o = { max_title_length = 100, -- wrap the cursor around the top and bottom of the list wrap = true, - -- reset cursor navigation when open the list - reset_cursor_on_close = true, -- set dynamic keybinds to bind when the list is open key_move_begin = "HOME", key_move_end = "END", @@ -116,7 +114,6 @@ function changedFile() end --modifying the list settings -local original_open = list.open list.header = o.header list.cursor = o.cursor list.indent = o.indent @@ -139,26 +136,34 @@ function list:format_header_string(str) end --update the list when the current edition changes -function edition_list(curr_edition) - list.list = {} - local edition_list = mp.get_property_native('edition-list', {}) - for i = 1, #edition_list do - local item = {} - local title = edition_list[i].title - if not title then title = "Edition " .. string.format("%02.f", i) end - if o.max_title_length > 0 and title:len() > o.max_title_length + 5 then - title = title:sub(1, o.max_title_length) .. " ..." - end - if (i - 1 == curr_edition) then - list.selected = curr_edition + 1 - item.style = o.active_style - item.ass = "● " .. list.ass_escape(title) - else - item.ass = "○ " .. list.ass_escape(title) - end - list.list[i] = item - end - list:update() +local function edition_list() + mp.observe_property('edition-list', 'native', function(_, edition_list) + mp.observe_property('current-edition', 'number', function(_, curr_edition) + list.list = {} + if edition_list == nil then + list:update() + return + end + for i = 1, #edition_list do + local item = {} + local title = edition_list[i].title + if not title then title = "Edition " .. string.format("%02.f", i) end + if o.max_title_length > 0 and title:len() > o.max_title_length + 5 then + title = title:sub(1, o.max_title_length) .. " ..." + end + if (i - 1 == curr_edition) then + list.selected = curr_edition + 1 + item.style = o.active_style + item.ass = "● " .. list.ass_escape(title) + else + item.ass = "○ " .. list.ass_escape(title) + end + list.list[i] = item + end + list:update() + end) + end) + list:toggle() end --jump to the selected edition @@ -168,21 +173,6 @@ local function select_edition() end end ---reset cursor navigation when open the list -local function reset_cursor() - if o.reset_cursor_on_close then - if mp.get_property('editions') then - list.selected = mp.get_property_number('current-edition') + 1 - list:update() - end - end -end - -function list:open() - reset_cursor() - original_open(self) -end - --dynamic keybinds to bind when the list is open list.keybinds = {} @@ -203,17 +193,7 @@ add_keys(o.key_move_end, 'move_end', function() list:move_end() end, {}) add_keys(o.key_select_edition, 'select_edition', select_edition, {}) add_keys(o.key_close_browser, 'close_browser', function() list:close() end, {}) -mp.register_script_message("toggle-edition-browser", function() list:toggle() end) - -mp.observe_property('current-edition', 'number', function(_, curr_edition) - editionChanged() - edition_list(curr_edition) -end) - -mp.observe_property('edition-list', 'native', function() - local curr_edition = mp.get_property_number("current-edition") - if curr_edition then edition_list(curr_edition) end -end) +mp.register_script_message("toggle-edition-browser", edition_list) mp.register_event('file-loaded', main) mp.register_event('end-file', function()