thinkpad.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. 'disabled_start_bat0',
  19. 'disabled_start_bat1',
  20. 'warn_bat0',
  21. 'warn_bat1'
  22. ],
  23. }, class Thinkpad extends Adw.PreferencesPage {
  24. constructor(window) {
  25. super({});
  26. window._settings.bind(
  27. 'start-bat0',
  28. this._start_bat0,
  29. 'value',
  30. Gio.SettingsBindFlags.DEFAULT
  31. );
  32. window._settings.bind(
  33. 'end-bat0',
  34. this._end_bat0,
  35. 'value',
  36. Gio.SettingsBindFlags.DEFAULT
  37. );
  38. window._settings.bind(
  39. 'start-bat1',
  40. this._start_bat1,
  41. 'value',
  42. Gio.SettingsBindFlags.DEFAULT
  43. );
  44. window._settings.bind(
  45. 'end-bat1',
  46. this._end_bat1,
  47. 'value',
  48. Gio.SettingsBindFlags.DEFAULT
  49. );
  50. window._settings.bind(
  51. 'disabled-start-bat0-value',
  52. this._disabled_start_bat0,
  53. 'value',
  54. Gio.SettingsBindFlags.DEFAULT
  55. );
  56. window._settings.bind(
  57. 'disabled-start-bat1-value',
  58. this._disabled_start_bat1,
  59. 'value',
  60. Gio.SettingsBindFlags.DEFAULT
  61. );
  62. window._settings.connect('changed::start-bat0', () => {
  63. if (this._start_bat0.value >= this._end_bat0.value) {
  64. this._end_bat0.value = this._start_bat0.value + 1;
  65. }
  66. this._updateWarnings();
  67. });
  68. window._settings.connect('changed::end-bat0', () => {
  69. if (this._start_bat0.value >= this._end_bat0.value) {
  70. this._start_bat0.value = this._end_bat0.value - 1;
  71. }
  72. this._updateWarnings();
  73. });
  74. window._settings.connect('changed::start-bat1', () => {
  75. if (this._start_bat1.value >= this._end_bat1.value) {
  76. this._end_bat1.value = this._start_bat1.value + 1;
  77. }
  78. this._updateWarnings();
  79. });
  80. window._settings.connect('changed::end-bat1', () => {
  81. if (this._start_bat1.value >= this._end_bat1.value) {
  82. this._start_bat1.value = this._end_bat1.value - 1;
  83. }
  84. this._updateWarnings();
  85. });
  86. window._settings.connect('changed::disabled-start-bat0-value', () => {
  87. this._updateWarnings();
  88. });
  89. window._settings.connect('changed::disabled-start-bat1-value', () => {
  90. this._updateWarnings();
  91. });
  92. const bat0 = window._driver.batteries.find(battery => battery.name === 'BAT0');
  93. const bat1 = window._driver.batteries.find(battery => battery.name === 'BAT1');
  94. this._apply_bat0.connect('clicked', () => {
  95. bat0.enable();
  96. });
  97. this._apply_bat1.connect('clicked', () => {
  98. bat1.enable();
  99. });
  100. this._apply_bat0.visible = bat0.isAvailable;
  101. bat0.connect('notify::is-available', () => {
  102. this._apply_bat0.visible = bat0.isAvailable;
  103. });
  104. this._apply_bat0.sensitive = bat0.pendingChanges;
  105. bat0.connect('notify::pending-changes', () => {
  106. this._apply_bat0.sensitive = bat0.pendingChanges;
  107. });
  108. this._apply_bat1.visible = bat1.isAvailable;
  109. bat1.connect('notify::is-available', () => {
  110. this._apply_bat1.visible = bat1.isAvailable;
  111. });
  112. this._apply_bat1.sensitive = bat1.pendingChanges;
  113. bat1.connect('notify::pending-changes', () => {
  114. this._apply_bat1.sensitive = bat1.pendingChanges;
  115. });
  116. this._updateWarnings();
  117. this._reset_thresholds_dialog.connect('response', (obj, response, data) => {
  118. if (response === 'reset') {
  119. const keys = [
  120. 'start-bat0',
  121. 'end-bat0',
  122. 'start-bat1',
  123. 'end-bat1',
  124. 'disabled-start-bat0-value',
  125. 'disabled-start-bat1-value',
  126. ];
  127. keys.forEach(key => {
  128. window._settings.reset(key);
  129. });
  130. window._driver.enableAll();
  131. }
  132. });
  133. this._reset.connect('clicked', () => {
  134. this._reset_thresholds_dialog.transientFor = this.root;
  135. this._reset_thresholds_dialog.present();
  136. });
  137. }
  138. _updateWarnings() {
  139. this._warn_bat0.reveal_child = this._start_bat0.value === this._disabled_start_bat0.value && this._end_bat0.value === 100;
  140. this._warn_bat1.reveal_child = this._start_bat1.value === this._disabled_start_bat1.value && this._end_bat1.value === 100;
  141. }
  142. });