Add copy buttons for request and response content in history items

This commit is contained in:
vesp 2025-12-24 13:49:55 +01:00
parent 9d976478df
commit 2797a320c1
2 changed files with 99 additions and 13 deletions

View File

@ -87,13 +87,32 @@
<!-- Request Section -->
<child>
<object class="GtkLabel">
<property name="label">Request:</property>
<property name="xalign">0</property>
<object class="GtkBox">
<property name="orientation">horizontal</property>
<property name="spacing">6</property>
<property name="margin-top">6</property>
<style>
<class name="heading"/>
</style>
<child>
<object class="GtkLabel">
<property name="label">Request:</property>
<property name="xalign">0</property>
<property name="hexpand">True</property>
<style>
<class name="heading"/>
</style>
</object>
</child>
<child>
<object class="GtkButton" id="copy_request_button">
<property name="icon-name">edit-copy-symbolic</property>
<property name="tooltip-text">Copy request to clipboard</property>
<property name="valign">center</property>
<signal name="clicked" handler="on_copy_request_clicked" swapped="no"/>
<style>
<class name="flat"/>
<class name="circular"/>
</style>
</object>
</child>
</object>
</child>
@ -172,13 +191,32 @@
<!-- Response Section -->
<child>
<object class="GtkLabel">
<property name="label">Response:</property>
<property name="xalign">0</property>
<object class="GtkBox">
<property name="orientation">horizontal</property>
<property name="spacing">6</property>
<property name="margin-top">6</property>
<style>
<class name="heading"/>
</style>
<child>
<object class="GtkLabel">
<property name="label">Response:</property>
<property name="xalign">0</property>
<property name="hexpand">True</property>
<style>
<class name="heading"/>
</style>
</object>
</child>
<child>
<object class="GtkButton" id="copy_response_button">
<property name="icon-name">edit-copy-symbolic</property>
<property name="tooltip-text">Copy response to clipboard</property>
<property name="valign">center</property>
<signal name="clicked" handler="on_copy_response_clicked" swapped="no"/>
<style>
<class name="flat"/>
<class name="circular"/>
</style>
</object>
</child>
</object>
</child>

View File

@ -17,7 +17,7 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later
from gi.repository import Gtk, GObject
from gi.repository import Gtk, GObject, Gdk
from datetime import datetime
@ -38,11 +38,13 @@ class HistoryItem(Gtk.Box):
request_body_text = Gtk.Template.Child()
request_expander = Gtk.Template.Child()
request_expander_label = Gtk.Template.Child()
copy_request_button = Gtk.Template.Child()
response_headers_label = Gtk.Template.Child()
response_body_scroll = Gtk.Template.Child()
response_body_text = Gtk.Template.Child()
response_expander = Gtk.Template.Child()
response_expander_label = Gtk.Template.Child()
copy_response_button = Gtk.Template.Child()
load_button = Gtk.Template.Child()
__gsignals__ = {
@ -174,6 +176,52 @@ class HistoryItem(Gtk.Box):
self.response_body_scroll.set_max_content_height(60)
self.response_expander_label.set_text("Show full response")
@Gtk.Template.Callback()
def on_copy_request_clicked(self, button):
"""Copy full request to clipboard."""
# Build full request text
request_text = f"{self.entry.request.method} {self.entry.request.url}\n\n"
# Add headers
if self.entry.request.headers:
request_text += "Headers:\n"
for key, value in self.entry.request.headers.items():
request_text += f"{key}: {value}\n"
request_text += "\n"
# Add body
if self.entry.request.body:
request_text += "Body:\n"
request_text += self.entry.request.body
# Copy to clipboard
clipboard = Gdk.Display.get_default().get_clipboard()
clipboard.set(request_text)
@Gtk.Template.Callback()
def on_copy_response_clicked(self, button):
"""Copy full response to clipboard."""
if self.entry.response:
# Build full response text
response_text = f"{self.entry.response.status_code} {self.entry.response.status_text}\n\n"
# Add headers
if self.entry.response.headers:
response_text += self.entry.response.headers
response_text += "\n\n"
# Add body
if self.entry.response.body:
response_text += self.entry.response.body
elif self.entry.error:
response_text = f"Error:\n{self.entry.error}"
else:
response_text = "(no response)"
# Copy to clipboard
clipboard = Gdk.Display.get_default().get_clipboard()
clipboard.set(response_text)
def toggle_expanded(self):
"""Toggle between collapsed and expanded view."""
self.expanded = not self.expanded