Skip to content

Commit 5320fa5

Browse files
authored
feat: gracefully handle closing worker windows (#241)
1 parent e69ab89 commit 5320fa5

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

src/collision/functions/checksum.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ module Collision
7979
t_res = channel.receive
8080
res[t_res[0]] = t_res[1]
8181
end
82+
channel.close
8283
block.call(res)
8384
end
8485
end

src/collision/window.cr

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ module Collision
5353
@hash_results = Hash(Symbol, String).new
5454
@file_path_queued : Path? = nil
5555

56+
property working : Bool = false
57+
5658
def continue_queue
5759
return if @file_path_queued.nil?
5860
self.file = @file_path_queued.not_nil!
@@ -69,26 +71,45 @@ module Collision
6971
@file_path_queued = nil
7072
Collision.atomic_increase
7173
@fileInfo.title = filepath.basename.to_s
72-
Collision::LOGGER.debug { "Begin generating hashes" }
73-
Collision::Checksum.new.generate(filepath.to_s, @progressbar) do |res|
74-
GLib.idle_add do
75-
res.each do |hash_type, hash_value|
76-
@hash_results[hash_type] = hash_value
77-
@hash_rows[hash_type].subtitle = hash_value.size < 8 ? hash_value : Collision.split_by_4(hash_value)
78-
end
7974

80-
@mainStack.visible_child_name = "results"
81-
@headerbarViewSwitcher.visible = true
82-
@openFileBtn.visible = true
83-
@switcher_bar.visible = true
84-
Collision.atomic_decrease
85-
self.application.not_nil!.windows.each do |window|
86-
Window.cast(window).continue_queue
75+
begin
76+
self.working = true
77+
Collision::LOGGER.debug { "Begin generating hashes" }
78+
Collision::Checksum.new.generate(filepath.to_s, @progressbar) do |res|
79+
if !self.visible
80+
flow_queue
81+
next
8782
end
8883

89-
false
84+
GLib.idle_add do
85+
res.each do |hash_type, hash_value|
86+
@hash_results[hash_type] = hash_value
87+
@hash_rows[hash_type].subtitle = hash_value.size < 8 ? hash_value : Collision.split_by_4(hash_value)
88+
end
89+
90+
@mainStack.visible_child_name = "results"
91+
@headerbarViewSwitcher.visible = true
92+
@openFileBtn.visible = true
93+
@switcher_bar.visible = true
94+
flow_queue
95+
false
96+
end
9097
end
98+
rescue ex
99+
flow_queue
100+
raise ex
101+
end
102+
end
103+
104+
# Force inline queue recovery
105+
macro flow_queue
106+
self.working = false
107+
Collision.atomic_decrease
108+
self.application.not_nil!.windows.each do |window|
109+
Window.cast(window).continue_queue
91110
end
111+
112+
self.destroy unless self.visible
92113
end
93114

94115
# For Gio::File.
@@ -302,7 +323,7 @@ module Collision
302323
end
303324

304325
@mainDnd.enter_signal.connect do
305-
@dropOverlayRevealer.reveal_child = true
326+
@dropOverlayRevealer.reveal_child = true unless @mainStack.visible_child_name == "spinner"
306327
Gdk::DragAction::Copy
307328
end
308329

@@ -311,6 +332,8 @@ module Collision
311332
end
312333

313334
@mainDnd.drop_signal.connect do |value|
335+
next false if @mainStack.visible_child_name == "spinner"
336+
314337
if LibGObject.g_type_check_value_holds(value, Gdk::FileList.g_type)
315338
files = Gdk::FileList.new(LibGObject.g_value_get_boxed(value), GICrystal::Transfer::None).files
316339
file = nil
@@ -373,6 +396,12 @@ module Collision
373396
Collision::LOGGER.debug { ex }
374397
end
375398
end
399+
400+
self.close_request_signal.connect do
401+
self.hide_on_close = true if self.working && self.application.not_nil!.windows.size > 1
402+
403+
false
404+
end
376405
end
377406
end
378407
end

0 commit comments

Comments
 (0)