Skip to content

Commit

Permalink
feat: progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
GeopJr committed Nov 13, 2023
1 parent 332991c commit 88e3ba2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
27 changes: 18 additions & 9 deletions data/ui/application.ui
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,23 @@
<object class="GtkStackPage">
<property name="name">spinner</property>
<property name="child">
<object class="GtkSpinner">
<property name="valign">center</property>
<property name="halign">center</property>
<property name="hexpand">1</property>
<property name="vexpand">1</property>
<property name="spinning">1</property>
<property name="width-request">32</property>
<property name="height-request">32</property>
<object class="AdwStatusPage">
<property name="title" translatable="yes">Calculating Hashes</property>
<property name="description" translatable="yes">This might take a while</property>
<child>
<object class="AdwClamp">
<property name="tightening-threshold">100</property>
<property name="maximum-size">400</property>
<child>
<object class="GtkProgressBar" id="progressbar">
<property name="show-text">1</property>
<property name="valign">center</property>
<property name="hexpand">1</property>
<property name="vexpand">1</property>
</object>
</child>
</object>
</child>
</object>
</property>
</object>
Expand Down Expand Up @@ -359,4 +368,4 @@
</object>
</property>
</template>
</interface>
</interface>
8 changes: 7 additions & 1 deletion src/collision/functions/checksum.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module Collision::Checksum
yield res
end

def generate(filename : String, &block : Hash(Symbol, String) ->)
def generate(filename : String, progressbar : Gtk::ProgressBar? = nil, &block : Hash(Symbol, String) ->)
hash_amount = Collision::HASH_FUNCTIONS.size
Collision::HASH_FUNCTIONS.each_with_index do |hash_key, hash_value, i|
proc = ->(fiber_no : Int32) do
Expand All @@ -67,9 +67,15 @@ module Collision::Checksum

Collision::Checksum.spawn do
res = Hash(Symbol, String).new
step = 1/hash_amount
hash_amount.times do |i|
t_res = @@channel.receive
res[t_res[0]] = t_res[1]

unless progressbar.nil?
progressbar.fraction = Math.min(progressbar.fraction + step, 1.0)
progressbar.text = sprintf(Gettext.gettext("%d of %d hashes calculated"), {i + 1, hash_amount})
end
end

on_finished(res, &block)
Expand Down
6 changes: 5 additions & 1 deletion src/collision/window.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Collision
"compareBtnStack",
"switcher_bar",
"hash_row_container",
"progressbar",
}
)]
class Window < Adw::ApplicationWindow
Expand All @@ -39,6 +40,7 @@ module Collision
@mainStack : Gtk::Stack
@fileInfo : Adw::StatusPage
@switcher_bar : Adw::ViewSwitcherBar
@progressbar : Gtk::ProgressBar

@verifyOverlayLabel : Gtk::Label
@verifyTextView : Gtk::TextView
Expand All @@ -53,7 +55,7 @@ module Collision
@fileInfo.description = Collision::FileUtils.real_path(filepath)

Collision::LOGGER.debug { "Begin generating hashes" }
Collision::Checksum.generate(filepath.to_s) do |res|
Collision::Checksum.generate(filepath.to_s, @progressbar) do |res|
sleep 500.milliseconds
GLib.idle_add do
res.each do |hash_type, hash_value|
Expand Down Expand Up @@ -89,6 +91,7 @@ module Collision
end

def loading
@progressbar.fraction = 0.0
@mainStack.visible_child_name = "spinner"
@headerbarStack.visible_child_name = "empty"
@openFileBtn.visible = false
Expand Down Expand Up @@ -208,6 +211,7 @@ module Collision
@compareBtnImage = Gtk::Image.cast(template_child("compareBtnImage"))
@compareBtnLabel = Gtk::Label.cast(template_child("compareBtnLabel"))
@compareBtnStack = Gtk::Stack.cast(template_child("compareBtnStack"))
@progressbar = Gtk::ProgressBar.cast(template_child("progressbar"))

@mainFileChooserNative = Gtk::FileChooserNative.cast(template_child("mainFileChooserNative"))
@compareBtnFileChooserNative = Gtk::FileChooserNative.cast(template_child("compareBtnFileChooserNative"))
Expand Down

0 comments on commit 88e3ba2

Please sign in to comment.