Skip to content

Instantly share code, notes, and snippets.

@juusechec
Last active September 14, 2020 11:46
Show Gist options
  • Select an option

  • Save juusechec/e0bf2cb8e0fbfe93438c29c91a9c3d61 to your computer and use it in GitHub Desktop.

Select an option

Save juusechec/e0bf2cb8e0fbfe93438c29c91a9c3d61 to your computer and use it in GitHub Desktop.
// Copyright (c) Ulisses 2019
// License: MIT
// Modified by @juusechec
const Meta = imports.gi.Meta;
const Lang = imports.lang;
const Main = imports.ui.main;
function init(em) {
return new HoppingWindow(em);
}
function HoppingWindow(em) {
this.corner = 2;
}
HoppingWindow.prototype = {
enable: function () {
this.workspaceSwitchSignal = global.workspace_manager.connect("workspace-switched", Lang.bind(this, this.try_spawn))
},
disable: function () {
this.despawn_window();
global.workspace_manager.disconnect(this.workspaceSwitchSignal)
},
try_spawn: function () {
this.despawn_window();
let target = this.find_window("YouTube") ||
this.find_window("Anime") ||
this.find_window("Online");
if (target) {
this.spawn_window(target);
}
},
find_window: function (title) {
let workspaces_count = global.workspace_manager.n_workspaces;
// global.log('workspaces_count', workspaces_count);
let active_workspace_index = global.workspace_manager.get_active_workspace_index();
// global.log('active_workspace_index', active_workspace_index);
for (let i = 0; i < workspaces_count; i++) {
if (i != active_workspace_index) {
let workspace = global.workspace_manager.get_workspace_by_index(i);
const window = this.find_window_in_workspace(workspace, title);
if (window) {
return window;
}
}
}
},
find_window_in_workspace: function (workspace, title) {
let windows = workspace.list_windows();
// global.log('windows.length', windows.length);
for (let i in windows) {
// global.log('windows[i]', windows[i], windows[i].get_title());
if (windows[i].get_title().search(title) > -1) {
return windows[i];
}
}
},
despawn_window: function () {
if (!this.preview) {
return;
}
this.preview.destroy();
this.preview = null;
},
spawn_window: function (win) {
this.despawn_window();
this.preview = new imports.gi.St.Button({
style_class: "youtube-preview"
});
let th = this.generate_texture(win, 350);
this.preview.add_actor(th);
function increment(i) {
return i + 1;
}
let event = Lang.bind(this, _ => this.switchCorner(increment));
this.preview.connect("enter-event", event);
this.switchCorner();
Main.layoutManager.addChrome(this.preview);
},
generate_texture: function (win, size) {
let mutw = win.get_compositor_private();
if (!mutw) {
return;
}
let framerect = win.get_frame_rect();
let width = framerect.width;
let height = framerect.height;
let scale = Math.min(1.0, size / width, size / height);
let th = new imports.gi.Clutter.Clone({
source: mutw,
reactive: true,
width: width * scale,
height: height * scale
});
return th;
}
}
HoppingWindow.prototype.switchCorner = function (increment) {
if (typeof increment == 'function') {
this.corner = increment(this.corner) % 4
}
let g = Main.layoutManager.getWorkAreaForMonitor(0);
let border_size = 0;
let drawable_rect = [
g.x, g.y, g.x + g.width - this.preview.get_width(), g.y + g.height - this.preview.get_height(),
];
let points = [
[
drawable_rect[0], drawable_rect[1],
],
[
drawable_rect[0], drawable_rect[3],
],
[
drawable_rect[2] - 0, 0,
],
[
drawable_rect[2], drawable_rect[3],
],
];
// global.log("corner: " + this.corner);
this.posX = points[this.corner][0];
this.posY = points[this.corner][1];
// global.log("position_x: " + this.posX + ", position_y: " + this.posY);
this.preview.set_position(this.posX, this.posY);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment