forked from GnomeSnapExtensions/gSnap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitors.ts
155 lines (131 loc) · 4.71 KB
/
monitors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// GJS import system
declare var imports: any;
declare var global: any;
const Main = imports.ui.main;
import {
Window,
WindowType,
WorkspaceManager as WorkspaceManagerInterface
} from "./gnometypes";
import { log } from "./logging";
import { getIntSetting } from "./settings";
import * as SETTINGS from './settings_data';
import * as tilespec from "./tilespec";
// Getter for accesing "get_active_workspace" on GNOME <=2.28 and >= 2.30
const WorkspaceManager: WorkspaceManagerInterface = (
global.screen || global.workspace_manager);
export interface WorkArea {
x: number;
y: number;
width: number;
height: number;
}
export function areEqual(a: WorkArea, b: WorkArea): boolean
{
return a.x == b.x
&& a.y == b.y
&& a.width == b.width
&& a.height == b.height;
}
type MonitorTier = 'primary' | 'secondary';
function getMonitorTier(monitor: Monitor): MonitorTier {
return isPrimaryMonitor(monitor) ? 'primary' : 'secondary';
}
interface Insets {
top: number;
bottom: number;
left: number;
right: number;
}
function getMonitorInsets(tier: MonitorTier): Insets {
switch (tier) {
case 'primary':
return {
top: getIntSetting(SETTINGS.INSETS_PRIMARY_TOP),
bottom: getIntSetting(SETTINGS.INSETS_PRIMARY_BOTTOM),
left: getIntSetting(SETTINGS.INSETS_PRIMARY_LEFT),
right: getIntSetting(SETTINGS.INSETS_PRIMARY_RIGHT)
}; // Insets on primary monitor
case 'secondary':
return {
top: getIntSetting(SETTINGS.INSETS_SECONDARY_TOP),
bottom: getIntSetting(SETTINGS.INSETS_SECONDARY_BOTTOM),
left: getIntSetting(SETTINGS.INSETS_SECONDARY_LEFT),
right: getIntSetting(SETTINGS.INSETS_SECONDARY_RIGHT)
};
default:
throw new Error(`unknown monitor name ${JSON.stringify(tier)}`);
}
}
export function getWindowsOfMonitor(monitor: Monitor) : Window[] {
let monitors = activeMonitors();
let windows = WorkspaceManager
.get_active_workspace()
.list_windows()
.filter(w => w.get_window_type() == WindowType.NORMAL
&& !w.is_hidden()
&& monitors[w.get_monitor()] == monitor);
return windows;
}
function getMonitorKey(monitor: Monitor): string {
return monitor.x + ":" + monitor.width + ":" + monitor.y + ":" + monitor.height;
}
// TODO: This type is incomplete. Its definition is based purely on usage in
// this file and may be missing methods from the Gnome object.
export interface Monitor {
index: number;
x: number;
y: number;
height: number;
width: number;
};
export function activeMonitors(): Monitor[] {
return Main.layoutManager.monitors;
}
/**
* Determine if the given monitor is the primary monitor.
* @param {Object} monitor The given monitor to evaluate.
* @returns {boolean} True if the given monitor is the primary monitor.
* */
function isPrimaryMonitor(monitor: Monitor): boolean {
return Main.layoutManager.primaryMonitor.x == monitor.x && Main.layoutManager.primaryMonitor.y == monitor.y;
}
export function getWorkAreaByMonitor(monitor: Monitor): WorkArea | null {
const monitors = activeMonitors();
for (let i = 0; i < monitors.length; i++) {
let mon = monitors[i];
if (mon.x == monitor.x && mon.y == monitor.y) {
return getWorkArea(monitor);
}
}
return null;
}
export function workAreaRectByMonitorIndex(monitorIndex: number): tilespec.Rect | null {
const monitor = activeMonitors()[monitorIndex];
if (!monitor) {
return null;
}
const waLegacy = getWorkAreaByMonitor(monitor);
if(!waLegacy) {
return null;
}
return (new tilespec.Rect(
new tilespec.XY(waLegacy.x, waLegacy.y),
new tilespec.Size(waLegacy.width, waLegacy.height)));
}
function getWorkArea(monitor: Monitor): WorkArea {
const wkspace = WorkspaceManager.get_active_workspace();
const work_area = wkspace.get_work_area_for_monitor(monitor.index);
const insets = getMonitorInsets(getMonitorTier(monitor));
const result = {
x: work_area.x + insets.left,
y: work_area.y + insets.top,
width: work_area.width - insets.left - insets.right,
height: work_area.height - insets.top - insets.bottom
};
log(`getWorkArea m:${monitor.index}, x: ${result.x} y:${result.y} w:${result.width} h:${result.height}`);
return result;
}
export function getCurrentMonitorIndex() : number {
return global.display.get_current_monitor();
}