|
1 | | -/* |
2 | | - * Copyright 2024-25 Alexander Browne |
3 | | - * Copyright 2020 Evan Welsh (https://gjs.guide/extensions/review-guidelines/review-guidelines.html#remove-main-loop-sources) |
4 | | - * Copyright 2020 Jeff Channell (https://github.com/jeffchannell/jiggle/blob/master/cursor.js) |
5 | | - */ |
6 | | - |
7 | | -/* extension.js |
8 | | - * |
9 | | - * This program is free software: you can redistribute it and/or modify |
10 | | - * it under the terms of the GNU General Public License as published by |
11 | | - * the Free Software Foundation, either version 2 of the License, or |
12 | | - * (at your option) any later version. |
13 | | - * |
14 | | - * This program is distributed in the hope that it will be useful, |
15 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | | - * GNU General Public License for more details. |
18 | | - * |
19 | | - * You should have received a copy of the GNU General Public License |
20 | | - * along with this program. If not, see <http://www.gnu.org/licenses/>. |
21 | | - * |
22 | | - * SPDX-License-Identifier: GPL-2.0-or-later |
23 | | - */ |
24 | | - |
25 | | -import Clutter from 'gi://Clutter'; |
26 | | -import GLib from 'gi://GLib'; |
27 | | - |
28 | | -import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js'; |
| 1 | +import GLib from 'gi://GLib' |
| 2 | +import Clutter from 'gi://Clutter' |
| 3 | +import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js' |
| 4 | + |
29 | 5 |
|
30 | 6 | export default class HideCursor extends Extension { |
31 | | - enable() { |
32 | | - this._hideCursor = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 5, () => { |
33 | | - let tracker = global.backend.get_cursor_tracker(); |
34 | | - const seat = Clutter.get_default_backend().get_default_seat(); |
| 7 | + TICK_TIMEOUT = 1 |
| 8 | + |
| 9 | + enable() { |
| 10 | + this._settings = this.getSettings() |
| 11 | + this.HIDE_TIMEOUT = this._settings.get_int('timeout') * 1000 |
| 12 | + this._settingsConnectionID = this._settings.connect('changed::timeout', () => |
| 13 | + this.HIDE_TIMEOUT = this._settings.get_int('timeout') * 1000) |
| 14 | + |
| 15 | + this._seat = Clutter.get_default_backend().get_default_seat() |
| 16 | + this._timer = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, this.TICK_TIMEOUT, this.tick) |
| 17 | + this._tracker = global.backend.get_cursor_tracker() |
| 18 | + this._positionChangedId = this._tracker.connect('position-invalidated', this.move) |
| 19 | + this._lastMove = Date.now() |
| 20 | + } |
| 21 | + |
| 22 | + disable() { |
| 23 | + if (this._positionChangedId) { |
| 24 | + this._tracker.disconnect(this._positionChangedId) |
| 25 | + this._positionChangedId = null |
| 26 | + } |
| 27 | + |
| 28 | + if (this._settingsConnectionID) { |
| 29 | + this._settings.disconnect(this._settingsConnectionID) |
| 30 | + this._settingsConnectionID = null |
| 31 | + } |
| 32 | + |
| 33 | + if (this._tracker) { |
| 34 | + if (this._hasVisibilityInhibited && !this._tracker.get_pointer_visible()) |
| 35 | + this._tracker.uninhibit_cursor_visibility() |
| 36 | + this._tracker = null |
| 37 | + this._hasVisibilityInhibited = null |
| 38 | + } |
| 39 | + |
| 40 | + if (this._timer) { |
| 41 | + GLib.Source.remove(this._timer) |
| 42 | + this._timer = null |
| 43 | + } |
| 44 | + |
| 45 | + if (this._hasFocusInhibited) { |
| 46 | + this._seat.uninhibit_unfocus() |
| 47 | + this._hasFocusInhibited = null |
| 48 | + } |
35 | 49 |
|
36 | | - if (!seat.is_unfocus_inhibited()) |
37 | | - seat.inhibit_unfocus(); |
38 | | - tracker.set_pointer_visible(false); |
| 50 | + this._seat = null |
| 51 | + this._lastMove = null |
| 52 | + this._settings = null |
| 53 | + this.HIDE_TIMEOUT = null |
| 54 | + } |
39 | 55 |
|
40 | | - return GLib.SOURCE_CONTINUE; |
41 | | - }); |
| 56 | + tick = () => { |
| 57 | + if (Date.now() - this._lastMove > this.HIDE_TIMEOUT && this._tracker.get_pointer_visible()) { |
| 58 | + if (!this._hasFocusInhibited) { |
| 59 | + this._seat.inhibit_unfocus() |
| 60 | + this._hasFocusInhibited = true |
| 61 | + } |
| 62 | + this._tracker.inhibit_cursor_visibility() |
| 63 | + this._hasVisibilityInhibited = true |
42 | 64 | } |
43 | 65 |
|
44 | | - disable() { |
45 | | - if (this._hideCursor) { |
46 | | - GLib.Source.remove(this._hideCursor); |
47 | | - this._hideCursor = null; |
48 | | - } |
49 | | - let tracker = global.backend.get_cursor_tracker(); |
50 | | - const seat = Clutter.get_default_backend().get_default_seat(); |
51 | | - |
52 | | - if (seat.is_unfocus_inhibited()) |
53 | | - seat.uninhibit_unfocus(); |
54 | | - tracker.set_pointer_visible(true); |
| 66 | + return GLib.SOURCE_CONTINUE |
| 67 | + } |
| 68 | + |
| 69 | + move = () => { |
| 70 | + this._lastMove = Date.now() |
| 71 | + |
| 72 | + if (this._hasFocusInhibited) { |
| 73 | + this._seat.uninhibit_unfocus() |
| 74 | + this._hasFocusInhibited = false |
55 | 75 | } |
| 76 | + |
| 77 | + if (!this._tracker.get_pointer_visible()) |
| 78 | + this._tracker.uninhibit_cursor_visibility() |
| 79 | + |
| 80 | + this._hasVisibilityInhibited = false |
| 81 | + } |
56 | 82 | } |
0 commit comments