Tooltip: Dont use Gtk StyleContext (#1865)

This commit is contained in:
Leonhard 2024-02-25 00:30:27 +01:00 committed by GitHub
parent 4223c8970c
commit 97c651ec3e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 91 deletions

View File

@ -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.
*/

View File

@ -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.
@ -30,97 +20,49 @@ 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));
var label_style_context = new Gtk.StyleContext ();
label_style_context.set_path (tooltip_widget_path);
var text_rgba = (Gdk.RGBA) label_style_context.get_property (
Gtk.STYLE_PROPERTY_COLOR,
Gtk.StateFlags.NORMAL
);
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,
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,
};
}
public void set_text (string new_text, bool redraw = true) {
text = new_text;
if (redraw) {
resize ();
}
}
public void set_max_width (float new_max_width, bool redraw = true) {
max_width = new_max_width;
if (redraw) {
resize ();
}
}
private void resize () {
visible = (text.length != 0);
if (!visible) {
return;
}
// First set the text
if (text_actor != null) {
remove_child (text_actor);
}
text_actor = new Clutter.Text () {
color = text_color,
x = padding.left,
y = padding.top,
ellipsize = Pango.EllipsizeMode.MIDDLE
margin_left = 6,
margin_top = 6,
margin_bottom = 6,
margin_right = 6,
ellipsize = Pango.EllipsizeMode.MIDDLE,
color = text_color
};
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;
layout_manager = new Clutter.BinLayout ();
}
//Failsafe that if by accident the size doesn't change we still redraw
content.invalidate ();
public void set_text (string new_text) {
text_actor.text = new_text;
}
public void set_max_width (float new_max_width) {
max_width = new_max_width;
queue_relayout ();
}
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 ());
}
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 ();
@ -128,7 +70,16 @@ public class Gala.Tooltip : CanvasActor {
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 ();
}

View File

@ -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);
}