summaryrefslogtreecommitdiff
path: root/lib/astal/gtk3/src/widget
diff options
context:
space:
mode:
Diffstat (limited to 'lib/astal/gtk3/src/widget')
-rw-r--r--lib/astal/gtk3/src/widget/box.vala53
-rw-r--r--lib/astal/gtk3/src/widget/button.vala111
-rw-r--r--lib/astal/gtk3/src/widget/centerbox.vala55
-rw-r--r--lib/astal/gtk3/src/widget/circularprogress.vala206
-rw-r--r--lib/astal/gtk3/src/widget/eventbox.vala73
-rw-r--r--lib/astal/gtk3/src/widget/icon.vala115
-rw-r--r--lib/astal/gtk3/src/widget/label.vala24
-rw-r--r--lib/astal/gtk3/src/widget/levelbar.vala16
-rw-r--r--lib/astal/gtk3/src/widget/overlay.vala65
-rw-r--r--lib/astal/gtk3/src/widget/scrollable.vala48
-rw-r--r--lib/astal/gtk3/src/widget/slider.vala94
-rw-r--r--lib/astal/gtk3/src/widget/stack.vala40
-rw-r--r--lib/astal/gtk3/src/widget/widget.vala157
-rw-r--r--lib/astal/gtk3/src/widget/window.vala293
14 files changed, 1350 insertions, 0 deletions
diff --git a/lib/astal/gtk3/src/widget/box.vala b/lib/astal/gtk3/src/widget/box.vala
new file mode 100644
index 0000000..d049161
--- /dev/null
+++ b/lib/astal/gtk3/src/widget/box.vala
@@ -0,0 +1,53 @@
+public class Astal.Box : Gtk.Box {
+ /**
+ * Corresponds to [[email protected] :orientation].
+ */
+ [CCode (notify = false)]
+ public bool vertical {
+ get { return orientation == Gtk.Orientation.VERTICAL; }
+ set { orientation = value ? Gtk.Orientation.VERTICAL : Gtk.Orientation.HORIZONTAL; }
+ }
+
+ public List<weak Gtk.Widget> children {
+ set { _set_children(value); }
+ owned get { return get_children(); }
+ }
+
+ public new Gtk.Widget child {
+ owned get { return _get_child(); }
+ set { _set_child(value); }
+ }
+
+ construct {
+ notify["orientation"].connect(() => {
+ notify_property("vertical");
+ });
+ }
+
+ private void _set_child(Gtk.Widget child) {
+ var list = new List<weak Gtk.Widget>();
+ list.append(child);
+ _set_children(list);
+ }
+
+ private Gtk.Widget? _get_child() {
+ foreach(var child in get_children())
+ return child;
+
+ return null;
+ }
+
+ private void _set_children(List<weak Gtk.Widget> arr) {
+ foreach(var child in get_children()) {
+ remove(child);
+ }
+
+ foreach(var child in arr)
+ add(child);
+ }
+
+ public Box(bool vertical, List<weak Gtk.Widget> children) {
+ this.vertical = vertical;
+ _set_children(children);
+ }
+}
diff --git a/lib/astal/gtk3/src/widget/button.vala b/lib/astal/gtk3/src/widget/button.vala
new file mode 100644
index 0000000..2d3095a
--- /dev/null
+++ b/lib/astal/gtk3/src/widget/button.vala
@@ -0,0 +1,111 @@
+/**
+ * This button has no extra functionality on top if its base [[email protected]] class.
+ *
+ * The purpose of this Button subclass is to have a destructable
+ * struct as the argument in GJS event handlers.
+ */
+public class Astal.Button : Gtk.Button {
+ public signal void hover (HoverEvent event);
+ public signal void hover_lost (HoverEvent event);
+ public signal void click (ClickEvent event);
+ public signal void click_release (ClickEvent event);
+ public signal void scroll (ScrollEvent event);
+
+ construct {
+ add_events(Gdk.EventMask.SCROLL_MASK);
+ add_events(Gdk.EventMask.SMOOTH_SCROLL_MASK);
+
+ enter_notify_event.connect((self, event) => {
+ hover(HoverEvent(event) { lost = false });
+ });
+
+ leave_notify_event.connect((self, event) => {
+ hover_lost(HoverEvent(event) { lost = true });
+ });
+
+ button_press_event.connect((event) => {
+ click(ClickEvent(event) { release = false });
+ });
+
+ button_release_event.connect((event) => {
+ click_release(ClickEvent(event) { release = true });
+ });
+
+ scroll_event.connect((event) => {
+ scroll(ScrollEvent(event));
+ });
+ }
+}
+
+public enum Astal.MouseButton {
+ PRIMARY = 1,
+ MIDDLE = 2,
+ SECONDARY = 3,
+ BACK = 4,
+ FORWARD = 5,
+}
+
+/**
+ * Struct for [[email protected]]
+ */
+public struct Astal.ClickEvent {
+ bool release;
+ uint time;
+ double x;
+ double y;
+ Gdk.ModifierType modifier;
+ MouseButton button;
+
+ public ClickEvent(Gdk.EventButton event) {
+ this.time = event.time;
+ this.x = event.x;
+ this.y = event.y;
+ this.button = (MouseButton)event.button;
+ this.modifier = event.state;
+ }
+}
+
+/**
+ * Struct for [[email protected]]
+ */
+public struct Astal.HoverEvent {
+ bool lost;
+ uint time;
+ double x;
+ double y;
+ Gdk.ModifierType modifier;
+ Gdk.CrossingMode mode;
+ Gdk.NotifyType detail;
+
+ public HoverEvent(Gdk.EventCrossing event) {
+ this.time = event.time;
+ this.x = event.x;
+ this.y = event.y;
+ this.modifier = event.state;
+ this.mode = event.mode;
+ this.detail = event.detail;
+ }
+}
+
+/**
+ * Struct for [[email protected]]
+ */
+public struct Astal.ScrollEvent {
+ uint time;
+ double x;
+ double y;
+ Gdk.ModifierType modifier;
+ Gdk.ScrollDirection direction;
+ double delta_x;
+ double delta_y;
+
+ public ScrollEvent(Gdk.EventScroll event) {
+ this.time = event.time;
+ this.x = event.x;
+ this.y = event.y;
+ this.modifier = event.state;
+ this.direction = event.direction;
+ this.delta_x = event.delta_x;
+ this.delta_y = event.delta_y;
+ }
+}
diff --git a/lib/astal/gtk3/src/widget/centerbox.vala b/lib/astal/gtk3/src/widget/centerbox.vala
new file mode 100644
index 0000000..d74a2c4
--- /dev/null
+++ b/lib/astal/gtk3/src/widget/centerbox.vala
@@ -0,0 +1,55 @@
+public class Astal.CenterBox : Gtk.Box {
+ /**
+ * Corresponds to [[email protected] :orientation].
+ */
+ [CCode (notify = false)]
+ public bool vertical {
+ get { return orientation == Gtk.Orientation.VERTICAL; }
+ set { orientation = value ? Gtk.Orientation.VERTICAL : Gtk.Orientation.HORIZONTAL; }
+ }
+
+ construct {
+ notify["orientation"].connect(() => {
+ notify_property("vertical");
+ });
+ }
+
+ static construct {
+ set_css_name("centerbox");
+ }
+
+ private Gtk.Widget _start_widget;
+ public Gtk.Widget start_widget {
+ get { return _start_widget; }
+ set {
+ if (_start_widget != null)
+ remove(_start_widget);
+
+ if (value != null)
+ pack_start(value, true, true, 0);
+ }
+ }
+
+ private Gtk.Widget _end_widget;
+ public Gtk.Widget end_widget {
+ get { return _end_widget; }
+ set {
+ if (_end_widget != null)
+ remove(_end_widget);
+
+ if (value != null)
+ pack_end(value, true, true, 0);
+ }
+ }
+
+ public Gtk.Widget center_widget {
+ get { return get_center_widget(); }
+ set {
+ if (center_widget != null)
+ remove(center_widget);
+
+ if (value != null)
+ set_center_widget(value);
+ }
+ }
+}
diff --git a/lib/astal/gtk3/src/widget/circularprogress.vala b/lib/astal/gtk3/src/widget/circularprogress.vala
new file mode 100644
index 0000000..a3ecdf1
--- /dev/null
+++ b/lib/astal/gtk3/src/widget/circularprogress.vala
@@ -0,0 +1,206 @@
+/**
+ * CircularProgress is a subclass of [[email protected]] which provides a circular progress bar
+ * with customizable properties such as starting and ending points,
+ * progress value, and visual features like rounded ends and inversion of progress direction.
+ */
+public class Astal.CircularProgress : Gtk.Bin {
+ /**
+ * The starting point of the progress circle,
+ * where 0 represents 3 o'clock position or 0° degrees and 1 represents 360°.
+ */
+ public double start_at { get; set; }
+
+ /**
+ * The cutoff point of the background color of the progress circle.
+ */
+ public double end_at { get; set; }
+
+ /**
+ * The value which determines the arc of the drawn foreground color.
+ * Should be a value between 0 and 1.
+ */
+ public double value { get; set; }
+
+ /**
+ * Inverts the progress direction, making it draw counterclockwise.
+ */
+ public bool inverted { get; set; }
+
+ /**
+ * Renders rounded ends at both the start and the end of the progress bar.
+ */
+ public bool rounded { get; set; }
+
+ construct {
+ notify["start-at"].connect(queue_draw);
+ notify["end-at"].connect(queue_draw);
+ notify["value"].connect(queue_draw);
+ notify["inverted"].connect(queue_draw);
+ notify["rounded"].connect(queue_draw);
+ notify["child"].connect(queue_draw);
+ }
+
+ static construct {
+ set_css_name("circular-progress");
+ }
+
+ public override void get_preferred_height(out int minh, out int nath) {
+ var val = get_style_context().get_property("min-height", Gtk.StateFlags.NORMAL);
+ if (val.get_int() <= 0) {
+ minh = 40;
+ nath = 40;
+ }
+
+ minh = val.get_int();
+ nath = val.get_int();
+ }
+
+ public override void get_preferred_width(out int minw, out int natw) {
+ var val = get_style_context().get_property("min-width", Gtk.StateFlags.NORMAL);
+ if (val.get_int() <= 0) {
+ minw = 40;
+ natw = 40;
+ }
+
+ minw = val.get_int();
+ natw = val.get_int();
+ }
+
+ private double to_radian(double percentage) {
+ percentage = Math.floor(percentage * 100);
+ return (percentage / 100) * (2 * Math.PI);
+ }
+
+ private bool is_full_circle(double start, double end, double epsilon = 1e-10) {
+ // Ensure that start and end are between 0 and 1
+ start = (start % 1 + 1) % 1;
+ end = (end % 1 + 1) % 1;
+
+ // Check if the difference between start and end is close to 1
+ return Math.fabs(start - end) <= epsilon;
+ }
+
+ private double scale_arc_value(double start, double end, double value) {
+ // Ensure that start and end are between 0 and 1
+ start = (start % 1 + 1) % 1;
+ end = (end % 1 + 1) % 1;
+
+ // Calculate the length of the arc
+ var arc_length = end - start;
+ if (arc_length < 0)
+ arc_length += 1; // Adjust for circular representation
+
+ // Calculate the position on the arc based on the percentage value
+ var scaled = arc_length + value;
+
+ // Ensure the position is between 0 and 1
+ return (scaled % 1 + 1) % 1;
+ }
+
+ private double min(double[] arr) {
+ double min = arr[0];
+ foreach(var i in arr)
+ if (min > i) min = i;
+ return min;
+ }
+
+ private double max(double[] arr) {
+ double max = arr[0];
+ foreach(var i in arr)
+ if (max < i) max = i;
+ return max;
+ }
+
+ public override bool draw(Cairo.Context cr) {
+ Gtk.Allocation allocation;
+ get_allocation(out allocation);
+
+ var styles = get_style_context();
+ var width = allocation.width;
+ var height = allocation.height;
+ var thickness = styles.get_property("font-size", Gtk.StateFlags.NORMAL).get_double();
+ var margin = styles.get_margin(Gtk.StateFlags.NORMAL);
+ var fg = styles.get_color(Gtk.StateFlags.NORMAL);
+ var bg = styles.get_background_color(Gtk.StateFlags.NORMAL);
+
+ var bg_stroke = thickness + min({margin.bottom, margin.top, margin.left, margin.right});
+ var fg_stroke = thickness;
+ var radius = min({width, height}) / 2.0 - max({bg_stroke, fg_stroke}) / 2.0;
+ var center_x = width / 2;
+ var center_y = height / 2;
+
+ var start_background = to_radian(start_at);
+ var end_background = to_radian(end_at);
+ var ranged_value = value + start_at;
+
+ var is_circle = is_full_circle(this.start_at, this.end_at);
+
+ if (is_circle) {
+ // Redefine end_draw in radius to create an accurate full circle
+ end_background = start_background + 2 * Math.PI;
+ ranged_value = to_radian(value);
+ } else {
+ // Range the value for the arc shape
+ ranged_value = to_radian(scale_arc_value(
+ start_at,
+ end_at,
+ value
+ ));
+ }
+
+ double start_progress, end_progress;
+
+ if (inverted) {
+ start_progress = end_background - ranged_value;
+ end_progress = end_background;
+ } else {
+ start_progress = start_background;
+ end_progress = start_background + ranged_value;
+ }
+
+ // Draw background
+ cr.set_source_rgba(bg.red, bg.green, bg.blue, bg.alpha);
+ cr.arc(center_x, center_y, radius, start_background, end_background);
+ cr.set_line_width(bg_stroke);
+ cr.stroke();
+
+ // Draw rounded background ends
+ if (rounded) {
+ var start_x = center_x + Math.cos(start_background) * radius;
+ var start_y = center_y + Math.sin(start_background) * radius;
+ var end_x = center_x + Math.cos(end_background) * radius;
+ var end_y = center_y + Math.sin(end_background) * radius;
+ cr.set_line_width(0);
+ cr.arc(start_x, start_y, bg_stroke / 2, 0, 0 - 0.01);
+ cr.fill();
+ cr.arc(end_x, end_y, bg_stroke / 2, 0, 0 - 0.01);
+ cr.fill();
+ }
+
+ // Draw progress
+ cr.set_source_rgba(fg.red, fg.green, fg.blue, fg.alpha);
+ cr.arc(center_x, center_y, radius, start_progress, end_progress);
+ cr.set_line_width(fg_stroke);
+ cr.stroke();
+
+ // Draw rounded progress ends
+ if (rounded) {
+ var start_x = center_x + Math.cos(start_progress) * radius;
+ var start_y = center_y + Math.sin(start_progress) * radius;
+ var end_x = center_x + Math.cos(end_progress) * radius;
+ var end_y = center_y + Math.sin(end_progress) * radius;
+ cr.set_line_width(0);
+ cr.arc(start_x, start_y, fg_stroke / 2, 0, 0 - 0.01);
+ cr.fill();
+ cr.arc(end_x, end_y, fg_stroke / 2, 0, 0 - 0.01);
+ cr.fill();
+ }
+
+ if (get_child() != null) {
+ get_child().size_allocate(allocation);
+ propagate_draw(get_child(), cr);
+ }
+
+ return true;
+ }
+}
diff --git a/lib/astal/gtk3/src/widget/eventbox.vala b/lib/astal/gtk3/src/widget/eventbox.vala
new file mode 100644
index 0000000..0b588e9
--- /dev/null
+++ b/lib/astal/gtk3/src/widget/eventbox.vala
@@ -0,0 +1,73 @@
+/**
+ * EventBox is a [[email protected]] subclass which is meant to fix an issue with its
+ * [[email protected]::enter_notify_event] and [[email protected]::leave_notify_event] when nesting EventBoxes
+ *
+ * Its css selector is `eventbox`.
+ */
+public class Astal.EventBox : Gtk.EventBox {
+ public signal void hover (HoverEvent event);
+ public signal void hover_lost (HoverEvent event);
+ public signal void click (ClickEvent event);
+ public signal void click_release (ClickEvent event);
+ public signal void scroll (ScrollEvent event);
+ public signal void motion (MotionEvent event);
+
+ static construct {
+ set_css_name("eventbox");
+ }
+
+ construct {
+ add_events(Gdk.EventMask.SCROLL_MASK);
+ add_events(Gdk.EventMask.SMOOTH_SCROLL_MASK);
+ add_events(Gdk.EventMask.POINTER_MOTION_MASK);
+
+ enter_notify_event.connect((self, event) => {
+ if (event.window == self.get_window() &&
+ event.detail != Gdk.NotifyType.INFERIOR) {
+ this.set_state_flags(Gtk.StateFlags.PRELIGHT, false);
+ hover(HoverEvent(event) { lost = false });
+ }
+ });
+
+ leave_notify_event.connect((self, event) => {
+ if (event.window == self.get_window() &&
+ event.detail != Gdk.NotifyType.INFERIOR) {
+ this.unset_state_flags(Gtk.StateFlags.PRELIGHT);
+ hover_lost(HoverEvent(event) { lost = true });
+ }
+ });
+
+ button_press_event.connect((event) => {
+ click(ClickEvent(event) { release = false });
+ });
+
+ button_release_event.connect((event) => {
+ click_release(ClickEvent(event) { release = true });
+ });
+
+ scroll_event.connect((event) => {
+ scroll(ScrollEvent(event));
+ });
+
+ motion_notify_event.connect((event) => {
+ motion(MotionEvent(event));
+ });
+ }
+}
+
+/**
+ * Struct for [[email protected]]
+ */
+public struct Astal.MotionEvent {
+ uint time;
+ double x;
+ double y;
+ Gdk.ModifierType modifier;
+
+ public MotionEvent(Gdk.EventMotion event) {
+ this.time = event.time;
+ this.x = event.x;
+ this.y = event.y;
+ this.modifier = event.state;
+ }
+}
diff --git a/lib/astal/gtk3/src/widget/icon.vala b/lib/astal/gtk3/src/widget/icon.vala
new file mode 100644
index 0000000..9a20359
--- /dev/null
+++ b/lib/astal/gtk3/src/widget/icon.vala
@@ -0,0 +1,115 @@
+/**
+ * [[email protected]] subclass meant to be used only for icons.
+ *
+ * It's size is calculated from `font-size` css property.
+ * Its css selector is `icon`.
+ */
+public class Astal.Icon : Gtk.Image {
+ private IconType type = IconType.NAMED;
+ private double size { get; set; default = 14; }
+
+ public new Gdk.Pixbuf pixbuf { get; set; }
+ public GLib.Icon g_icon { get; set; }
+
+ /**
+ * Either a named icon or a path to a file.
+ */
+ public string icon { get; set; default = ""; }
+
+ public static Gtk.IconInfo? lookup_icon(string icon) {
+ var theme = Gtk.IconTheme.get_default();
+ return theme.lookup_icon(icon, 16, Gtk.IconLookupFlags.USE_BUILTIN);
+ }
+
+ private async void display_icon() {
+ switch(type) {
+ case IconType.NAMED:
+ icon_name = icon;
+ pixel_size = (int)size;
+ break;
+ case IconType.FILE:
+ try {
+ var file = File.new_for_path(icon);
+ var stream = yield file.read_async();
+ var pb = yield new Gdk.Pixbuf.from_stream_at_scale_async(
+ stream,
+ (int)size * scale_factor,
+ (int)size * scale_factor,
+ true,
+ null
+ );
+ var cs = Gdk.cairo_surface_create_from_pixbuf(pb, 0, this.get_window());
+ set_from_surface(cs);
+ } catch (Error err) {
+ printerr(err.message);
+ }
+ break;
+ case IconType.PIXBUF:
+ var pb_scaled = pixbuf.scale_simple(
+ (int)size * scale_factor,
+ (int)size * scale_factor,
+ Gdk.InterpType.BILINEAR
+ );
+ if (pb_scaled != null) {
+ var cs = Gdk.cairo_surface_create_from_pixbuf(pb_scaled, 0, this.get_window());
+ set_from_surface(cs);
+ }
+ break;
+ case IconType.GICON:
+ pixel_size = (int)size;
+ gicon = g_icon;
+ break;
+
+ }
+ }
+
+ static construct {
+ set_css_name("icon");
+ }
+
+ construct {
+ notify["icon"].connect(() => {
+ if(FileUtils.test(icon, GLib.FileTest.EXISTS))
+ type = IconType.FILE;
+ else if (lookup_icon(icon) != null)
+ type = IconType.NAMED;
+ else {
+ type = IconType.NAMED;
+ warning("cannot assign %s as icon, "+
+ "it is not a file nor a named icon", icon);
+ }
+ display_icon.begin();
+ });
+
+ notify["pixbuf"].connect(() => {
+ type = IconType.PIXBUF;
+ display_icon.begin();
+ });
+
+ notify["g-icon"].connect(() => {
+ type = IconType.GICON;
+ display_icon.begin();
+ });
+
+ size_allocate.connect(() => {
+ size = get_style_context()
+ .get_property("font-size", Gtk.StateFlags.NORMAL).get_double();
+
+ display_icon.begin();
+ });
+
+ get_style_context().changed.connect(() => {
+ size = get_style_context()
+ .get_property("font-size", Gtk.StateFlags.NORMAL).get_double();
+
+ display_icon.begin();
+ });
+ }
+}
+
+private enum Astal.IconType {
+ NAMED,
+ FILE,
+ PIXBUF,
+ GICON,
+}
diff --git a/lib/astal/gtk3/src/widget/label.vala b/lib/astal/gtk3/src/widget/label.vala
new file mode 100644
index 0000000..899cba9
--- /dev/null
+++ b/lib/astal/gtk3/src/widget/label.vala
@@ -0,0 +1,24 @@
+using Pango;
+
+public class Astal.Label : Gtk.Label {
+ /**
+ * Shortcut for setting [[email protected]:ellipsize] to [[email protected]]
+ */
+ public bool truncate {
+ set { ellipsize = value ? EllipsizeMode.END : EllipsizeMode.NONE; }
+ get { return ellipsize == EllipsizeMode.END; }
+ }
+
+ /**
+ * Shortcut for setting [[email protected]:justify] to [[email protected]]
+ */
+ public new bool justify_fill {
+ set { justify = value ? Gtk.Justification.FILL : Gtk.Justification.LEFT; }
+ get { return justify == Gtk.Justification.FILL; }
+ }
+
+ construct {
+ notify["ellipsize"].connect(() => notify_property("truncate"));
+ notify["justify"].connect(() => notify_property("justify_fill"));
+ }
+}
diff --git a/lib/astal/gtk3/src/widget/levelbar.vala b/lib/astal/gtk3/src/widget/levelbar.vala
new file mode 100644
index 0000000..3e98afb
--- /dev/null
+++ b/lib/astal/gtk3/src/widget/levelbar.vala
@@ -0,0 +1,16 @@
+public class Astal.LevelBar : Gtk.LevelBar {
+ /**
+ * Corresponds to [[email protected] :orientation].
+ */
+ [CCode (notify = false)]
+ public bool vertical {
+ get { return orientation == Gtk.Orientation.VERTICAL; }
+ set { orientation = value ? Gtk.Orientation.VERTICAL : Gtk.Orientation.HORIZONTAL; }
+ }
+
+ construct {
+ notify["orientation"].connect(() => {
+ notify_property("vertical");
+ });
+ }
+}
diff --git a/lib/astal/gtk3/src/widget/overlay.vala b/lib/astal/gtk3/src/widget/overlay.vala
new file mode 100644
index 0000000..ed5f03b
--- /dev/null
+++ b/lib/astal/gtk3/src/widget/overlay.vala
@@ -0,0 +1,65 @@
+public class Astal.Overlay : Gtk.Overlay {
+ public bool pass_through { get; set; }
+
+ /**
+ * First [[email protected]:overlays] element.
+ *
+ * WARNING: setting this value will remove every overlay but the first.
+ */
+ public Gtk.Widget? overlay {
+ get { return overlays.nth_data(0); }
+ set {
+ foreach (var ch in get_children()) {
+ if (ch != child)
+ remove(ch);
+ }
+
+ if (value != null)
+ add_overlay(value);
+ }
+ }
+
+ /**
+ * Sets the overlays of this Overlay. [[email protected]_overlay].
+ */
+ public List<weak Gtk.Widget> overlays {
+ owned get { return get_children(); }
+ set {
+ foreach (var ch in get_children()) {
+ if (ch != child)
+ remove(ch);
+ }
+
+ foreach (var ch in value)
+ add_overlay(ch);
+ }
+ }
+
+ public new Gtk.Widget? child {
+ get { return get_child(); }
+ set {
+ var ch = get_child();
+ if (ch != null)
+ remove(ch);
+
+ if (value != null)
+ add(value);
+ }
+ }
+
+ construct {
+ notify["pass-through"].connect(() => {
+ update_pass_through();
+ });
+ }
+
+ private void update_pass_through() {
+ foreach (var child in get_children())
+ set_overlay_pass_through(child, pass_through);
+ }
+
+ public new void add_overlay(Gtk.Widget widget) {
+ base.add_overlay(widget);
+ set_overlay_pass_through(widget, pass_through);
+ }
+}
diff --git a/lib/astal/gtk3/src/widget/scrollable.vala b/lib/astal/gtk3/src/widget/scrollable.vala
new file mode 100644
index 0000000..57a440c
--- /dev/null
+++ b/lib/astal/gtk3/src/widget/scrollable.vala
@@ -0,0 +1,48 @@
+/**
+ * Subclass of [[email protected]] which has its policy default to
+ *
+ * Its css selector is `scrollable`.
+ * Its child getter returns the child of the inner
+ * [[email protected]], instead of the viewport.
+ */
+public class Astal.Scrollable : Gtk.ScrolledWindow {
+ private Gtk.PolicyType _hscroll = Gtk.PolicyType.AUTOMATIC;
+ private Gtk.PolicyType _vscroll = Gtk.PolicyType.AUTOMATIC;
+
+ public Gtk.PolicyType hscroll {
+ get { return _hscroll; }
+ set {
+ _hscroll = value;
+ set_policy(value, vscroll);
+ }
+ }
+
+ public Gtk.PolicyType vscroll {
+ get { return _vscroll; }
+ set {
+ _vscroll = value;
+ set_policy(hscroll, value);
+ }
+ }
+
+ static construct {
+ set_css_name("scrollable");
+ }
+
+ construct {
+ if (hadjustment != null)
+ hadjustment = new Gtk.Adjustment(0,0,0,0,0,0);
+
+ if (vadjustment != null)
+ vadjustment = new Gtk.Adjustment(0,0,0,0,0,0);
+ }
+
+ public new Gtk.Widget get_child() {
+ var ch = base.get_child();
+ if (ch is Gtk.Viewport) {
+ return ch.get_child();
+ }
+ return ch;
+ }
+}
diff --git a/lib/astal/gtk3/src/widget/slider.vala b/lib/astal/gtk3/src/widget/slider.vala
new file mode 100644
index 0000000..97cfb69
--- /dev/null
+++ b/lib/astal/gtk3/src/widget/slider.vala
@@ -0,0 +1,94 @@
+/**
+ * Subclass of [[email protected]] which adds a signal and property for the drag state.
+ */
+public class Astal.Slider : Gtk.Scale {
+ /**
+ * Corresponds to [[email protected] :orientation].
+ */
+ [CCode (notify = false)]
+ public bool vertical {
+ get { return orientation == Gtk.Orientation.VERTICAL; }
+ set { orientation = value ? Gtk.Orientation.VERTICAL : Gtk.Orientation.HORIZONTAL; }
+ }
+
+ /**
+ * Emitted when the user drags the slider or uses keyboard arrows and its value changes.
+ */
+ public signal void dragged();
+
+ construct {
+ draw_value = false;
+
+ if (adjustment == null)
+ adjustment = new Gtk.Adjustment(0,0,0,0,0,0);
+
+ if (max == 0 && min == 0) {
+ max = 1;
+ }
+
+ if (step == 0) {
+ step = 0.05;
+ }
+
+ notify["orientation"].connect(() => {
+ notify_property("vertical");
+ });
+
+ button_press_event.connect(() => { dragging = true; });
+ key_press_event.connect(() => { dragging = true; });
+ button_release_event.connect(() => { dragging = false; });
+ key_release_event.connect(() => { dragging = false; });
+ scroll_event.connect((event) => {
+ dragging = true;
+ if (event.delta_y > 0)
+ value -= step;
+ else
+ value += step;
+ dragging = false;
+ });
+
+ value_changed.connect(() => {
+ if (dragging)
+ dragged();
+ });
+ }
+
+ /**
+ * `true` when the user drags the slider or uses keyboard arrows.
+ */
+ public bool dragging { get; private set; }
+
+ /**
+ * Value of this slider. Defaults to `0`.
+ */
+ public double value {
+ get { return adjustment.value; }
+ set { if (!dragging) adjustment.value = value; }
+ }
+
+ /**
+ * Minimum possible value of this slider. Defaults to `0`.
+ */
+ public double min {
+ get { return adjustment.lower; }
+ set { adjustment.lower = value; }
+ }
+
+ /**
+ * Maximum possible value of this slider. Defaults to `1`.
+ */
+ public double max {
+ get { return adjustment.upper; }
+ set { adjustment.upper = value; }
+ }
+
+ /**
+ * Size of step increments. Defaults to `0.05`.
+ */
+ public double step {
+ get { return adjustment.step_increment; }
+ set { adjustment.step_increment = value; }
+ }
+
+ // TODO: marks
+}
diff --git a/lib/astal/gtk3/src/widget/stack.vala b/lib/astal/gtk3/src/widget/stack.vala
new file mode 100644
index 0000000..4e856a6
--- /dev/null
+++ b/lib/astal/gtk3/src/widget/stack.vala
@@ -0,0 +1,40 @@
+/**
+ * Subclass of [[email protected]] that has a children setter which
+ * invokes [[email protected]_named] with the child's [[email protected]:name] property.
+ */
+public class Astal.Stack : Gtk.Stack {
+ /**
+ * Same as [[email protected]:visible-child-name].
+ */
+ [CCode (notify = false)]
+ public string shown {
+ get { return visible_child_name; }
+ set { visible_child_name = value; }
+ }
+
+ public List<weak Gtk.Widget> children {
+ set { _set_children(value); }
+ owned get { return get_children(); }
+ }
+
+ private void _set_children(List<weak Gtk.Widget> arr) {
+ foreach(var child in get_children()) {
+ remove(child);
+ }
+
+ var i = 0;
+ foreach(var child in arr) {
+ if (child.name != null) {
+ add_named(child, child.name);
+ } else {
+ add_named(child, (++i).to_string());
+ }
+ }
+ }
+
+ construct {
+ notify["visible_child_name"].connect(() => {
+ notify_property("shown");
+ });
+ }
+}
diff --git a/lib/astal/gtk3/src/widget/widget.vala b/lib/astal/gtk3/src/widget/widget.vala
new file mode 100644
index 0000000..2506bc8
--- /dev/null
+++ b/lib/astal/gtk3/src/widget/widget.vala
@@ -0,0 +1,157 @@
+namespace Astal {
+private class Css {
+ private static HashTable<Gtk.Widget, Gtk.CssProvider> _providers;
+ public static HashTable<Gtk.Widget, Gtk.CssProvider> providers {
+ get {
+ if (_providers == null) {
+ _providers = new HashTable<Gtk.Widget, Gtk.CssProvider>(
+ (w) => (uint)w,
+ (a, b) => a == b);
+ }
+
+ return _providers;
+ }
+ }
+}
+
+private void remove_provider(Gtk.Widget widget) {
+ var providers = Css.providers;
+
+ if (providers.contains(widget)) {
+ var p = providers.get(widget);
+ widget.get_style_context().remove_provider(p);
+ providers.remove(widget);
+ p.dispose();
+ }
+}
+
+public void widget_set_css(Gtk.Widget widget, string css) {
+ var providers = Css.providers;
+
+ if (providers.contains(widget)) {
+ remove_provider(widget);
+ } else {
+ widget.destroy.connect(() => {
+ remove_provider(widget);
+ });
+ }
+
+ var style = !css.contains("{") || !css.contains("}")
+ ? "* { ".concat(css, "}") : css;
+
+ var p = new Gtk.CssProvider();
+ widget.get_style_context()
+ .add_provider(p, Gtk.STYLE_PROVIDER_PRIORITY_USER);
+
+ try {
+ p.load_from_data(style, style.length);
+ providers.set(widget, p);
+ } catch (Error err) {
+ warning(err.message);
+ }
+}
+
+public string widget_get_css(Gtk.Widget widget) {
+ var providers = Css.providers;
+
+ if (providers.contains(widget))
+ return providers.get(widget).to_string();
+
+ return "";
+}
+
+public void widget_set_class_names(Gtk.Widget widget, string[] class_names) {
+ foreach (var name in widget_get_class_names(widget))
+ widget_toggle_class_name(widget, name, false);
+
+ foreach (var name in class_names)
+ widget_toggle_class_name(widget, name, true);
+}
+
+public List<weak string> widget_get_class_names(Gtk.Widget widget) {
+ return widget.get_style_context().list_classes();
+}
+
+public void widget_toggle_class_name(
+ Gtk.Widget widget,
+ string class_name,
+ bool condition = true
+) {
+ var c = widget.get_style_context();
+ if (condition)
+ c.add_class(class_name);
+ else
+ c.remove_class(class_name);
+}
+
+private class Cursor {
+ private static HashTable<Gtk.Widget, string> _cursors;
+ public static HashTable<Gtk.Widget, string> cursors {
+ get {
+ if (_cursors == null) {
+ _cursors = new HashTable<Gtk.Widget, string>(
+ (w) => (uint)w,
+ (a, b) => a == b);
+ }
+ return _cursors;
+ }
+ }
+}
+
+private void widget_setup_cursor(Gtk.Widget widget) {
+ widget.add_events(Gdk.EventMask.ENTER_NOTIFY_MASK);
+ widget.add_events(Gdk.EventMask.LEAVE_NOTIFY_MASK);
+ widget.enter_notify_event.connect(() => {
+ widget.get_window().set_cursor(
+ new Gdk.Cursor.from_name(
+ Gdk.Display.get_default(),
+ Cursor.cursors.get(widget)));
+ return false;
+ });
+ widget.leave_notify_event.connect(() => {
+ widget.get_window().set_cursor(
+ new Gdk.Cursor.from_name(
+ Gdk.Display.get_default(),
+ "default"));
+ return false;
+ });
+ widget.destroy.connect(() => {
+ if (Cursor.cursors.contains(widget))
+ Cursor.cursors.remove(widget);
+ });
+}
+
+public void widget_set_cursor(Gtk.Widget widget, string cursor) {
+ if (!Cursor.cursors.contains(widget))
+ widget_setup_cursor(widget);
+
+ Cursor.cursors.set(widget, cursor);
+}
+
+public string widget_get_cursor(Gtk.Widget widget) {
+ return Cursor.cursors.get(widget);
+}
+
+private class ClickThrough {
+ private static HashTable<Gtk.Widget, bool> _click_through;
+ public static HashTable<Gtk.Widget, bool> click_through {
+ get {
+ if (_click_through == null) {
+ _click_through = new HashTable<Gtk.Widget, bool>(
+ (w) => (uint)w,
+ (a, b) => a == b);
+ }
+ return _click_through;
+ }
+ }
+}
+
+public void widget_set_click_through(Gtk.Widget widget, bool click_through) {
+ ClickThrough.click_through.set(widget, click_through);
+ widget.input_shape_combine_region(click_through ? new Cairo.Region() : null);
+}
+
+public bool widget_get_click_through(Gtk.Widget widget) {
+ return ClickThrough.click_through.get(widget);
+}
+}
diff --git a/lib/astal/gtk3/src/widget/window.vala b/lib/astal/gtk3/src/widget/window.vala
new file mode 100644
index 0000000..9287200
--- /dev/null
+++ b/lib/astal/gtk3/src/widget/window.vala
@@ -0,0 +1,293 @@
+using GtkLayerShell;
+
+[Flags]
+public enum Astal.WindowAnchor {
+ NONE,
+ TOP,
+ RIGHT,
+ LEFT,
+ BOTTOM,
+}
+
+public enum Astal.Exclusivity {
+ NORMAL,
+ /**
+ * Request the compositor to allocate space for this window.
+ */
+ EXCLUSIVE,
+ /**
+ * Request the compositor to stack layers on top of each other.
+ */
+ IGNORE,
+}
+
+public enum Astal.Layer {
+ BACKGROUND = 0, // GtkLayerShell.Layer.BACKGROUND
+ BOTTOM = 1, // GtkLayerShell.Layer.BOTTOM
+ TOP = 2, // GtkLayerShell.Layer.TOP
+ OVERLAY = 3, // GtkLayerShell.Layer.OVERLAY
+}
+
+public enum Astal.Keymode {
+ /**
+ * Window should not receive keyboard events.
+ */
+ NONE = 0, // GtkLayerShell.KeyboardMode.NONE
+ /**
+ * Window should have exclusive focus if it is on the top or overlay layer.
+ */
+ EXCLUSIVE = 1, // GtkLayerShell.KeyboardMode.EXCLUSIVE
+ /**
+ * Focus and Unfocues the window as needed.
+ */
+ ON_DEMAND = 2, // GtkLayerShell.KeyboardMode.ON_DEMAND
+}
+
+/**
+ * Subclass of [[email protected]] which integrates GtkLayerShell as class fields.
+ */
+public class Astal.Window : Gtk.Window {
+ private static bool check(string action) {
+ if (!is_supported()) {
+ critical(@"can not $action on window: layer shell not supported");
+ print("tip: running from an xwayland terminal can cause this, for example VsCode");
+ return true;
+ }
+ return false;
+ }
+
+ private InhibitManager? inhibit_manager;
+ private Inhibitor? inhibitor;
+
+ construct {
+ if (check("initialize layer shell"))
+ return;
+
+ // If the window has no size allocatoted when it gets mapped.
+ // It won't show up later either when it size changes by adding children.
+ height_request = 1;
+ width_request = 1;
+
+ init_for_window(this);
+ inhibit_manager = InhibitManager.get_default();
+ }
+
+ /**
+ * When `true` it will permit inhibiting the idle behavior such as screen blanking, locking, and screensaving.
+ */
+ public bool inhibit {
+ set {
+ if (inhibit_manager == null) {
+ return;
+ }
+ if (value && inhibitor == null) {
+ inhibitor = inhibit_manager.inhibit(this);
+ }
+ else if (!value && inhibitor != null) {
+ inhibitor = null;
+ }
+ }
+ get {
+ return inhibitor != null;
+ }
+ }
+
+ public override void show() {
+ base.show();
+ if(inhibit) {
+ inhibitor = inhibit_manager.inhibit(this);
+ }
+ }
+
+ /**
+ * Namespace of this window. This can be used to target the layer in compositor rules.
+ */
+ public string namespace {
+ get { return get_namespace(this); }
+ set { set_namespace(this, value); }
+ }
+
+ /**
+ * Edges to anchor the window to.
+ *
+ * If two perpendicular edges are anchored, the surface will be anchored to that corner.
+ * If two opposite edges are anchored, the window will be stretched across the screen in that direction.
+ */
+ public WindowAnchor anchor {
+ set {
+ if (check("set anchor"))
+ return;
+
+ set_anchor(this, Edge.TOP, WindowAnchor.TOP in value);
+ set_anchor(this, Edge.BOTTOM, WindowAnchor.BOTTOM in value);
+ set_anchor(this, Edge.LEFT, WindowAnchor.LEFT in value);
+ set_anchor(this, Edge.RIGHT, WindowAnchor.RIGHT in value);
+ }
+ get {
+ var a = WindowAnchor.NONE;
+ if (get_anchor(this, Edge.TOP))
+ a = a | WindowAnchor.TOP;
+
+ if (get_anchor(this, Edge.RIGHT))
+ a = a | WindowAnchor.RIGHT;
+
+ if (get_anchor(this, Edge.LEFT))
+ a = a | WindowAnchor.LEFT;
+
+ if (get_anchor(this, Edge.BOTTOM))
+ a = a | WindowAnchor.BOTTOM;
+
+ return a;
+ }
+ }
+
+ /**
+ * Exclusivity of this window.
+ */
+ public Exclusivity exclusivity {
+ set {
+ if (check("set exclusivity"))
+ return;
+
+ switch (value) {
+ case Exclusivity.NORMAL:
+ set_exclusive_zone(this, 0);
+ break;
+ case Exclusivity.EXCLUSIVE:
+ auto_exclusive_zone_enable(this);
+ break;
+ case Exclusivity.IGNORE:
+ set_exclusive_zone(this, -1);
+ break;
+ }
+ }
+ get {
+ if (auto_exclusive_zone_is_enabled(this))
+ return Exclusivity.EXCLUSIVE;
+
+ if (get_exclusive_zone(this) == -1)
+ return Exclusivity.IGNORE;
+
+ return Exclusivity.NORMAL;
+ }
+ }
+
+ /**
+ * Which layer to appear this window on.
+ */
+ public Layer layer {
+ get { return (Layer)get_layer(this); }
+ set {
+ if (check("set layer"))
+ return;
+
+ set_layer(this, (GtkLayerShell.Layer)value);
+ }
+ }
+
+ /**
+ * Keyboard mode of this window.
+ */
+ public Keymode keymode {
+ get { return (Keymode)get_keyboard_mode(this); }
+ set {
+ if (check("set keymode"))
+ return;
+
+ set_keyboard_mode(this, (GtkLayerShell.KeyboardMode)value);
+ }
+ }
+
+ /**
+ * Which monitor to appear this window on.
+ */
+ public Gdk.Monitor gdkmonitor {
+ get { return get_monitor(this); }
+ set {
+ if (check("set gdkmonitor"))
+ return;
+
+ set_monitor (this, value);
+ }
+ }
+
+ public new int margin_top {
+ get { return GtkLayerShell.get_margin(this, Edge.TOP); }
+ set {
+ if (check("set margin_top"))
+ return;
+
+ GtkLayerShell.set_margin(this, Edge.TOP, value);
+ }
+ }
+
+ public new int margin_bottom {
+ get { return GtkLayerShell.get_margin(this, Edge.BOTTOM); }
+ set {
+ if (check("set margin_bottom"))
+ return;
+
+ GtkLayerShell.set_margin(this, Edge.BOTTOM, value);
+ }
+ }
+
+ public new int margin_left {
+ get { return GtkLayerShell.get_margin(this, Edge.LEFT); }
+ set {
+ if (check("set margin_left"))
+ return;
+
+ GtkLayerShell.set_margin(this, Edge.LEFT, value);
+ }
+ }
+
+ public new int margin_right {
+ get { return GtkLayerShell.get_margin(this, Edge.RIGHT); }
+ set {
+ if (check("set margin_right"))
+ return;
+
+ GtkLayerShell.set_margin(this, Edge.RIGHT, value);
+ }
+ }
+
+ public new int margin {
+ set {
+ if (check("set margin"))
+ return;
+
+ margin_top = value;
+ margin_right = value;
+ margin_bottom = value;
+ margin_left = value;
+ }
+ }
+
+ /**
+ * Which monitor to appear this window on.
+ *
+ * CAUTION: the id might not be the same mapped by the compositor.
+ */
+ public int monitor {
+ set {
+ if (check("set monitor"))
+ return;
+
+ if (value < 0)
+ set_monitor(this, (Gdk.Monitor)null);
+
+ var m = Gdk.Display.get_default().get_monitor(value);
+ set_monitor(this, m);
+ }
+ get {
+ var m = get_monitor(this);
+ var d = Gdk.Display.get_default();
+ for (var i = 0; i < d.get_n_monitors(); ++i) {
+ if (m == d.get_monitor(i))
+ return i;
+ }
+
+ return -1;
+ }
+ }
+}