Skip to content

Commit

Permalink
Cache cursors for tools which change the cursor based on mouse events
Browse files Browse the repository at this point in the history
Fixes: #918
  • Loading branch information
cameronwhite committed Jul 7, 2024
1 parent 0ce4aab commit f59b5be
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 26 deletions.
3 changes: 2 additions & 1 deletion Pinta.Tools/Handles/MoveHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public sealed class MoveHandle : IToolHandle
private static readonly Cairo.Color fill_color = new (0, 0, 1, 1);
private static readonly Cairo.Color selection_fill_color = new (1, 0.5, 0, 1);
private static readonly Cairo.Color stroke_color = new (1, 1, 1, 0.7);
private static Gdk.Cursor default_cursor = GdkExtensions.CursorFromName (Pinta.Resources.StandardCursors.Default);

public PointD CanvasPosition { get; set; }

Expand All @@ -25,7 +26,7 @@ public sealed class MoveHandle : IToolHandle
/// </summary>
public bool Selected { get; set; } = false;

public string CursorName { get; init; } = Pinta.Resources.StandardCursors.Default;
public Gdk.Cursor Cursor { get; init; } = default_cursor;

/// <summary>
/// Tests whether the window point is inside the handle's area.
Expand Down
7 changes: 5 additions & 2 deletions Pinta.Tools/Tools/EllipseSelectTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ namespace Pinta.Tools;

public sealed class EllipseSelectTool : SelectTool
{
public EllipseSelectTool (IServiceProvider services) : base (services) { }
public EllipseSelectTool (IServiceProvider services) : base (services)
{
DefaultCursor = Gdk.Cursor.NewFromTexture (Resources.GetIcon ("Cursor.EllipseSelect.png"), 9, 18, null);
}

public override string Name => Translations.GetString ("Ellipse Select");
public override string Icon => Pinta.Resources.Icons.ToolSelectEllipse;
public override string StatusBarText => Translations.GetString ("Click and drag to draw an elliptical selection.\nHold Shift to constrain to a circle.");
public override Gdk.Cursor DefaultCursor => Gdk.Cursor.NewFromTexture (Resources.GetIcon ("Cursor.EllipseSelect.png"), 9, 18, null);
public override Gdk.Cursor DefaultCursor { get; }
public override int Priority => 15;

protected override void DrawShape (Document document, RectangleD r, Layer l)
Expand Down
5 changes: 3 additions & 2 deletions Pinta.Tools/Tools/EllipseTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public sealed class EllipseTool : ShapeTool
public EllipseTool (IServiceProvider services) : base (services)
{
this.services = services;
DefaultCursor = Gdk.Cursor.NewFromTexture (Resources.GetIcon ("Cursor.Ellipse.png"), 9, 18, null);

BaseEditEngine.CorrespondingTools[ShapeType] = this;
}

Expand All @@ -45,8 +47,7 @@ public override string Name
public override string Icon
=> Pinta.Resources.Icons.ToolEllipse;

public override Gdk.Cursor DefaultCursor
=> Gdk.Cursor.NewFromTexture (Resources.GetIcon ("Cursor.Ellipse.png"), 9, 18, null);
public override Gdk.Cursor DefaultCursor { get; }

public override int Priority => 43;

Expand Down
4 changes: 3 additions & 1 deletion Pinta.Tools/Tools/LineCurveTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ public sealed class LineCurveTool : ShapeTool
public LineCurveTool (IServiceProvider services) : base (services)
{
this.services = services;
DefaultCursor = Gdk.Cursor.NewFromTexture (Resources.GetIcon ("Cursor.Line.png"), 9, 18, null);

BaseEditEngine.CorrespondingTools[ShapeType] = this;
}

public override string Name => Translations.GetString ("Line/Curve");
public override string Icon => Pinta.Resources.Icons.ToolLine;
public override Gdk.Cursor DefaultCursor => Gdk.Cursor.NewFromTexture (Resources.GetIcon ("Cursor.Line.png"), 9, 18, null);
public override Gdk.Cursor DefaultCursor { get; }
public override int Priority => 37;

public override BaseEditEngine.ShapeTypes ShapeType
Expand Down
7 changes: 5 additions & 2 deletions Pinta.Tools/Tools/RectangleSelectTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ namespace Pinta.Tools;

public sealed class RectangleSelectTool : SelectTool
{
public RectangleSelectTool (IServiceProvider services) : base (services) { }
public RectangleSelectTool (IServiceProvider services) : base (services)
{
DefaultCursor = Gdk.Cursor.NewFromTexture (Resources.GetIcon ("Cursor.RectangleSelect.png"), 9, 18, null);
}

public override string Name => Translations.GetString ("Rectangle Select");
public override string Icon => Pinta.Resources.Icons.ToolSelectRectangle;
public override string StatusBarText => Translations.GetString (
"Click and drag to draw a rectangular selection." +
"\nHold Shift to constrain to a square.");
public override Gdk.Cursor DefaultCursor => Gdk.Cursor.NewFromTexture (Resources.GetIcon ("Cursor.RectangleSelect.png"), 9, 18, null);
public override Gdk.Cursor DefaultCursor { get; }
public override int Priority => 13;

protected override void DrawShape (Document document, RectangleD r, Layer l)
Expand Down
4 changes: 3 additions & 1 deletion Pinta.Tools/Tools/RectangleTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ public sealed class RectangleTool : ShapeTool
public RectangleTool (IServiceProvider services) : base (services)
{
this.services = services;
DefaultCursor = Gdk.Cursor.NewFromTexture (Resources.GetIcon ("Cursor.Rectangle.png"), 9, 18, null);

BaseEditEngine.CorrespondingTools[ShapeType] = this;
}

public override string Name => Translations.GetString ("Rectangle");
public override string Icon => Pinta.Resources.Icons.ToolRectangle;
public override Gdk.Cursor DefaultCursor => Gdk.Cursor.NewFromTexture (Resources.GetIcon ("Cursor.Rectangle.png"), 9, 18, null);
public override Gdk.Cursor DefaultCursor { get; }
public override int Priority => 39;

public override BaseEditEngine.ShapeTypes ShapeType
Expand Down
5 changes: 3 additions & 2 deletions Pinta.Tools/Tools/RoundedRectangleTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public sealed class RoundedRectangleTool : ShapeTool
public RoundedRectangleTool (IServiceProvider services) : base (services)
{
this.services = services;
DefaultCursor = Gdk.Cursor.NewFromTexture (Resources.GetIcon ("Cursor.RoundedRectangle.png"), 9, 18, null);

BaseEditEngine.CorrespondingTools[ShapeType] = this;
}

Expand All @@ -44,8 +46,7 @@ public override string Name
public override string Icon
=> Pinta.Resources.Icons.ToolRectangleRounded;

public override Gdk.Cursor DefaultCursor
=> Gdk.Cursor.NewFromTexture (Resources.GetIcon ("Cursor.RoundedRectangle.png"), 9, 18, null);
public override Gdk.Cursor DefaultCursor { get; }

public override int Priority
=> 41;
Expand Down
23 changes: 10 additions & 13 deletions Pinta.Tools/Tools/SelectTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public abstract class SelectTool : BaseTool

private readonly MoveHandle[] handles = new MoveHandle[8];
private int? active_handle;
private string? active_cursor_name;

public override Gdk.Key ShortcutKey => Gdk.Key.S;
protected override bool ShowAntialiasingButton => false;
Expand All @@ -59,14 +58,14 @@ public SelectTool (IServiceProvider services) : base (services)
tools = services.GetService<IToolService> ();
workspace = services.GetService<IWorkspaceService> ();

handles[0] = new MoveHandle { CursorName = Pinta.Resources.StandardCursors.ResizeNW };
handles[1] = new MoveHandle { CursorName = Pinta.Resources.StandardCursors.ResizeSW };
handles[2] = new MoveHandle { CursorName = Pinta.Resources.StandardCursors.ResizeNE };
handles[3] = new MoveHandle { CursorName = Pinta.Resources.StandardCursors.ResizeSE };
handles[4] = new MoveHandle { CursorName = Pinta.Resources.StandardCursors.ResizeW };
handles[5] = new MoveHandle { CursorName = Pinta.Resources.StandardCursors.ResizeN };
handles[6] = new MoveHandle { CursorName = Pinta.Resources.StandardCursors.ResizeE };
handles[7] = new MoveHandle { CursorName = Pinta.Resources.StandardCursors.ResizeS };
handles[0] = new MoveHandle { Cursor = GdkExtensions.CursorFromName (Pinta.Resources.StandardCursors.ResizeNW) };
handles[1] = new MoveHandle { Cursor = GdkExtensions.CursorFromName (Pinta.Resources.StandardCursors.ResizeSW) };
handles[2] = new MoveHandle { Cursor = GdkExtensions.CursorFromName (Pinta.Resources.StandardCursors.ResizeNE) };
handles[3] = new MoveHandle { Cursor = GdkExtensions.CursorFromName (Pinta.Resources.StandardCursors.ResizeSE) };
handles[4] = new MoveHandle { Cursor = GdkExtensions.CursorFromName (Pinta.Resources.StandardCursors.ResizeW) };
handles[5] = new MoveHandle { Cursor = GdkExtensions.CursorFromName (Pinta.Resources.StandardCursors.ResizeN) };
handles[6] = new MoveHandle { Cursor = GdkExtensions.CursorFromName (Pinta.Resources.StandardCursors.ResizeE) };
handles[7] = new MoveHandle { Cursor = GdkExtensions.CursorFromName (Pinta.Resources.StandardCursors.ResizeS) };

workspace.SelectionChanged += AfterSelectionChange;
}
Expand Down Expand Up @@ -347,14 +346,12 @@ private void UpdateCursor (Document document, PointD window_point)
{
var active_handle = FindHandleUnderPoint (window_point);
if (active_handle is not null) {
SetCursor (Cursor.NewFromName (active_handle.CursorName, null));
active_cursor_name = active_handle.CursorName;
SetCursor (active_handle.Cursor);
return;
}

if (active_cursor_name != null) {
if (CurrentCursor != DefaultCursor) {
SetCursor (DefaultCursor);
active_cursor_name = null;
}
}

Expand Down
5 changes: 3 additions & 2 deletions Pinta.Tools/Tools/TextTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ public override int Priority
public override string StatusBarText
=> Translations.GetString ("Left click to place cursor, then type desired text. Text color is primary color.");

public override Gdk.Cursor DefaultCursor
=> GdkExtensions.CursorFromName (Pinta.Resources.StandardCursors.Text);
public override Gdk.Cursor DefaultCursor { get; }

private readonly IWorkspaceService workspace;
private readonly IPaletteService palette;
Expand All @@ -118,6 +117,8 @@ public TextTool (IServiceProvider services) : base (services)
im_context.OnPreeditEnd += OnPreeditEnd;

layout = new Pinta.Core.TextLayout ();

DefaultCursor = GdkExtensions.CursorFromName (Pinta.Resources.StandardCursors.Text);
}
#endregion

Expand Down

0 comments on commit f59b5be

Please sign in to comment.