Add error handling to the runDialog

Show the errors to the user instead of silently logging them to the terminal,
also rework positioning to get rid of magic numbers.

Signed-off-by: Adel Gadllah <adel.gadllah@gmail.com>

http://bugzilla.gnome.org/show_bug.cgi?id=593840
This commit is contained in:
Adel Gadllah 2009-09-08 20:04:18 +02:00 committed by Owen W. Taylor
parent 25410a730e
commit 0882da0a71

View File

@ -21,8 +21,10 @@ BOX_BACKGROUND_COLOR.from_pixel(0x000000cc);
const BOX_TEXT_COLOR = new Clutter.Color(); const BOX_TEXT_COLOR = new Clutter.Color();
BOX_TEXT_COLOR.from_pixel(0xffffffff); BOX_TEXT_COLOR.from_pixel(0xffffffff);
const BOX_WIDTH = 320; const DIALOG_WIDTH = 320;
const BOX_HEIGHT = 56; const DIALOG_PADDING = 6;
const ICON_SIZE = 24;
const ICON_BOX_SIZE = 36;
function RunDialog() { function RunDialog() {
this._init(); this._init();
@ -64,41 +66,73 @@ RunDialog.prototype = {
reactive: true }); reactive: true });
this._group.add_actor(this._overlay); this._group.add_actor(this._overlay);
let boxGroup = new Clutter.Group(); let boxH = new Big.Box({ orientation: Big.BoxOrientation.HORIZONTAL,
boxGroup.set_position((global.screen_width - BOX_WIDTH) / 2, x_align: Big.BoxAlignment.CENTER,
(global.screen_height - BOX_HEIGHT) / 2); y_align: Big.BoxAlignment.CENTER,
this._group.add_actor(boxGroup); width: global.screen_width,
height: global.screen_height });
let box = new Big.Box({ background_color: BOX_BACKGROUND_COLOR, this._group.add_actor(boxH);
let boxV = new Big.Box({ orientation: Big.BoxOrientation.VERTICAL,
y_align: Big.BoxAlignment.CENTER });
boxH.append(boxV, Big.BoxPackFlags.NONE);
let dialogBox = new Big.Box({ orientation: Big.BoxOrientation.VERTICAL,
background_color: BOX_BACKGROUND_COLOR,
corner_radius: 4, corner_radius: 4,
reactive: false, reactive: false,
width: BOX_WIDTH, padding: DIALOG_PADDING,
height: BOX_HEIGHT width: DIALOG_WIDTH });
});
boxGroup.add_actor(box); boxH.append(dialogBox, Big.BoxPackFlags.NONE);
let label = new Clutter.Text({ color: BOX_TEXT_COLOR, let label = new Clutter.Text({ color: BOX_TEXT_COLOR,
font_name: '18px Sans', font_name: '18px Sans',
text: _("Please enter a command:") }); text: _("Please enter a command:") });
label.set_position(6, 6);
boxGroup.add_actor(label); dialogBox.append(label, Big.BoxPackFlags.EXPAND);
this._entry = new Clutter.Text({ color: BOX_TEXT_COLOR, this._entry = new Clutter.Text({ color: BOX_TEXT_COLOR,
font_name: '20px Sans Bold', font_name: '20px Sans Bold',
editable: true, editable: true,
activatable: true, activatable: true,
singleLineMode: true, singleLineMode: true });
text: '',
width: BOX_WIDTH - 12, dialogBox.append(this._entry, Big.BoxPackFlags.EXPAND);
height: BOX_HEIGHT - 12 });
// TODO: Implement relative positioning using Tidy. this._errorBox = new Big.Box({ orientation: Big.BoxOrientation.HORIZONTAL,
this._entry.set_position(6, 30); padding_top: DIALOG_PADDING });
boxGroup.add_actor(this._entry);
dialogBox.append(this._errorBox, Big.BoxPackFlags.EXPAND);
let iconBox = new Big.Box({ orientation: Big.BoxOrientation.VERTICAL,
y_align: Big.BoxAlignment.CENTER,
x_align: Big.BoxAlignment.CENTER,
width: ICON_BOX_SIZE,
height: ICON_BOX_SIZE });
this._errorBox.append(iconBox, Big.BoxPackFlags.NONE);
this._commandError = false;
let errorIcon = Shell.TextureCache.get_default().load_icon_name("gtk-dialog-error", ICON_SIZE);
iconBox.append(errorIcon, Big.BoxPackFlags.EXPAND);
this._errorMessage = new Clutter.Text({ color: BOX_TEXT_COLOR,
font_name: '18px Sans Bold',
line_wrap: true });
this._errorBox.append(this._errorMessage, Big.BoxPackFlags.EXPAND);
this._errorBox.hide();
this._entry.connect('activate', Lang.bind(this, function (o, e) { this._entry.connect('activate', Lang.bind(this, function (o, e) {
this._run(o.get_text()); this._run(o.get_text());
if (!this._commandError)
this.close(); this.close();
return false;
})); }));
this._entry.connect('key-press-event', Lang.bind(this, function(o, e) { this._entry.connect('key-press-event', Lang.bind(this, function(o, e) {
@ -117,12 +151,22 @@ RunDialog.prototype = {
f(); f();
} else if (command) { } else if (command) {
try { try {
this._commandError = false;
let [ok, len, args] = GLib.shell_parse_argv(command); let [ok, len, args] = GLib.shell_parse_argv(command);
let p = new Shell.Process({'args' : args}); let p = new Shell.Process({'args' : args});
p.run(); p.run();
} catch (e) { } catch (e) {
// TODO: Give the user direct feedback. this._commandError = true;
log('Could not run command ' + command + ': ' + e); /*
* The exception contains an error string like:
* Error invoking Shell.run: Failed to execute child process "foo"
* (No such file or directory)
* We are only interested in the actual error, so parse that out.
*/
let m = /.+\((.+)\)/.exec(e);
let errorStr = "Execution of '" + command + "' failed:\n" + m[1];
this._errorMessage.set_text(errorStr);
this._errorBox.show();
} }
} }
}, },
@ -147,6 +191,9 @@ RunDialog.prototype = {
this._isOpen = false; this._isOpen = false;
this._errorBox.hide();
this._commandError = false;
this._group.hide(); this._group.hide();
this._entry.text = ''; this._entry.text = '';