Skip to content

Commit

Permalink
Add #MaxThreadsPerHotkey
Browse files Browse the repository at this point in the history
  • Loading branch information
phil294 committed Jul 7, 2023
1 parent 3e4c72f commit 73f1442
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
10 changes: 5 additions & 5 deletions docs/index.html
Expand Up @@ -324,7 +324,7 @@ <h2>Table of contents </h2>
<a class="tbd" href="#h_MaxThreadsBuffer.htm">#MaxThreadsBuffer</a>
</li>
<li>
<a class="tbd" href="#h_MaxThreadsPerHotkey.htm">#MaxThreadsPerHotkey</a>
<a href="#h_MaxThreadsPerHotkey.htm">#MaxThreadsPerHotkey</a>
</li>
<li>
<a href="#Hotkey.htm">Hotkey</a>
Expand Down Expand Up @@ -820,7 +820,7 @@ <h2>Table of contents </h2>
<a class="tbd" href="#h_MaxThreadsBuffer.htm">#MaxThreadsBuffer</a>
</li>
<li>
<a class="tbd" href="#h_MaxThreadsPerHotkey.htm">#MaxThreadsPerHotkey</a>
<a href="#h_MaxThreadsPerHotkey.htm">#MaxThreadsPerHotkey</a>
</li>
<li>
<a class="tbd" href="#h_NoTrayIcon.htm">#NoTrayIcon</a>
Expand Down Expand Up @@ -1100,7 +1100,7 @@ <h3 class="calibre7"><br class="calibre12" />
<br class="calibre12" /> How can a repeating action be stopped without exiting the script?
</h3>
<p class="calibre8">To stop an action that is repeating inside a <a href="#Loop.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">Loop</a>, consider the following example hotkey, which both starts and stops its own repeating action. In other words, pressing the hotkey once will start the Loop. Pressing the same hotkey again will stop it.</p>
<pre class="calibre22"><span class="tbd">#MaxThreadsPerHotkey</span> 3
<pre class="calibre22">#MaxThreadsPerHotkey 3
#z::
#MaxThreadsPerHotkey 1
if KeepWinZRunning = y ; This means an underlying <a href="#Threads.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">thread</a> is already running the loop below.
Expand Down Expand Up @@ -2840,7 +2840,7 @@ <h2 class="calibre9"><span class="calibre23">The "Last Found" Window </span></h2
<td height="16" class="calibre4">Causes some or all <a href="#Hotkeys.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">hotkeys</a> to buffer rather than ignore keypresses when their <a href="#h_MaxThreadsPerHotkey.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">#MaxThreadsPerHotkey</a> limit has been reached.</td>
</tr>
<tr class="calibre3">
<td height="16" class="tbd calibre4"><a href="#h_MaxThreadsPerHotkey.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">#MaxThreadsPerHotkey</a></td>
<td height="16" class="calibre4"><a href="#h_MaxThreadsPerHotkey.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">#MaxThreadsPerHotkey</a></td>
<td height="16" class="calibre4">Sets the maximum number of simultaneous <a href="#Threads.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">threads</a> per <a href="#Hotkeys.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">hotkey</a>.</td>
</tr>
<tr class="calibre3">
Expand Down Expand Up @@ -7502,7 +7502,7 @@ <h2 class="calibre9"><span class="calibre23">The "Last Found" Window </span></h2
#z::MsgBox, But not this one. </pre>
</div>
</div>
<div class="calibreMain tbd">
<div class="calibreMain">
<div class="calibreEbookContent">
<a id="h_MaxThreadsPerHotkey.htm" href="#h_MaxThreadsPerHotkey.htm">#</a> <h2 class="calibre17">#MaxThreadsPerHotkey</h2>
<hr size="2" class="calibre24" />
Expand Down
5 changes: 4 additions & 1 deletion src/build/parser.cr
Expand Up @@ -106,6 +106,9 @@ module Build
raise Exception.new ((e.message || "") + "\n#Include line: #{i}"), e.cause
end
end
elsif first_word.starts_with?("#maxthreadsperhotkey")
@runner_settings.max_threads_per_hotkey = args.to_u8? || 1_u8
raise "#MaxThreadsPerHotkey maximum value is 20" if @runner_settings.max_threads_per_hotkey > 20
elsif line.starts_with?("#!") && line_no == 0 # hashbang
elsif first_word == "if"
split = args.split(/ |\n/, 3, remove_empty: true)
Expand Down Expand Up @@ -158,7 +161,7 @@ module Build
end
else # Hotkey
@cmds << Cmd::ControlFlow::Label.new line_no, [label.downcase]
@hotkeys << Run::Hotkey.new label, priority: 0, escape_char: @runner_settings.escape_char
@hotkeys << Run::Hotkey.new label, priority: 0, escape_char: @runner_settings.escape_char, max_threads: @runner_settings.max_threads_per_hotkey
if ! instant_action.empty?
add_line "#{instant_action}", line_no
add_line "Return", line_no
Expand Down
17 changes: 14 additions & 3 deletions src/run/display/hotkey.cr
Expand Up @@ -9,10 +9,12 @@ module Run
getter modifier_variants = [] of UInt32
getter no_grab = false
property exempt_from_suspension = false
def initialize(@key_str, *, @priority, escape_char)
getter max_threads : UInt8
@threads = [] of Thread
def initialize(@key_str, *, @priority, escape_char, @max_threads)
init(escape_char)
end
def initialize(@cmd, @key_str, *, @priority, escape_char, @active = true)
def initialize(@cmd, @key_str, *, @priority, escape_char, @active = true, @max_threads)
init(escape_char)
end

Expand Down Expand Up @@ -50,7 +52,16 @@ module Run
end
end
def trigger(runner)
runner.not_nil!.add_thread @cmd.not_nil!, @priority
@threads.reject! &.done
# TODO: (commands not implemented yet):
# && ! @cmd_is_a?(Cmd::KeyHistory) && ! @cmd_is_a?(Cmd::ListLines) && ! @cmd_is_a?(Cmd::ListVars) && ! @cmd_is_a?(ListHotkeys)
if @threads.size >= @max_threads && ! @cmd.is_a?(Cmd::ControlFlow::ExitApp) && ! @cmd.is_a?(Cmd::Misc::Pause) && ! @cmd.is_a?(Cmd::Gtk::Edit) && ! @cmd.is_a?(Cmd::Misc::Reload)
# TODO: logger warn? what does win ahk do?
STDERR.puts "WARN: Skipping thread for hotkey press '#{key_str}' because #{@threads.size} threads are already running (max_threads==#{@max_threads}"
return
end
thread = runner.not_nil!.add_thread @cmd.not_nil!, @priority
@threads << thread
end
end
end
2 changes: 1 addition & 1 deletion src/run/display/hotkeys.cr
Expand Up @@ -46,7 +46,7 @@ module Run
active_state = hotkey.active if active_state.nil?
else
raise RuntimeException.new "Nonexistent Hotkey.\n\nSpecifically: #{hotkey_label}" if ! cmd_label
hotkey = Hotkey.new(cmd.not_nil!, hotkey_label, priority: priority, escape_char: @runner.settings.escape_char)
hotkey = Hotkey.new(cmd.not_nil!, hotkey_label, priority: priority, escape_char: @runner.settings.escape_char, max_threads: @runner.settings.max_threads_per_hotkey)
hotkey.exempt_from_suspension = cmd.is_a?(Cmd::Misc::Suspend)
@hotkeys << hotkey
active_state = true if active_state.nil?
Expand Down
3 changes: 3 additions & 0 deletions src/run/runner.cr
Expand Up @@ -19,6 +19,9 @@ module Run
property escape_char = '`'
property hotstring_end_chars = ['-', '(', ')', '[', ']', '{', '}', ':', ';', '\'', '"', '/', '\\', ',', '.', '?', '!', '\n', ' ', '\t', '\r']
property single_instance : SingleInstance?
# Can't be altered after parsing but we still need to remember this value somewhere
# for the dynamic creation of hotkeys.
property max_threads_per_hotkey = 1_u8
end

# can start a completely fresh and isolated ahk execution instance with its own
Expand Down

0 comments on commit 73f1442

Please sign in to comment.