Last active
October 20, 2019 09:44
-
-
Save olejorgenb/434dcc616343a50d9d63f2a700ad89b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env gjs | |
/* | |
Create a window with given size constraints | |
Run it with: | |
gjs $FILENAME | |
*/ | |
const Gio = imports.gi.Gio; | |
const GLib = imports.gi.GLib; | |
const Gtk = imports.gi.Gtk; | |
const Gdk = imports.gi.Gdk; | |
const Lang = imports.lang; | |
// Get application folder and add it into the imports path | |
function getAppFileInfo() { | |
let stack = (new Error()).stack, | |
stackLine = stack.split('\n')[1], | |
coincidence, path, file; | |
if (!stackLine) throw new Error('Could not find current file (1)'); | |
coincidence = new RegExp('@(.+):\\d+').exec(stackLine); | |
if (!coincidence) throw new Error('Could not find current file (2)'); | |
path = coincidence[1]; | |
file = Gio.File.new_for_path(path); | |
return [file.get_path(), file.get_parent().get_path(), file.get_basename()]; | |
} | |
const path = getAppFileInfo()[1]; | |
imports.searchPath.push(path); | |
const App = function () { | |
let args = ARGV; | |
if (args[0] === "--paint-delay") { | |
this.paintDelay = parseInt(args[1]); | |
args = args.slice(2); | |
} | |
if (args.length % 2 === 1) { | |
this.title = args[args.length-1] | |
args = args.slice(0, -1) | |
} else { | |
this.title = `Constrained window`; | |
} | |
let {hints, flags} = parseHints(args) | |
this.hints = hints; | |
this.flags = flags; | |
GLib.set_prgname(this.title); | |
}; | |
App.prototype.run = function (ARGV) { | |
this.application = new Gtk.Application(); | |
this.application.connect('activate', () => { this.onActivate(); }); | |
this.application.connect('startup', () => { this.onStartup(); }); | |
this.application.run([]); | |
}; | |
App.prototype.onActivate = function () { | |
this.window.show_all(); | |
}; | |
App.prototype.onStartup = function() { | |
this.buildUI(); | |
}; | |
App.prototype.buildUI = function() { | |
this.window = new Gtk.ApplicationWindow({ application: this.application, | |
title: this.title, | |
default_height: 200, | |
default_width: 200, | |
window_position: Gtk.WindowPosition.CENTER }); | |
this.window.set_geometry_hints(null, this.hints, this.flags); | |
if (this.paintDelay) { | |
this.window.connect('draw', () => { | |
GLib.usleep(1000*this.paintDelay); | |
}) | |
} | |
}; | |
let flagOfHint = { | |
min_width: Gdk.WindowHints.MIN_SIZE, | |
min_height: Gdk.WindowHints.MIN_SIZE, | |
max_width: Gdk.WindowHints.MAX_SIZE, | |
max_height: Gdk.WindowHints.MAX_SIZE, | |
base_width: Gdk.WindowHints.BASE_SIZE, | |
base_height: Gdk.WindowHints.BASE_SIZE, | |
width_inc: Gdk.WindowHints.RESIZE_INC, | |
height_inc: Gdk.WindowHints.RESIZE_INC, | |
min_aspect: Gdk.WindowHints.ASPECT, | |
max_aspect: Gdk.WindowHints.ASPECT, | |
} | |
function parseHints(args) { | |
// NB: the supposed to work -1 values for the hints doesn't work.. Provide | |
// default values that represent 0 and "infinty" instead | |
let hints = new Gdk.Geometry({min_width: 0, min_height: 0, max_width: 999999, max_height: 999999, base_width: -1, base_height: -1, width_inc: 1, height_inc: 1, min_aspect: 0, max_aspect: 999999}); | |
let flags = 0; | |
for (let i = 0; i < args.length-1; i+=2) { | |
let name = args[i]; | |
let value = args[i+1]; | |
hints[name] = parseFloat(value); | |
flags |= flagOfHint[name] | |
} | |
return {hints, flags}; | |
} | |
if (ARGV[0] === "--help") { | |
print("usage: [--paint-delay DELAY_MS] [HINT VALUE ...] [WINDOW_TITLE]"); | |
print("Hints:"); | |
print(Object.keys(flagOfHint).join("\n")); | |
} else { | |
let app = new App(); | |
app.run(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment