summaryrefslogtreecommitdiff
path: root/raveos-gnome-theme/theme-data/extensions/installed/ShutdownTimer@deminder/modules/text-box.js
blob: b97cdb1020cf54f56f1f33bffd545ed49c28c6b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// SPDX-FileCopyrightText: 2023 Deminder <tremminder@gmail.com>
// SPDX-License-Identifier: GPL-3.0-or-later

import St from 'gi://St';
import Clutter from 'gi://Clutter';

import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { throttleTimeout, logDebug } from './util.js';
import {
  foregroundActive,
  observeForegroundActive,
  unobserveForegroundActive,
} from './session-mode-aware.js';

export class Textbox {
  textboxes = [];
  constructor({ settings }) {
    this.settings = settings;
    [this.syncThrottled, this.syncThrottledCancel] = throttleTimeout(
      this.sync.bind(this),
      50
    );
    this.showSettingsId = settings.connect(
      'changed::show-textboxes-value',
      this.sync.bind(this)
    );
    observeForegroundActive(this, fgActive => {
      if (!fgActive) {
        this.hideAll();
      }
    });
  }

  destroy() {
    if (this.showSettingsId) {
      unobserveForegroundActive(this);
      this.hideAll();
      this.settings.disconnect(this.showSettingsId);
      this.showSettingsId = null;
    }
  }

  sync() {
    this.syncThrottledCancel();
    // remove hidden textboxes
    this.textboxes = this.textboxes.filter(t => {
      if (t['_hidden']) {
        const sid = t['_sourceId'];
        if (sid) {
          clearTimeout(sid);
        }
        delete t['_sourceId'];
        t.destroy();
      }
      return !t['_hidden'];
    });
    const monitor = Main.layoutManager.primaryMonitor;
    let heightOffset = 0;
    this.textboxes.forEach((textbox, i) => {
      if (!('_sourceId' in textbox)) {
        // start fadeout of textbox after 3 seconds
        textbox['_sourceId'] = setTimeout(() => {
          textbox['_sourceId'] = 0;
          textbox.ease({
            opacity: 0,
            duration: 1000,
            mode: Clutter.AnimationMode.EASE_OUT_QUAD,
            onComplete: () => {
              textbox['_hidden'] = 1;
              this.syncThrottled();
            },
          });
        }, 3000);
      }
      textbox.visible = this.settings.get_boolean('show-textboxes-value');
      if (!textbox.visible) return;

      if (i === 0) {
        heightOffset = -textbox.height / 2;
      }
      textbox.set_position(
        monitor.x + Math.floor(monitor.width / 2 - textbox.width / 2),
        monitor.y + Math.floor(monitor.height / 2 + heightOffset)
      );
      heightOffset += textbox.height + 10;
      if (textbox['_sourceId']) {
        // set opacity before fadeout starts
        textbox.opacity =
          i === 0
            ? 255
            : Math.max(
                25,
                25 + 230 * (1 - heightOffset / (monitor.height / 2))
              );
      }
    });
  }

  hideAll() {
    for (const t of this.textboxes) {
      t['_hidden'] = 1;
    }
    this.sync();
  }

  /**
   * Show a textbox message on the primary monitor
   *
   * @param textmsg
   */
  showTextbox(textmsg) {
    if (textmsg && foregroundActive()) {
      for (const t of this.textboxes) {
        // replace old textbox if it has the same text
        if (t.text === textmsg) {
          t['_hidden'] = 1;
        }
      }
      logDebug(`show textbox: ${textmsg}`);
      const textbox = new St.Label({
        style_class: 'textbox-label',
        text: textmsg,
        opacity: 0,
      });
      Main.uiGroup.add_child(textbox);
      this.textboxes.unshift(textbox);
      this.syncThrottled();
    }
  }
}