Skip to content

Commit

Permalink
Fix regression with canvas focus introduced by 50863f2
Browse files Browse the repository at this point in the history
When the root canvas window was made focusable instead of the canvas drawing area widget, this broke other code which explicitly was trying to make the drawing area focusable. All code now tries to set focus on the root canvas window, which from the changes in 50863f2 is the ideal choice to avoid unexpected scrolling while zoomed in

Fixes: #2070032
  • Loading branch information
cameronwhite committed Dec 22, 2024
1 parent 9373c5c commit 68b7596
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
14 changes: 14 additions & 0 deletions Pinta.Core/Classes/DocumentWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ internal DocumentWorkspace (Document document)

#region Public Properties
public Gtk.DrawingArea Canvas { get; set; } = null!; // NRT - This is set soon after creation
public Gtk.Widget CanvasWindow { get; set; } = null!; // NRT - This is set soon after creation

/// <summary>
/// Returns whether the zoomed image fits in the window without requiring scrolling.
Expand Down Expand Up @@ -177,6 +178,19 @@ public void InvalidateWindowRect (RectangleI windowRect)
OnCanvasInvalidated (new CanvasInvalidatedEventArgs (windowRect));
}

/// <summary>
/// Grabs focus to the canvas widget. This can be used to avoid leaving focus in
/// toolbar widgets, for example.
/// </summary>
public void GrabFocusToCanvas ()
{
bool gained_focus = CanvasWindow.GrabFocus ();
// Log a warning if something went wrong, e.g. there is a non-focusable widget
// in the hierarchy.
if (!gained_focus)
Console.Error.WriteLine ("Failed to gain focus on the canvas widget!");
}

/// <summary>
/// Determines whether the rectangle lies (at least partially) outside the canvas area.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Pinta.Core/Extensions/Gtk/GtkExtensions.Widget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static Gtk.SpinButton CreateToolBarSpinButton (
// tools can handle subsequent key events.
spin.OnValueChanged += (o, e) => {
if (PintaCore.Workspace.HasOpenDocuments)
PintaCore.Workspace.ActiveWorkspace.Canvas.GrabFocus ();
PintaCore.Workspace.ActiveWorkspace.GrabFocusToCanvas ();
};
return spin;
}
Expand Down
4 changes: 2 additions & 2 deletions Pinta.Gui.Widgets/Widgets/Canvas/CanvasWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public CanvasWindow (
scroll_controller.OnDecelerate += (_, _) => gesture_zoom.IsActive (); // Cancel scroll deceleration when zooming
vp.AddController (scroll_controller);

// The mouse handler in PintaCanvas grabs focus away from toolbar widgets.
// The mouse handler in PintaCanvas grabs focus away from toolbar widgets, along
// with DocumentWorkpace.GrabFocusToCanvas()
Focusable = true;

Canvas = new PintaCanvas (this, document, canvasGrid) { Name = "canvas" };

// Rulers
Expand Down
2 changes: 1 addition & 1 deletion Pinta.Tools/Tools/TextTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ protected override void OnSaveSettings (ISettingsService settings)
private void HandleFontChanged (object? sender, EventArgs e)
{
if (workspace.HasOpenDocuments)
workspace.ActiveDocument.Workspace.Canvas.GrabFocus ();
workspace.ActiveDocument.Workspace.GrabFocusToCanvas ();

UpdateFont ();
}
Expand Down
25 changes: 13 additions & 12 deletions Pinta/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void Activate ()

private void Workspace_DocumentClosed (object? sender, DocumentEventArgs e)
{
var tab = FindTabWithCanvas ((PintaCanvas) e.Document.Workspace.Canvas);
var tab = FindTabWithCanvas ((CanvasWindow) e.Document.Workspace.CanvasWindow);

if (tab != null)
canvas_pad.Notebook.RemoveTab (tab);
Expand Down Expand Up @@ -164,6 +164,7 @@ private void Workspace_DocumentCreated (object? sender, DocumentEventArgs e)
RulersVisible = PintaCore.Actions.View.Rulers.Value,
RulerMetric = GetCurrentRulerMetric ()
};
doc.Workspace.CanvasWindow = canvas;
doc.Workspace.Canvas = canvas.Canvas;

DocumentViewContent my_content = new (doc, canvas);
Expand Down Expand Up @@ -223,9 +224,9 @@ private bool HandleGlobalKeyPress (
// first shot at handling the event if
// the mouse pointer is on the canvas
if (PintaCore.Workspace.HasOpenDocuments) {
var canvas_window = ((PintaCanvas) PintaCore.Workspace.ActiveWorkspace.Canvas).CanvasWindow;
var canvas_window = (CanvasWindow) PintaCore.Workspace.ActiveWorkspace.CanvasWindow;

if ((canvas_window.Canvas.HasFocus || canvas_window.IsMouseOnCanvas) &&
if ((canvas_window.HasFocus || canvas_window.IsMouseOnCanvas) &&
canvas_window.Canvas.DoKeyPressEvent (controller, args)) {
return true;
}
Expand All @@ -249,9 +250,9 @@ private void HandleGlobalKeyRelease (
// Give the Canvas (and by extension the tools)
// first shot at handling the event if
// the mouse pointer is on the canvas
var canvas_window = ((PintaCanvas) PintaCore.Workspace.ActiveWorkspace.Canvas).CanvasWindow;
var canvas_window = (CanvasWindow) PintaCore.Workspace.ActiveWorkspace.CanvasWindow;

if (canvas_window.Canvas.HasFocus || canvas_window.IsMouseOnCanvas)
if (canvas_window.HasFocus || canvas_window.IsMouseOnCanvas)
canvas_window.Canvas.DoKeyReleaseEvent (controller, args);
}

Expand Down Expand Up @@ -575,10 +576,10 @@ private void ZoomToWindow_Activated (object sender, EventArgs e)
int image_x = PintaCore.Workspace.ImageSize.Width;
int image_y = PintaCore.Workspace.ImageSize.Height;

var canvas_window = PintaCore.Workspace.ActiveWorkspace.Canvas.Parent!;
var canvas_viewport = PintaCore.Workspace.ActiveWorkspace.Canvas.Parent!;

int window_x = canvas_window.GetAllocatedWidth ();
int window_y = canvas_window.GetAllocatedHeight ();
int window_x = canvas_viewport.GetAllocatedWidth ();
int window_y = canvas_viewport.GetAllocatedHeight ();

double ratio =
(image_x / (double) window_x >= image_y / (double) window_y)
Expand Down Expand Up @@ -608,16 +609,16 @@ private void ActiveDocumentChanged (object? sender, EventArgs e)
PintaCore.Actions.View.ResumeZoomUpdate ();

var doc = PintaCore.Workspace.ActiveDocument;
var tab = FindTabWithCanvas ((PintaCanvas) doc.Workspace.Canvas);
var tab = FindTabWithCanvas ((CanvasWindow) doc.Workspace.CanvasWindow);

if (tab != null)
canvas_pad.Notebook.ActiveItem = tab;

doc.Workspace.Canvas.GrabFocus ();
doc.Workspace.GrabFocusToCanvas ();
}

private IDockNotebookItem? FindTabWithCanvas (PintaCanvas canvas) =>
private IDockNotebookItem? FindTabWithCanvas (CanvasWindow canvas_window) =>
canvas_pad.Notebook.Items
.Where (i => ((CanvasWindow) i.Widget).Canvas == canvas)
.Where (i => ((CanvasWindow) i.Widget) == canvas_window)
.FirstOrDefault ();
}

0 comments on commit 68b7596

Please sign in to comment.