Skip to content

Commit

Permalink
Merge pull request #74 from Adithyan-KV/file-chooser
Browse files Browse the repository at this point in the history
Implements opening images using file chooser
  • Loading branch information
peteruithoven authored Nov 25, 2021
2 parents b65eb3e + 680addb commit ca0fe63
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions src/DropArea.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Resizer {

private Gtk.Image image;
private Gtk.Image image2;
private Gtk.Label drag_label;
private Gtk.Box overlay_box;

construct {
image = new Gtk.Image ();
Expand All @@ -41,7 +41,20 @@ namespace Resizer {
image2.margin_top = 6;
image2.visible = false;

drag_label = new Gtk.Label (_("Drop Image(s) Here"));
overlay_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
overlay_box.valign = Gtk.Align.CENTER;
overlay_box.halign = Gtk.Align.CENTER;

var or_label = new Gtk.Label (_("or"));
var or_label_style_context = or_label.get_style_context ();
or_label_style_context.add_class (Granite.STYLE_CLASS_H2_LABEL);
or_label_style_context.add_class (Gtk.STYLE_CLASS_DIM_LABEL);

var select_button = new Gtk.Button.with_label (_("Select image"));
select_button.get_style_context ().add_class (Gtk.STYLE_CLASS_SUGGESTED_ACTION);
select_button.clicked.connect (open_files_using_file_chooser);

var drag_label = new Gtk.Label (_("Drop Image(s) Here"));
drag_label.justify = Gtk.Justification.CENTER;

var drag_label_style_context = drag_label.get_style_context ();
Expand All @@ -53,8 +66,12 @@ namespace Resizer {
images.halign = Gtk.Align.CENTER;
images.put (image2, 0, 0);
images.put (image, 0, 0);
this.add(images);
this.add_overlay (drag_label);

overlay_box.pack_start (drag_label);
overlay_box.pack_start (or_label);
overlay_box.pack_start (select_button);
this.add (images);
this.add_overlay (overlay_box);
}
public void show_preview(File[] files) throws Error {

Expand All @@ -66,8 +83,8 @@ namespace Resizer {
true
);

drag_label.no_show_all = true;
drag_label.visible = false;
overlay_box.no_show_all = true;
overlay_box.visible = false;

image.set_from_pixbuf (pixbuf);
image.height_request = pixbuf.height;
Expand All @@ -91,5 +108,26 @@ namespace Resizer {
image.margin_top = 6;
}
}

private void open_files_using_file_chooser () {
var file_chooser = new Gtk.FileChooserNative (_("Open Image(s)"),
null,
Gtk.FileChooserAction.OPEN,
_("Open"),
_("Cancel"));

var files = new GenericArray<File> ();
file_chooser.select_multiple = true;
var response = file_chooser.run ();
if (response == Gtk.ResponseType.ACCEPT) {
var uris = file_chooser.get_uris ();
foreach (var uri in uris) {
stdout.printf ("opening: %s\n", uri);
var file = File.new_for_uri (uri);
files.add (file);
Resizer.get_default ().files = files.data;
}
}
}
}
}

0 comments on commit ca0fe63

Please sign in to comment.