class Gtk::Application

Overview

Gtk::Application is a high-level API for writing applications.

It supports many aspects of writing a GTK application in a convenient fashion, without enforcing a one-size-fits-all model.

Currently, Gtk::Application handles GTK initialization, application uniqueness, session management, provides some basic scriptability and desktop shell integration by exporting actions and menus and manages a list of toplevel windows whose life-cycle is automatically tied to the life-cycle of your application.

While Gtk::Application works fine with plain Gtk::Windows, it is recommended to use it together with Gtk::ApplicationWindow.

Automatic resources

Gtk::Application will automatically load menus from the Gtk::Builder resource located at "gtk/menus.ui", relative to the application's resource base path (see Gio::Application#resource_base_path=). The menu with the ID "menubar" is taken as the application's menubar. Additional menus (most interesting submenus) can be named and accessed via Gtk::Application#menu_by_id which allows for dynamic population of a part of the menu structure.

It is also possible to provide the menubar manually using Gtk::Application#menubar=.

Gtk::Application will also automatically setup an icon search path for the default icon theme by appending "icons" to the resource base path. This allows your application to easily store its icons as resources. See Gtk::IconTheme#add_resource_path for more information.

If there is a resource located at gtk/help-overlay.ui which defines a Gtk::ShortcutsWindow with ID help_overlay then Gtk::Application associates an instance of this shortcuts window with each Gtk::ApplicationWindow and sets up the keyboard accelerator Control+? to open it. To create a menu item that displays the shortcuts window, associate the item with the action win.show-help-overlay.

A simple application

A simple example is available in the GTK source code repository

Gtk::Application optionally registers with a session manager of the users session (if you set the [property@Gtk.Application:register-session] property) and offers various functionality related to the session life-cycle.

An application can block various ways to end the session with the Gtk::Application#inhibit function. Typical use cases for this kind of inhibiting are long-running, uninterruptible operations, such as burning a CD or performing a disk backup. The session manager may not honor the inhibitor, but it can be expected to inform the user about the negative consequences of ending the session while inhibitors are present.

See Also

HowDoI: Using Gtk::Application, Getting Started with GTK: Basics

Included Modules

Defined in:

lib/gi-crystal/src/auto/gtk-4.0/application.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Gio::ActionMap

add_action(action : Gio::Action) : Nil add_action, add_action_entries(entries : Enumerable(Gio::ActionEntry), user_data : Pointer(Void) | Nil) : Nil add_action_entries, lookup_action(action_name : String) : Gio::Action | Nil lookup_action, remove_action(action_name : String) : Nil remove_action, to_unsafe to_unsafe

Constructor methods inherited from module Gio::ActionMap

cast(obj : GObject::Object) : self cast

Class methods inherited from module Gio::ActionMap

cast?(obj : GObject::Object) : self | Nil cast?, g_type : UInt64 g_type

Instance methods inherited from module Gio::ActionGroup

action_added(action_name : String) : Nil action_added, action_added_signal action_added_signal, action_enabled(action_name : String) : Bool action_enabled, action_enabled_changed(action_name : String, enabled : Bool) : Nil action_enabled_changed, action_enabled_changed_signal action_enabled_changed_signal, action_parameter_type(action_name : String) : GLib::VariantType | Nil action_parameter_type, action_removed(action_name : String) : Nil action_removed, action_removed_signal action_removed_signal, action_state(action_name : String) : GLib::Variant | Nil action_state, action_state_changed(action_name : String, state : _) : Nil action_state_changed, action_state_changed_signal action_state_changed_signal, action_state_hint(action_name : String) : GLib::Variant | Nil action_state_hint, action_state_type(action_name : String) : GLib::VariantType | Nil action_state_type, activate_action(action_name : String, parameter : _ | Nil) : Nil activate_action, change_action_state(action_name : String, value : _) : Nil change_action_state, has_action(action_name : String) : Bool has_action, list_actions : Enumerable(String) list_actions, query_action(action_name : String, enabled : Bool) : Bool query_action, to_unsafe to_unsafe

Constructor methods inherited from module Gio::ActionGroup

cast(obj : GObject::Object) : self cast

Class methods inherited from module Gio::ActionGroup

cast?(obj : GObject::Object) : self | Nil cast?, g_type : UInt64 g_type

Instance methods inherited from class Gio::Application

==(other : self) ==, action_group=(action_group : Gio::ActionGroup | Nil) : Nil action_group=, activate : Nil activate, activate_signal activate_signal, add_main_option(long_name : String, short_name : Int8, flags : GLib::OptionFlags, arg : GLib::OptionArg, description : String, arg_description : String | Nil) : Nil add_main_option, add_main_option_entries(entries : Enumerable(GLib::OptionEntry)) : Nil
add_main_option_entries(*entries : GLib::OptionEntry)
add_main_option_entries
, add_option_group(group : GLib::OptionGroup) : Nil add_option_group, application_id : String | Nil application_id, application_id=(value : String) : String
application_id=(application_id : String | Nil) : Nil
application_id=
, bind_busy_property(object : GObject::Object, property : String) : Nil bind_busy_property, command_line_signal command_line_signal, dbus_connection : Gio::DBusConnection | Nil dbus_connection, dbus_object_path : String | Nil dbus_object_path, flags : Gio::ApplicationFlags flags, flags=(flags : Gio::ApplicationFlags) : Nil flags=, handle_local_options_signal handle_local_options_signal, hash(hasher) hash, hold : Nil hold, inactivity_timeout : UInt32 inactivity_timeout, inactivity_timeout=(inactivity_timeout : UInt32) : Nil inactivity_timeout=, is_busy : Bool is_busy, is_busy? : Bool is_busy?, is_registered : Bool is_registered, is_registered? : Bool is_registered?, is_remote : Bool is_remote, is_remote? : Bool is_remote?, mark_busy : Nil mark_busy, name_lost_signal name_lost_signal, open(files : Enumerable(Gio::File), hint : String) : Nil open, open_signal open_signal, option_context_description=(description : String | Nil) : Nil option_context_description=, option_context_parameter_string=(parameter_string : String | Nil) : Nil option_context_parameter_string=, option_context_summary=(summary : String | Nil) : Nil option_context_summary=, quit : Nil quit, register(cancellable : Gio::Cancellable | Nil) : Bool register, release : Nil release, resource_base_path : String | Nil resource_base_path, resource_base_path=(value : String) : String
resource_base_path=(resource_path : String | Nil) : Nil
resource_base_path=
, run(argv : Enumerable(String) | Nil) : Int32
run(argv : Enumerable(String) | Nil) : Int32
run : Int32
run(*argv : String)
run
, send_notification(id : String | Nil, notification : Gio::Notification) : Nil send_notification, set_default : Nil set_default, shutdown_signal shutdown_signal, startup_signal startup_signal, unbind_busy_property(object : GObject::Object, property : String) : Nil unbind_busy_property, unmark_busy : Nil unmark_busy, withdraw_notification(id : String) : Nil withdraw_notification

Constructor methods inherited from class Gio::Application

new(application_id : String | Nil, flags : Gio::ApplicationFlags) : self
new
new(*, action_group : Gio::ActionGroup | Nil = nil, application_id : String | Nil = nil, flags : Gio::ApplicationFlags | Nil = nil, inactivity_timeout : UInt32 | Nil = nil, is_busy : Bool | Nil = nil, is_registered : Bool | Nil = nil, is_remote : Bool | Nil = nil, resource_base_path : String | Nil = nil)
new

Class methods inherited from class Gio::Application

default : Gio::Application | Nil default, g_type : UInt64 g_type, id_is_valid(application_id : String) : Bool id_is_valid

Instance methods inherited from module Gio::ActionMap

add_action(action : Gio::Action) : Nil add_action, add_action_entries(entries : Enumerable(Gio::ActionEntry), user_data : Pointer(Void) | Nil) : Nil add_action_entries, lookup_action(action_name : String) : Gio::Action | Nil lookup_action, remove_action(action_name : String) : Nil remove_action, to_unsafe to_unsafe

Constructor methods inherited from module Gio::ActionMap

cast(obj : GObject::Object) : self cast

Class methods inherited from module Gio::ActionMap

cast?(obj : GObject::Object) : self | Nil cast?, g_type : UInt64 g_type

Instance methods inherited from module Gio::ActionGroup

action_added(action_name : String) : Nil action_added, action_added_signal action_added_signal, action_enabled(action_name : String) : Bool action_enabled, action_enabled_changed(action_name : String, enabled : Bool) : Nil action_enabled_changed, action_enabled_changed_signal action_enabled_changed_signal, action_parameter_type(action_name : String) : GLib::VariantType | Nil action_parameter_type, action_removed(action_name : String) : Nil action_removed, action_removed_signal action_removed_signal, action_state(action_name : String) : GLib::Variant | Nil action_state, action_state_changed(action_name : String, state : _) : Nil action_state_changed, action_state_changed_signal action_state_changed_signal, action_state_hint(action_name : String) : GLib::Variant | Nil action_state_hint, action_state_type(action_name : String) : GLib::VariantType | Nil action_state_type, activate_action(action_name : String, parameter : _ | Nil) : Nil activate_action, change_action_state(action_name : String, value : _) : Nil change_action_state, has_action(action_name : String) : Bool has_action, list_actions : Enumerable(String) list_actions, query_action(action_name : String, enabled : Bool) : Bool query_action, to_unsafe to_unsafe

Constructor methods inherited from module Gio::ActionGroup

cast(obj : GObject::Object) : self cast

Class methods inherited from module Gio::ActionGroup

cast?(obj : GObject::Object) : self | Nil cast?, g_type : UInt64 g_type

Instance methods inherited from class GObject::Object

==(other : self) ==, bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void) | Nil data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, hash(hasher) hash, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void) | Nil qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void) | Nil) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void) | Nil steal_data, steal_qdata(quark : UInt32) : Pointer(Void) | Nil steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
new
new
, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

cast?(obj : GObject::Object) : self | Nil cast?, compat_control(what : UInt64, data : Pointer(Void) | Nil) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Macros inherited from class GObject::Object

previous_vfunc(*args) previous_vfunc, previous_vfunc!(*args) previous_vfunc!, signal(signature) signal

Constructor Detail

def self.new(application_id : String | Nil, flags : Gio::ApplicationFlags) : self #

Creates a new Gtk::Application instance.

When using Gtk::Application, it is not necessary to call Gtk::init manually. It is called as soon as the application gets registered as the primary instance.

Concretely, Gtk::init is called in the default handler for the GApplication::startup signal. Therefore, Gtk::Application subclasses should always chain up in their GApplication::startup handler before using any GTK API.

Note that commandline arguments are not passed to Gtk::init.

If application_id is not nil, then it must be valid. See g_application_id_is_valid().

If no application ID is given then some features (most notably application uniqueness) will be disabled.


[View source]
def self.new #

Initialize a new Application.


[View source]
def self.new(*, action_group : Gio::ActionGroup | Nil = nil, active_window : Gtk::Window | Nil = nil, application_id : String | Nil = nil, flags : Gio::ApplicationFlags | Nil = nil, inactivity_timeout : UInt32 | Nil = nil, is_busy : Bool | Nil = nil, is_registered : Bool | Nil = nil, is_remote : Bool | Nil = nil, menubar : Gio::MenuModel | Nil = nil, register_session : Bool | Nil = nil, resource_base_path : String | Nil = nil, screensaver_active : Bool | Nil = nil) #

[View source]

Class Method Detail

def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


def accels_for_action(detailed_action_name : String) : Enumerable(String) #

Gets the accelerators that are currently associated with the given action.


[View source]
def actions_for_accel(accel : String) : Enumerable(String) #

Returns the list of actions (possibly empty) that accel maps to.

Each item in the list is a detailed action name in the usual form.

This might be useful to discover if an accel already exists in order to prevent installation of a conflicting accelerator (from an accelerator editor or a plugin system, for example). Note that having more than one action per accelerator may not be a bad thing and might make sense in cases where the actions never appear in the same context.

In case there are no actions for a given accelerator, an empty array is returned. NULL is never returned.

It is a programmer error to pass an invalid accelerator string.

If you are unsure, check it with Gtk::accelerator_parse first.


[View source]
def active_window : Gtk::Window | Nil #

Gets the “active” window for the application.

The active window is the one that was most recently focused (within the application). This window may not have the focus at the moment if another application has it — this is just the most recently-focused window within this application.


[View source]
def add_window(window : Gtk::Window) : Nil #

Adds a window to application.

This call can only happen after the application has started; typically, you should add new application windows in response to the emission of the GApplication::activate signal.

This call is equivalent to setting the Gtk::Window#application property of window to application.

Normally, the connection between the application and the window will remain until the window is destroyed, but you can explicitly remove it with Gtk::Application#remove_window.

GTK will keep the application running as long as it has any windows.


[View source]
def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)


def inhibit(window : Gtk::Window | Nil, flags : Gtk::ApplicationInhibitFlags, reason : String | Nil) : UInt32 #

Inform the session manager that certain types of actions should be inhibited.

This is not guaranteed to work on all platforms and for all types of actions.

Applications should invoke this method when they begin an operation that should not be interrupted, such as creating a CD or DVD. The types of actions that may be blocked are specified by the flags parameter. When the application completes the operation it should call Gtk::Application#uninhibit to remove the inhibitor. Note that an application can have multiple inhibitors, and all of them must be individually removed. Inhibitors are also cleared when the application exits.

Applications should not expect that they will always be able to block the action. In most cases, users will be given the option to force the action to take place.

The reason message should be short and to the point.

If window is given, the session manager may point the user to this window to find out more about why the action is inhibited.


[View source]
def list_action_descriptions : Enumerable(String) #

Lists the detailed action names which have associated accelerators.

See Gtk::Application#accels_for_action=.


[View source]
def menu_by_id(id : String) : Gio::Menu | Nil #

Gets a menu from automatically loaded resources.

See the section on Automatic resources for more information.


[View source]
def menubar : Gio::MenuModel | Nil #

Returns the menu model that has been set with Gtk::Application#menubar=.


[View source]
def menubar=(menubar : Gio::MenuModel | Nil) : Nil #

Sets or unsets the menubar for windows of application.

This is a menubar in the traditional sense.

This can only be done in the primary instance of the application, after it has been registered. GApplication::startup is a good place to call this.

Depending on the desktop environment, this may appear at the top of each window, or at the top of the screen. In some environments, if both the application menu and the menubar are set, the application menu will be presented as if it were the first item of the menubar. Other environments treat the two as completely separate — for example, the application menu may be rendered by the desktop shell while the menubar (if set) remains in each individual window.

Use the base GActionMap interface to add actions, to respond to the user selecting these menu items.


[View source]
def query_end_signal #

[View source]
def register_session=(value : Bool) : Bool #

[View source]
def register_session? : Bool #

[View source]
def remove_window(window : Gtk::Window) : Nil #

Remove a window from application.

If window belongs to application then this call is equivalent to setting the Gtk::Window#application property of window to NULL.

The application may stop running as a result of a call to this function, if window was the last window of the application.


[View source]
def screensaver_active? : Bool #

[View source]
def set_accels_for_action(detailed_action_name : String, accels : Enumerable(String)) : Nil #

Sets zero or more keyboard accelerators that will trigger the given action.

The first item in accels will be the primary accelerator, which may be displayed in the UI.

To remove all accelerators for an action, use an empty, zero-terminated array for accels.

For the detailed_action_name, see g_action_parse_detailed_name() and g_action_print_detailed_name().


[View source]
def uninhibit(cookie : UInt32) : Nil #

Removes an inhibitor that has been previously established.

See Gtk::Application#inhibit.

Inhibitors are also cleared when the application exits.


[View source]
def window_added_signal #

[View source]
def window_by_id(id : UInt32) : Gtk::Window | Nil #

Returns the Gtk::ApplicationWindow with the given ID.

The ID of a Gtk::ApplicationWindow can be retrieved with Gtk::ApplicationWindow#id.


[View source]
def window_removed_signal #

[View source]
def windows : GLib::List #

Gets a list of the Gtk::Window instances associated with application.

The list is sorted by most recently focused window, such that the first element is the currently focused window. (Useful for choosing a parent for a transient window.)

The list that is returned should not be modified in any way. It will only remain valid until the next focus change or window creation or deletion.


[View source]