aboutsummaryrefslogtreecommitdiff
path: root/linux/home/.config/ags/service/wallpaper.ts
blob: 865c6d93a3d895b462db9dd84223cf3d224b2331 (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
import options from 'options';
import { dependencies, sh } from 'lib/utils';

export type Resolution = 1920 | 1366 | 3840;
export type Market = 'random' | 'en-US' | 'ja-JP' | 'en-AU' | 'en-GB' | 'de-DE' | 'en-NZ' | 'en-CA';

const WP = `${Utils.HOME}/pictures/wallpapers`;
const Cache = `${Utils.HOME}/Pictures/Wallpapers/Bing`;

class Wallpaper extends Service {
  static {
    Service.register(
      this,
      {},
      {
        wallpaper: ['string'],
      },
    );
  }

  #blockMonitor = false;

  #wallpaper() {
    if (!dependencies('swww')) return;

    sh('hyprctl cursorpos').then(pos => {
      sh(['swww', 'img', '--transition-type', 'grow', '--transition-pos', pos.replace(' ', ''), WP]).then(() => {
        this.changed('wallpaper');
      });
    });
  }

  async #setWallpaper(path: string) {
    this.#blockMonitor = true;

    await sh(`cp "${path}" "${WP}"`);
    this.#wallpaper();

    this.#blockMonitor = false;
  }

  async #fetchBing() {
    // Check if wallpaper functionality is enabled
    if (!options.wallpaper.enable.value) {
      console.log('Wallpaper functionality is disabled.');
      return;
    }

    try {
      const res = await Utils.fetch('https://bing.biturl.top/', {
        params: {
          resolution: options.wallpaper.resolution.value,
          format: 'json',
          image_format: 'jpg',
          index: 'random',
          mkt: options.wallpaper.market.value,
        },
      });

      if (!res.ok) {
        console.warn('Failed to fetch from Bing:', res.statusText);
        return;
      }

      const data = await res.json();
      const { url } = data;

      if (!url) {
        console.warn('No URL found in Bing response:', data);
        return;
      }

      const file = `${Cache}/${url.replace('https://www.bing.com/th?id=', '')}`;

      Utils.ensureDirectory(Cache);

      if (!(await Utils.fileExists(file))) {
        await sh(`curl "${url}" --output "${file}"`);
        await this.#setWallpaper(file);
      } else {
        console.log(`Wallpaper already exists: ${file}`);
      }
    } catch (error) {
      console.error('Error fetching wallpaper:', error);
    }
  }

  readonly random = () => {
    // Check if wallpaper functionality is enabled
    if (!options.wallpaper.enable.value) {
      console.log('Wallpaper functionality is disabled.');
      return;
    }
    this.#fetchBing();
  };

  readonly set = (path: string) => {
    this.#setWallpaper(path);
  };

  get wallpaper() {
    return WP;
  }
  constructor() {
    super();

    // Respect wallpaper.enable option
    if (!options.wallpaper.enable.value) {
      console.log('Wallpaper functionality is disabled, not starting swww-daemon.');
      return;
    }

    if (!dependencies('swww')) return;

    // Monitor and set wallpaper if enabled
    Utils.monitorFile(WP, () => {
      if (!this.#blockMonitor) this.#wallpaper();
    });

    // Start swww-daemon only when wallpaper is enabled
    Utils.execAsync('swww-daemon')
      .then(this.#wallpaper)
      .catch(() => null);
  }
}

export default new Wallpaper();