Skip to content

Commit

Permalink
Replace FileChooserButtons with custom FileChooserEntry (tkashkin#131)
Browse files Browse the repository at this point in the history
Former-commit-id: fca3a8a
  • Loading branch information
tkashkin committed Nov 10, 2018
1 parent 8ad3ed7 commit a985187
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 137 deletions.
1 change: 1 addition & 0 deletions po/POTFILES
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ src/ui/views/GameDetailsView/blocks/GOGDetails.vala
src/ui/views/GameDetailsView/blocks/SteamDetails.vala
src/ui/widgets/AutoSizeImage.vala
src/ui/widgets/ActionButton.vala
src/ui/widgets/FileChooserEntry.vala
src/ui/widgets/CompatToolOptions.vala
src/ui/widgets/CompatToolPicker.vala
src/ui/widgets/TagRow.vala
Expand Down
52 changes: 20 additions & 32 deletions src/data/Runnable.vala
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,13 @@ namespace GameHub.Data
}
}

public virtual FileChooserDialog setup_executable_chooser()
public virtual FileChooser setup_executable_chooser()
{
var chooser = new FileChooserDialog(_("Select executable"), GameHub.UI.Windows.MainWindow.instance, FileChooserAction.OPEN);
var filter = new FileFilter();

filter.add_mime_type("application/x-executable");
filter.add_mime_type("application/x-elf");
filter.add_mime_type("application/x-sh");
filter.add_mime_type("text/x-shellscript");

filter.add_mime_type("application/x-dosexec");
filter.add_mime_type("application/x-ms-dos-executable");
filter.add_mime_type("application/dos-exe");
filter.add_mime_type("application/exe");
filter.add_mime_type("application/msdos-windows");
filter.add_mime_type("application/x-exe");
filter.add_mime_type("application/x-msdownload");
filter.add_mime_type("application/x-winexe");

chooser.set_filter(filter);
#if GTK_3_22
var chooser = new FileChooserNative(_("Select executable"), GameHub.UI.Windows.MainWindow.instance, FileChooserAction.OPEN, _("Select"), _("Cancel"));
#else
var chooser = new FileChooserDialog(_("Select executable"), GameHub.UI.Windows.MainWindow.instance, FileChooserAction.OPEN, _("Select"), ResponseType.ACCEPT, _("Cancel"), ResponseType.CANCEL);
#endif

try
{
Expand All @@ -91,31 +78,32 @@ namespace GameHub.Data
warning(e.message);
}

chooser.add_button(_("Cancel"), ResponseType.CANCEL);
var select_btn = chooser.add_button(_("Select"), ResponseType.ACCEPT);

select_btn.get_style_context().add_class(Gtk.STYLE_CLASS_SUGGESTED_ACTION);
select_btn.grab_default();

return chooser;
}

private int run_executable_chooser(FileChooser chooser)
{
#if GTK_3_22
return (chooser as FileChooserNative).run();
#else
return (chooser as FileChooserDialog).run();
#endif
}

public virtual void choose_executable(bool update=true)
{
var chooser = setup_executable_chooser();

if(chooser.run() == ResponseType.ACCEPT)
if(run_executable_chooser(chooser) == ResponseType.ACCEPT)
{
set_chosen_executable(chooser, update);
set_chosen_executable(chooser.get_file(), update);
}

chooser.destroy();
}

public virtual void set_chosen_executable(FileChooserDialog chooser, bool update=true)
public virtual void set_chosen_executable(File? file, bool update=true)
{
executable = chooser.get_file();
if(executable.query_exists())
executable = file;
if(executable != null && executable.query_exists())
{
Utils.run({"chmod", "+x", executable.get_path()});
}
Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ sources = [

'ui/widgets/AutoSizeImage.vala',
'ui/widgets/ActionButton.vala',
'ui/widgets/FileChooserEntry.vala',
'ui/widgets/CompatToolOptions.vala',
'ui/widgets/CompatToolPicker.vala',
'ui/widgets/TagRow.vala',
Expand Down
56 changes: 18 additions & 38 deletions src/ui/dialogs/GamePropertiesDialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ namespace GameHub.UI.Dialogs
private Entry name_entry;
private AutoSizeImage image_view;
private AutoSizeImage icon_view;
private Entry image_entry;
private Entry icon_entry;
private FileChooserEntry image_entry;
private FileChooserEntry icon_entry;

private Box properties_box;
private Box image_search_links;
Expand Down Expand Up @@ -217,11 +217,10 @@ namespace GameHub.UI.Dialogs
executable_header.xpad = 8;
properties_box.add(executable_header);

var executable_picker_dialog = game.setup_executable_chooser();
var executable_picker = new FileChooserButton.with_dialog(executable_picker_dialog);
var executable_picker = new FileChooserEntry(_("Select executable"), FileChooserAction.OPEN, "application-x-executable", _("Executable"), false, true);
try
{
executable_picker.set_file(game.executable);
executable_picker.select_file(game.executable);
}
catch(Error e)
{
Expand All @@ -231,7 +230,7 @@ namespace GameHub.UI.Dialogs
properties_box.add(executable_picker);

executable_picker.file_set.connect(() => {
game.set_chosen_executable(executable_picker_dialog);
game.set_chosen_executable(executable_picker.file);
});

var args_entry = new Entry();
Expand Down Expand Up @@ -321,8 +320,8 @@ namespace GameHub.UI.Dialogs

private void set_image_url(bool replace=false)
{
var url = image_entry.text.strip();
if(url.length == 0) url = game.image;
var url = image_entry.uri;
if(url == null || url.length == 0) url = game.image;
if(replace)
{
game.image = url;
Expand All @@ -335,8 +334,8 @@ namespace GameHub.UI.Dialogs

private void set_icon_url(bool replace=false)
{
var url = icon_entry.text.strip();
if(url.length == 0) url = game.icon;
var url = icon_entry.uri;
if(url == null || url.length == 0) url = game.icon;
if(replace)
{
game.icon = url;
Expand All @@ -347,36 +346,17 @@ namespace GameHub.UI.Dialogs
}
}

private Entry add_image_entry(string text, string icon)
private FileChooserEntry add_image_entry(string text, string icon)
{
var entry = new Entry();
entry.placeholder_text = entry.primary_icon_tooltip_text = text;
entry.primary_icon_name = icon;
entry.primary_icon_activatable = false;
entry.secondary_icon_name = "folder-symbolic";
entry.secondary_icon_activatable = true;
entry.secondary_icon_tooltip_text = _("Select file");
var entry = new FileChooserEntry(text, FileChooserAction.OPEN, icon, text, true);
entry.margin = 4;
entry.activate.connect(() => { set_image_url(false); set_icon_url(false); });
entry.focus_out_event.connect(() => { set_image_url(); set_icon_url(); return false; });
entry.icon_press.connect((icon, event) => {
if(icon == EntryIconPosition.SECONDARY && ((EventButton) event).button == 1)
{
#if GTK_3_22
var chooser = new FileChooserNative(_("Select file"), GameHub.UI.Windows.MainWindow.instance, FileChooserAction.OPEN, _("Select"), _("Cancel"));
#else
var chooser = new FileChooserDialog(_("Select file"), GameHub.UI.Windows.MainWindow.instance, FileChooserAction.OPEN, _("Select"), ResponseType.ACCEPT, _("Cancel"), ResponseType.CANCEL);
#endif
var filter = new FileFilter();
filter.add_mime_type("image/*");
chooser.set_filter(filter);
if(chooser.run() == ResponseType.ACCEPT)
{
entry.text = chooser.get_uri();
entry.activate();
}
}
});

var filter = new FileFilter();
filter.add_mime_type("image/*");
entry.chooser.set_filter(filter);

entry.uri_set.connect(() => { set_image_url(false); set_icon_url(false); });

return entry;
}

Expand Down
16 changes: 9 additions & 7 deletions src/ui/dialogs/SettingsDialog/SettingsDialogTab.vala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ along with GameHub. If not, see <https://www.gnu.org/licenses/>.

using Gtk;
using Granite;

using GameHub.Utils;
using GameHub.UI.Widgets;

namespace GameHub.UI.Dialogs.SettingsDialog
{
Expand Down Expand Up @@ -72,14 +74,14 @@ namespace GameHub.UI.Dialogs.SettingsDialog
return add_widget(hbox);
}

protected Box add_file_chooser(string text, FileChooserAction mode, string val, owned EntryAction action, bool create=true)
protected Box add_file_chooser(string text, FileChooserAction mode, string val, owned EntryAction action, bool create=true, string? icon=null, bool allow_url=false, bool allow_executable=false)
{
var chooser = new FileChooserButton(text, mode);
chooser.create_folders = create;
chooser.show_hidden = true;
chooser.select_filename(FSUtils.expand(val));
chooser.tooltip_text = chooser.get_filename();
chooser.file_set.connect(() => { chooser.tooltip_text = chooser.get_filename(); action(chooser.get_filename()); });
var chooser = new FileChooserEntry(text, mode, icon, null, allow_url, allow_executable);
chooser.chooser.create_folders = create;
chooser.chooser.show_hidden = true;
chooser.select_file(FSUtils.file(val));
chooser.tooltip_text = chooser.file.get_path();
chooser.file_set.connect(() => { chooser.tooltip_text = chooser.file != null ? chooser.file.get_path() : null; action(chooser.tooltip_text); });
chooser.set_size_request(280, -1);

var label = new Label(text);
Expand Down
6 changes: 4 additions & 2 deletions src/ui/dialogs/SettingsDialog/tabs/Collection.vala
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ along with GameHub. If not, see <https://www.gnu.org/licenses/>.

using Gtk;
using Granite;

using GameHub.Utils;
using GameHub.UI.Widgets;

namespace GameHub.UI.Dialogs.SettingsDialog.Tabs
{
public class Collection: SettingsDialogTab
{
private FileChooserButton collection_root;
private FileChooserEntry collection_root;

private Entry gog_game_dir;
private Entry gog_installers;
Expand All @@ -45,7 +47,7 @@ namespace GameHub.UI.Dialogs.SettingsDialog.Tabs
var gog = FSUtils.Paths.Collection.GOG.get_instance();
var humble = FSUtils.Paths.Collection.Humble.get_instance();

collection_root = add_file_chooser(_("Collection directory"), FileChooserAction.SELECT_FOLDER, collection.root, v => { collection.root = v; update_hints(); }).get_children().last().data as FileChooserButton;
collection_root = add_file_chooser(_("Collection directory"), FileChooserAction.SELECT_FOLDER, collection.root, v => { collection.root = v; update_hints(); }).get_children().last().data as FileChooserEntry;

add_separator();

Expand Down
54 changes: 19 additions & 35 deletions src/ui/dialogs/SettingsDialog/tabs/Emulators.vala
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ namespace GameHub.UI.Dialogs.SettingsDialog.Tabs
private Granite.Widgets.ModeButton mode;

private new Entry name;
private FileChooserButton emudir;
private FileChooserButton executable;
private FileChooserEntry emudir;
private FileChooserEntry executable;
private Label executable_label;
private Entry arguments;
private Label arguments_label;
Expand Down Expand Up @@ -240,10 +240,10 @@ namespace GameHub.UI.Dialogs.SettingsDialog.Tabs
emudir = add_filechooser(_("Directory"), _("Select emulator directory"), FileChooserAction.SELECT_FOLDER, true);

executable.file_set.connect(() => {
emulator.executable = executable.get_file();
if(name.text.strip().length == 0)
emulator.executable = executable.file;
if(name.text.strip().length == 0 && executable.file != null)
{
name.text = executable.get_file().get_basename();
name.text = executable.file.get_basename();
}
update();
});
Expand All @@ -252,8 +252,7 @@ namespace GameHub.UI.Dialogs.SettingsDialog.Tabs
{
try
{
emudir.set_file(emulator.install_dir);
emudir.file_set();
emudir.select_file(emulator.install_dir);
}
catch(Error e)
{
Expand All @@ -265,8 +264,7 @@ namespace GameHub.UI.Dialogs.SettingsDialog.Tabs
{
try
{
executable.set_file(emulator.executable);
executable.file_set();
executable.select_file(emulator.executable);
}
catch(Error e)
{
Expand Down Expand Up @@ -308,28 +306,21 @@ namespace GameHub.UI.Dialogs.SettingsDialog.Tabs

private void update()
{
if(mode.selected == 0 && executable.get_file() != null && emudir.get_file() == null)
if(mode.selected == 0 && executable.file != null && emudir.file == null)
{
try
{
emudir.select_file(executable.get_file().get_parent());
}
catch(Error e)
{
warning(e.message);
}
emudir.select_file(executable.file.get_parent());
}

emulator.name = title;
emulator.arguments = arguments.text.strip();

emulator.install_dir = emudir.get_file();
emulator.install_dir = emudir.file;

executable_label.label = mode.selected == 0 ? _("Executable") : _("Installer");
arguments.sensitive = arguments_label.sensitive = mode.selected == 0;

run_btn.sensitive = emulator.name.length > 0 && executable.get_file() != null && mode.selected == 0 && emudir.get_file() != null;
save_btn.sensitive = emulator.name.length > 0 && executable.get_file() != null && ((mode.selected == 0 && emudir.get_file() != null) || mode.selected == 1);
run_btn.sensitive = emulator.name.length > 0 && executable.file != null && mode.selected == 0 && emudir.file != null;
save_btn.sensitive = emulator.name.length > 0 && executable.file != null && ((mode.selected == 0 && emudir.file != null) || mode.selected == 1);

emulator.notify_property("use-compat");
}
Expand All @@ -338,7 +329,7 @@ namespace GameHub.UI.Dialogs.SettingsDialog.Tabs
{
update();

if(mode.selected == 1 && executable.get_file() != null && emudir.get_file() != null)
if(mode.selected == 1 && executable.file != null && emudir.file != null)
{
sensitive = false;

Expand All @@ -349,14 +340,7 @@ namespace GameHub.UI.Dialogs.SettingsDialog.Tabs
emulator.install.end(res);
sensitive = true;
mode.selected = 0;
try
{
executable.select_file(emulator.executable);
}
catch(Error e)
{
warning(e.message);
}
executable.select_file(emulator.executable);
emulator.save();
});

Expand Down Expand Up @@ -398,7 +382,7 @@ namespace GameHub.UI.Dialogs.SettingsDialog.Tabs
return entry;
}

private FileChooserButton add_filechooser(string text, string title, FileChooserAction action=FileChooserAction.OPEN, bool required=true, out Label label=null)
private FileChooserEntry add_filechooser(string text, string title, FileChooserAction action=FileChooserAction.OPEN, bool required=true, out Label label=null)
{
label = new Label(text);
label.halign = Align.START;
Expand All @@ -409,12 +393,12 @@ namespace GameHub.UI.Dialogs.SettingsDialog.Tabs
{
label.get_style_context().add_class("category-label");
}
var button = new FileChooserButton(title, action);
button.set_size_request(180, -1);
var entry = new FileChooserEntry(title, action, null, null, false, action == FileChooserAction.OPEN);
entry.set_size_request(180, -1);
attach(label, 0, rows);
attach(button, 1, rows);
attach(entry, 1, rows);
rows++;
return button;
return entry;
}

private void add_separator()
Expand Down
Loading

0 comments on commit a985187

Please sign in to comment.