From 9fab13452a26ed55c01047d4225f699f43bba20d Mon Sep 17 00:00:00 2001 From: Aylur Date: Mon, 14 Oct 2024 16:45:03 +0000 Subject: feat: Astal3 --- lib/astal/gtk3/src/application.vala | 217 ++++++++++++++++++++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 lib/astal/gtk3/src/application.vala (limited to 'lib/astal/gtk3/src/application.vala') diff --git a/lib/astal/gtk3/src/application.vala b/lib/astal/gtk3/src/application.vala new file mode 100644 index 0000000..8539aa0 --- /dev/null +++ b/lib/astal/gtk3/src/application.vala @@ -0,0 +1,217 @@ +[DBus (name="io.Astal.Application")] +public class Astal.Application : Gtk.Application, AstalIO.Application { + private List css_providers = new List(); + private SocketService service; + private DBusConnection conn; + private string socket_path { get; set; } + private string _instance_name; + + [DBus (visible=false)] + public signal void monitor_added(Gdk.Monitor monitor); + + [DBus (visible=false)] + public signal void monitor_removed(Gdk.Monitor monitor); + + [DBus (visible=false)] + public signal void window_toggled(Gtk.Window window); + + [DBus (visible=false)] + public List monitors { + owned get { + var display = Gdk.Display.get_default(); + var list = new List(); + for (var i = 0; i <= display.get_n_monitors(); ++i) { + var mon = display.get_monitor(i); + if (mon != null) { + list.append(mon); + } + } + return list; + } + } + + [DBus (visible=false)] + public string instance_name { + owned get { return _instance_name; } + construct set { + application_id = "io.Astal." + value; + _instance_name = value; + } + } + + [DBus (visible=false)] + public List windows { + get { return get_windows(); } + } + + [DBus (visible=false)] + public Gtk.Settings settings { + get { return Gtk.Settings.get_default(); } + } + + [DBus (visible=false)] + public Gdk.Screen screen { + get { return Gdk.Screen.get_default(); } + } + + [DBus (visible=false)] + public string gtk_theme { + owned get { return settings.gtk_theme_name; } + set { settings.gtk_theme_name = value; } + } + + [DBus (visible=false)] + public string icon_theme { + owned get { return settings.gtk_icon_theme_name; } + set { settings.gtk_icon_theme_name = value; } + } + + [DBus (visible=false)] + public string cursor_theme { + owned get { return settings.gtk_cursor_theme_name; } + set { settings.gtk_cursor_theme_name = value; } + } + + [DBus (visible=false)] + public void reset_css() { + foreach(var provider in css_providers) { + Gtk.StyleContext.remove_provider_for_screen(screen, provider); + } + css_providers = new List(); + } + + public void inspector() throws DBusError, IOError { + Gtk.Window.set_interactive_debugging(true); + } + + [DBus (visible=false)] + public Gtk.Window? get_window(string name) { + foreach(var win in windows) { + if (win.name == name) + return win; + } + + critical("no window with name \"%s\"".printf(name)); + return null; + } + + public void toggle_window(string window) throws Error { + var win = get_window(window); + if (win != null) { + win.visible = !win.visible; + } else { + throw new IOError.FAILED("window not found"); + } + } + + [DBus (visible=false)] + public void apply_css(string style, bool reset = false) { + var provider = new Gtk.CssProvider(); + + if (reset) + reset_css(); + + try { + if (FileUtils.test(style, FileTest.EXISTS)) + provider.load_from_path(style); + else + provider.load_from_data(style); + } catch (Error err) { + critical(err.message); + } + + Gtk.StyleContext.add_provider_for_screen( + screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_USER); + + css_providers.append(provider); + } + + [DBus (visible=false)] + public void add_icons(string? path) { + if (path != null) { + Gtk.IconTheme.get_default().prepend_search_path(path); + } + } + + [DBus (visible=false)] + public virtual void request(string msg, SocketConnection conn) { + AstalIO.write_sock.begin(conn, @"missing response implementation on $application_id"); + } + + /** + * should be called before `run()` + * the return value indicates if instance is already running + */ + [DBus (visible=false)] + public void acquire_socket() { + try { + service = AstalIO.acquire_socket(this); + + Bus.own_name( + BusType.SESSION, + "io.Astal." + instance_name, + BusNameOwnerFlags.NONE, + (conn) => { + try { + this.conn = conn; + conn.register_object("/io/Astal/Application", this); + } catch (Error err) { + critical(err.message); + } + }, + () => {}, + () => {} + ); + } catch (Error err) { + critical("could not acquire socket %s\n", application_id); + critical(err.message); + } + } + + public new void quit() throws DBusError, IOError { + if (service != null) { + if (FileUtils.test(socket_path, GLib.FileTest.EXISTS)){ + try { + File.new_for_path(socket_path).delete(null); + } catch (Error err) { + warning(err.message); + } + } + } + + base.quit(); + } + + construct { + if (instance_name == null) + instance_name = "astal"; + + activate.connect(() => { + var display = Gdk.Display.get_default(); + display.monitor_added.connect((mon) => { + monitor_added(mon); + notify_property("monitors"); + }); + display.monitor_removed.connect((mon) => { + monitor_removed(mon); + notify_property("monitors"); + }); + }); + + window_added.connect((window) => { + ulong id1, id2; + id1 = window.notify["visible"].connect(() => window_toggled(window)); + id2 = window_removed.connect((removed) => { + if (removed == window) { + window.disconnect(id1); + this.disconnect(id2); + } + }); + }); + + shutdown.connect(() => { try { quit(); } catch(Error err) {} }); + Unix.signal_add(1, () => { try { quit(); } catch(Error err) {} }, Priority.HIGH); + Unix.signal_add(2, () => { try { quit(); } catch(Error err) {} }, Priority.HIGH); + Unix.signal_add(15, () => { try { quit(); } catch(Error err) {} }, Priority.HIGH); + } +} -- cgit v1.2.3 From 2f71cd4c08bb4514efe43533e6a5d03535204c29 Mon Sep 17 00:00:00 2001 From: Aylur Date: Tue, 15 Oct 2024 01:26:32 +0200 Subject: refactor lua and gjs lib --- lib/astal/gtk3/src/application.vala | 68 +++++++++++++++---------------------- 1 file changed, 27 insertions(+), 41 deletions(-) (limited to 'lib/astal/gtk3/src/application.vala') diff --git a/lib/astal/gtk3/src/application.vala b/lib/astal/gtk3/src/application.vala index 8539aa0..2255333 100644 --- a/lib/astal/gtk3/src/application.vala +++ b/lib/astal/gtk3/src/application.vala @@ -3,8 +3,9 @@ public class Astal.Application : Gtk.Application, AstalIO.Application { private List css_providers = new List(); private SocketService service; private DBusConnection conn; - private string socket_path { get; set; } - private string _instance_name; + private string _instance_name = "astal"; + + public string socket_path { get; private set; } [DBus (visible=false)] public signal void monitor_added(Gdk.Monitor monitor); @@ -34,8 +35,8 @@ public class Astal.Application : Gtk.Application, AstalIO.Application { public string instance_name { owned get { return _instance_name; } construct set { - application_id = "io.Astal." + value; - _instance_name = value; + _instance_name = value != null ? value : "astal"; + application_id = @"io.Astal.$_instance_name"; } } @@ -138,54 +139,39 @@ public class Astal.Application : Gtk.Application, AstalIO.Application { AstalIO.write_sock.begin(conn, @"missing response implementation on $application_id"); } - /** - * should be called before `run()` - * the return value indicates if instance is already running - */ [DBus (visible=false)] - public void acquire_socket() { - try { - service = AstalIO.acquire_socket(this); - - Bus.own_name( - BusType.SESSION, - "io.Astal." + instance_name, - BusNameOwnerFlags.NONE, - (conn) => { - try { - this.conn = conn; - conn.register_object("/io/Astal/Application", this); - } catch (Error err) { - critical(err.message); - } - }, - () => {}, - () => {} - ); - } catch (Error err) { - critical("could not acquire socket %s\n", application_id); - critical(err.message); - } - } + public void acquire_socket() throws Error { + string path; + service = AstalIO.acquire_socket(this, out path); + socket_path = path; - public new void quit() throws DBusError, IOError { - if (service != null) { - if (FileUtils.test(socket_path, GLib.FileTest.EXISTS)){ + Bus.own_name( + BusType.SESSION, + application_id, + BusNameOwnerFlags.NONE, + (conn) => { try { - File.new_for_path(socket_path).delete(null); + this.conn = conn; + conn.register_object("/io/Astal/Application", this); } catch (Error err) { - warning(err.message); + critical(err.message); } - } + }, + () => {}, + () => {} + ); + } + + public new void quit() throws DBusError, IOError { + if (service != null) { + service.stop(); + service.close(); } base.quit(); } construct { - if (instance_name == null) - instance_name = "astal"; - activate.connect(() => { var display = Gdk.Display.get_default(); display.monitor_added.connect((mon) => { -- cgit v1.2.3 From dde50e333a237efb4ddaf522398dedf1a69999d1 Mon Sep 17 00:00:00 2001 From: Aylur Date: Tue, 15 Oct 2024 11:47:26 +0000 Subject: docs: update references flake --- lib/astal/gtk3/src/application.vala | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'lib/astal/gtk3/src/application.vala') diff --git a/lib/astal/gtk3/src/application.vala b/lib/astal/gtk3/src/application.vala index 2255333..1210d88 100644 --- a/lib/astal/gtk3/src/application.vala +++ b/lib/astal/gtk3/src/application.vala @@ -4,8 +4,7 @@ public class Astal.Application : Gtk.Application, AstalIO.Application { private SocketService service; private DBusConnection conn; private string _instance_name = "astal"; - - public string socket_path { get; private set; } + private string socket_path { get; private set; } [DBus (visible=false)] public signal void monitor_added(Gdk.Monitor monitor); @@ -45,13 +44,11 @@ public class Astal.Application : Gtk.Application, AstalIO.Application { get { return get_windows(); } } - [DBus (visible=false)] - public Gtk.Settings settings { + private Gtk.Settings settings { get { return Gtk.Settings.get_default(); } } - [DBus (visible=false)] - public Gdk.Screen screen { + private Gdk.Screen screen { get { return Gdk.Screen.get_default(); } } -- cgit v1.2.3 From 306e64998c1bf1fb997c1098ae92d6edfef31cd2 Mon Sep 17 00:00:00 2001 From: Aylur Date: Wed, 23 Oct 2024 20:37:32 +0000 Subject: docs: astal3 and io comments --- lib/astal/gtk3/src/application.vala | 65 +++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'lib/astal/gtk3/src/application.vala') diff --git a/lib/astal/gtk3/src/application.vala b/lib/astal/gtk3/src/application.vala index 1210d88..82ee797 100644 --- a/lib/astal/gtk3/src/application.vala +++ b/lib/astal/gtk3/src/application.vala @@ -6,15 +6,28 @@ public class Astal.Application : Gtk.Application, AstalIO.Application { private string _instance_name = "astal"; private string socket_path { get; private set; } + /** + * Emitted when a new monitor is added to [class@Gdk.Display]. + */ [DBus (visible=false)] public signal void monitor_added(Gdk.Monitor monitor); + /** + * Emitted when a monitor is disconnected from [class@Gdk.Display]. + */ [DBus (visible=false)] public signal void monitor_removed(Gdk.Monitor monitor); + /** + * Emitted when a window that has been added using + * [method@Gtk.Application.add_window] changes its visibility . + */ [DBus (visible=false)] public signal void window_toggled(Gtk.Window window); + /** + * Get all monitors from [class@Gdk.Display]. + */ [DBus (visible=false)] public List monitors { owned get { @@ -30,6 +43,11 @@ public class Astal.Application : Gtk.Application, AstalIO.Application { } } + /** + * A unique instance name. + * + * This is the identifier used by the AstalIO package and the CLI. + */ [DBus (visible=false)] public string instance_name { owned get { return _instance_name; } @@ -39,6 +57,9 @@ public class Astal.Application : Gtk.Application, AstalIO.Application { } } + /** + * Windows that has been added to this app using [method@Gtk.Application.add_window]. + */ [DBus (visible=false)] public List windows { get { return get_windows(); } @@ -52,24 +73,36 @@ public class Astal.Application : Gtk.Application, AstalIO.Application { get { return Gdk.Screen.get_default(); } } + /** + * Shortcut for [property@Gtk.Settings:gtk_theme_name] + */ [DBus (visible=false)] public string gtk_theme { owned get { return settings.gtk_theme_name; } set { settings.gtk_theme_name = value; } } + /** + * Shortcut for [property@Gtk.Settings:gtk_icon_theme_name] + */ [DBus (visible=false)] public string icon_theme { owned get { return settings.gtk_icon_theme_name; } set { settings.gtk_icon_theme_name = value; } } + /** + * Shortcut for [property@Gtk.Settings:gtk_cursor_theme_name] + */ [DBus (visible=false)] public string cursor_theme { owned get { return settings.gtk_cursor_theme_name; } set { settings.gtk_cursor_theme_name = value; } } + /** + * Remove all [class@Gtk.StyleContext] providers. + */ [DBus (visible=false)] public void reset_css() { foreach(var provider in css_providers) { @@ -78,10 +111,17 @@ public class Astal.Application : Gtk.Application, AstalIO.Application { css_providers = new List(); } + /** + * Shortcut for [func@Gtk.Window.set_interactive_debugging]. + */ public void inspector() throws DBusError, IOError { Gtk.Window.set_interactive_debugging(true); } + /** + * Get a window by its [property@Gtk.Widget:name] that has been added to this app + * using [method@Gtk.Application.add_window]. + */ [DBus (visible=false)] public Gtk.Window? get_window(string name) { foreach(var win in windows) { @@ -93,6 +133,10 @@ public class Astal.Application : Gtk.Application, AstalIO.Application { return null; } + /** + * Toggle the visibility of a window by its [property@Gtk.Widget:name] + * that has been added to this app using [method@Gtk.Application.add_window]. + */ public void toggle_window(string window) throws Error { var win = get_window(window); if (win != null) { @@ -102,6 +146,11 @@ public class Astal.Application : Gtk.Application, AstalIO.Application { } } + /** + * Add a new [class@Gtk.StyleContext] provider. + * + * @param style Css string or a path to a css file. + */ [DBus (visible=false)] public void apply_css(string style, bool reset = false) { var provider = new Gtk.CssProvider(); @@ -124,6 +173,9 @@ public class Astal.Application : Gtk.Application, AstalIO.Application { css_providers.append(provider); } + /** + * Shortcut for [method@Gtk.IconTheme.prepend_search_path]. + */ [DBus (visible=false)] public void add_icons(string? path) { if (path != null) { @@ -131,11 +183,21 @@ public class Astal.Application : Gtk.Application, AstalIO.Application { } } + /** + * Handler for an incoming request. + * + * @param msg Body of the message + * @param conn The connection which expects the response. + */ [DBus (visible=false)] public virtual void request(string msg, SocketConnection conn) { AstalIO.write_sock.begin(conn, @"missing response implementation on $application_id"); } + /** + * Attempt to acquire the astal socket for this app identified by its [property@AstalIO.Application:instance_name]. + * If the socket is in use by another app with the same name an [error@AstalIO.AppError.NAME_OCCUPIED] is thrown. + */ [DBus (visible=false)] public void acquire_socket() throws Error { string path; @@ -159,6 +221,9 @@ public class Astal.Application : Gtk.Application, AstalIO.Application { ); } + /** + * Quit and stop the socket if it was acquired. + */ public new void quit() throws DBusError, IOError { if (service != null) { service.stop(); -- cgit v1.2.3