Skip to content

Commit

Permalink
feat: FileChooserNative => FileDialog (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeopJr authored Oct 1, 2024
1 parent 28a3485 commit 7855e7e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 46 deletions.
9 changes: 0 additions & 9 deletions data/ui/application.ui
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@
<attribute name="action">app.about</attribute>
</item>
</menu>
<object class="GtkFileChooserNative" id="mainFileChooserNative">
<property name="title" translatable="yes">Choose a File</property>
<property name="modal">1</property>
<property name="select-multiple">1</property>
</object>
<object class="GtkFileChooserNative" id="compareBtnFileChooserNative">
<property name="title" translatable="yes">Choose a File</property>
<property name="modal">1</property>
</object>
<template class="Collision-Window" parent="AdwApplicationWindow">
<property name="width-request">360</property>
<property name="height-request">360</property>
Expand Down
4 changes: 2 additions & 2 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ shards:

gi-crystal:
git: https://github.com/hugopl/gi-crystal.git
version: 0.22.3
version: 0.24.0

gtk4:
git: https://github.com/hugopl/gtk4.cr.git
version: 0.16.1
version: 0.17.0

harfbuzz:
git: https://github.com/hugopl/harfbuzz.cr.git
Expand Down
62 changes: 27 additions & 35 deletions src/collision/window.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ module Collision
resource: "/dev/geopjr/Collision/ui/application.ui",
children: {
"welcomeBtn",
"mainFileChooserNative",
"compareBtnFileChooserNative",
"mainStack",
"fileInfo",
"headerbarStack",
Expand Down Expand Up @@ -35,8 +33,6 @@ module Collision
@compareBtnLabel : Gtk::Label
@compareBtnStack : Gtk::Stack
@openFileBtn : Gtk::Button
@mainFileChooserNative : Gtk::FileChooserNative
@compareBtnFileChooserNative : Gtk::FileChooserNative
@mainStack : Gtk::Stack
@fileInfo : Adw::StatusPage
@switcher_bar : Adw::ViewSwitcherBar
Expand Down Expand Up @@ -82,7 +78,23 @@ module Collision
end

def on_open_btn_clicked
@mainFileChooserNative.show
Gtk::FileDialog.new(
title: Gettext.gettext("Choose a File"),
modal: true
).open_multiple(self, nil) do |obj, result|
next if (files = obj.as(Gtk::FileDialog).open_multiple_finish(result)).nil?

gio_files = [] of Gio::File
files.n_items.times do |i|
gio_files << Gio::File.cast(files.item(i).not_nil!)
end

loading
self.file = gio_files.shift
open_files(Adw::Application.cast(self.application.not_nil!), gio_files) unless gio_files.empty? || self.application.nil?
rescue ex
Collision::LOGGER.debug { ex }
end
end

def on_drop(file : Gio::File)
Expand Down Expand Up @@ -227,49 +239,29 @@ module Collision
@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"))
@mainFileChooserNative.transient_for = self
@compareBtnFileChooserNative.transient_for = self

@verifyTextView.buffer.notify_signal["text"].connect do
Collision::LOGGER.debug { "Verify tool notify event" }

handle_input_change(@verifyTextView.buffer.text)
end

@mainFileChooserNative.response_signal.connect do |response|
next unless response == -3

gio_files = [] of Gio::File
files = @mainFileChooserNative.files
files.n_items.times do |i|
gio_files << Gio::File.cast(files.item(i).not_nil!)
end

loading
self.file = gio_files.shift
open_files(Adw::Application.cast(self.application.not_nil!), gio_files) unless gio_files.empty? || self.application.nil?
rescue ex
Collision::LOGGER.debug { ex }
end

@compareBtnFileChooserNative.response_signal.connect do |response|
next unless response == -3

self.comparefile = @compareBtnFileChooserNative.file.not_nil!
rescue ex
Collision::LOGGER.debug { ex }
end

@welcomeBtn.clicked_signal.connect(->on_open_btn_clicked)
@openFileBtn.clicked_signal.connect(->on_open_btn_clicked)

@mainStack.add_controller(Collision::DragNDrop.new(->on_drop(Gio::File)).controller)
@compareBtn.add_controller(Collision::DragNDrop.new(->comparefile=(Gio::File)).controller)

@compareBtn.clicked_signal.connect do
@compareBtnFileChooserNative.show
Gtk::FileDialog.new(
title: Gettext.gettext("Choose a File"),
modal: true
).open(self, nil) do |obj, result|
next if (file = obj.as(Gtk::FileDialog).open_finish(result)).nil?

self.comparefile = file
rescue ex
Collision::LOGGER.debug { ex }
end
end
end
end
Expand Down

0 comments on commit 7855e7e

Please sign in to comment.