| 1 |
/* |
| 2 |
* Copyright 2015 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
import QtQuick 2.4 |
| 18 |
import Lomiri.Components 1.3 |
| 19 |
import Lomiri.Components.Popups 1.3 |
| 20 |
|
| 21 |
Popover { |
| 22 |
id: popover |
| 23 |
objectName: "text_input_contextmenu" |
| 24 |
property Item target |
| 25 |
property bool canCopy: target && target.selectedText !== "" && !popover.password |
| 26 |
property bool password: target && target.hasOwnProperty('echoMode') && target.echoMode == TextInput.Password |
| 27 |
property list<Action> actions: [ |
| 28 |
Action { |
| 29 |
text: i18n.dtr('lomiri-ui-toolkit', "Select All") |
| 30 |
iconName: "edit-select-all" |
| 31 |
enabled: target && target.text !== "" && target.selectedText === "" |
| 32 |
visible: target && (target.selectedText === "" || popover.password) |
| 33 |
onTriggered: target.selectAll() |
| 34 |
}, |
| 35 |
Action { |
| 36 |
text: i18n.dtr('lomiri-ui-toolkit', "Cut") |
| 37 |
iconName: "edit-cut" |
| 38 |
// If paste/editing is not possible, then disable also "Cut" operation |
| 39 |
// It is applicable for ReadOnly's TextFields and TextAreas |
| 40 |
enabled: !target.readOnly |
| 41 |
visible: popover.canCopy |
| 42 |
onTriggered: { |
| 43 |
PopupUtils.close(popover); |
| 44 |
target.cut(); |
| 45 |
} |
| 46 |
}, |
| 47 |
Action { |
| 48 |
text: i18n.dtr('lomiri-ui-toolkit', "Copy") |
| 49 |
iconName: "edit-copy" |
| 50 |
visible: popover.canCopy |
| 51 |
onTriggered: { |
| 52 |
PopupUtils.close(popover); |
| 53 |
target.copy(); |
| 54 |
} |
| 55 |
}, |
| 56 |
Action { |
| 57 |
text: i18n.dtr('lomiri-ui-toolkit', "Paste") |
| 58 |
iconName: "edit-paste" |
| 59 |
enabled: target && target.canPaste |
| 60 |
onTriggered: { |
| 61 |
PopupUtils.close(popover); |
| 62 |
target.paste(); |
| 63 |
} |
| 64 |
} |
| 65 |
] |
| 66 |
|
| 67 |
function show() { |
| 68 |
visible = true; |
| 69 |
__foreground.show(); |
| 70 |
// No input focus on the context menu! |
| 71 |
} |
| 72 |
|
| 73 |
// removes hide animation |
| 74 |
function hide() { |
| 75 |
popover.visible = false; |
| 76 |
} |
| 77 |
|
| 78 |
autoClose: false |
| 79 |
contentHeight: row.childrenRect.height + padding * 2 |
| 80 |
contentWidth: row.childrenRect.width + padding * 2 |
| 81 |
property int padding: units.gu(1) |
| 82 |
Row { |
| 83 |
id: row |
| 84 |
height: units.gu(6) |
| 85 |
x: popover.padding |
| 86 |
y: popover.padding |
| 87 |
|
| 88 |
Repeater { |
| 89 |
model: actions.length |
| 90 |
AbstractButton { |
| 91 |
id: button |
| 92 |
width: Math.max(units.gu(5), implicitWidth) + units.gu(2) |
| 93 |
height: units.gu(6) |
| 94 |
action: actions[modelData] |
| 95 |
styleName: "ToolbarButtonStyle" |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |