Skip to content

Commit

Permalink
Merge pull request #203 from thenaterhood/dev
Browse files Browse the repository at this point in the history
Next Release
  • Loading branch information
thenaterhood authored Oct 4, 2024
2 parents ec47e40 + c961c5c commit acb0c83
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 18 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.6.2
%define unmangled_version 3.6.2
%define version 3.6.3
%define unmangled_version 3.6.3
%define release 1

Summary: A simple screenshot tool
Expand Down
1 change: 0 additions & 1 deletion src/gscreenshot/frontend/gtk/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ def update_preview(self, pixbuf):
This assumes the pixbuf has already been resized appropriately.
'''
# view the previewPixbuf in the image_preview widget
self._preview.set_from_pixbuf(pixbuf)

def copy_to_clipboard(self, pixbuf):
Expand Down
19 changes: 13 additions & 6 deletions src/gscreenshot/scaling/scaling_factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def get_scaling_factor() -> float:
"""Get the scaling factor for the display"""
methods = [
get_scaling_from_gdk,
get_scaling_from_gtk,
get_scaling_from_qt,
get_scaling_from_xft_dpi,
]
Expand All @@ -23,14 +23,21 @@ def get_scaling_factor() -> float:
return 1


def get_scaling_from_gdk() -> typing.Optional[float]:
"""Get scaling factor from Gdk"""
def get_scaling_from_gtk() -> typing.Optional[float]:
"""Get scaling factor from Gtk"""
# pylint: disable=import-outside-toplevel
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gdk # type: ignore
display = Gdk.Display.get_default()
monitor = display.get_primary_monitor() or display.get_monitor(0)
from gi.repository import Gtk # type: ignore

window = Gtk.Window()
screen = window.get_screen()
display = screen.get_display()
monitor = (
display.get_monitor_at_window(screen.get_root_window())
or display.get_primary_monitor()
or display.get_monitor(0)
)
if monitor:
return monitor.get_scale_factor()
return None
Expand Down
4 changes: 4 additions & 0 deletions src/gscreenshot/screenshooter/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ class NoSupportedScreenshooterError(BaseException):
def __init__(self, msg="No supported screenshot backend found", required=None):
BaseException.__init__(self, msg)
self.required = required


class ScreenshotError(BaseException):
'''Generic screenshot error'''
16 changes: 11 additions & 5 deletions src/gscreenshot/screenshooter/screenshooter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from gscreenshot.selector.exceptions import SelectionExecError, SelectionParseError
from gscreenshot.selector.exceptions import SelectionCancelled, NoSupportedSelectorError
from gscreenshot.util import GSCapabilities
from .exceptions import ScreenshotError


class Screenshooter(object):
Expand Down Expand Up @@ -243,15 +244,20 @@ def _call_screenshooter(self, screenshooter: str,
params = []

params = [screenshooter] + params
self._screenshot = None
try:
subprocess.check_output(params)
screenshot_output = subprocess.check_output(params)
if not os.path.exists(self._tempfile):
if len(screenshot_output.decode()) > 0:
raise ScreenshotError(f"screenshot failed: {screenshot_output.decode()}")
raise ScreenshotError("screenshot failed but provided no output")

self._screenshot = Screenshot(PIL.Image.open(self._tempfile))
os.unlink(self._tempfile)
except (subprocess.CalledProcessError, IOError, OSError):
self._screenshot = None
return False
except (subprocess.CalledProcessError, IOError, OSError, ScreenshotError) as exc:
print(repr(exc))

return True
return self._screenshot is not None

def __repr__(self) -> str:
return f'{self.__class__.__name__}(selector={self._selector})'
12 changes: 8 additions & 4 deletions src/gscreenshot/screenshooter/xdg_desktop_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,19 @@ def get_request_handle(self, token) -> str:

@staticmethod
def callback(response, result):
'''callback function when a screenshot is completed'''
'''
callback function when a screenshot is completed
Do not raise exceptions or exit in this function - this is a dbus callback
and will not fail in a convenient place.
'''
if response == 0:
uri = result["uri"]
path = uri.replace("file://", "")
shutil.move(path, sys.argv[1])
loop.quit()
else:
loop.quit()
sys.exit(1)
print(response, result)

loop.quit()


class XdgDesktopPortal(Screenshooter):
Expand Down
3 changes: 3 additions & 0 deletions src/gscreenshot/screenshot/screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def get_preview(self, width: int, height: int, with_border=False) -> Image.Image
except AttributeError: # PIL < 9.0
antialias_algo = Image.ANTIALIAS

if thumbnail.height/height < .1 and thumbnail.width/width < .1:
thumbnail = thumbnail.resize((thumbnail.width*10, thumbnail.height*10))

thumbnail.thumbnail((width, height), antialias_algo)

if with_border:
Expand Down

0 comments on commit acb0c83

Please sign in to comment.