widgets.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /** @license (c) aylur. GPL v3 */
  2. import Adw from "gi://Adw";
  3. import Gio from "gi://Gio";
  4. import Gdk from "gi://Gdk";
  5. import Gtk from "gi://Gtk";
  6. import GObject from "gi://GObject";
  7. // GNOME imports
  8. import { gettext as _ } from "resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js";
  9. // Shared state
  10. import { Logger } from "../shared/logger.js";
  11. export class PreferencesPage extends Adw.PreferencesPage {
  12. static {
  13. GObject.registerClass(this);
  14. }
  15. add_group({ title, description = "", children, header_suffix = "" }) {
  16. const group = new Adw.PreferencesGroup({ title, description });
  17. for (const child of children) group.add(child);
  18. if (header_suffix) group.set_header_suffix(header_suffix);
  19. this.add(group);
  20. return group;
  21. }
  22. }
  23. export class SwitchRow extends Adw.ActionRow {
  24. static {
  25. GObject.registerClass(this);
  26. }
  27. constructor({ title, settings, bind, subtitle = "", experimental = false }) {
  28. super({ title, subtitle });
  29. const gswitch = new Gtk.Switch({
  30. active: settings.get_boolean(bind),
  31. valign: Gtk.Align.CENTER,
  32. });
  33. settings.bind(bind, gswitch, "active", Gio.SettingsBindFlags.DEFAULT);
  34. if (experimental) {
  35. const icon = new Gtk.Image({ icon_name: "bug-symbolic" });
  36. icon.set_tooltip_markup(
  37. _("<b>CAUTION</b>: Enabling this setting can lead to bugs or cause the shell to crash")
  38. );
  39. this.add_suffix(icon);
  40. }
  41. this.add_suffix(gswitch);
  42. this.activatable_widget = gswitch;
  43. }
  44. }
  45. export class ColorRow extends Adw.ActionRow {
  46. static {
  47. GObject.registerClass(this);
  48. }
  49. constructor({ title, init, onChange, subtitle = "" }) {
  50. super({ title, subtitle });
  51. let rgba = new Gdk.RGBA();
  52. rgba.parse(init);
  53. this.colorButton = new Gtk.ColorButton({ rgba, use_alpha: true, valign: Gtk.Align.CENTER });
  54. this.colorButton.connect("color-set", () => {
  55. onChange(this.colorButton.get_rgba().to_string());
  56. });
  57. this.add_suffix(this.colorButton);
  58. this.activatable_widget = this.colorButton;
  59. }
  60. }
  61. export class SpinButtonRow extends Adw.ActionRow {
  62. static {
  63. GObject.registerClass(this);
  64. }
  65. constructor({
  66. title,
  67. range: [low, high, step],
  68. subtitle = "",
  69. init = undefined,
  70. onChange = undefined,
  71. max_width_chars = undefined,
  72. max_length = undefined,
  73. width_chars = undefined,
  74. xalign = undefined,
  75. settings = undefined,
  76. bind = undefined,
  77. }) {
  78. super({ title, subtitle });
  79. const gspin = Gtk.SpinButton.new_with_range(low, high, step);
  80. gspin.xalign = 1;
  81. if (bind && settings) {
  82. settings.bind(bind, gspin, "value", Gio.SettingsBindFlags.DEFAULT);
  83. } else if (init) {
  84. gspin.value = init;
  85. gspin.connect("value-changed", (widget) => {
  86. onChange?.(widget.value);
  87. });
  88. }
  89. this.add_suffix(gspin);
  90. this.set_css_classes(["spin"]);
  91. this.activatable_widget = gspin;
  92. }
  93. }
  94. export class DropDownRow extends Adw.ActionRow {
  95. static {
  96. GObject.registerClass(this);
  97. }
  98. /**
  99. * @type {string}
  100. * Name of the gsetting key to bind to
  101. */
  102. bind;
  103. /**
  104. * @type {'b'|'y'|'n'|'q'|'i'|'u'|'x'|'t'|'h'|'d'|'s'|'o'|'g'|'?'|'a'|'m'}
  105. * - b: the type string of G_VARIANT_TYPE_BOOLEAN; a boolean value.
  106. * - y: the type string of G_VARIANT_TYPE_BYTE; a byte.
  107. * - n: the type string of G_VARIANT_TYPE_INT16; a signed 16 bit integer.
  108. * - q: the type string of G_VARIANT_TYPE_UINT16; an unsigned 16 bit integer.
  109. * - i: the type string of G_VARIANT_TYPE_INT32; a signed 32 bit integer.
  110. * - u: the type string of G_VARIANT_TYPE_UINT32; an unsigned 32 bit integer.
  111. * - x: the type string of G_VARIANT_TYPE_INT64; a signed 64 bit integer.
  112. * - t: the type string of G_VARIANT_TYPE_UINT64; an unsigned 64 bit integer.
  113. * - h: the type string of G_VARIANT_TYPE_HANDLE; a signed 32 bit value that, by convention, is used as an index into an array of file descriptors that are sent alongside a D-Bus message.
  114. * - d: the type string of G_VARIANT_TYPE_DOUBLE; a double precision floating point value.
  115. * - s: the type string of G_VARIANT_TYPE_STRING; a string.
  116. * - o: the type string of G_VARIANT_TYPE_OBJECT_PATH; a string in the form of a D-Bus object path.
  117. * - g: the type string of G_VARIANT_TYPE_SIGNATURE; a string in the form of a D-Bus type signature.
  118. * - ?: the type string of G_VARIANT_TYPE_BASIC; an indefinite type that is a supertype of any of the basic types.
  119. * - v: the type string of G_VARIANT_TYPE_VARIANT; a container type that contain any other type of value.
  120. * - a: used as a prefix on another type string to mean an array of that type; the type string “ai”, for example, is the type of an array of signed 32-bit integers.
  121. * - m: used as a prefix on another type string to mean a “maybe”, or “nullable”, version of that type; the type string “ms”, for example, is the type of a value that maybe contains a string, or maybe contains nothing.
  122. */
  123. type;
  124. selected = 0;
  125. /** @type {{name: string; id: string}[]} */
  126. items;
  127. model = new Gtk.StringList();
  128. /** @type {Gtk.DropDown} */
  129. dropdown;
  130. constructor({ title, settings, bind, items, subtitle = "", type }) {
  131. super({ title, subtitle });
  132. this.settings = settings;
  133. this.items = items;
  134. this.bind = bind;
  135. this.type = type ?? this.settings.get_value(bind)?.get_type() ?? "?";
  136. this.#build();
  137. this.add_suffix(this.dropdown);
  138. this.add_suffix(new ResetButton({ settings, bind, onReset: () => this.reset() }));
  139. }
  140. reset() {
  141. this.dropdown.selected = 0;
  142. this.selected = 0;
  143. }
  144. #build() {
  145. for (const { name, id } of this.items) {
  146. this.model.append(name);
  147. if (this.#get() === id) this.selected = this.items.findIndex((x) => x.id === id);
  148. }
  149. const { model, selected } = this;
  150. this.dropdown = new Gtk.DropDown({ valign: Gtk.Align.CENTER, model, selected });
  151. this.dropdown.connect("notify::selected", () => this.#onSelected());
  152. this.activatable_widget = this.dropdown;
  153. }
  154. #onSelected() {
  155. this.selected = this.dropdown.selected;
  156. const { id } = this.items[this.selected];
  157. Logger.debug("setting", id, this.selected);
  158. this.#set(this.bind, id);
  159. }
  160. static #settingsTypes = {
  161. b: "boolean",
  162. y: "byte",
  163. n: "int16",
  164. q: "uint16",
  165. i: "int32",
  166. u: "uint",
  167. x: "int64",
  168. t: "uint64",
  169. d: "double",
  170. s: "string",
  171. o: "objv",
  172. };
  173. /**
  174. * @param {string} x
  175. */
  176. #get(x = this.bind) {
  177. const methodName = `get_${DropDownRow.#settingsTypes[this.type] ?? "value"}`;
  178. return this.settings[methodName]?.(x);
  179. }
  180. /**
  181. * @param {string} x
  182. * @param {unknown} y
  183. */
  184. #set(x, y) {
  185. const methodName = `set_${DropDownRow.#settingsTypes[this.type] ?? "value"}`;
  186. Logger.log(`${methodName}(${x}, ${y})`);
  187. return this.settings[methodName]?.(x, y);
  188. }
  189. }
  190. export class ClearButton extends Gtk.Button {
  191. static {
  192. GObject.registerClass(this);
  193. }
  194. constructor({ settings = undefined, bind = undefined, onClear }) {
  195. super({
  196. icon_name: "edit-clear-symbolic",
  197. tooltip_text: _("Clear shortcut"),
  198. css_classes: ["flat", "circular"],
  199. valign: Gtk.Align.CENTER,
  200. });
  201. this.connect("clicked", () => {
  202. onClear?.();
  203. });
  204. }
  205. }
  206. export class ResetButton extends Gtk.Button {
  207. static {
  208. GObject.registerClass(this);
  209. }
  210. constructor({ settings = undefined, bind = undefined, onReset }) {
  211. super({
  212. icon_name: "edit-undo-symbolic",
  213. tooltip_text: _("Reset to default"),
  214. css_classes: ["flat", "circular"],
  215. valign: Gtk.Align.CENTER,
  216. });
  217. this.connect("clicked", () => {
  218. settings?.reset(bind);
  219. onReset?.();
  220. });
  221. }
  222. }
  223. export class RemoveButton extends Gtk.Button {
  224. static {
  225. GObject.registerClass(this);
  226. }
  227. constructor({ item, parent, onRemove }) {
  228. super({
  229. icon_name: "edit-delete-symbolic",
  230. tooltip_text: _("Remove Item"),
  231. css_classes: ["flat", "circular"],
  232. valign: Gtk.Align.CENTER,
  233. });
  234. this.connect("clicked", () => {
  235. onRemove?.(item, parent);
  236. });
  237. }
  238. }
  239. export class EntryRow extends Adw.EntryRow {
  240. static {
  241. GObject.registerClass(this);
  242. }
  243. constructor({ title, settings, bind, map }) {
  244. super({ title });
  245. this.connect("changed", () => {
  246. const text = this.get_text();
  247. if (typeof text === "string")
  248. if (map) {
  249. map.to(settings, bind, text);
  250. } else {
  251. settings.set_string(bind, text);
  252. }
  253. });
  254. const current = map ? map.from(settings, bind) : settings.get_string(bind);
  255. this.set_text(current ?? "");
  256. this.add_suffix(
  257. new ClearButton({
  258. settings,
  259. bind,
  260. onClear: () => {
  261. this.set_text("");
  262. },
  263. })
  264. );
  265. this.add_suffix(
  266. new ResetButton({
  267. settings,
  268. bind,
  269. onReset: () => {
  270. this.set_text((map ? map.from(settings, bind) : settings.get_string(bind)) ?? "");
  271. },
  272. })
  273. );
  274. }
  275. }
  276. export class RadioRow extends Adw.ActionRow {
  277. static {
  278. GObject.registerClass(this);
  279. }
  280. static orientation = Gtk.Orientation.HORIZONTAL;
  281. static spacing = 3;
  282. static valign = Gtk.Align.CENTER;
  283. constructor({ title, subtitle = "", settings, bind, options }) {
  284. super({ title, subtitle });
  285. const current = settings.get_string(bind);
  286. const labels = Object.fromEntries(Object.entries(options).map(([k, v]) => [v, k]));
  287. const { orientation, spacing, valign } = RadioRow;
  288. const hbox = new Gtk.Box({ orientation, spacing, valign });
  289. let group;
  290. for (const [key, label] of Object.entries(options)) {
  291. const toggle = new Gtk.ToggleButton({ label, ...(group && { group }) });
  292. group ||= toggle;
  293. toggle.active = key === current;
  294. toggle.set_css_classes(["flat"]);
  295. toggle.connect("clicked", () => {
  296. if (toggle.active) {
  297. settings.set_string(bind, labels[toggle.label]);
  298. }
  299. });
  300. hbox.append(toggle);
  301. }
  302. this.add_suffix(hbox);
  303. }
  304. }
  305. export class RemoveItemRow extends Adw.ActionRow {
  306. static {
  307. GObject.registerClass(this);
  308. }
  309. constructor({ title, subtitle = "", onRemove = undefined }) {
  310. super({ title, subtitle });
  311. const rmbutton = new RemoveButton({
  312. item: subtitle,
  313. parent: this,
  314. onRemove: onRemove,
  315. });
  316. this.add_suffix(rmbutton);
  317. }
  318. }