webpin/src/WebWindow.vala

185 lines
6.6 KiB
Vala
Raw Normal View History

/*-
* Copyright (c) 2015 Erasmo Marín <erasmo.marin@gmail.com>
* Copyright (c) 2017-2017 Artem Anufrij <artem.anufrij@live.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* The Noise authors hereby grant permission for non-GPL compatible
* GStreamer plugins to be used and distributed together with GStreamer
* and Noise. This permission is above and beyond the permissions granted
* by the GPL license by which Noise is covered. If you modify this code
* you may extend this exception to your version of the code, but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* Authored by: Artem Anufrij <artem.anufrij@live.de>
*/
namespace Webpin {
2017-08-01 17:09:52 +01:00
public class WebWindow : Gtk.ApplicationWindow {
private bool is_full_screen = false;
private string style_str = """@define-color titlebar_color @titlebar_color;""";
//widgets
private WebApp web_app;
Gtk.Spinner spinner;
2017-08-01 17:09:52 +01:00
public WebWindow (string webapp_name, string webapp_uri) {
set_wmclass(webapp_uri, webapp_uri);
web_app = new WebApp(webapp_name, webapp_uri);
var headerbar = new Gtk.HeaderBar ();
headerbar.title = webapp_name;
2017-08-10 20:18:37 +01:00
headerbar.show_close_button = true;
spinner = new Gtk.Spinner ();
spinner.set_size_request (16, 16);
headerbar.pack_end (spinner);
//style
if (web_app.ui_color != "none") {
try {
2017-07-31 23:24:50 +01:00
debug ("set color");
var style_cp = style_str.replace ("@titlebar_color", web_app.ui_color);
var style_provider = new Gtk.CssProvider ();
style_provider.load_from_data (style_cp, -1);
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");
2017-07-31 22:49:48 +01:00
}
}
web_app.theme_color_changed.connect( (color)=> {
try {
2017-07-31 23:24:50 +01:00
debug ("set color");
var style_cp = style_str.replace ("@titlebar_color", color);
var style_provider = new Gtk.CssProvider ();
style_provider.load_from_data (style_cp, -1);
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");
}
});
2017-07-31 19:27:03 +01:00
this.set_titlebar (headerbar);
2017-07-31 19:27:03 +01:00
var info = DesktopFile.get_app_by_url(webapp_uri);
2017-07-31 22:49:48 +01:00
var width = info.get_string("WebpinWindowWidth");
var height = info.get_string("WebpinWindowHeight");
2017-07-31 19:27:03 +01:00
if(width !=null && height != null)
set_default_size (int.parse(width), int.parse(height));
else
set_default_size (1000, 600);
this.delete_event.connect (() => {
update_window_state(this.get_allocated_width (), this.get_allocated_height () );
return false;
});
2017-07-31 19:27:03 +01:00
this.destroy.connect(Gtk.main_quit);
2017-07-31 19:27:03 +01:00
web_app.external_request.connect ((action) => {
debug ("Web app external request: %s", action.get_request ().uri);
try {
Process.spawn_command_line_async ("xdg-open " + action.get_request ().uri);
} catch (Error e) {
warning (e.message);
}
});
2017-07-31 19:27:03 +01:00
web_app.request_begin.connect (() => {
spinner.active = true;
});
web_app.request_finished.connect (() => {
spinner.active = false;
});
add(web_app);
show_all();
}
2017-07-31 19:27:03 +01:00
public new void fullscreen () {
is_full_screen = true;
base.fullscreen();
}
2017-07-31 19:27:03 +01:00
public new void unfullscreen () {
is_full_screen = false;
base.unfullscreen();
}
2017-07-31 19:27:03 +01:00
public void toggle_fullscreen() {
if(is_full_screen)
unfullscreen();
else
fullscreen();
is_full_screen = !is_full_screen;
2017-07-31 19:27:03 +01:00
}
public void update_window_state (int width, int height) {
var file = web_app.get_desktop_file();
2017-07-31 22:49:48 +01:00
file.edit_propertie ("WebpinWindowWidth", width.to_string());
file.edit_propertie ("WebpinWindowHeight", height.to_string());
}
public override bool key_press_event (Gdk.EventKey event) {
bool handled = true;
switch (event.keyval) {
case Gdk.Key.Escape:
unfullscreen();
break;
case Gdk.Key.F11:
toggle_fullscreen();
break;
case Gdk.Key.Left:
if (Gdk.ModifierType.MOD1_MASK in event.state) {
web_app.app_view.go_back ();
}
break;
case Gdk.Key.Right:
if (Gdk.ModifierType.MOD1_MASK in event.state) {
web_app.app_view.go_forward ();
}
break;
default:
handled = false;
break;
}
if (handled)
return true;
2017-07-31 19:27:03 +01:00
2017-07-31 22:49:48 +01:00
return (base.key_press_event != null) ? base.key_press_event (event) : true;
}
2017-07-31 19:27:03 +01:00
private bool should_use_dark_theme (string theme_color) {
Gdk.RGBA color = {};
color.parse (theme_color);
2017-07-31 19:27:03 +01:00
double prom = (color.red + color.blue + color.green)/3;
2017-07-31 19:27:03 +01:00
if (prom < 0.5)
return true;
return false;
}
2017-07-31 19:27:03 +01:00
}
}