gnome-shell/js/ui/realms/realmWindowMenu.js

133 lines
3.4 KiB
JavaScript

const { Shell, GObject } = imports.gi;
const PopupMenu = imports.ui.popupMenu;
function _windowAppId(window) {
const tracker = Shell.WindowTracker.get_default();
const app = tracker.get_window_app(window);
if (app) {
return app.get_id();
} else {
log(`No app found for window ${window.get_description()}`)
return null;
}
}
function windowMenuDebugString(window) {
const id = _windowAppId(window);
const realm_name = windowRealmName(window);
if (!realm_name) {
return id;
} else if (window.is_on_foreign_workspace_context()) {
return `${id} [${realm_name}]`;
} else {
return `${id} (${realm_name})`;
}
}
function _createMoveWindowItem(label, realm_name, window) {
let item = new PopupMenu.PopupMenuItem(label);
item.connect('activate', () => {
let realms = Shell.Realms.get_default();
let realm = realms.realm_by_name(realm_name);
if (realm) {
realm.move_window_to_context(window);
}
});
return item;
}
// Return name of the realm the application this window belongs to is running in.
function windowRealmName(window) {
const realms = Shell.Realms.get_default();
if (realms.is_citadel_window(window)) {
return "Citadel"
}
let realm = realms.realm_by_window(window);
if (realm) {
return realm.realm_name;
} else {
return null;
}
}
// Return name of realm the context this window is currently located on belongs to
function windowContextRealmName(window) {
if (window.on_all_workspaces) {
return windowRealmName(window);
}
let ws = window.get_workspace();
if (!ws) {
return null;
}
const realms = Shell.Realms.get_default();
let realm = realms.realm_by_context_id(ws.get_context_id());
if (realm) {
return realm.realm_name;
} else {
return null;
}
}
function enableFrameItem(window) {
const realms = Shell.Realms.get_default();
const frames = realms.window_frames();
if (!frames.has_frame(window)) {
return null;
}
let enabled = frames.is_frame_enabled(window);
let item = new PopupMenu.PopupMenuItem("Display colored window frame");
if (enabled) {
item.setOrnament(PopupMenu.Ornament.CHECK);
}
item.connect('activate', () => {
let realms = Shell.Realms.get_default();
const frames = realms.window_frames();
frames.set_frame_enabled(window, !enabled);
});
return item;
}
function realmWindowMenu(window) {
const realm_name = windowContextRealmName(window);
if (!realm_name) {
return null;
}
const realms = Shell.Realms.get_default();
let other_realms = [];
let running_realms = realms.get_running_realms();
running_realms.forEach(realm => {
if (realm.realm_name != realm_name) {
other_realms.push(realm.realm_name);
}
});
if (other_realms.length == 0) {
return null;
} else if (other_realms.length == 1) {
let name = other_realms[0];
return _createMoveWindowItem(`Move to realm-${name}`, name, window);
}
let subMenu = new PopupMenu.PopupSubMenuMenuItem('Move to Realm...', true);
other_realms.forEach(name => {
let item = _createMoveWindowItem(`realm-${name}`, name, window);
subMenu.menu.addMenuItem(item);
});
return subMenu;
}