1
0
Fork 0

Added more key bindings and a context menu

This commit is contained in:
TheFGFSEagle 2023-01-06 12:21:26 +01:00 committed by James Turner
parent a25674337d
commit d21158fdbf
2 changed files with 48 additions and 5 deletions

View file

@ -175,7 +175,7 @@ gui.Menu = {
# @param icon: str optional Path to the icon (relative to canvas.style._dir_widgets) or nil if none should be displayed # @param icon: str optional Path to the icon (relative to canvas.style._dir_widgets) or nil if none should be displayed
# @param enabled: bool optional Whether the item should be enabled (1) or disabled (0) # @param enabled: bool optional Whether the item should be enabled (1) or disabled (0)
# @return canvas.gui.MenuItem The item that was created # @return canvas.gui.MenuItem The item that was created
createItem: func(text = nil, cb = "TheEagle", shortcut = "", icon = nil, enabled = 1) { createItem: func(text = nil, cb = nil, shortcut = "", icon = nil, enabled = 1) {
if (text == nil) { if (text == nil) {
die("cannot create a menu item without text"); die("cannot create a menu item without text");
} }

View file

@ -15,8 +15,18 @@ gui.widgets.LineEdit = {
m._selection_start = 0; m._selection_start = 0;
m._selection_end = 0; m._selection_end = 0;
m.context_menu = gui.Menu.new();
m.context_menu.createItem(text: "Copy", cb: func() { m.copy(); }, shortcut: "Ctrl+C");
m.context_menu.createItem(text: "Cut", cb: func() { m.cut(); }, shortcut: "Ctrl+X");
m.context_menu.createItem(text: "Paste", cb: func() { m.paste(); }, shortcut: "Ctrl+V");
m.context_menu.createItem(text: "Clear", cb: func() { m.clear(); }, shortcut: "Ctrl+D");
m.context_menu.createItem(text: "Select all", cb: func() { m.selectAll(); }, shortcut: "Ctrl+A");
return m; return m;
}, },
showContextMenu: func(e) {
me.context_menu.show(e.screenX, e.screenY);
},
setText: func(text) setText: func(text)
{ {
if (text == nil) { if (text == nil) {
@ -48,6 +58,9 @@ gui.widgets.LineEdit = {
{ {
return me._text; return me._text;
}, },
selectedText: func() {
return utf8.substr(me._text, me._selection_start, me._selection_end);
},
setMaxLength: func(len) setMaxLength: func(len)
{ {
me._max_length = len; me._max_length = len;
@ -101,10 +114,21 @@ gui.widgets.LineEdit = {
return me; return me;
}, },
copy: func() {
clipboard.setText(me.selectedText());
},
cut: func() {
clipboard.setText(me.selectedText());
me.removeSelection();
},
paste: func(mode = nil) paste: func(mode = nil)
{ {
me.insert(clipboard.getText(mode != nil ? mode : clipboard.CLIPBOARD)); me.insert(clipboard.getText(mode != nil ? mode : clipboard.CLIPBOARD));
}, },
selectAll: func() {
me._selection_start = 0;
me._selection_end = utf8.size(me._text) - 1;
},
# Remove selected text # Remove selected text
removeSelection: func() removeSelection: func()
{ {
@ -138,7 +162,7 @@ gui.widgets.LineEdit = {
return me; return me;
}, },
# Remove selection or if nothing is selected the character after the cursor # Remove selection or if nothing is selected the character after the cursor
del: func() delete: func()
{ {
if( me._selection_start == me._selection_end ) if( me._selection_start == me._selection_end )
{ {
@ -169,7 +193,7 @@ gui.widgets.LineEdit = {
else if( e.key == "Backspace" ) else if( e.key == "Backspace" )
me.backspace(); me.backspace();
else if( e.key == "Delete" ) else if( e.key == "Delete" )
me.del(); me.delete();
else if( e.key == "Left" ) else if( e.key == "Left" )
me.moveCursor(me._cursor - 1); me.moveCursor(me._cursor - 1);
else if( e.key == "Right") else if( e.key == "Right")
@ -178,8 +202,27 @@ gui.widgets.LineEdit = {
me.home(); me.home();
else if( e.key == "End" ) else if( e.key == "End" )
me.end(); me.end();
else if( e.keyCode == `v` and e.ctrlKey ) else if (e.ctrlKey) {
me.paste(); if (e.keyCode == `c`) {
me.copy();
} elsif (e.keyCode == `v`) {
me.paste();
} elsif (e.keyCode == `x`) {
me.cut();
} elsif (e.keyCode == `d`) {
me.clear();
} elsif (e.keyCode == `a`) {
me.selectAll();
}
}
}); });
el.addEventListener("click", func(e) {
if (e.button == 2) {
me.showContextMenu(e);
}
});
},
del: func() {
me.context_menu.del();
} }
}; };