Class Application
- All Implemented Interfaces:
Proxy
,ActionGroup
,ActionMap
- Direct Known Subclasses:
Application
GtkApplication
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, GtkApplication
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 GtkApplication
works fine with plain Window
s, it is
recommended to use it together with ApplicationWindow
.
Automatic resources
GtkApplication
will automatically load menus from the GtkBuilder
resource located at "gtk/menus.ui", relative to the application's
resource base path (see Application.setResourceBasePath(java.lang.String)
).
The menu with the ID "menubar" is taken as the application's
menubar. Additional menus (most interesting submenus) can be named
and accessed via getMenuById(java.lang.String)
which allows for
dynamic population of a part of the menu structure.
Note that automatic resource loading uses the resource base path that is set at construction time and will not work if the resource base path is changed at a later time.
It is also possible to provide the menubar manually using
setMenubar(org.gnome.gio.MenuModel)
.
GtkApplication
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 IconTheme.addResourcePath(java.lang.String)
for more
information.
If there is a resource located at gtk/help-overlay.ui
which
defines a ShortcutsWindow
with ID help_overlay
then
GtkApplication
associates an instance of this shortcuts window with
each 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
GtkApplication
optionally registers with a session manager of the
users session (if you set the 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 inhibit(org.gnome.gtk.Window, java.util.Set<org.gnome.gtk.ApplicationInhibitFlags>, java.lang.String)
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
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
static class
Application.Builder<B extends Application.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static interface
Functional interface declaration of theQueryEndCallback
callback.static interface
Functional interface declaration of theWindowAddedCallback
callback.static interface
Functional interface declaration of theWindowRemovedCallback
callback.Nested classes/interfaces inherited from class org.gnome.gio.Application
Application.ActivateCallback, Application.CommandLineCallback, Application.HandleLocalOptionsCallback, Application.NameLostCallback, Application.OpenCallback, Application.ShutdownCallback, Application.StartupCallback
Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
Nested classes/interfaces inherited from interface org.gnome.gio.ActionGroup
ActionGroup.ActionAddedCallback, ActionGroup.ActionEnabledChangedCallback, ActionGroup.ActionGroupImpl, ActionGroup.ActionGroupInterface, ActionGroup.ActionRemovedCallback, ActionGroup.ActionStateChangedCallback
Nested classes/interfaces inherited from interface org.gnome.gio.ActionMap
ActionMap.ActionMapImpl, ActionMap.ActionMapInterface
-
Constructor Summary
ConstructorDescriptionApplication
(@Nullable String applicationId, Set<ApplicationFlags> flags) Creates a newGtkApplication
instance.Application
(@Nullable String applicationId, ApplicationFlags... flags) Creates a newGtkApplication
instance.Application
(MemorySegment address) Create a Application proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a window toapplication
.protected Application
asParent()
Returns this instance as if it were its parent type.static Application.Builder
<? extends Application.Builder> builder()
AApplication.Builder
object constructs aApplication
with the specified properties.void
Emits the "query-end" signal.void
emitWindowAdded
(Window window) Emits the "window-added" signal.void
emitWindowRemoved
(Window window) Emits the "window-removed" signal.String[]
getAccelsForAction
(String detailedActionName) Gets the accelerators that are currently associated with the given action.String[]
getActionsForAccel
(String accel) Returns the list of actions (possibly empty) thataccel
maps to.Gets the “active” window for the application.static MemoryLayout
The memory layout of the native struct.Returns the menu model that has been set withsetMenubar(org.gnome.gio.MenuModel)
.getMenuById
(String id) Gets a menu from automatically loaded resources.static Type
getType()
Get the GType of the Application classgetWindowById
(int id) Returns theApplicationWindow
with the given ID.Gets a list of theWindow
instances associated withapplication
.int
inhibit
(@Nullable Window window, Set<ApplicationInhibitFlags> flags, @Nullable String reason) Inform the session manager that certain types of actions should be inhibited.int
inhibit
(@Nullable Window window, ApplicationInhibitFlags flags, @Nullable String reason) Inform the session manager that certain types of actions should be inhibited.String[]
Lists the detailed action names which have associated accelerators.onQueryEnd
(Application.QueryEndCallback handler) Emitted when the session manager is about to end the session.Emitted when aWindow
is removed fromapplication
.void
removeWindow
(Window window) Remove a window fromapplication
.void
setAccelsForAction
(String detailedActionName, String[] accels) Sets zero or more keyboard accelerators that will trigger the given action.void
setMenubar
(@Nullable MenuModel menubar) Sets or unsets the menubar for windows ofapplication
.void
uninhibit
(int cookie) Removes an inhibitor that has been previously established.protected void
windowAdded
(Window window) Signal emitted when aGtkWindow
is added to application through gtk_application_add_window().protected void
windowRemoved
(Window window) Signal emitted when aGtkWindow
is removed from application, either as a side-effect of being destroyed or explicitly through gtk_application_remove_window().Methods inherited from class org.gnome.gio.Application
activate, addMainOption, addMainOption, addMainOptionEntries, addOptionGroup, addPlatformData, afterEmit, beforeEmit, bindBusyProperty, commandLine, dbusRegister, dbusUnregister, emitActivate, emitCommandLine, emitHandleLocalOptions, emitNameLost, emitOpen, emitShutdown, emitStartup, getApplicationId, getDbusConnection, getDbusObjectPath, getDefault, getFlags, getInactivityTimeout, getIsBusy, getIsRegistered, getIsRemote, getResourceBasePath, getVersion, handleLocalOptions, hold, idIsValid, localCommandLine, markBusy, nameLost, onActivate, onCommandLine, onHandleLocalOptions, onNameLost, onOpen, onShutdown, onStartup, open, quit, quitMainloop, register, release, run, runMainloop, sendNotification, setActionGroup, setApplicationId, setDefault, setFlags, setFlags, setInactivityTimeout, setOptionContextDescription, setOptionContextParameterString, setOptionContextSummary, setResourceBasePath, setVersion, shutdown, startup, unbindBusyProperty, unmarkBusy, withdrawNotification
Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newInstance, newInstance, newv, notify_, notify_, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.gnome.gio.ActionGroup
actionAdded, actionEnabledChanged, actionRemoved, actionStateChanged, activateAction, changeActionState, emitActionAdded, emitActionEnabledChanged, emitActionRemoved, emitActionStateChanged, getActionEnabled, getActionParameterType, getActionState, getActionStateHint, getActionStateType, hasAction, listActions, onActionAdded, onActionEnabledChanged, onActionRemoved, onActionStateChanged, queryAction
Methods inherited from interface org.gnome.gio.ActionMap
addAction, addActionEntries, lookupAction, removeAction, removeActionEntries
-
Constructor Details
-
Application
Create a Application proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
Application
Creates a newGtkApplication
instance.When using
GtkApplication
, it is not necessary to callGtk.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 theGApplication::startup
signal. Therefore,GtkApplication
subclasses should always chain up in theirGApplication::startup
handler before using any GTK API.Note that commandline arguments are not passed to
Gtk.init()
.If
application_id
is notnull
, then it must be valid. Seeg_application_id_is_valid()
.If no application ID is given then some features (most notably application uniqueness) will be disabled.
- Parameters:
applicationId
- The application IDflags
- the application flags
-
Application
Creates a newGtkApplication
instance.When using
GtkApplication
, it is not necessary to callGtk.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 theGApplication::startup
signal. Therefore,GtkApplication
subclasses should always chain up in theirGApplication::startup
handler before using any GTK API.Note that commandline arguments are not passed to
Gtk.init()
.If
application_id
is notnull
, then it must be valid. Seeg_application_id_is_valid()
.If no application ID is given then some features (most notably application uniqueness) will be disabled.
- Parameters:
applicationId
- The application IDflags
- the application flags
-
-
Method Details
-
getType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuper
keyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()
doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName()
. This will call the native function pointer of this virtual method in the typeclass of the parent type.- Overrides:
asParent
in classApplication
-
addWindow
Adds a window toapplication
.This call can only happen after the
application
has started; typically, you should add new application windows in response to the emission of theGApplication::activate
signal.This call is equivalent to setting the
Gtk.Window:application
property ofwindow
toapplication
.Normally, the connection between the application and the window will remain until the window is destroyed, but you can explicitly remove it with
removeWindow(org.gnome.gtk.Window)
.GTK will keep the
application
running as long as it has any windows.- Parameters:
window
- aGtkWindow
-
getAccelsForAction
-
getActionsForAccel
Returns the list of actions (possibly empty) thataccel
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.acceleratorParse(java.lang.String, io.github.jwharm.javagi.base.Out<java.lang.Integer>, io.github.jwharm.javagi.base.Out<java.util.Set<org.gnome.gdk.ModifierType>>)
first.- Parameters:
accel
- an accelerator that can be parsed byGtk.acceleratorParse(java.lang.String, io.github.jwharm.javagi.base.Out<java.lang.Integer>, io.github.jwharm.javagi.base.Out<java.util.Set<org.gnome.gdk.ModifierType>>)
- Returns:
- a
null
-terminated array of actions foraccel
-
getActiveWindow
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.
- Returns:
- the active window
-
getMenuById
Gets a menu from automatically loaded resources.See the section on Automatic resources for more information.
- Parameters:
id
- the id of the menu to look up- Returns:
- Gets the menu with the given id from the automatically loaded resources
-
getMenubar
Returns the menu model that has been set withsetMenubar(org.gnome.gio.MenuModel)
.- Returns:
- the menubar for windows of
application
-
getWindowById
Returns theApplicationWindow
with the given ID.The ID of a
GtkApplicationWindow
can be retrieved withApplicationWindow#getId
.- Parameters:
id
- an identifier number- Returns:
- the window for the given
id
-
getWindows
Gets a list of theWindow
instances associated withapplication
.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.
- Returns:
- a
GList
ofGtkWindow
instances
-
inhibit
public int inhibit(@Nullable @Nullable Window window, Set<ApplicationInhibitFlags> flags, @Nullable @Nullable String reason) 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 calluninhibit(int)
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.- Parameters:
window
- aGtkWindow
flags
- what types of actions should be inhibitedreason
- a short, human-readable string that explains why these operations are inhibited- Returns:
- A non-zero cookie that is used to uniquely identify this
request. It should be used as an argument to
uninhibit(int)
in order to remove the request. If the platform does not support inhibiting or the request failed for some reason, 0 is returned.
-
inhibit
public int inhibit(@Nullable @Nullable Window window, ApplicationInhibitFlags flags, @Nullable @Nullable String reason) 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 calluninhibit(int)
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.- Parameters:
window
- aGtkWindow
flags
- what types of actions should be inhibitedreason
- a short, human-readable string that explains why these operations are inhibited- Returns:
- A non-zero cookie that is used to uniquely identify this
request. It should be used as an argument to
uninhibit(int)
in order to remove the request. If the platform does not support inhibiting or the request failed for some reason, 0 is returned.
-
listActionDescriptions
Lists the detailed action names which have associated accelerators.See
setAccelsForAction(java.lang.String, java.lang.String[])
.- Returns:
- the detailed action names
-
removeWindow
Remove a window fromapplication
.If
window
belongs toapplication
then this call is equivalent to setting theGtk.Window:application
property ofwindow
toNULL
.The application may stop running as a result of a call to this function, if
window
was the last window of theapplication
.- Parameters:
window
- aGtkWindow
-
setAccelsForAction
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
, seeg_action_parse_detailed_name()
andg_action_print_detailed_name()
.- Parameters:
detailedActionName
- a detailed action name, specifying an action and target to associate accelerators withaccels
- a list of accelerators in the format understood byGtk.acceleratorParse(java.lang.String, io.github.jwharm.javagi.base.Out<java.lang.Integer>, io.github.jwharm.javagi.base.Out<java.util.Set<org.gnome.gdk.ModifierType>>)
-
setMenubar
Sets or unsets the menubar for windows ofapplication
.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.- Parameters:
menubar
- aGMenuModel
-
uninhibit
public void uninhibit(int cookie) Removes an inhibitor that has been previously established.Inhibitors are also cleared when the application exits.
- Parameters:
cookie
- a cookie that was returned byinhibit(org.gnome.gtk.Window, java.util.Set<org.gnome.gtk.ApplicationInhibitFlags>, java.lang.String)
-
windowAdded
Signal emitted when aGtkWindow
is added to application through gtk_application_add_window(). -
windowRemoved
Signal emitted when aGtkWindow
is removed from application, either as a side-effect of being destroyed or explicitly through gtk_application_remove_window(). -
onQueryEnd
public SignalConnection<Application.QueryEndCallback> onQueryEnd(Application.QueryEndCallback handler) Emitted when the session manager is about to end the session.This signal is only emitted if
Gtk.Application:register-session
isTRUE
. Applications can connect to this signal and callinhibit(org.gnome.gtk.Window, java.util.Set<org.gnome.gtk.ApplicationInhibitFlags>, java.lang.String)
withGTK_APPLICATION_INHIBIT_LOGOUT
to delay the end of the session until state has been saved.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitQueryEnd
public void emitQueryEnd()Emits the "query-end" signal. SeeonQueryEnd(org.gnome.gtk.Application.QueryEndCallback)
. -
onWindowAdded
public SignalConnection<Application.WindowAddedCallback> onWindowAdded(Application.WindowAddedCallback handler) - Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitWindowAdded
Emits the "window-added" signal. SeeonWindowAdded(org.gnome.gtk.Application.WindowAddedCallback)
. -
onWindowRemoved
public SignalConnection<Application.WindowRemovedCallback> onWindowRemoved(Application.WindowRemovedCallback handler) Emitted when aWindow
is removed fromapplication
.This can happen as a side-effect of the window being destroyed or explicitly through
removeWindow(org.gnome.gtk.Window)
.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitWindowRemoved
Emits the "window-removed" signal. SeeonWindowRemoved(org.gnome.gtk.Application.WindowRemovedCallback)
. -
builder
AApplication.Builder
object constructs aApplication
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withApplication.Builder.build()
.
-