1515#
1616# SPDX-License-Identifier: GPL-3.0-or-later
1717
18+
1819import os
1920from typing import Optional , Tuple
20- from gi .repository import Gtk , Gio , Gdk
21+ from gi .repository import Gtk , Gio , Gdk , GLib , Xdp
2122from gradia .clipboard import save_texture_to_file
2223
2324ImportFormat = Tuple [str , str ]
@@ -176,6 +177,63 @@ def _handle_clipboard_texture(
176177 self .window ._set_loading_state (False )
177178
178179
180+ class ScreenshotImageLoader (BaseImageLoader ):
181+ """Handles loading images through screenshot capture"""
182+
183+ def __init__ (self , window : Gtk .ApplicationWindow , temp_dir : str ) -> None :
184+ super ().__init__ (window , temp_dir )
185+ self .portal = Xdp .Portal ()
186+
187+ def take_screenshot (self ) -> None :
188+ """Initiate screenshot capture"""
189+ try :
190+ self .portal .take_screenshot (
191+ None ,
192+ Xdp .ScreenshotFlags .INTERACTIVE ,
193+ None ,
194+ self ._on_screenshot_taken ,
195+ None
196+ )
197+ except Exception as e :
198+ print (f"Failed to initiate screenshot: { e } " )
199+ self .window ._show_notification (_ ("Failed to take screenshot" ))
200+
201+ def _on_screenshot_taken (self , portal_object , result , user_data ) -> None :
202+ """Handle screenshot completion"""
203+ try :
204+ uri = self .portal .take_screenshot_finish (result )
205+ self ._handle_screenshot_uri (uri )
206+ except GLib .Error as e :
207+ print (f"Screenshot error: { e } " )
208+ self .window ._show_notification (_ ("Screenshot cancelled" ))
209+
210+ def _handle_screenshot_uri (self , uri : str ) -> None :
211+ """Process the screenshot URI and convert to local file"""
212+ try :
213+ file = Gio .File .new_for_uri (uri )
214+ success , contents , _unused = file .load_contents (None )
215+ if not success or not contents :
216+ raise Exception ("Failed to load screenshot data" )
217+
218+ temp_filename = f"screenshot_{ os .urandom (6 ).hex ()} .png"
219+ temp_path = os .path .join (self .temp_dir , temp_filename )
220+
221+ with open (temp_path , 'wb' ) as f :
222+ f .write (contents )
223+
224+ filename = _ ("Screenshot" )
225+ location = _ ("Screenshot" )
226+
227+ self ._set_image_and_update_ui (temp_path , filename , location )
228+ self .window ._show_notification (_ ("Screenshot captured!" ))
229+
230+ except Exception as e :
231+ print (f"Error processing screenshot: { e } " )
232+ self .window ._show_notification (_ ("Failed to process screenshot" ))
233+ finally :
234+ self .window ._set_loading_state (False )
235+
236+
179237class ImportManager :
180238 def __init__ (self , window : Gtk .ApplicationWindow , temp_dir : str ) -> None :
181239 self .window : Gtk .ApplicationWindow = window
@@ -184,6 +242,7 @@ def __init__(self, window: Gtk.ApplicationWindow, temp_dir: str) -> None:
184242 self .file_loader : FileDialogImageLoader = FileDialogImageLoader (window , temp_dir )
185243 self .drag_drop_loader : DragDropImageLoader = DragDropImageLoader (window , temp_dir )
186244 self .clipboard_loader : ClipboardImageLoader = ClipboardImageLoader (window , temp_dir )
245+ self .screenshot_loader : ScreenshotImageLoader = ScreenshotImageLoader (window , temp_dir )
187246
188247 def open_file_dialog (self ) -> None :
189248 self .file_loader .open_file_dialog ()
@@ -197,3 +256,5 @@ def _on_drop_action(self, action: Optional[object], param: object) -> None:
197256 def load_from_clipboard (self ) -> None :
198257 self .clipboard_loader .load_from_clipboard ()
199258
259+ def take_screenshot (self ) -> None :
260+ self .screenshot_loader .take_screenshot ()
0 commit comments