|
Packit |
1470ea |
//!/usr/bin/gjs
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
imports.gi.versions.Gdk = '3.0';
|
|
Packit |
1470ea |
imports.gi.versions.Gtk = '3.0';
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
const Gdk = imports.gi.Gdk;
|
|
Packit |
1470ea |
const GLib = imports.gi.GLib;
|
|
Packit |
1470ea |
const Gio = imports.gi.Gio;
|
|
Packit |
1470ea |
const Gtk = imports.gi.Gtk;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class TooltipExample {
|
|
Packit |
1470ea |
// Create the application
|
|
Packit |
1470ea |
constructor() {
|
|
Packit |
1470ea |
this.application = new Gtk.Application({
|
|
Packit |
1470ea |
application_id: 'org.example.jstooltip'
|
|
Packit |
1470ea |
});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Connect 'activate' and 'startup' signals to the callback functions
|
|
Packit |
1470ea |
this.application.connect('activate', this._onActivate.bind(this));
|
|
Packit |
1470ea |
this.application.connect('startup', this._onStartup.bind(this));
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Callback function for 'activate' signal presents windows when active
|
|
Packit |
1470ea |
_onActivate() {
|
|
Packit |
1470ea |
this.window.present();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Callback function for 'startup' signal builds the UI
|
|
Packit |
1470ea |
_onStartup() {
|
|
Packit |
1470ea |
this._buildUI();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Build the application's UI
|
|
Packit |
1470ea |
_buildUI() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the application window
|
|
Packit |
1470ea |
this.window = new Gtk.ApplicationWindow({
|
|
Packit |
1470ea |
application: this.application,
|
|
Packit |
1470ea |
window_position: Gtk.WindowPosition.CENTER,
|
|
Packit |
1470ea |
title: "Toolbar with Tooltips Example",
|
|
Packit |
1470ea |
default_width: 400,
|
|
Packit |
1470ea |
default_height: 200,
|
|
Packit |
1470ea |
border_width: 10
|
|
Packit |
1470ea |
});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this.grid = new Gtk.Grid();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this.toolbar = this._createToolbar();
|
|
Packit |
1470ea |
this.toolbar.set_hexpand(true);
|
|
Packit |
1470ea |
this.toolbar.show();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this.grid.attach(this.toolbar, 0, 0, 1, 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this.window.add(this.grid);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this._newAction = new Gio.SimpleAction({ name: "new" });
|
|
Packit |
1470ea |
this._newAction.connect("activate", this._newCallback.bind(this));
|
|
Packit |
1470ea |
this.window.add_action(this._newAction);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this._openAction = new Gio.SimpleAction({ name: "open" });
|
|
Packit |
1470ea |
this._openAction.connect("activate", this._openCallback.bind(this));
|
|
Packit |
1470ea |
this.window.add_action(this._openAction);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this._undoAction = new Gio.SimpleAction({ name: "undo" });
|
|
Packit |
1470ea |
this._undoAction.connect("activate", this._undoCallback.bind(this));
|
|
Packit |
1470ea |
this.window.add_action(this._undoAction);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this._fullScreenAction = new Gio.SimpleAction({ name: "fullscreenToggle" });
|
|
Packit |
1470ea |
this._fullScreenAction.connect("activate",
|
|
Packit |
1470ea |
this._fullScreenCallback.bind(this));
|
|
Packit |
1470ea |
this.window.add_action(this._fullScreenAction);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this.window.show_all();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_createToolbar() {
|
|
Packit |
1470ea |
this.toolbar = new Gtk.Toolbar();
|
|
Packit |
1470ea |
this.toolbar.get_style_context().add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// button for the "new" action
|
|
Packit |
1470ea |
this.newButton = Gtk.ToolButton.new_from_stock(Gtk.STOCK_NEW);
|
|
Packit |
1470ea |
// with a tooltip with a given text
|
|
Packit |
1470ea |
this.newButton.set_tooltip_text("Create a new file");
|
|
Packit |
1470ea |
this.newButton.set_is_important(true);
|
|
Packit |
1470ea |
this.toolbar.insert(this.newButton, 0);
|
|
Packit |
1470ea |
this.newButton.show();
|
|
Packit |
1470ea |
this.newButton.set_action_name("win.new");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// button for the "open" action
|
|
Packit |
1470ea |
this.openButton = Gtk.ToolButton.new_from_stock(Gtk.STOCK_OPEN);
|
|
Packit |
1470ea |
// with a tooltip with a given text in the Pango markup language
|
|
Packit |
1470ea |
this.openButton.set_tooltip_markup("Open an existing file");
|
|
Packit |
1470ea |
this.openButton.set_is_important(true);
|
|
Packit |
1470ea |
this.toolbar.insert(this.openButton, 1);
|
|
Packit |
1470ea |
this.openButton.show();
|
|
Packit |
1470ea |
this.openButton.set_action_name("win.open");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// button for the "undo" action
|
|
Packit |
1470ea |
this.undoButton = Gtk.ToolButton.new_from_stock(Gtk.STOCK_UNDO);
|
|
Packit |
1470ea |
// with a tooltip with an image
|
|
Packit |
1470ea |
// set true the property "has-tooltip"
|
|
Packit |
1470ea |
this.undoButton.set_property("has-tooltip", true);
|
|
Packit |
1470ea |
// connect to the callback function that for the tooltip
|
|
Packit |
1470ea |
// with the signal "query-tooltip"
|
|
Packit |
1470ea |
this.undoButton.connect("query-tooltip", this._undoTooltipCallback.bind(this));
|
|
Packit |
1470ea |
this.undoButton.set_is_important(true);
|
|
Packit |
1470ea |
this.toolbar.insert(this.undoButton, 2);
|
|
Packit |
1470ea |
this.undoButton.show();
|
|
Packit |
1470ea |
this.undoButton.set_action_name("win.undo");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// button for the "fullscreen/leave fullscreen" action
|
|
Packit |
1470ea |
this.fullscreenButton = Gtk.ToolButton.new_from_stock(Gtk.STOCK_FULLSCREEN);
|
|
Packit |
1470ea |
this.fullscreenButton.set_is_important(true);
|
|
Packit |
1470ea |
this.toolbar.insert(this.fullscreenButton, 3);
|
|
Packit |
1470ea |
this.fullscreenButton.set_action_name("win.fullscreenToggle");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
return this.toolbar;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_newCallback(action, parameter) {
|
|
Packit |
1470ea |
print("You clicked \"New\".");
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_openCallback(action, parameter) {
|
|
Packit |
1470ea |
print("You clicked \"Open\".");
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// the callback function for the tooltip of the "undo" button
|
|
Packit |
1470ea |
_undoTooltipCallback(widget, x, y, keyboard_mode, tooltip) {
|
|
Packit |
1470ea |
// set the text for the tooltip
|
|
Packit |
1470ea |
tooltip.set_text("Undo your last action");
|
|
Packit |
1470ea |
// set an icon fot the tooltip
|
|
Packit |
1470ea |
tooltip.set_icon_from_stock(Gtk.STOCK_UNDO, Gtk.IconSize.MENU);
|
|
Packit |
1470ea |
// show the tooltip
|
|
Packit |
1470ea |
return true;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_undoCallback(action, parameter) {
|
|
Packit |
1470ea |
print("You clicked \"Undo\".");
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_fullScreenCallback() {
|
|
Packit |
1470ea |
if ((this.window.get_window().get_state() & Gdk.WindowState.FULLSCREEN) != 0 ){
|
|
Packit |
1470ea |
this.fullscreenButton.set_stock_id(Gtk.STOCK_FULLSCREEN);
|
|
Packit |
1470ea |
this.fullscreenButton.set_tooltip_text("Make your window fullscreen");
|
|
Packit |
1470ea |
this.window.unfullscreen();
|
|
Packit |
1470ea |
} else {
|
|
Packit |
1470ea |
this.fullscreenButton.set_stock_id(Gtk.STOCK_LEAVE_FULLSCREEN);
|
|
Packit |
1470ea |
this.fullscreenButton.set_tooltip_text("Leave fullscreen");
|
|
Packit |
1470ea |
this.window.fullscreen();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
};
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Run the application
|
|
Packit |
1470ea |
let app = new TooltipExample ();
|
|
Packit |
1470ea |
app.application.run (ARGV);
|