menu.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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().get_next_sibling()
  25. .get_first_child().get_first_child().get_first_child(); // AdwHeaderBar
  26. headerbar.pack_start(builder.get_object('info_menu'));
  27. // setup menu actions
  28. const actionGroup = new Gio.SimpleActionGroup();
  29. window.insert_action_group('prefs', actionGroup);
  30. // a list of actions with their associated link
  31. const actions = [
  32. {
  33. name: 'open-bug-report',
  34. link: 'https://github.com/aunetx/blur-my-shell/issues'
  35. },
  36. {
  37. name: 'open-readme',
  38. link: 'https://github.com/aunetx/blur-my-shell'
  39. },
  40. {
  41. name: 'open-license',
  42. link: 'https://github.com/aunetx/blur-my-shell/blob/master/LICENSE'
  43. },
  44. {
  45. name: 'donate-github',
  46. link: 'https://github.com/sponsors/aunetx'
  47. },
  48. {
  49. name: 'donate-kofi',
  50. link: 'https://ko-fi.com/aunetx'
  51. },
  52. ];
  53. actions.forEach(action => {
  54. let act = new Gio.SimpleAction({ name: action.name });
  55. act.connect(
  56. 'activate',
  57. () => Gtk.show_uri(window, action.link, Gdk.CURRENT_TIME)
  58. );
  59. actionGroup.add_action(act);
  60. });
  61. }