selection.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2013 otto.allmendinger@gmail.com
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. the Software, and to permit persons to whom the Software is furnished to do so,
  9. subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. */
  19. 'use strict';
  20. import GObject from 'gi://GObject';
  21. import Meta from 'gi://Meta';
  22. import Clutter from 'gi://Clutter';
  23. import St from 'gi://St';
  24. import * as Main from 'resource:///org/gnome/shell/ui/main.js';
  25. import * as Signals from 'resource:///org/gnome/shell/misc/signals.js';
  26. import {gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js';
  27. import * as Lib from './convenience.js';
  28. import * as Ext from './extension.js';
  29. import * as UtilNotify from './utilnotify.js';
  30. import {DisplayApi} from './display_module.js';
  31. /**
  32. * @type {Capture}
  33. */
  34. class Capture extends Signals.EventEmitter {
  35. constructor() {
  36. super();
  37. Lib.TalkativeLog('-£-capture selection init');
  38. this._mouseDown = false;
  39. this.monitor = Main.layoutManager.focusMonitor;
  40. this._areaSelection = new St.Widget({
  41. name: 'area-selection',
  42. style_class: 'area-selection',
  43. visible: 'true',
  44. reactive: 'true',
  45. x: -10,
  46. y: -10,
  47. });
  48. Main.uiGroup.add_child(this._areaSelection);
  49. this._areaResolution = new St.Label({
  50. style_class: 'area-resolution',
  51. text: '',
  52. });
  53. this._areaResolution.opacity = 255;
  54. this._areaResolution.set_position(0, 0);
  55. Main.uiGroup.add_child(this._areaResolution);
  56. this._grab = Main.pushModal(this._areaSelection);
  57. if (this._grab) {
  58. this._signalCapturedEvent = this._areaSelection.connect(
  59. 'captured-event',
  60. this._onCaptureEvent.bind(this)
  61. );
  62. this._setCaptureCursor();
  63. } else {
  64. Lib.TalkativeLog('-£-Main.pushModal() === false');
  65. }
  66. }
  67. /**
  68. * @private
  69. */
  70. _setDefaultCursor() {
  71. DisplayApi.set_cursor(Meta.Cursor.DEFAULT);
  72. }
  73. /**
  74. * @private
  75. */
  76. _setCaptureCursor() {
  77. DisplayApi.set_cursor(Meta.Cursor.CROSSHAIR);
  78. }
  79. /**
  80. * @param {Clutter.Actor} actor the actor that received the event
  81. * @param {Clutter.Event} event a Clutter.Event
  82. * @private
  83. */
  84. _onCaptureEvent(actor, event) {
  85. if (event.type() === Clutter.EventType.KEY_PRESS) {
  86. if (event.get_key_symbol() === Clutter.KEY_Escape)
  87. this._stop();
  88. }
  89. this.emit('captured-event', event);
  90. }
  91. /**
  92. * Draws a on-screen rectangle showing the area that will be captured by screen cast.
  93. *
  94. * @param {object} rect rectangle
  95. * @param {number} rect.x left position in pixels
  96. * @param {number} rect.y top position in pixels
  97. * @param {number} rect.w width in pixels
  98. * @param {number} rect.h height in pixels
  99. * @param {boolean} showResolution whether to display the size of the selected area
  100. */
  101. drawSelection({x, y, w, h}, showResolution) {
  102. this._areaSelection.set_position(x, y);
  103. this._areaSelection.set_size(w, h);
  104. if (showResolution && w > 100 && h > 50) {
  105. this._areaResolution.set_text(`${w} X ${h}`);
  106. this._areaResolution.set_position(
  107. x + (w / 2 - this._areaResolution.width / 2),
  108. y + (h / 2 - this._areaResolution.height / 2)
  109. );
  110. } else {
  111. this._areaResolution.set_position(0, 0);
  112. this._areaResolution.set_text('');
  113. }
  114. }
  115. /**
  116. * Clear drawing selection
  117. */
  118. clearSelection() {
  119. this.drawSelection(
  120. {
  121. x: -10,
  122. y: -10,
  123. w: 0,
  124. h: 0,
  125. },
  126. false
  127. );
  128. }
  129. /**
  130. * @private
  131. */
  132. _stop() {
  133. Lib.TalkativeLog('-£-capture selection stop');
  134. this._areaSelection.disconnect(this._signalCapturedEvent);
  135. this._setDefaultCursor();
  136. Main.uiGroup.remove_child(this._areaSelection);
  137. Main.popModal(this._grab);
  138. Main.uiGroup.remove_child(this._areaResolution);
  139. this._areaSelection.destroy();
  140. this.emit('stop');
  141. }
  142. _saveRect(x, y, h, w) {
  143. Lib.TalkativeLog(`-£-selection x:${x} y:${y} height:${h} width:${w}`);
  144. Ext.Indicator.saveSelectedRect(x, y, h, w);
  145. Ext.Indicator._doDelayAction();
  146. }
  147. toString() {
  148. return this.GTypeName;
  149. }
  150. }
  151. export class SelectionArea extends Signals.EventEmitter {
  152. constructor() {
  153. super();
  154. Lib.TalkativeLog('-£-area selection init');
  155. this._mouseDown = false;
  156. this._capture = new Capture();
  157. this._capture.connect('captured-event', this._onEvent.bind(this));
  158. this._capture.connect('stop', () => {
  159. this._ctrlNotify.resetAlert();
  160. this.emit('stop');
  161. });
  162. this._ctrlNotify = new UtilNotify.NotifyManager();
  163. this._ctrlNotify.createAlert(
  164. _('Select an area for recording or press [ESC] to abort')
  165. );
  166. }
  167. /**
  168. * @param {Clutter.actor} capture the actor the captured the event
  169. * @param {Clutter.Event} event a Clutter.Event
  170. * @private
  171. */
  172. _onEvent(capture, event) {
  173. let type = event.type();
  174. let [x, y] = global.get_pointer();
  175. if (type === Clutter.EventType.BUTTON_PRESS) {
  176. [this._startX, this._startY] = [x, y];
  177. this._mouseDown = true;
  178. } else if (this._mouseDown) {
  179. let rect = _getRectangle(this._startX, this._startY, x, y);
  180. if (type === Clutter.EventType.MOTION) {
  181. this._capture.drawSelection(rect, true);
  182. } else if (type === Clutter.EventType.BUTTON_RELEASE) {
  183. this._capture._stop();
  184. Lib.TalkativeLog(`-£-area x: ${rect.x} y: ${rect.y} height: ${rect.h} width: ${rect.w}`);
  185. this._capture._saveRect(rect.x, rect.y, rect.h, rect.w);
  186. }
  187. }
  188. }
  189. toString() {
  190. return this.GTypeName;
  191. }
  192. }
  193. export class SelectionWindow extends Signals.EventEmitter {
  194. constructor() {
  195. super();
  196. Lib.TalkativeLog('-£-window selection init');
  197. this._windows = global.get_window_actors();
  198. this._capture = new Capture();
  199. this._capture.connect('captured-event', this._onEvent.bind(this));
  200. this._capture.connect('stop', () => {
  201. this._ctrlNotify.resetAlert();
  202. this.emit('stop');
  203. });
  204. this._ctrlNotify = new UtilNotify.NotifyManager();
  205. this._ctrlNotify.createAlert(
  206. _('Select a window for recording or press [ESC] to abort')
  207. );
  208. }
  209. /**
  210. * @param {Clutter.Actor} capture the actor the captured the event
  211. * @param {Clutter.Event} event a Clutter.Event
  212. * @private
  213. */
  214. _onEvent(capture, event) {
  215. let type = event.type();
  216. let [x, y] = global.get_pointer();
  217. this._selectedWindow = _selectWindow(this._windows, x, y);
  218. if (this._selectedWindow)
  219. this._highlightWindow(this._selectedWindow);
  220. else
  221. this._clearHighlight();
  222. if (type === Clutter.EventType.BUTTON_PRESS) {
  223. if (this._selectedWindow) {
  224. this._capture._stop();
  225. let maxHeight = global.screen_height;
  226. let maxWidth = global.screen_width;
  227. Lib.TalkativeLog(`-£-global screen area H: ${maxHeight} W: ${maxWidth}`);
  228. let [w, h] = this._selectedWindow.get_size();
  229. let [wx, wy] = this._selectedWindow.get_position();
  230. Lib.TalkativeLog(`-£-windows pre wx: ${wx} wy: ${wy} height: ${h} width: ${w}`);
  231. if (wx < 0)
  232. wx = 0;
  233. if (wy < 0)
  234. wy = 0;
  235. if (wx + w > maxWidth)
  236. w = maxWidth - wx;
  237. if (wy + h > maxHeight)
  238. h = maxHeight - wy;
  239. Lib.TalkativeLog(`-£-windows post wx: ${wx} wy: ${wy} height: ${h} width: ${w}`);
  240. this._capture._saveRect(wx, wy, h, w);
  241. }
  242. }
  243. }
  244. /**
  245. * @param {Clutter.Actor} win the window to highlight
  246. * @private
  247. */
  248. _highlightWindow(win) {
  249. let rect = _getWindowRectangle(win);
  250. Lib.TalkativeLog(`-£-window highlight on, pos/meas: x:${rect.x} y:${rect.y} w:${rect.w} h:${rect.h}`);
  251. this._capture.drawSelection(rect, false);
  252. }
  253. /**
  254. * @private
  255. */
  256. _clearHighlight() {
  257. Lib.TalkativeLog('-£-window highlight off');
  258. this._capture.clearSelection();
  259. }
  260. toString() {
  261. return this.GTypeName;
  262. }
  263. }
  264. export class SelectionDesktop extends Signals.EventEmitter {
  265. constructor() {
  266. super();
  267. Lib.TalkativeLog('-£-desktop selection init');
  268. const displayCount = DisplayApi.number_displays();
  269. Lib.TalkativeLog(`-£-Number of monitor ${displayCount}`);
  270. for (let i = 0; i < displayCount; i++) {
  271. let tmpM = DisplayApi.display_geometry_for_index(i);
  272. Lib.TalkativeLog(`-£-monitor ${i} geometry x=${tmpM.x} y=${tmpM.y} w=${tmpM.width} h=${tmpM.height}`);
  273. }
  274. this._capture = new Capture();
  275. this._capture.connect('captured-event', this._onEvent.bind(this));
  276. this._capture.connect('stop', () => {
  277. this._ctrlNotify.resetAlert();
  278. this.emit('stop');
  279. });
  280. this._ctrlNotify = new UtilNotify.NotifyManager();
  281. this._ctrlNotify.createAlert(
  282. _('Select a desktop for recording or press [ESC] to abort')
  283. );
  284. }
  285. /**
  286. * @param {Clutter.Actor} capture the actor that captured the event
  287. * @param {Clutter.Event} event a Clutter.Event
  288. * @private
  289. */
  290. _onEvent(capture, event) {
  291. let type = event.type();
  292. if (type === Clutter.EventType.BUTTON_PRESS) {
  293. this._capture._stop();
  294. let tmpM = Main.layoutManager.currentMonitor;
  295. let x = tmpM.x;
  296. let y = tmpM.y;
  297. let height = tmpM.height;
  298. let width = tmpM.width;
  299. Lib.TalkativeLog(`-£-desktop x: ${x} y: ${y} height: ${height} width: ${width}`);
  300. this._capture._saveRect(x, y, height, width);
  301. }
  302. }
  303. toString() {
  304. return this.GTypeName;
  305. }
  306. }
  307. export class AreaRecording extends GObject.Object {
  308. constructor() {
  309. super();
  310. Lib.TalkativeLog('-£-area recording init');
  311. this._areaRecording = new St.Widget({
  312. name: 'area-recording',
  313. style_class: 'area-recording',
  314. visible: 'true',
  315. reactive: 'false',
  316. x: -10,
  317. y: -10,
  318. });
  319. let [recX, recY, recW, recH] = Ext.Indicator.getSelectedRect();
  320. let tmpH = Main.layoutManager.currentMonitor.height;
  321. let tmpW = Main.layoutManager.currentMonitor.width;
  322. Main.uiGroup.add_child(this._areaRecording);
  323. Main.overview.connect('showing', () => {
  324. Lib.TalkativeLog('-£-overview opening');
  325. Main.uiGroup.remove_child(this._areaRecording);
  326. });
  327. Main.overview.connect('hidden', () => {
  328. Lib.TalkativeLog('-£-overview closed');
  329. Main.uiGroup.add_child(this._areaRecording);
  330. });
  331. if (recX + recW <= tmpW - 5 && recY + recH <= tmpH - 5)
  332. this.drawArea(recX - 2, recY - 2, recW + 4, recH + 4);
  333. }
  334. /**
  335. * @param {number} x left position
  336. * @param {number} y top position
  337. * @param {number} w width
  338. * @param {number} h height
  339. */
  340. drawArea(x, y, w, h) {
  341. Lib.TalkativeLog('-£-draw area recording');
  342. this._visible = true;
  343. this._areaRecording.set_position(x, y);
  344. this._areaRecording.set_size(w, h);
  345. }
  346. /**
  347. * Clears the drawing area
  348. */
  349. clearArea() {
  350. Lib.TalkativeLog('-£-hide area recording');
  351. this._visible = false;
  352. this.drawArea(-10, -10, 0, 0);
  353. }
  354. /**
  355. * @returns {boolean}
  356. */
  357. isVisible() {
  358. return this._visible;
  359. }
  360. toString() {
  361. return this.GTypeName;
  362. }
  363. }
  364. /**
  365. * @param {number} x1 left position
  366. * @param {number} y1 top position
  367. * @param {number} x2 right position
  368. * @param {number} y2 bottom position
  369. * @returns {{x: number, y: number, w: number, h: number}}
  370. */
  371. function _getRectangle(x1, y1, x2, y2) {
  372. return {
  373. x: Math.min(x1, x2),
  374. y: Math.min(y1, y2),
  375. w: Math.abs(x1 - x2),
  376. h: Math.abs(y1 - y2),
  377. };
  378. }
  379. /**
  380. * @param {Clutter.Actor} win a Clutter.Actor
  381. * @returns {{x: number, y: number, w: number, h: number}}
  382. */
  383. function _getWindowRectangle(win) {
  384. let [tw, th] = win.get_size();
  385. let [tx, ty] = win.get_position();
  386. return {
  387. x: tx,
  388. y: ty,
  389. w: tw,
  390. h: th,
  391. };
  392. }
  393. /**
  394. * @param {Array(Clutter.Actor)} windows all windows on the display
  395. * @param {number} x left position
  396. * @param {number} y top position
  397. * @returns {Clutter.Actor}
  398. */
  399. function _selectWindow(windows, x, y) {
  400. let filtered = windows.filter(win => {
  401. if (
  402. win !== undefined &&
  403. win.visible &&
  404. typeof win.get_meta_window === 'function'
  405. ) {
  406. Lib.TalkativeLog(`-£-selectWin x:${x} y:${y}`);
  407. let [w, h] = win.get_size();
  408. let [wx, wy] = win.get_position();
  409. Lib.TalkativeLog(`-£-selectWin w:${w} h:${h} wx:${wx} wy:${wy}`);
  410. return wx <= x && wy <= y && wx + w >= x && wy + h >= y;
  411. } else {
  412. return false;
  413. }
  414. });
  415. filtered.sort((a, b) => {
  416. return (
  417. a.get_meta_window().get_layer() <= b.get_meta_window().get_layer()
  418. );
  419. });
  420. return filtered[0];
  421. }