menu.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import Gdk from 'gi://Gdk';
  2. import Gtk from 'gi://Gtk';
  3. import Gio from 'gi://Gio';
  4. import GLib from 'gi://GLib';
  5. export function addMenu(window) {
  6. const builder = new Gtk.Builder();
  7. // add a dummy page and remove it immediately, to access headerbar
  8. builder.add_from_file(GLib.filename_from_uri(GLib.uri_resolve_relative(import.meta.url, '../ui/menu.ui', GLib.UriFlags.NONE))[0]);
  9. let menu_util = builder.get_object('menu_util');
  10. window.add(menu_util);
  11. try {
  12. addMenuToHeader(window, builder);
  13. } catch (error) {
  14. // could not add menu... not so bad
  15. }
  16. window.remove(menu_util);
  17. }
  18. function addMenuToHeader(window, builder) {
  19. // a little hack to get to the headerbar
  20. const page = builder.get_object('menu_util');
  21. const pages_stack = page.get_parent(); // AdwViewStack
  22. const content_stack = pages_stack.get_parent().get_parent(); // GtkStack
  23. const preferences = content_stack.get_parent(); // GtkBox
  24. const headerbar = preferences.get_first_child(); // AdwHeaderBar
  25. headerbar.pack_start(builder.get_object('info_menu'));
  26. // setup menu actions
  27. const actionGroup = new Gio.SimpleActionGroup();
  28. window.insert_action_group('prefs', actionGroup);
  29. // a list of actions with their associated link
  30. const actions = [
  31. {
  32. name: 'open-bug-report',
  33. link: 'https://github.com/aunetx/blur-my-shell/issues'
  34. },
  35. {
  36. name: 'open-readme',
  37. link: 'https://github.com/aunetx/blur-my-shell'
  38. },
  39. {
  40. name: 'open-license',
  41. link: 'https://github.com/aunetx/blur-my-shell/blob/master/LICENSE'
  42. },
  43. {
  44. name: 'donate-github',
  45. link: 'https://github.com/sponsors/aunetx'
  46. },
  47. {
  48. name: 'donate-kofi',
  49. link: 'https://ko-fi.com/aunetx'
  50. },
  51. ];
  52. actions.forEach(action => {
  53. let act = new Gio.SimpleAction({ name: action.name });
  54. act.connect(
  55. 'activate',
  56. _ => Gtk.show_uri(window, action.link, Gdk.CURRENT_TIME)
  57. );
  58. actionGroup.add_action(act);
  59. });
  60. }