Skip to content

Commit

Permalink
Merge pull request #181 from thenaterhood/dev
Browse files Browse the repository at this point in the history
Next Release
  • Loading branch information
thenaterhood authored Mar 16, 2024
2 parents 4a0a079 + 2a173e7 commit 609c280
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 26 deletions.
4 changes: 2 additions & 2 deletions specs/gscreenshot.spec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%define name gscreenshot
%define version 3.4.2
%define unmangled_version 3.4.2
%define version 3.4.3
%define unmangled_version 3.4.3
%define release 1

Summary: A simple screenshot tool
Expand Down
10 changes: 9 additions & 1 deletion src/gscreenshot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
_ = gettext.gettext


#pylint: disable=missing-class-docstring
class GscreenshotClipboardException(Exception):
pass


class Gscreenshot(object):
"""
Gscreenshot application
Expand Down Expand Up @@ -607,13 +612,15 @@ def copy_last_screenshot_to_clipboard(self) -> bool:
'-t',
'image/png'
]
clipper_name = "xclip"

if session_is_wayland():
params = [
'wl-copy',
'-t',
'image/png'
]
clipper_name = "wl-copy"

with io.BytesIO() as png_data:
image.save(png_data, "PNG")
Expand All @@ -629,7 +636,8 @@ def copy_last_screenshot_to_clipboard(self) -> bool:
xclip.communicate(input=png_data.getvalue())
return True
except (OSError, subprocess.CalledProcessError):
return False
#pylint: disable=raise-missing-from
raise GscreenshotClipboardException(clipper_name)

def get_last_save_directory(self) -> str:
"""Returns the path of the last save directory"""
Expand Down
11 changes: 6 additions & 5 deletions src/gscreenshot/frontend/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sys
import gettext

from gscreenshot import Gscreenshot
from gscreenshot import Gscreenshot, GscreenshotClipboardException
from gscreenshot.screenshooter.exceptions import NoSupportedScreenshooterError

_ = gettext.gettext
Expand Down Expand Up @@ -148,25 +148,26 @@ def run():
print(_("failed to show screenshot notification - is notify-send working?"))

shot_saved = False
should_save_shot = (args.filename is not False or args.clip is False)
exit_code = 0

if args.filename is not False:
shot_saved = gscreenshot.save_last_image(args.filename)
elif args.clip is False:
shot_saved = gscreenshot.save_last_image()

if should_save_shot and not shot_saved:
if (args.filename is not False or args.clip is False) and not shot_saved:
exit_code = 1
print(_("Failed to save screenshot!"))

if args.open is not False:
gscreenshot.open_last_screenshot()

if args.clip is not False:
if not gscreenshot.copy_last_screenshot_to_clipboard():
try:
gscreenshot.copy_last_screenshot_to_clipboard()
except GscreenshotClipboardException as error:
tmp_file = gscreenshot.save_and_return_path()
print(_("Could not clip image! Xclip failed to run."))
print(_("Could not clip image! {0} failed to run.").format(error))

if tmp_file is not None:
print(_("Your screenshot was saved to {0}").format(tmp_file))
Expand Down
10 changes: 7 additions & 3 deletions src/gscreenshot/frontend/gtk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from time import sleep
from pkg_resources import resource_string, resource_filename
import pygtkcompat
from gscreenshot import Gscreenshot
from gscreenshot import Gscreenshot, GscreenshotClipboardException
from gscreenshot.frontend.gtk.dialogs import OpenWithDialog, WarningDialog
from gscreenshot.frontend.gtk.dialogs import FileSaveDialog, FileOpenDialog
from gscreenshot.frontend.gtk.view import View
Expand Down Expand Up @@ -364,9 +364,13 @@ def on_button_copy_clicked(self, *_):
pixbuf = self._image_to_pixbuf(img)

if not self._view.copy_to_clipboard(pixbuf):
if not self._app.copy_last_screenshot_to_clipboard():
try:
self._app.copy_last_screenshot_to_clipboard()
except GscreenshotClipboardException as error:
warning_dialog = WarningDialog(
i18n("Your clipboard doesn't support persistence and xclip isn't available."),
i18n(
"Your clipboard doesn't support persistence and {0} isn't available."
).format(error),
self._view.get_window())
self._view.run_dialog(warning_dialog)
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ msgid "Using {0} screenshot backend"
msgstr ""

#: src/gscreenshot/frontend/gtk.py:152
msgid "Your clipboard doesn't support persistence and xclip isn't available."
msgid "Your clipboard doesn't support persistence and {0} isn't available."
msgstr ""

#: src/gscreenshot/frontend/gtk.py:161
Expand Down Expand Up @@ -161,7 +161,7 @@ msgstr ""
msgid "Failed to save screenshot!"
msgstr ""

msgid "Could not clip image! Xclip failed to run."
msgid "Could not clip image! {0} failed to run."
msgstr ""

msgid "Your screenshot was saved to {0}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ msgid "Using {0} screenshot backend"
msgstr "Usando la herramienta de captura de pantalla de {0}"

#: src/gscreenshot/frontend/gtk.py:152
msgid "Your clipboard doesn't support persistence and xclip isn't available."
msgstr "Su portapapeles no admite la persistencia y xclip no está instalado."
msgid "Your clipboard doesn't support persistence and {0} isn't available."
msgstr "Su portapapeles no admite la persistencia y {0} no está instalado."

#: src/gscreenshot/frontend/gtk.py:161
msgid "Please install xdg-open to open files."
Expand Down Expand Up @@ -171,8 +171,8 @@ msgstr "No se tomó ninguna captura de pantalla"
msgid "Failed to save screenshot!"
msgstr "¡No se pudo guardar la captura de pantalla!"

msgid "Could not clip image! Xclip failed to run."
msgstr "No se pudo copiar la imagen. Xclip no se pudo ejecutar."
msgid "Could not clip image! {0} failed to run."
msgstr "No se pudo copiar la imagen. {0} no se pudo ejecutar."

msgid "Your screenshot was saved to {0}"
msgstr "Tu captura de pantalla se guardó en {0}"
Expand Down
4 changes: 2 additions & 2 deletions src/gscreenshot/resources/locale/gscreenshot.pot
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ msgid "Using {0} screenshot backend"
msgstr ""

#: src/gscreenshot/frontend/gtk.py:152
msgid "Your clipboard doesn't support persistence and xclip isn't available."
msgid "Your clipboard doesn't support persistence and {0} isn't available."
msgstr ""

#: src/gscreenshot/frontend/gtk.py:161
Expand Down Expand Up @@ -160,7 +160,7 @@ msgstr ""
msgid "Failed to save screenshot!"
msgstr ""

msgid "Could not clip image! Xclip failed to run."
msgid "Could not clip image! {0} failed to run."
msgstr ""

msgid "Your screenshot was saved to {0}"
Expand Down
2 changes: 1 addition & 1 deletion src/gscreenshot/screenshooter/xdg_desktop_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def request(self, parent_window = ''):

request_token = f"gscreenshot_{token_hex}"

options = { 'handle_token': request_token }
options = { 'handle_token': request_token, 'interactive': False }

self.bus.add_signal_receiver(
self.callback,
Expand Down
15 changes: 13 additions & 2 deletions src/gscreenshot/selector/slurp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'''
Wrapper for the slurp screen selector utility
'''
from time import sleep
import typing
from gscreenshot.selector import RegionSelector
from gscreenshot.util import find_executable, GSCapabilities
Expand Down Expand Up @@ -39,7 +40,12 @@ def region_select(self) -> typing.Tuple[int, int, int, int]:
Returns:
(x top left, y top left, x bottom right, y bottom right)
"""
return self._get_boundary_interactive(['slurp', '-f', 'X=%x,Y=%y,W=%w,H=%h'])
ret = self._get_boundary_interactive([
'slurp', '-f', 'X=%x,Y=%y,W=%w,H=%h',
'-b', '#00000011', '-s', '#00000011', '-c', '#808080FF'])
# Sleep so we hopefully don't catch the edge of slurp closing
sleep(0.1)
return ret

def window_select(self) -> typing.Tuple[int, int, int, int]:
"""
Expand All @@ -48,7 +54,12 @@ def window_select(self) -> typing.Tuple[int, int, int, int]:
Returns:
(x top left, y top left, x bottom right, y bottom right)
"""
return self._get_boundary_interactive(['slurp', '-f', 'X=%x,Y=%y,W=%w,H=%h'])
ret = self._get_boundary_interactive([
'slurp', '-f', 'X=%x,Y=%y,W=%w,H=%h',
'-b', '#00000011', '-s', '#00000011', '-c', '#808080FF'])
# Sleep so we hopefully don't catch the edge of slurp closing
sleep(0.1)
return ret

@staticmethod
def can_run() -> bool:
Expand Down
11 changes: 7 additions & 4 deletions test/gscreenshot/test_gscreenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import subprocess
import unittest
from unittest.mock import Mock, mock_open, ANY
from src.gscreenshot import Gscreenshot
from src.gscreenshot import Gscreenshot, GscreenshotClipboardException


class GscreenshotTest(unittest.TestCase):
Expand Down Expand Up @@ -293,7 +293,12 @@ def test_copy_to_clipboard_process_error(self, mock_subprocess, mock_util):
# We can't mock the exception itself
mock_subprocess.CalledProcessError = subprocess.CalledProcessError
self.gscreenshot.screenshot_full_display()
success = self.gscreenshot.copy_last_screenshot_to_clipboard()

try:
self.gscreenshot.copy_last_screenshot_to_clipboard()
self.assertFalse(True, "expected exception but it didn't happen")
except GscreenshotClipboardException as error:
self.assertEqual(str(error), "xclip")

self.fake_image.save.assert_called_once()

Expand All @@ -309,5 +314,3 @@ def test_copy_to_clipboard_process_error(self, mock_subprocess, mock_util):
stdout=None,
stderr=None
)

self.assertFalse(success)

0 comments on commit 609c280

Please sign in to comment.