| 1 |
// SPDX-FileCopyrightText: 2013 Owen Taylor <[email protected]> |
| 2 |
// SPDX-FileCopyrightText: 2013 Richard Hughes <[email protected]> |
| 3 |
// SPDX-FileCopyrightText: 2014 Florian Müllner <[email protected]> |
| 4 |
// SPDX-FileCopyrightText: 2016 Will Thompson <[email protected]> |
| 5 |
// SPDX-FileCopyrightText: 2017 Florian Müllner <[email protected]> |
| 6 |
// SPDX-FileCopyrightText: 2019 Adrien Plazas <[email protected]> |
| 7 |
// SPDX-FileCopyrightText: 2019 Willy Stadnick <[email protected]> |
| 8 |
// |
| 9 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 10 |
|
| 11 |
import Clutter from 'gi://Clutter'; |
| 12 |
import Meta from 'gi://Meta'; |
| 13 |
import Shell from 'gi://Shell'; |
| 14 |
import St from 'gi://St'; |
| 15 |
|
| 16 |
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js'; |
| 17 |
|
| 18 |
import * as Main from 'resource:///org/gnome/shell/ui/main.js'; |
| 19 |
|
| 20 |
const MESSAGE_FADE_TIME = 2000; |
| 21 |
|
| 22 |
export default class ScreenshotWindowSizerExtension extends Extension { |
| 23 |
SIZES = [ |
| 24 |
[624, 351], |
| 25 |
[800, 450], |
| 26 |
[1024, 576], |
| 27 |
[1000, 700], |
| 28 |
[1200, 675], |
| 29 |
[1600, 900], |
| 30 |
[360, 654], // Phone portrait maximized |
| 31 |
[720, 360], // Phone landscape fullscreen |
| 32 |
]; |
| 33 |
|
| 34 |
_flashMessage(message) { |
| 35 |
if (!this._text) { |
| 36 |
this._text = new St.Label({style_class: 'screenshot-sizer-message'}); |
| 37 |
Main.uiGroup.add_child(this._text); |
| 38 |
} |
| 39 |
|
| 40 |
this._text.remove_all_transitions(); |
| 41 |
this._text.text = message; |
| 42 |
|
| 43 |
this._text.opacity = 255; |
| 44 |
|
| 45 |
const monitor = Main.layoutManager.primaryMonitor; |
| 46 |
this._text.set_position( |
| 47 |
monitor.x + Math.floor(monitor.width / 2 - this._text.width / 2), |
| 48 |
monitor.y + Math.floor(monitor.height / 2 - this._text.height / 2)); |
| 49 |
|
| 50 |
this._text.ease({ |
| 51 |
opacity: 0, |
| 52 |
duration: MESSAGE_FADE_TIME, |
| 53 |
mode: Clutter.AnimationMode.EASE_OUT_QUAD, |
| 54 |
onComplete: () => this._hideMessage(), |
| 55 |
}); |
| 56 |
} |
| 57 |
|
| 58 |
_hideMessage() { |
| 59 |
this._text.destroy(); |
| 60 |
delete this._text; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @param {Meta.Display} display - the display |
| 65 |
* @param {Meta.Window=} window - for per-window bindings, the window |
| 66 |
* @param {Clutter.Event} event - the triggering event |
| 67 |
* @param {Meta.KeyBinding} binding - the key binding |
| 68 |
*/ |
| 69 |
_cycleScreenshotSizes(display, window, event, binding) { |
| 70 |
const backwards = binding.is_reversed(); |
| 71 |
|
| 72 |
// Unmaximize first |
| 73 |
if (window.is_maximized()) |
| 74 |
window.unmaximize(); |
| 75 |
|
| 76 |
const workArea = window.get_work_area_current_monitor(); |
| 77 |
const outerRect = window.get_frame_rect(); |
| 78 |
|
| 79 |
// Double both axes if on a hidpi display |
| 80 |
const scaleFactor = St.ThemeContext.get_for_stage(global.stage).scale_factor; |
| 81 |
const scaledSizes = this.SIZES.map(size => size.map(wh => wh * scaleFactor)) |
| 82 |
.filter(([w, h]) => w <= workArea.width && h <= workArea.height); |
| 83 |
|
| 84 |
// Find the nearest 16:9 size for the current window size |
| 85 |
let nearestIndex; |
| 86 |
let nearestError; |
| 87 |
|
| 88 |
for (let i = 0; i < scaledSizes.length; i++) { |
| 89 |
const [width, height] = scaledSizes[i]; |
| 90 |
|
| 91 |
// get the best initial window size |
| 92 |
const error = Math.abs(width - outerRect.width) + Math.abs(height - outerRect.height); |
| 93 |
if (nearestIndex === undefined || error < nearestError) { |
| 94 |
nearestIndex = i; |
| 95 |
nearestError = error; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// get the next size up or down from ideal |
| 100 |
const newIndex = (nearestIndex + (backwards ? -1 : 1)) % scaledSizes.length; |
| 101 |
const [newWidth, newHeight] = scaledSizes.at(newIndex); |
| 102 |
|
| 103 |
// Push the window onscreen if it would be resized offscreen |
| 104 |
let newX = outerRect.x; |
| 105 |
let newY = outerRect.y; |
| 106 |
if (newX + newWidth > workArea.x + workArea.width) |
| 107 |
newX = Math.max(workArea.x + workArea.width - newWidth); |
| 108 |
if (newY + newHeight > workArea.y + workArea.height) |
| 109 |
newY = Math.max(workArea.y + workArea.height - newHeight); |
| 110 |
|
| 111 |
const id = window.connect('size-changed', () => { |
| 112 |
window.disconnect(id); |
| 113 |
this._notifySizeChange(window); |
| 114 |
}); |
| 115 |
window.move_resize_frame(true, newX, newY, newWidth, newHeight); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @param {Meta.Window} window - the window whose size changed |
| 120 |
*/ |
| 121 |
_notifySizeChange(window) { |
| 122 |
const {scaleFactor} = St.ThemeContext.get_for_stage(global.stage); |
| 123 |
const newOuterRect = window.get_frame_rect(); |
| 124 |
let message = '%d×%d'.format( |
| 125 |
newOuterRect.width / scaleFactor, |
| 126 |
newOuterRect.height / scaleFactor); |
| 127 |
|
| 128 |
// The new size might have been constrained by geometry hints (e.g. for |
| 129 |
// a terminal) - in that case, include the actual ratio to the message |
| 130 |
// we flash |
| 131 |
const actualNumerator = 9 * newOuterRect.width / newOuterRect.height; |
| 132 |
if (Math.abs(actualNumerator - 16) > 0.01) |
| 133 |
message += ' (%.2f:9)'.format(actualNumerator); |
| 134 |
|
| 135 |
this._flashMessage(message); |
| 136 |
} |
| 137 |
|
| 138 |
enable() { |
| 139 |
Main.wm.addKeybinding( |
| 140 |
'cycle-screenshot-sizes', |
| 141 |
this.getSettings(), |
| 142 |
Meta.KeyBindingFlags.PER_WINDOW, |
| 143 |
Shell.ActionMode.NORMAL, |
| 144 |
this._cycleScreenshotSizes.bind(this)); |
| 145 |
Main.wm.addKeybinding( |
| 146 |
'cycle-screenshot-sizes-backward', |
| 147 |
this.getSettings(), |
| 148 |
Meta.KeyBindingFlags.PER_WINDOW | Meta.KeyBindingFlags.IS_REVERSED, |
| 149 |
Shell.ActionMode.NORMAL, |
| 150 |
this._cycleScreenshotSizes.bind(this)); |
| 151 |
} |
| 152 |
|
| 153 |
disable() { |
| 154 |
Main.wm.removeKeybinding('cycle-screenshot-sizes'); |
| 155 |
Main.wm.removeKeybinding('cycle-screenshot-sizes-backward'); |
| 156 |
} |
| 157 |
} |