Skip to content

Commit

Permalink
Merge branch 'master' into leolost/window-clone-no-gtk
Browse files Browse the repository at this point in the history
  • Loading branch information
leolost2605 authored Feb 24, 2024
2 parents ae554e7 + 97c651e commit 2da008a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 86 deletions.
3 changes: 3 additions & 0 deletions lib/Drawing/Color.vala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ namespace Gala.Drawing {
* A class containing an RGBA color and methods for more powerful color manipulation.
*/
public class Color : GLib.Object, SettingsSerializable {
public const Gdk.RGBA TOOLTIP_BACKGROUND = { 0, 0, 0, 1};
public const Gdk.RGBA TOOLTIP_TEXT_COLOR = { 1, 1, 1, 1};

/**
* The value of the red channel, with 0 being the lowest value and 1.0 being the greatest value.
*/
Expand Down
121 changes: 36 additions & 85 deletions src/Widgets/Tooltip.vala
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,10 @@
* Clutter actor to display text in a tooltip-like component.
*/
public class Gala.Tooltip : CanvasActor {
private static Clutter.Color text_color;
private static Gtk.Border padding;
private static Gtk.StyleContext style_context;

/**
* Actor to display the Tooltip text.
*/
private Clutter.Text? text_actor = null;

/**
* Text displayed in the Tooltip.
* @see set_text
*/
private string text;
private Clutter.Text text_actor;

/**
* Maximum width of the Tooltip.
Expand All @@ -30,105 +20,66 @@ public class Gala.Tooltip : CanvasActor {
public float max_width;

construct {
text = "";
max_width = 200;

resize ();
}

private static void create_gtk_objects () {
var tooltip_widget_path = new Gtk.WidgetPath ();
var pos = tooltip_widget_path.append_type (typeof (Gtk.Window));
tooltip_widget_path.iter_set_object_name (pos, "tooltip");
tooltip_widget_path.iter_add_class (pos, Gtk.STYLE_CLASS_CSD);
tooltip_widget_path.iter_add_class (pos, Gtk.STYLE_CLASS_BACKGROUND);

style_context = new Gtk.StyleContext ();
style_context.set_path (tooltip_widget_path);

padding = style_context.get_padding (Gtk.StateFlags.NORMAL);

tooltip_widget_path.append_type (typeof (Gtk.Label));
Clutter.Color text_color = {
(uint8) Drawing.Color.TOOLTIP_TEXT_COLOR.red * uint8.MAX,
(uint8) Drawing.Color.TOOLTIP_TEXT_COLOR.green * uint8.MAX,
(uint8) Drawing.Color.TOOLTIP_TEXT_COLOR.blue * uint8.MAX,
(uint8) Drawing.Color.TOOLTIP_TEXT_COLOR.alpha * uint8.MAX,
};

var label_style_context = new Gtk.StyleContext ();
label_style_context.set_path (tooltip_widget_path);
text_actor = new Clutter.Text () {
margin_left = 6,
margin_top = 6,
margin_bottom = 6,
margin_right = 6,
ellipsize = Pango.EllipsizeMode.MIDDLE,
color = text_color
};

var text_rgba = (Gdk.RGBA) label_style_context.get_property (
Gtk.STYLE_PROPERTY_COLOR,
Gtk.StateFlags.NORMAL
);
add_child (text_actor);

text_color = Clutter.Color () {
red = (uint8) text_rgba.red * uint8.MAX,
green = (uint8) text_rgba.green * uint8.MAX,
blue = (uint8) text_rgba.blue * uint8.MAX,
alpha = (uint8) text_rgba.alpha * uint8.MAX,
};
layout_manager = new Clutter.BinLayout ();
}

public void set_text (string new_text, bool redraw = true) {
text = new_text;

if (redraw) {
resize ();
}
public void set_text (string new_text) {
text_actor.text = new_text;
}

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

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

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

if (!visible) {
return;
}

// First set the text
if (text_actor != null) {
remove_child (text_actor);
protected override void allocate (Clutter.ActorBox box) {
if (box.get_width () > max_width) {
box.set_origin (box.get_x () + ((box.get_width () - max_width) / 2), box.get_y ());
box.set_size (max_width, box.get_height ());
}

text_actor = new Clutter.Text () {
color = text_color,
x = padding.left,
y = padding.top,
ellipsize = Pango.EllipsizeMode.MIDDLE
};
text_actor.text = text;

if ((text_actor.width + padding.left + padding.right) > max_width) {
text_actor.width = max_width - padding.left - padding.right;
}

add_child (text_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;

//Failsafe that if by accident the size doesn't change we still redraw
content.invalidate ();
base.allocate (box);
}

protected override void draw (Cairo.Context ctx, int width, int height) {
if (style_context == null) {
create_gtk_objects ();
}

ctx.save ();
ctx.set_operator (Cairo.Operator.CLEAR);
ctx.paint ();
ctx.clip ();
ctx.reset_clip ();
ctx.set_operator (Cairo.Operator.OVER);

style_context.render_background (ctx, 0, 0, width, height);
var background_color = Drawing.Color.TOOLTIP_BACKGROUND;
ctx.set_source_rgba (
background_color.red,
background_color.green,
background_color.blue,
background_color.alpha
);

Drawing.Utilities.cairo_rounded_rectangle (ctx, 0, 0, width, height, 4);
ctx.fill ();

ctx.restore ();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Widgets/WindowClone.vala
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ public class Gala.WindowClone : Clutter.Actor {
close_button.opacity = show ? 255 : 0;
window_title.opacity = close_button.opacity;

window_title.set_text (window.get_title () ?? "", false);
window_title.set_text (window.get_title () ?? "");
window_title.set_max_width (dest_width - InternalUtils.scale_to_int (TITLE_MAX_WIDTH_MARGIN, scale_factor));
set_window_title_position (dest_width, dest_height, scale_factor);
}
Expand Down

0 comments on commit 2da008a

Please sign in to comment.