Repository navigation
Expand file tree
/
Copy pathprocess_slicer.lua
More file actions
132 lines (101 loc) · 3.64 KB
/
Copy pathprocess_slicer.lua
File metadata and controls
132 lines (101 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
--[[============================================================================
process_slicer.lua
============================================================================]]--
--[[
ProcessSlicer for Paketti - allows slicing up long-running operations into smaller chunks
to maintain UI responsiveness and show progress.
Main benefits:
- Shows current progress of operations
- Allows users to abort operations
- Prevents Renoise from thinking the script is frozen
- Maintains UI responsiveness during heavy operations
]]
class "ProcessSlicer"
function ProcessSlicer:__init(process_func, ...)
assert(type(process_func) == "function",
"Expected a function as first argument")
self.__process_func = process_func
self.__process_func_args = {...}
self.__process_thread = nil
self.__cancelled = false
end
--------------------------------------------------------------------------------
-- Returns true when the current process is running
function ProcessSlicer:running()
return (self.__process_thread ~= nil)
end
--------------------------------------------------------------------------------
-- Start a process
function ProcessSlicer:start()
assert(not self:running(), "Process already running")
self.__process_thread = coroutine.create(self.__process_func)
renoise.tool().app_idle_observable:add_notifier(
ProcessSlicer.__on_idle, self)
end
--------------------------------------------------------------------------------
-- Stop a running process
function ProcessSlicer:stop()
assert(self:running(), "Process not running")
renoise.tool().app_idle_observable:remove_notifier(
ProcessSlicer.__on_idle, self)
self.__process_thread = nil
end
--------------------------------------------------------------------------------
-- Cancel the process
function ProcessSlicer:cancel()
self.__cancelled = true
end
--------------------------------------------------------------------------------
-- Check if process was cancelled
function ProcessSlicer:was_cancelled()
return self.__cancelled
end
--------------------------------------------------------------------------------
-- Internal function called during idle to continue processing
function ProcessSlicer:__on_idle()
assert(self.__process_thread ~= nil, "ProcessSlicer internal error: " ..
"Expected no idle call with no thread running")
-- Continue or start the process while it's still active
if (coroutine.status(self.__process_thread) == 'suspended') then
local succeeded, error_message = coroutine.resume(
self.__process_thread, unpack(self.__process_func_args))
if (not succeeded) then
-- Stop the process on errors
self:stop()
-- Forward the error
error(error_message)
end
-- Stop when the process function completed
elseif (coroutine.status(self.__process_thread) == 'dead') then
self:stop()
end
end
-- Helper function to create a progress dialog
function ProcessSlicer:create_dialog(title)
local vb = renoise.ViewBuilder()
local dialog = nil
local DEFAULT_MARGIN = renoise.ViewBuilder.DEFAULT_CONTROL_MARGIN
local DEFAULT_SPACING = renoise.ViewBuilder.DEFAULT_CONTROL_SPACING
local dialog_content = vb:column {
margin = DEFAULT_MARGIN,
spacing = DEFAULT_SPACING,
vb:text {
id = "progress_text",
text = "Processing..."
},
vb:button {
id = "cancel_button",
text = "Cancel",
width = 80,
notifier = function()
self:cancel()
if dialog and dialog.visible then
dialog:close()
end
end
}
}
dialog = renoise.app():show_custom_dialog(
title or "Processing...", dialog_content)
return dialog, vb
end