screenshot-ui: Add support for disable-save-to-disk

When disable-save-to-disk is set, we only save the screenshot to the
clipboard, and therefore don't add the "open file" and "open folder"
actions to the notification.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1954>
This commit is contained in:
Ivan Molodetskikh 2022-01-27 20:29:02 +03:00 committed by Marge Bot
parent 27bcf0da48
commit 7d43038312

View File

@ -1012,6 +1012,8 @@ class ScreenshotUI extends St.Widget {
visible: false,
});
this._lockdownSettings = new Gio.Settings({ schema_id: 'org.gnome.desktop.lockdown' });
// The full-screen screenshot has a separate container so that we can
// show it without the screenshot UI fade-in for a nicer animation.
this._stageScreenshotContainer = new St.Widget({ visible: false });
@ -1536,27 +1538,14 @@ class ScreenshotUI extends St.Widget {
const clipboard = St.Clipboard.get_default();
clipboard.set_content(St.ClipboardType.CLIPBOARD, 'image/png', bytes);
const dir = Gio.File.new_for_path(GLib.build_filenamev([
GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_PICTURES),
// Translators: name of the folder under ~/Pictures for screenshots.
_('Screenshots'),
]));
try {
dir.make_directory_with_parents(null);
} catch (e) {
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS))
throw e;
}
const time = GLib.DateTime.new_now_local();
const timestamp = time.format('%Y-%m-%d %H-%M-%S');
// Translators: this is the name of the file that the screenshot is
// saved to. The placeholder is a timestamp, e.g. "2017-05-21 12-24-03".
const name = _('Screenshot from %s').format(timestamp);
// If the target file already exists, try appending a suffix with an
// increasing number to it.
// This will be set in the first save to disk branch and then accessed
// in the second save to disk branch, so we need to declare it outside.
let file;
// The function is declared here rather than inside the condition to
// satisfy eslint.
/**
* Returns a filename suffix with an increasingly large index.
@ -1570,7 +1559,30 @@ class ScreenshotUI extends St.Widget {
yield '-%s'.format(i);
}
let file;
const disableSaveToDisk =
this._lockdownSettings.get_boolean('disable-save-to-disk');
if (!disableSaveToDisk) {
const dir = Gio.File.new_for_path(GLib.build_filenamev([
GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_PICTURES),
// Translators: name of the folder under ~/Pictures for screenshots.
_('Screenshots'),
]));
try {
dir.make_directory_with_parents(null);
} catch (e) {
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS))
throw e;
}
const timestamp = time.format('%Y-%m-%d %H-%M-%S');
// Translators: this is the name of the file that the screenshot is
// saved to. The placeholder is a timestamp, e.g. "2017-05-21 12-24-03".
const name = _('Screenshot from %s').format(timestamp);
// If the target file already exists, try appending a suffix with an
// increasing number to it.
for (const suffix of suffixes()) {
file = Gio.File.new_for_path(GLib.build_filenamev([
dir.get_path(), '%s%s.png'.format(name, suffix),
@ -1588,6 +1600,7 @@ class ScreenshotUI extends St.Widget {
// Add it to recent files.
Gtk.RecentManager.get_default().add_item(file.get_uri());
}
// Create a St.ImageContent icon for the notification. We want
// St.ImageContent specifically because it preserves the aspect ratio when
@ -1617,6 +1630,8 @@ class ScreenshotUI extends St.Widget {
_('You can paste the image from the clipboard.'),
{ datetime: time, gicon: content }
);
if (!disableSaveToDisk) {
// Translators: button on the screenshot notification.
notification.addAction(_('Show in Files'), () => {
const app =
@ -1638,8 +1653,9 @@ class ScreenshotUI extends St.Widget {
logError(err, 'Error opening screenshot');
}
});
notification.setTransient(true);
}
notification.setTransient(true);
Main.messageTray.add(source);
source.showNotification(notification);
}