tools: add icon to restore on-canvas text editor position

This commit adds a button inside the on-canvas text editor
that restores the editor to its original position after it has
been moved. The button is only visible when the editor has been
moved, and disappears when it returns to its default position.
This commit is contained in:
Gabriele Barbero 2025-06-14 18:20:03 +02:00 committed by Jehan
parent a193d45ab8
commit dc1bd701ac
6 changed files with 78 additions and 7 deletions

View file

@ -256,10 +256,7 @@ text_tool_restore_on_canvas_editor_position_cmd_callback (GimpAction *action,
{
GimpTextTool *text_tool = GIMP_TEXT_TOOL (data);
if (text_tool->layer)
gimp_text_layer_set_style_overlay_position (text_tool->layer, FALSE, 0, 0);
gimp_text_tool_editor_position (text_tool);
gimp_text_tool_restore_on_canvas_editor_position (text_tool);
}

View file

@ -293,6 +293,9 @@ gimp_text_tool_editor_position (GimpTextTool *text_tool)
x, y, -1,
text_tool->drag_offset_x + 25,
text_tool->drag_offset_y + 25);
gimp_text_style_show_restore_position_button (GIMP_TEXT_STYLE_EDITOR (text_tool->style_editor),
TRUE);
}
else
{
@ -2067,9 +2070,10 @@ gimp_text_tool_style_overlay_button_motion (GtkWidget *widget,
if (text_tool->overlay_dragging)
{
GimpTool *tool = GIMP_TOOL (text_tool);
GimpDisplayShell *shell = gimp_display_get_shell (tool->display);
gdouble x, y;
GimpTool *tool = GIMP_TOOL (text_tool);
GimpDisplayShell *shell = gimp_display_get_shell (tool->display);
GimpTextStyleEditor *style_editor;
gdouble x, y;
gdk_window_get_device_position_double (gtk_widget_get_window (GTK_WIDGET (shell)),
event->device,
@ -2087,6 +2091,9 @@ gimp_text_tool_style_overlay_button_motion (GtkWidget *widget,
gimp_text_layer_set_style_overlay_position (text_tool->layer, TRUE,
x, y);
style_editor = GIMP_TEXT_STYLE_EDITOR (text_tool->style_editor);
gimp_text_style_show_restore_position_button (style_editor, TRUE);
}
return TRUE;

View file

@ -66,6 +66,7 @@
#include "widgets/gimphelp-ids.h"
#include "widgets/gimpmenufactory.h"
#include "widgets/gimptextbuffer.h"
#include "widgets/gimptextstyleeditor.h"
#include "widgets/gimpuimanager.h"
#include "widgets/gimpviewabledialog.h"
@ -2482,3 +2483,15 @@ gimp_text_tool_get_direction (GimpTextTool *text_tool)
GimpTextOptions *options = GIMP_TEXT_TOOL_GET_OPTIONS (text_tool);
return options->base_dir;
}
void
gimp_text_tool_restore_on_canvas_editor_position (GimpTextTool *text_tool)
{
GimpTextStyleEditor *editor = GIMP_TEXT_STYLE_EDITOR (text_tool->style_editor);
if (text_tool->layer)
gimp_text_layer_set_style_overlay_position (text_tool->layer, FALSE, 0, 0);
gimp_text_tool_editor_position (text_tool);
gimp_text_style_show_restore_position_button (editor, FALSE);
}

View file

@ -135,3 +135,6 @@ void gimp_text_tool_clear_layout (GimpTextTool *text_tool);
gboolean gimp_text_tool_ensure_layout (GimpTextTool *text_tool);
void gimp_text_tool_apply (GimpTextTool *text_tool,
gboolean push_undo);
void gimp_text_tool_restore_on_canvas_editor_position
(GimpTextTool *text_tool);

View file

@ -33,6 +33,10 @@
#include "core/gimpcontainer.h"
#include "core/gimpcontext.h"
#include "tools/tools-types.h"
#include "tools/tool_manager.h"
#include "tools/gimptexttool.h"
#include "text/gimptext.h"
#include "text/gimpfont.h"
@ -42,6 +46,7 @@
#include "gimptextbuffer.h"
#include "gimptextstyleeditor.h"
#include "gimptexttag.h"
#include "gimpwidgets-constructors.h"
#include "gimpwidgets-utils.h"
#include "gimp-intl.h"
@ -130,6 +135,10 @@ static gboolean gimp_text_style_editor_notify_leave_dnd_handler
GdkEventCrossing event,
gpointer user_data);
static void gimp_text_style_editor_restore_position_callback
(GtkWidget *button,
GimpTextStyleEditor *editor);
G_DEFINE_TYPE (GimpTextStyleEditor, gimp_text_style_editor,
GTK_TYPE_BOX)
@ -279,6 +288,23 @@ gimp_text_style_editor_init (GimpTextStyleEditor *editor)
G_CALLBACK (gimp_text_style_editor_size_changed),
editor);
/* Restore position button */
editor->restore_position_button = gimp_icon_button_new (GIMP_ICON_RESET, NULL);
gtk_button_set_relief (GTK_BUTTON (editor->restore_position_button),
GTK_RELIEF_NONE);
gtk_image_set_from_icon_name (GTK_IMAGE (gtk_bin_get_child (GTK_BIN (editor->restore_position_button))),
GIMP_ICON_RESET, GTK_ICON_SIZE_MENU);
gimp_help_set_help_data (editor->restore_position_button,
_("Restore On-Canvas Editor Position"), NULL);
g_signal_connect (editor->restore_position_button, "clicked",
G_CALLBACK (gimp_text_style_editor_restore_position_callback),
editor);
gtk_box_pack_end (GTK_BOX (editor->upper_hbox), editor->restore_position_button,
FALSE, FALSE, 0);
/* lower row */
editor->lower_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
@ -675,6 +701,16 @@ gimp_text_style_editor_list_tags (GimpTextStyleEditor *editor,
return g_list_reverse (tags);
}
void
gimp_text_style_show_restore_position_button (GimpTextStyleEditor *editor,
gboolean show)
{
if (show)
gtk_widget_show (editor->restore_position_button);
else
gtk_widget_hide (editor->restore_position_button);
}
/* private functions */
@ -1415,3 +1451,13 @@ gimp_text_style_editor_notify_leave_dnd_handler (GtkWidget *widget,
return FALSE;
}
static void
gimp_text_style_editor_restore_position_callback (GtkWidget *button,
GimpTextStyleEditor *editor)
{
GimpTool *tool = tool_manager_get_active (editor->gimp);
GimpTextTool *text_tool = GIMP_TEXT_TOOL (tool);
gimp_text_tool_restore_on_canvas_editor_position (text_tool);
}

View file

@ -49,6 +49,7 @@ struct _GimpTextStyleEditor
GtkWidget *upper_hbox;
GtkWidget *lower_hbox;
GtkWidget *restore_position_button;
GtkWidget *font_entry;
GtkWidget *size_entry;
@ -85,3 +86,7 @@ GtkWidget * gimp_text_style_editor_new (Gimp *gimp,
GList * gimp_text_style_editor_list_tags (GimpTextStyleEditor *editor,
GList **remove_tags);
void gimp_text_style_show_restore_position_button
(GimpTextStyleEditor *editor,
gboolean show);