From a1157abceff58a31185f2333f9684d863627bee3 Mon Sep 17 00:00:00 2001 From: Artem Anufrij Date: Sun, 3 Sep 2017 00:54:07 +0200 Subject: [PATCH] added 'run in background' option --- src/Application.vala | 43 ++++++++++++++++++++++++------------ src/Objects/DesktopFile.vala | 31 ++++++++++++++++++++------ src/WebWindow.vala | 34 +++++++++++++++------------- src/Widgets/Assistant.vala | 29 ++++++++++++++---------- 4 files changed, 89 insertions(+), 48 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index 9e7a72c..f062c23 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -31,6 +31,8 @@ namespace Webpin { static WebpinApp _instance = null; + public GLib.List app_list; + public static WebpinApp instance { get { if (_instance == null) @@ -40,10 +42,12 @@ namespace Webpin { } construct { - program_name = "Image Burner"; + program_name = "Webpin"; exec_name = "com.github.artemanufrij.webpin"; application_id = "com.github.artemanufrij.webpin"; app_launcher = application_id + ".desktop"; + flags |= GLib.ApplicationFlags.HANDLES_OPEN; + app_list = new GLib.List (); } public Gtk.Window mainwindow; @@ -55,23 +59,34 @@ namespace Webpin { } mainwindow = new MainWindow (); + mainwindow.destroy.connect (() => { mainwindow = null; }); mainwindow.set_application(this); } + + public override void open (File[] files, string hint) { + debug (files [0].get_uri ()); + start_webapp (files [0].get_uri ()); + } + + public void start_webapp (string url) { + foreach (var item in app_list) { + if (item.desktop_file.url == url) { + item.present (); + return; + } + } + + var app_info = Webpin.DesktopFile.get_app_by_url (url); + var desktop_file = new Webpin.DesktopFile.from_desktopappinfo(app_info); + var app = new WebWindow(desktop_file); + app.destroy.connect (() => { app_list.remove (app); }); + app.set_application (this); + app_list.append (app); + } } } static int main (string[] args) { - Gtk.init (ref args); - - if (args.length < 2 || args[1] == "--about" || args[1] == "-d") { - var app = Webpin.WebpinApp.instance; - return app.run (args); - } else { - var app_info = Webpin.DesktopFile.get_app_by_url (args[1]); - var app = new Webpin.WebWindow(app_info.get_display_name (), args[1]); - app.show_all (); - } - - Gtk.main (); - return 0; + var app = Webpin.WebpinApp.instance; + return app.run (args); } diff --git a/src/Objects/DesktopFile.vala b/src/Objects/DesktopFile.vala index 6f87a0e..6af60aa 100644 --- a/src/Objects/DesktopFile.vala +++ b/src/Objects/DesktopFile.vala @@ -45,15 +45,30 @@ namespace Webpin { WebpinThemeColor=none"""; private GLib.KeyFile file; + public DesktopAppInfo info { get; set; } public string name { get; private set; } public string url { get; private set; } public string icon { get; private set; } + public bool stay_open { get; private set; } + public bool hide_on_close { + get { + this.file = new GLib.KeyFile(); + try { + file.load_from_file (info.filename, KeyFileFlags.NONE); + return file.get_string ("Desktop Entry", "WebpinStayOpen") == "true"; + } catch (Error e) { + warning (e.message); + } + return false; + } + } - public DesktopFile (string name, string url, string icon) { + public DesktopFile (string name, string url, string icon, bool stay_open) { this.name = name; this.url = url.replace ("%", "%%"); this.icon = icon; + this.stay_open = stay_open; file = new GLib.KeyFile(); try { @@ -68,10 +83,12 @@ namespace Webpin { file.set_string ("Desktop Entry", "Exec", "com.github.artemanufrij.webpin " + url); file.set_string ("Desktop Entry", "Icon", icon); file.set_string ("Desktop Entry", "StartupWMClass", url); + file.set_string ("Desktop Entry", "WebpinStayOpen", stay_open.to_string ()); } public DesktopFile.from_desktopappinfo(GLib.DesktopAppInfo info) { - file = new GLib.KeyFile(); + this.info = info; + this.file = new GLib.KeyFile(); try { file.load_from_file (info.filename, KeyFileFlags.NONE); } catch (Error e) { @@ -81,6 +98,7 @@ namespace Webpin { this.icon = info.get_icon ().to_string (); try { this.url = file.get_string ("Desktop Entry", "Exec").substring (31); + this.stay_open = file.get_string ("Desktop Entry", "WebpinStayOpen") == "true"; } catch (Error e) { warning (e.message); } @@ -111,19 +129,18 @@ namespace Webpin { } catch (Error e) { warning (e.message); } - return return_value; } public bool delete_file () { try { string filename = GLib.Environment.get_user_data_dir () + "/applications/" +file.get_string("Desktop Entry", "Name") + "-webpin.desktop"; - File file = File.new_for_path (filename); - file.delete (); - } catch (Error e) { + File file = File.new_for_path (filename); + file.delete (); + } catch (Error e) { print(e.message + "\n"); return false; - } + } return true; } diff --git a/src/WebWindow.vala b/src/WebWindow.vala index e0c11a6..1cd0c0c 100644 --- a/src/WebWindow.vala +++ b/src/WebWindow.vala @@ -27,7 +27,7 @@ */ namespace Webpin { - public class WebWindow : Gtk.ApplicationWindow { + public class WebWindow : Gtk.Window { private bool is_full_screen = false; @@ -38,13 +38,17 @@ namespace Webpin { Gtk.Spinner spinner; - public WebWindow (string webapp_name, string webapp_uri) { + public DesktopFile desktop_file { get; private set; } - set_wmclass(webapp_uri, webapp_uri); - web_app = new WebApp(webapp_name, webapp_uri); + public WebWindow (DesktopFile desktop_file) { + this.desktop_file = desktop_file; + this.events |= Gdk.EventMask.STRUCTURE_MASK; + + set_wmclass(desktop_file.url, desktop_file.url); + web_app = new WebApp(desktop_file.name, desktop_file.url); var headerbar = new Gtk.HeaderBar (); - headerbar.title = webapp_name; + headerbar.title = desktop_file.name; headerbar.show_close_button = true; spinner = new Gtk.Spinner (); @@ -61,7 +65,7 @@ namespace Webpin { headerbar.get_style_context ().add_provider (style_provider, -1); Gtk.Settings.get_default ().set ("gtk-application-prefer-dark-theme", should_use_dark_theme (web_app.ui_color)); } catch (GLib.Error err) { - warning("Loading style failed"); + warning("Loading style failed"); } } @@ -74,17 +78,16 @@ namespace Webpin { headerbar.get_style_context ().add_provider (style_provider, -1); Gtk.Settings.get_default ().set ("gtk-application-prefer-dark-theme", should_use_dark_theme (color)); } catch (GLib.Error err) { - warning("Loading style failed"); + warning("Loading style failed"); } }); this.set_titlebar (headerbar); - var info = DesktopFile.get_app_by_url(webapp_uri); - var width = info.get_string ("WebpinWindowWidth"); - var height = info.get_string ("WebpinWindowHeight"); - var state = info.get_string ("WebpinWindowMaximized"); - var zoom = info.get_string ("WebpinWindowZoom"); + var width = desktop_file.info.get_string ("WebpinWindowWidth"); + var height = desktop_file.info.get_string ("WebpinWindowHeight"); + var state = desktop_file.info.get_string ("WebpinWindowMaximized"); + var zoom = desktop_file.info.get_string ("WebpinWindowZoom"); if (width != null && height != null) { set_default_size (int.parse(width), int.parse(height)); @@ -102,11 +105,12 @@ namespace Webpin { this.delete_event.connect (() => { update_window_state(this.get_allocated_width (), this.get_allocated_height (), this.is_maximized); - return false; + if (desktop_file.hide_on_close) { + this.hide (); + } + return desktop_file.hide_on_close; }); - this.destroy.connect(Gtk.main_quit); - web_app.external_request.connect ((action) => { debug ("Web app external request: %s", action.get_request ().uri); try { diff --git a/src/Widgets/Assistant.vala b/src/Widgets/Assistant.vala index 507a524..d93a536 100644 --- a/src/Widgets/Assistant.vala +++ b/src/Widgets/Assistant.vala @@ -41,6 +41,7 @@ namespace Webpin { private Gtk.Entry icon_name_entry; private Gtk.CheckButton save_cookies_check; private Gtk.CheckButton save_password_check; + private Gtk.CheckButton stay_open_when_closed; private Gtk.Popover icon_selector_popover; private Gtk.FileChooserDialog file_chooser; private Gtk.Button accept_button; @@ -114,6 +115,8 @@ namespace Webpin { save_cookies_check.active = true; save_password_check = new Gtk.CheckButton.with_label (_("Save login information")); save_password_check.active = false; + stay_open_when_closed = new Gtk.CheckButton.with_label (_("Run in background when closed")); + stay_open_when_closed.active = false; //app information section var app_input_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 5); @@ -131,9 +134,9 @@ namespace Webpin { var app_options_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 5); app_options_box.pack_start (save_cookies_check, true, false, 0); app_options_box.pack_start (save_password_check, true, false, 0); + app_options_box.pack_start (stay_open_when_closed, true, false, 0); app_options_box.halign = Gtk.Align.CENTER; - //create button accept_button = new Gtk.Button.with_label(_("Save app")); accept_button.halign = Gtk.Align.END; @@ -221,17 +224,17 @@ namespace Webpin { } private void on_icon_chooser_activate () { - var filter = new Gtk.FileFilter (); + var filter = new Gtk.FileFilter (); - filter.set_filter_name (_("Images")); - filter.add_pattern ("*.png"); - filter.add_pattern ("*.svg"); - filter.add_pattern ("*.jpg"); - filter.add_pattern ("*.jpeg"); - filter.add_pattern ("*.PNG"); - filter.add_pattern ("*.SVG"); - filter.add_pattern ("*.JPG"); - filter.add_pattern ("*.JPEG"); + filter.set_filter_name (_("Images")); + filter.add_pattern ("*.png"); + filter.add_pattern ("*.svg"); + filter.add_pattern ("*.jpg"); + filter.add_pattern ("*.jpeg"); + filter.add_pattern ("*.PNG"); + filter.add_pattern ("*.SVG"); + filter.add_pattern ("*.JPG"); + filter.add_pattern ("*.JPEG"); file_chooser = new Gtk.FileChooserDialog ("", null, Gtk.FileChooserAction.OPEN, @@ -299,12 +302,13 @@ namespace Webpin { string icon = icon_name_entry.get_text (); string name = app_name_entry.get_text (); string url = app_url_entry.get_text ().replace ("%", "%%"); + bool stay_open = stay_open_when_closed.active; if (icon == "") icon = default_app_icon; if (app_icon_valid && app_name_valid && app_url_valid) { - var desktop_file = new DesktopFile (name, url, icon); + var desktop_file = new DesktopFile (name, url, icon, stay_open); switch (mode) { case assistant_mode.new_app: application_created (desktop_file.save_to_file ()); @@ -322,6 +326,7 @@ namespace Webpin { app_name_entry.set_sensitive (false); app_url_entry.text = desktop_file.url.replace ("%%", "%"); icon_name_entry.text = desktop_file.icon; + stay_open_when_closed.active = desktop_file.stay_open; update_app_icon (); } }