Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a CanvasActor #1864

Merged
merged 6 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/CanvasActor.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2024 elementary, Inc. (https://elementary.io)
* SPDX-License-Identifier: LGPL-2.0-or-later
*/

public class Gala.CanvasActor : Clutter.Actor {
private Clutter.Canvas canvas;

construct {
canvas = new Clutter.Canvas ();
content = canvas;
canvas.draw.connect ((ctx, width, height) => {
draw (ctx, width, height);
return true;
});
}

public override void resource_scale_changed () {
canvas.set_scale_factor (get_resource_scale ());
}

public override void allocate (Clutter.ActorBox box) {
base.allocate (box);
canvas.set_size ((int)box.get_width (), (int)box.get_height ());
canvas.set_scale_factor (get_resource_scale ());
}

protected virtual void draw (Cairo.Context canvas, int width, int height) { }
}
1 change: 1 addition & 0 deletions lib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ gala_lib_sources = files(
'AppCache.vala',
'AppSystem.vala',
'BackgroundManager.vala',
'CanvasActor.vala',
'CloseButton.vala',
'Constants.vala',
'DragDropAction.vala',
Expand Down
15 changes: 3 additions & 12 deletions plugins/pip/SelectionArea.vala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

public class Gala.Plugins.PIP.SelectionArea : Clutter.Actor {
public class Gala.Plugins.PIP.SelectionArea : CanvasActor {
public signal void captured (int x, int y, int width, int height);
public signal void selected (int x, int y);
public signal void closed ();
Expand All @@ -42,13 +42,6 @@ public class Gala.Plugins.PIP.SelectionArea : Clutter.Actor {
wm.get_display ().get_size (out screen_width, out screen_height);
width = screen_width;
height = screen_height;

var canvas = new Clutter.Canvas ();
canvas.set_size (screen_width, screen_height);
canvas.draw.connect (draw_area);
set_content (canvas);

canvas.invalidate ();
}

#if HAS_MUTTER45
Expand Down Expand Up @@ -159,7 +152,7 @@ public class Gala.Plugins.PIP.SelectionArea : Clutter.Actor {
height = (start_point.y - end_point.y).abs ();
}

private bool draw_area (Cairo.Context ctx) {
protected override void draw (Cairo.Context ctx, int width, int height) {
ctx.save ();

ctx.set_operator (Cairo.Operator.CLEAR);
Expand All @@ -168,7 +161,7 @@ public class Gala.Plugins.PIP.SelectionArea : Clutter.Actor {
ctx.restore ();

if (!dragging) {
return true;
return;
}

int x, y, w, h;
Expand All @@ -182,7 +175,5 @@ public class Gala.Plugins.PIP.SelectionArea : Clutter.Actor {
ctx.set_source_rgb (0.7, 0.7, 0.7);
ctx.set_line_width (1.0);
ctx.stroke ();

return true;
}
}
28 changes: 10 additions & 18 deletions src/Widgets/IconGroup.vala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Gala {
* It also decides whether to draw the container shape, a plus sign or an ellipsis.
* Lastly it also includes the drawing code for the active highlight.
*/
public class IconGroup : Clutter.Actor {
public class IconGroup : CanvasActor {
public const int SIZE = 64;

private const int PLUS_SIZE = 6;
Expand Down Expand Up @@ -47,7 +47,7 @@ namespace Gala {
set {
if (value != _scale_factor) {
_scale_factor = value;
resize_canvas ();
resize ();
}
}
}
Expand All @@ -62,10 +62,6 @@ namespace Gala {
construct {
reactive = true;

var canvas = new Clutter.Canvas ();
canvas.draw.connect (draw);
content = canvas;

drag_action = new DragDropAction (DragDropActionType.SOURCE | DragDropActionType.DESTINATION, "multitaskingview-window");
drag_action.actor_clicked.connect (() => selected ());
drag_action.drag_begin.connect (drag_begin);
Expand All @@ -80,7 +76,7 @@ namespace Gala {

add_child (icon_container);

resize_canvas ();
resize ();

#if HAS_MUTTER46
icon_container.child_removed.connect_after (redraw);
Expand All @@ -97,13 +93,11 @@ namespace Gala {
#endif
}

private bool resize_canvas () {
private void resize () {
var size = InternalUtils.scale_to_int (SIZE, scale_factor);

width = size;
height = size;

return ((Clutter.Canvas) content).set_size (size, size);
}

/**
Expand Down Expand Up @@ -206,16 +200,14 @@ namespace Gala {
* Trigger a redraw
*/
public void redraw () {
if (!resize_canvas ()) {
content.invalidate ();
}
content.invalidate ();
}

/**
* Draw the background or plus sign and do layouting. We won't lose performance here
* by relayouting in the same function, as it's only ever called when we invalidate it.
*/
private bool draw (Cairo.Context cr) {
protected override void draw (Cairo.Context cr, int cr_width, int cr_height) {
clear_effects ();
cr.set_operator (Cairo.Operator.CLEAR);
cr.paint ();
Expand All @@ -228,7 +220,7 @@ namespace Gala {
var icon = (WindowIconActor) icon_container.get_child_at_index (0);
icon.place (0, 0, 64, scale_factor);

return false;
return;
}

// more than one => we need a folder
Expand Down Expand Up @@ -289,7 +281,7 @@ namespace Gala {
if (n_windows < 1) {
if (!Meta.Prefs.get_dynamic_workspaces ()
|| workspace_index != manager.get_n_workspaces () - 1)
return false;
return;

var buffer = new Drawing.BufferSurface (scaled_size, scaled_size);
var offset = scaled_size / 2 - InternalUtils.scale_to_int (PLUS_WIDTH, scale_factor) / 2;
Expand Down Expand Up @@ -330,7 +322,7 @@ namespace Gala {
cr.set_source_surface (buffer.surface, 0, 0);
cr.paint ();

return false;
return;
}

int size;
Expand Down Expand Up @@ -391,7 +383,7 @@ namespace Gala {
}
}

return false;
return;
}

private Clutter.Actor? drag_begin (float click_x, float click_y) {
Expand Down
15 changes: 3 additions & 12 deletions src/Widgets/SelectionArea.vala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//

namespace Gala {
public class SelectionArea : Clutter.Actor {
public class SelectionArea : CanvasActor {
public signal void closed ();

public WindowManager wm { get; construct; }
Expand All @@ -43,13 +43,6 @@ namespace Gala {
wm.get_display ().get_size (out screen_width, out screen_height);
width = screen_width;
height = screen_height;

var canvas = new Clutter.Canvas ();
canvas.set_size (screen_width, screen_height);
canvas.draw.connect (draw_area);
set_content (canvas);

canvas.invalidate ();
}

#if HAS_MUTTER45
Expand Down Expand Up @@ -155,7 +148,7 @@ namespace Gala {
}.expand (end_point);
}

private bool draw_area (Cairo.Context ctx) {
protected override void draw (Cairo.Context ctx, int width, int height) {
ctx.save ();

ctx.set_operator (Cairo.Operator.CLEAR);
Expand All @@ -164,7 +157,7 @@ namespace Gala {
ctx.restore ();

if (!dragging) {
return true;
return;
}

ctx.translate (0.5, 0.5);
Expand All @@ -178,8 +171,6 @@ namespace Gala {
ctx.set_source_rgb (0.7, 0.7, 0.7);
ctx.set_line_width (1.0);
ctx.stroke ();

return true;
}
}
}
28 changes: 8 additions & 20 deletions src/Widgets/Tooltip.vala
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@
/**
* Clutter actor to display text in a tooltip-like component.
*/
public class Gala.Tooltip : Clutter.Actor {
public class Gala.Tooltip : CanvasActor {
private static Clutter.Color text_color;
private static Gtk.Border padding;
private static Gtk.StyleContext style_context;

/**
* Canvas to draw the Tooltip background.
*/
private Clutter.Canvas background_canvas;

/**
* Actor to display the Tooltip text.
*/
Expand All @@ -38,11 +33,7 @@ public class Gala.Tooltip : Clutter.Actor {
text = "";
max_width = 200;

background_canvas = new Clutter.Canvas ();
background_canvas.draw.connect (draw_background);
content = background_canvas;

draw ();
resize ();
}

private static void create_gtk_objects () {
Expand Down Expand Up @@ -79,19 +70,19 @@ public class Gala.Tooltip : Clutter.Actor {
text = new_text;

if (redraw) {
draw ();
resize ();
}
}

public void set_max_width (float new_max_width, bool redraw = true) {
max_width = new_max_width;

if (redraw) {
draw ();
resize ();
}
}

private void draw () {
private void resize () {
visible = (text.length != 0);

if (!visible) {
Expand Down Expand Up @@ -120,13 +111,12 @@ public class Gala.Tooltip : Clutter.Actor {
// Adjust the size of the tooltip to the text
width = text_actor.width + padding.left + padding.right;
height = text_actor.height + padding.top + padding.bottom;
background_canvas.set_size ((int) width, (int) height);

// And paint the background
background_canvas.invalidate ();
//Failsafe that if by accident the size doesn't change we still redraw
content.invalidate ();
}

private static bool draw_background (Cairo.Context ctx, int width, int height) {
protected override void draw (Cairo.Context ctx, int width, int height) {
if (style_context == null) {
create_gtk_objects ();
}
Expand All @@ -141,7 +131,5 @@ public class Gala.Tooltip : Clutter.Actor {
style_context.render_background (ctx, 0, 0, width, height);

ctx.restore ();

return false;
}
}
23 changes: 4 additions & 19 deletions src/Widgets/WindowClone.vala
Original file line number Diff line number Diff line change
Expand Up @@ -866,17 +866,11 @@ public class Gala.WindowClone : Clutter.Actor {
/**
* Border to show around the selected window when using keyboard navigation.
*/
private class ActiveShape : Clutter.Actor {
private class ActiveShape : CanvasActor {
private static int border_radius = -1;
private const double COLOR_OPACITY = 0.8;

private Clutter.Canvas background_canvas;

construct {
background_canvas = new Clutter.Canvas ();
background_canvas.draw.connect (draw_background);
content = background_canvas;

notify["opacity"].connect (invalidate);
}

Expand All @@ -896,16 +890,16 @@ public class Gala.WindowClone : Clutter.Actor {
}

public void invalidate () {
background_canvas.invalidate ();
content.invalidate ();
}

private bool draw_background (Cairo.Context cr, int width, int height) {
protected override void draw (Cairo.Context cr, int width, int height) {
if (border_radius == -1) {
create_gtk_objects ();
}

if (!visible || opacity == 0) {
return Clutter.EVENT_PROPAGATE;
return;
}

var color = InternalUtils.get_theme_accent_color ();
Expand All @@ -918,15 +912,6 @@ public class Gala.WindowClone : Clutter.Actor {
Drawing.Utilities.cairo_rounded_rectangle (cr, 0, 0, width, height, border_radius);
cr.set_source_rgba (color.red, color.green, color.blue, COLOR_OPACITY);
cr.fill ();

return Clutter.EVENT_PROPAGATE;
}

public override void allocate (Clutter.ActorBox box) {
base.allocate (box);

background_canvas.set_size ((int) box.get_width (), (int) box.get_height ());
invalidate ();
}
}
}
Loading