thinkpad.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict'
  2. import GLib from 'gi://GLib';
  3. import Adw from 'gi://Adw';
  4. import GObject from 'gi://GObject';
  5. import Gio from 'gi://Gio';
  6. export const Thinkpad = GObject.registerClass({
  7. GTypeName: 'ThinkpadPrefs',
  8. Template: GLib.Uri.resolve_relative(import.meta.url, '../ui/thinkpad.ui',GLib.UriFlags.NONE),
  9. InternalChildren: [
  10. 'start_bat0',
  11. 'end_bat0',
  12. 'start_bat1',
  13. 'end_bat1',
  14. 'reset',
  15. 'apply_bat0',
  16. 'apply_bat1',
  17. 'reset_thresholds_dialog'
  18. ],
  19. }, class Thinkpad extends Adw.PreferencesPage {
  20. constructor(window) {
  21. super({});
  22. window._settings.bind(
  23. 'start-bat0',
  24. this._start_bat0,
  25. 'value',
  26. Gio.SettingsBindFlags.DEFAULT
  27. );
  28. window._settings.bind(
  29. 'end-bat0',
  30. this._end_bat0,
  31. 'value',
  32. Gio.SettingsBindFlags.DEFAULT
  33. );
  34. window._settings.bind(
  35. 'start-bat1',
  36. this._start_bat1,
  37. 'value',
  38. Gio.SettingsBindFlags.DEFAULT
  39. );
  40. window._settings.bind(
  41. 'end-bat1',
  42. this._end_bat1,
  43. 'value',
  44. Gio.SettingsBindFlags.DEFAULT
  45. );
  46. window._settings.connect('changed::start-bat0', () => {
  47. if (this._start_bat0.value >= this._end_bat0.value) {
  48. this._end_bat0.value = this._start_bat0.value + 1;
  49. }
  50. });
  51. window._settings.connect('changed::end-bat0', () => {
  52. if (this._start_bat0.value >= this._end_bat0.value) {
  53. this._start_bat0.value = this._end_bat0.value - 1;
  54. }
  55. });
  56. window._settings.connect('changed::start-bat1', () => {
  57. if (this._start_bat1.value >= this._end_bat1.value) {
  58. this._end_bat1.value = this._start_bat1.value + 1;
  59. }
  60. });
  61. window._settings.connect('changed::end-bat1', () => {
  62. if (this._start_bat1.value >= this._end_bat1.value) {
  63. this._start_bat1.value = this._end_bat1.value - 1;
  64. }
  65. });
  66. const bat0 = window._driver.batteries.find(battery => battery.name === 'BAT0');
  67. const bat1 = window._driver.batteries.find(battery => battery.name === 'BAT1');
  68. this._apply_bat0.connect('clicked', () => {
  69. bat0.enable();
  70. });
  71. this._apply_bat1.connect('clicked', () => {
  72. bat1.enable();
  73. });
  74. this._apply_bat0.visible = bat0.isAvailable;
  75. bat0.connect('notify::is-available', () => {
  76. this._apply_bat0.visible = bat0.isAvailable;
  77. });
  78. this._apply_bat0.sensitive = bat0.pendingChanges;
  79. bat0.connect('notify::pending-changes', () => {
  80. this._apply_bat0.sensitive = bat0.pendingChanges;
  81. });
  82. this._apply_bat1.visible = bat1.isAvailable;
  83. bat1.connect('notify::is-available', () => {
  84. this._apply_bat1.visible = bat1.isAvailable;
  85. });
  86. this._apply_bat1.sensitive = bat1.pendingChanges;
  87. bat1.connect('notify::pending-changes', () => {
  88. this._apply_bat1.sensitive = bat1.pendingChanges;
  89. });
  90. this._reset_thresholds_dialog.connect('response', (obj, response, data) => {
  91. if (response === 'reset') {
  92. const keys = [
  93. 'start-bat0',
  94. 'end-bat0',
  95. 'start-bat1',
  96. 'end-bat1'
  97. ];
  98. keys.forEach(key => {
  99. window._settings.reset(key);
  100. });
  101. window._driver.enableAll();
  102. }
  103. });
  104. this._reset.connect('clicked', () => {
  105. this._reset_thresholds_dialog.transientFor = this.root;
  106. this._reset_thresholds_dialog.present();
  107. });
  108. }
  109. });