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

Direct Known Subclasses

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 add_action_entries, lookup_action(action_name : String) : Gio::Action? lookup_action, remove_action(action_name : String) : Nil remove_action, to_unsafe to_unsafe

Class methods inherited from module Gio::ActionMap

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? action_parameter_type, action_removed(action_name : String) : Nil action_removed, action_removed_signal action_removed_signal, action_state(action_name : String) : GLib::Variant? 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? action_state_hint, action_state_type(action_name : String) : GLib::VariantType? action_state_type, activate_action(action_name : String, parameter : _?) : 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

Class methods inherited from module Gio::ActionGroup

g_type : UInt64 g_type

Instance methods inherited from class Gio::Application

action_group=(action_group : Gio::ActionGroup?) : 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 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? application_id, application_id=(value : String) : String
application_id=(application_id : String?) : 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? dbus_connection, dbus_object_path : String? dbus_object_path, flags : Gio::ApplicationFlags flags, flags=(flags : Gio::ApplicationFlags) : Nil flags=, handle_local_options_signal handle_local_options_signal, 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 option_context_description=, option_context_parameter_string=(parameter_string : String?) : Nil option_context_parameter_string=, option_context_summary=(summary : String?) : Nil option_context_summary=, quit : Nil quit, register(cancellable : Gio::Cancellable?) : Bool register, release : Nil release, resource_base_path : String? resource_base_path, resource_base_path=(value : String) : String
resource_base_path=(resource_path : String?) : Nil
resource_base_path=
, run(*argv : String) run, send_notification(id : String?, 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?, flags : Gio::ApplicationFlags) : self
new
new(*, action_group : Gio::ActionGroup? = nil, application_id : String? = nil, flags : Gio::ApplicationFlags? = nil, inactivity_timeout : UInt32? = nil, is_busy : Bool? = nil, is_registered : Bool? = nil, is_remote : Bool? = nil, resource_base_path : String? = nil)
new

Class methods inherited from class Gio::Application

default : Gio::Application? 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 add_action_entries, lookup_action(action_name : String) : Gio::Action? lookup_action, remove_action(action_name : String) : Nil remove_action, to_unsafe to_unsafe

Class methods inherited from module Gio::ActionMap

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? action_parameter_type, action_removed(action_name : String) : Nil action_removed, action_removed_signal action_removed_signal, action_state(action_name : String) : GLib::Variant? 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? action_state_hint, action_state_type(action_name : String) : GLib::VariantType? action_state_type, activate_action(action_name : String, parameter : _?) : 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

Class methods inherited from module Gio::ActionGroup

g_type : UInt64 g_type

Instance methods inherited from class GObject::Object

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)? data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, 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)? qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void)?) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void)? steal_data, steal_qdata(quark : UInt32) : Pointer(Void)? 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, 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

compat_control(what : UInt64, data : Pointer(Void)?) : 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

Constructor Detail

def self.new(application_id : String?, 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.


def self.new #

Initialize a new Application.


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

Class Method Detail

def self.g_type : UInt64 #

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


Instance Method Detail

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

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


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.


def active_window : Gtk::Window? #

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.


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.


def inhibit(window : Gtk::Window?, flags : Gtk::ApplicationInhibitFlags, reason : String?) : 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.


def list_action_descriptions : Enumerable(String) #

Lists the detailed action names which have associated accelerators.

See Gtk::Application#accels_for_action=.


def menu_by_id(id : String) : Gio::Menu? #

Gets a menu from automatically loaded resources.

See the section on Automatic resources for more information.


def menubar : Gio::MenuModel? #

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


def menubar=(menubar : Gio::MenuModel?) : 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.


def query_end_signal #

def register_session=(value : Bool) : Bool #

def register_session? : Bool #

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.


def screensaver_active? : Bool #

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().


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.


def window_added_signal #

def window_by_id(id : UInt32) : Gtk::Window? #

Returns the Gtk::ApplicationWindow with the given ID.

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


def window_removed_signal #

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.