import { Gtk } from "ags/gtk4"; import { createBinding } from "ags"; import { omitObjectKeys, variableToBoolean } from "../../../../modules/utils"; import { property, register, signal } from "ags/gobject"; import Pango from "gi://Pango?version=1.0"; @register({ GTypeName: "Tile" }) export class Tile extends Gtk.Box { @signal(Boolean) toggled(_state: boolean) {} @signal() enabled() {} @signal() disabled() {} @signal() clicked() { if(this.enableOnClicked) this.enable(); } @property(String) public icon: string; @property(String) public title: string; @property(String) public description: string = ""; @property(Boolean) public enableOnClicked: boolean = false; @property(Boolean) public state: boolean = false; @property(Boolean) public hasArrow: boolean = false; declare $signals: Gtk.Box.SignalSignatures & { "toggled": (state: boolean) => void; "enabled": () => void; "disabled": () => void; "clicked": () => void; }; public enable(): void { if(this.state) return; this.state = true; !this.has_css_class("enabled") && this.add_css_class("enabled"); this.emit("toggled", true); this.emit("enabled"); } public disable(): void { if(!this.state) return; this.state = false; this.remove_css_class("enabled"); this.emit("toggled", false); this.emit("disabled"); } constructor(props: Partial> & { icon: string; title: string; description?: string; state?: boolean; enableOnClicked?: boolean; hasArrow?: boolean; }) { super(omitObjectKeys(props, [ "icon", "title", "description", "state", "enableOnClicked" ])); this.add_css_class("tile"); this.add_controller( { // gets the icon part of the tile const { x, y, width, height } = this.get_first_child()!.get_allocation(); if((px < x || px > x+width) || (py < y || y > py+height)) this.emit("clicked"); }} /> as Gtk.GestureClick ); this.icon = props.icon; this.title = props.title; this.hexpand = true; if(props.hasArrow !== undefined) this.hasArrow = props.hasArrow; if(props.description !== undefined) this.description = props.description; if(props.state !== undefined) this.state = props.state; if(props.enableOnClicked !== undefined) this.enableOnClicked = props.enableOnClicked; this.state && this.add_css_class("enabled"); // fix no highlight when enabled on init this.prepend( { this.state ? this.disable() : this.enable(); }} /> as Gtk.Box ); this.append( as Gtk.Box ); if(this.hasArrow) this.append( as Gtk.Image ); } emit( signal: Signal, ...args: Parameters<(typeof this.$signals)[Signal]> ): void { super.emit(signal, ...args); } connect( signal: Signal, callback: (typeof this.$signals)[Signal] ): number { return super.connect(signal, callback); } }