|
1 | 1 | // -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*- |
2 | 2 | const Gdk = imports.gi.Gdk; |
| 3 | +const Gio = imports.gi.Gio; |
| 4 | +const Keybindings = imports.ui.keybindings; |
| 5 | +const Main = imports.ui.main; |
3 | 6 |
|
4 | 7 | function PointerTracker(){ |
5 | 8 | this._init(); |
@@ -31,3 +34,91 @@ PointerTracker.prototype = { |
31 | 34 | }; |
32 | 35 | } |
33 | 36 | }; |
| 37 | + |
| 38 | + |
| 39 | +var PointerSwitcher = class { |
| 40 | + constructor(wm) { |
| 41 | + this.settings = new Gio.Settings({ schema_id: "org.cinnamon.desktop.keybindings" }); |
| 42 | + this.settings.connect("changed", (settings, key) => { |
| 43 | + if (["pointer-next-monitor", "pointer-previous-monitor"].includes(key)) { |
| 44 | + this.update_settings(); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + this.update_settings() |
| 49 | + } |
| 50 | + |
| 51 | + update_settings() { |
| 52 | + Main.keybindingManager.addHotKeyArray( |
| 53 | + "pointer-next-monitor", |
| 54 | + this.settings.get_strv("pointer-next-monitor"), |
| 55 | + () => this.next_monitor() |
| 56 | + ); |
| 57 | + |
| 58 | + Main.keybindingManager.addHotKeyArray( |
| 59 | + "pointer-previous-monitor", |
| 60 | + this.settings.get_strv("pointer-previous-monitor"), |
| 61 | + () => this.previous_monitor() |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + get_normalized_pointer_position_for_monitor(index) { |
| 66 | + let [global_x, global_y, mods] = global.get_pointer(); |
| 67 | + const monitor = Main.layoutManager.monitors[index]; |
| 68 | + |
| 69 | + let rx = global_x - monitor.x; |
| 70 | + let ry = global_y - monitor.y; |
| 71 | + |
| 72 | + let nx = rx / monitor.width; |
| 73 | + let ny = ry / monitor.height; |
| 74 | + |
| 75 | + if (nx < 0 || nx > 1.0) |
| 76 | + nx = 0.5; |
| 77 | + if (ny < 0 || ny > 1.0) |
| 78 | + ny = 0.5; |
| 79 | + |
| 80 | + return [nx, ny]; |
| 81 | + } |
| 82 | + |
| 83 | + get_real_pointer_position(index, nx, ny) { |
| 84 | + const monitor = Main.layoutManager.monitors[index]; |
| 85 | + |
| 86 | + let real_x = (nx * monitor.width) + monitor.x; |
| 87 | + let real_y = (nx * monitor.height) + monitor.y; |
| 88 | + |
| 89 | + return [real_x, real_y]; |
| 90 | + } |
| 91 | + |
| 92 | + next_monitor() { |
| 93 | + const current = global.display.get_current_monitor(); |
| 94 | + const max = global.display.get_n_monitors() - 1; |
| 95 | + const new_mon = current + 1 <= max ? current + 1 : 0; |
| 96 | + |
| 97 | + this.move_from_to_monitor(current, new_mon); |
| 98 | + } |
| 99 | + |
| 100 | + previous_monitor() { |
| 101 | + const current = global.display.get_current_monitor(); |
| 102 | + const max = global.display.get_n_monitors() - 1; |
| 103 | + const new_mon = current - 1 >= 0 ? current - 1 : max; |
| 104 | + |
| 105 | + this.move_from_to_monitor(current, new_mon); |
| 106 | + } |
| 107 | + |
| 108 | + move_from_to_monitor(from_i, to_i) { |
| 109 | + const center = global.settings.get_boolean("center-warped-pointer") |
| 110 | + |
| 111 | + let nx, ny; |
| 112 | + |
| 113 | + if (global.settings.get_boolean("center-warped-pointer")) { |
| 114 | + nx = ny = 0.5; |
| 115 | + } else { |
| 116 | + [nx, ny] = this.get_normalized_pointer_position_for_monitor(from_i); |
| 117 | + } |
| 118 | + |
| 119 | + let [x, y] = this.get_real_pointer_position(to_i, nx, ny); |
| 120 | + |
| 121 | + global.set_pointer(x, y); |
| 122 | + } |
| 123 | +}; |
| 124 | + |
0 commit comments