|
Packit |
1470ea |
#!/usr/bin/gjs
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
imports.gi.versions.Gtk = '3.0';
|
|
Packit |
1470ea |
const Gtk = imports.gi.Gtk;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class ComboBoxTextExample {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the application itself
|
|
Packit |
1470ea |
constructor() {
|
|
Packit |
1470ea |
this.application = new Gtk.Application ({
|
|
Packit |
1470ea |
application_id: 'org.example.jscomboboxtext'});
|
|
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: "Welcome to GNOME",
|
|
Packit |
1470ea |
default_width: 200,
|
|
Packit |
1470ea |
border_width: 10 });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the combobox
|
|
Packit |
1470ea |
this._comboBoxText = new Gtk.ComboBoxText();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Populate the combobox
|
|
Packit |
1470ea |
let distros = ["Select distribution", "Fedora", "Mint", "Suse"];
|
|
Packit |
1470ea |
for (let i = 0; i < distros.length; i++)
|
|
Packit |
1470ea |
this._comboBoxText.append_text (distros[i]);
|
|
Packit |
1470ea |
this._comboBoxText.set_active (0);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Connect the combobox's 'changed' signal to our callback function
|
|
Packit |
1470ea |
this._comboBoxText.connect ('changed', this._onComboChanged.bind(this));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Add the combobox to the window
|
|
Packit |
1470ea |
this._window.add (this._comboBoxText);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Show the window and all child widgets
|
|
Packit |
1470ea |
this._window.show_all();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_onComboChanged() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// The responses we'll use for our messagedialog
|
|
Packit |
1470ea |
let responses = ["",
|
|
Packit |
1470ea |
"Fedora is a community distro sponsored by Red Hat.",
|
|
Packit |
1470ea |
"Mint is a popular distro based on Ubuntu.",
|
|
Packit |
1470ea |
"SUSE is a name shared by two separate distros."];
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Which combobox item is active?
|
|
Packit |
1470ea |
let activeItem = this._comboBoxText.get_active();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// No messagedialog if you chose "Select distribution"
|
|
Packit |
1470ea |
if (activeItem != 0) {
|
|
Packit |
1470ea |
this._popUp = new Gtk.MessageDialog ({
|
|
Packit |
1470ea |
transient_for: this._window,
|
|
Packit |
1470ea |
modal: true,
|
|
Packit |
1470ea |
buttons: Gtk.ButtonsType.OK,
|
|
Packit |
1470ea |
message_type: Gtk.MessageType.INFO,
|
|
Packit |
1470ea |
text: responses[activeItem]});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Connect the OK button to a handler function
|
|
Packit |
1470ea |
this._popUp.connect ('response', this._onDialogResponse.bind(this));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Show the messagedialog
|
|
Packit |
1470ea |
this._popUp.show();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_onDialogResponse() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this._popUp.destroy ();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
};
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Run the application
|
|
Packit |
1470ea |
let app = new ComboBoxTextExample ();
|
|
Packit |
1470ea |
app.application.run (ARGV);
|