Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions data/gala.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,49 @@
<description></description>
</key>
<key type="b" name="move-fullscreened-workspace">
<default>true</default>
<summary>Automatically move fullscreened windows to a new workspace</summary>
<description></description>
</key>
<default>true</default>
<summary>Automatically move fullscreened windows to a new workspace</summary>
<description></description>
</key>
</schema>

<schema path="/org/pantheon/desktop/gala/keybindings/launch-or-focus/" id="org.pantheon.desktop.gala.keybindings.launch-or-focus">
<key type="as" name="webbrowser">
<default><![CDATA[['<Super>b']]]></default>
<summary>Launch or focus default webbrowser</summary>
</key>
<key type="as" name="emailclient">
<default><![CDATA[['']]]></default>
<summary>Launch or focus default email client</summary>
</key>
<key type="as" name="calendar">
<default><![CDATA[['']]]></default>
<summary>Launch or focus default calendar</summary>
</key>
<key type="as" name="videoplayer">
<default><![CDATA[['']]]></default>
<summary>Launch or focus default videoplayer</summary>
</key>
<key type="as" name="musicplayer">
<default><![CDATA[['']]]></default>
<summary>Launch or focus default musicplayer</summary>
</key>
<key type="as" name="imageviewer">
<default><![CDATA[['']]]></default>
<summary>Launch or focus default imageviewer</summary>
</key>
<key type="as" name="texteditor">
<default><![CDATA[['<Super>e']]]></default>
<summary>Launch or focus default texteditor</summary>
</key>
<key type="as" name="filebrowser">
<default><![CDATA[['<Super>m']]]></default>
<summary>Launch or focus default filebrowser</summary>
</key>
<key type="as" name="elementary-terminal">
<default><![CDATA[['<Super>t']]]></default>
<summary>Launch or focus default terminal</summary>
</key>
</schema>

<schema path="/org/pantheon/desktop/gala/keybindings/" id="org.pantheon.desktop.gala.keybindings">
Expand Down
4 changes: 4 additions & 0 deletions lib/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ namespace Gala {
return app_cache.lookup_id (desktop_file);
}

public static GLib.DesktopAppInfo get_app_from_window (Meta.Window window) {
return window_to_desktop_cache[window];
}

private static GLib.DesktopAppInfo? lookup_desktop_wmclass (string? wm_class) {
if (wm_class == null) {
return null;
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ subdir('src')
subdir('daemon')
subdir('plugins/maskcorners')
subdir('plugins/pip')
subdir('plugins/launch-or-focus')
subdir('plugins/template')
if get_option('documentation')
subdir('docs')
Expand Down
118 changes: 118 additions & 0 deletions plugins/launch-or-focus/Main.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2021 Felix Andreas <fandreas@physik.hu-berlin.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

public class Gala.Plugins.LaunchOrFocus : Gala.Plugin {
private const string SCHEMA_DEFAULT = ".keybindings.launch-or-focus";

private Gala.WindowManager? wm = null;
private unowned Meta.Display? display = null;
private GLib.Settings settings_default;

public override void initialize (Gala.WindowManager wm) {
this.wm = wm;
this.display = wm.get_display ();

settings_default = new GLib.Settings (Config.SCHEMA + SCHEMA_DEFAULT);

add_default_keybinding ("webbrowser", "x-scheme-handler/http");
add_default_keybinding ("emailclient", "x-scheme-handler/mailto");
add_default_keybinding ("calendar", "text/calendar");
add_default_keybinding ("videoplayer", "video/x-ogm+ogg");
add_default_keybinding ("musicplayer", "audio/x-vorbis+ogg");
add_default_keybinding ("imageviewer", "image/jpeg");
add_default_keybinding ("texteditor", "text/plain");
add_default_keybinding ("filebrowser", "inode/directory");
// can't set default application for terminal
display.add_keybinding (
"elementary-terminal",
settings_default,
Meta.KeyBindingFlags.NONE,
(display, window, event, binding) => launch_or_focus ("io.elementary.terminal.desktop")
);
}

private void add_default_keybinding (string name, string content_type) {
display.add_keybinding (
name,
settings_default,
Meta.KeyBindingFlags.NONE,
(display, window, event, binding) => {
launch_or_focus (GLib.AppInfo.get_default_for_type (content_type, false).get_id ());
}
);
}

public override void destroy () {}

/*
* Case A: application is not running --> launch a new instance
* Case B: application is running without focus --> focus instance with highest z-index
* Case C: application is running and has focus --> focus another instance (lowest z-index)
*/
private void launch_or_focus (string desktop_id) {
var app_info = new GLib.DesktopAppInfo (desktop_id);
if (app_info == null) {
warning (@"Could not find DesktopAppInfo for desktop-id “$(desktop_id)“");
return;
}

var windows = new SList<Meta.Window> ();
foreach (unowned Meta.Window window in display.get_tab_list (Meta.TabList.NORMAL, null)) {
if (app_info.equal (Utils.get_app_from_window (window))) {
windows.append (window);
}
}

// Case A
if (windows.length () == 0) {
launch (app_info);
return;
}

var sorted_windows = display.sort_windows_by_stacking (windows);
var active_window = display.get_focus_window ();
var last_window = sorted_windows.data;
var first_window = sorted_windows.last ().data;
var time = display.get_current_time ();

// Case B
if (active_window != first_window) {
first_window.activate (time);
// Case C
} else {
last_window.activate (time);
}
}

private void launch (GLib.DesktopAppInfo app_info) {
try {
app_info.launch (null, null);
} catch (Error e) {
critical ("Unable to launch app: %s", e.message);
}
}
}

public Gala.PluginInfo register_plugin () {
return {
"launch-or-focus",
"Felix Andreas",
typeof (Gala.Plugins.LaunchOrFocus),
Gala.PluginFunction.ADDITION,
Gala.LoadPriority.IMMEDIATE
};
}
13 changes: 13 additions & 0 deletions plugins/launch-or-focus/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
launch_or_focus_sources = [
'Main.vala',
]

launch_or_focus_sources = shared_library(
'gala-launch-or-focus',
launch_or_focus_sources,
dependencies: [gala_dep, gala_base_dep],
include_directories: config_inc_dir,
install: true,
install_dir: plugins_dir,
install_rpath: mutter_typelib_dir,
)