Skip to content

Commit

Permalink
feat: Show icon on empty text (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
dantehemerson authored Jan 21, 2023
1 parent 73edc39 commit 2c28f7e
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 28 deletions.
157 changes: 157 additions & 0 deletions assets/one-thing-gnome.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 26 additions & 28 deletions widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,27 @@ class _Widget extends PanelMenu.Button {
// container for entry and icon elements
const calcBox = new St.BoxLayout();

const path = Me.dir
.get_child("assets")
.get_child("one-thing-gnome.svg")
.get_path();

this.icon = new St.Icon({
icon_name: "one-thing-gnome",
icon_size: 24,
gicon: Gio.icon_new_for_string(path),
});

calcBox.add(this.icon);
calcBox.add(this.panelText);

const textValue = this.panelText.get_text();
this._showIconIfTextEmpty(textValue);

this.add_actor(calcBox);
}

_initEvents() {
// events:
this.connect(
"button-press-event",
function () {
Expand All @@ -127,43 +141,27 @@ class _Widget extends PanelMenu.Button {
}.bind(this)
);

// @Deprecated: Allow to select all text on focus
// Depreacating it makes the entry fields works as a normal entry field around the world
// this.inputText.clutter_text.connect(
// "button-release-event",
// this._onButtonReleaseEntry.bind(this)
// );

this.inputText.clutter_text.connect(
"activate",
this._onActivateEntry.bind(this)
);
}

// @Deprecated: Allow to select all text on focus
// _onButtonReleaseEntry(actor) {
// if (
// actor.get_cursor_position() !== -1 &&
// actor.get_selection().length === 0
// ) {
// actor.set_selection(0, actor.get_text().length); // doesn't work to do this on button-press-event or key_focus_in; don't know why
// }
// }

_onActivateEntry(actor) {
let result;
try {
const expr = actor.get_text();
result = expr.toString();
} catch (e) {
result = "Unexpected error";
}
const textValue = actor.get_text();

if (this.panelText !== "") {
this.panelText.set_text(result);
this._showIconIfTextEmpty(textValue);

this.panelText.set_text(textValue);
this.menu.close();
}

_showIconIfTextEmpty(text) {
if (text === "") {
this.icon.show();
} else {
this.icon.hide();
}
this.menu.close();
}
}

Expand Down

0 comments on commit 2c28f7e

Please sign in to comment.